summaryrefslogtreecommitdiffstats
path: root/yql/essentials/utils/chunked_buffer.cpp
diff options
context:
space:
mode:
authoraneporada <[email protected]>2024-11-07 22:39:23 +0300
committeraneporada <[email protected]>2024-11-07 22:51:13 +0300
commit77acc99aab41efa1d6cf64879057312292355860 (patch)
tree7ddc3f2d6cf290872de90947fe275609b3783c88 /yql/essentials/utils/chunked_buffer.cpp
parent4b6cbdc40a3c0b3cee2bf7af07482f02334ef62c (diff)
Minimize dependency on YDB's TRope
commit_hash:e3b74d6e744bd014dc1fa0f41053fe886f7f4a49
Diffstat (limited to 'yql/essentials/utils/chunked_buffer.cpp')
-rw-r--r--yql/essentials/utils/chunked_buffer.cpp123
1 files changed, 123 insertions, 0 deletions
diff --git a/yql/essentials/utils/chunked_buffer.cpp b/yql/essentials/utils/chunked_buffer.cpp
new file mode 100644
index 00000000000..906da8dcae8
--- /dev/null
+++ b/yql/essentials/utils/chunked_buffer.cpp
@@ -0,0 +1,123 @@
+#include "chunked_buffer.h"
+
+#include <yql/essentials/utils/yql_panic.h>
+
+namespace NYql {
+
+TChunkedBuffer::TChunkedBuffer(TChunkedBuffer&& other) {
+ Items_ = std::move(other.Items_);
+}
+
+TChunkedBuffer& TChunkedBuffer::operator=(TChunkedBuffer&& other) {
+ Items_ = std::move(other.Items_);
+ return *this;
+}
+
+TChunkedBuffer::TChunkedBuffer(TStringBuf buf, const std::shared_ptr<const void>& owner) {
+ Append(buf, owner);
+}
+
+TChunkedBuffer::TChunkedBuffer(TString&& str) {
+ Append(std::move(str));
+}
+
+const TChunkedBuffer::TChunk& TChunkedBuffer::Front() const {
+ YQL_ENSURE(!Items_.empty());
+ return Items_.front();
+}
+
+size_t TChunkedBuffer::CopyTo(IOutputStream& dst, size_t toCopy) const {
+ size_t copied = 0;
+ for (auto& chunk : Items_) {
+ if (!toCopy) {
+ break;
+ }
+ size_t copyChunk = std::min(chunk.Buf.size(), toCopy);
+ dst.Write(chunk.Buf.data(), copyChunk);
+ toCopy -= copyChunk;
+ copied += copyChunk;
+ }
+ return copied;
+}
+
+size_t TChunkedBuffer::ContigousSize() const {
+ return Items_.empty() ? 0 : Front().Buf.size();
+}
+
+size_t TChunkedBuffer::Size() const {
+ size_t result = 0;
+ for (auto& item : Items_) {
+ result += item.Buf.size();
+ }
+ return result;
+}
+
+bool TChunkedBuffer::Empty() const {
+ return Items_.empty();
+}
+
+TChunkedBuffer& TChunkedBuffer::Append(TStringBuf buf, const std::shared_ptr<const void>& owner) {
+ if (!buf.empty()) {
+ Items_.emplace_back(TChunk{buf, owner});
+ }
+ return *this;
+}
+
+TChunkedBuffer& TChunkedBuffer::Append(TString&& str) {
+ if (!str.empty()) {
+ auto owner = std::make_shared<TString>(std::move(str));
+ Items_.emplace_back(TChunk{*owner, owner});
+ }
+ return *this;
+}
+
+TChunkedBuffer& TChunkedBuffer::Append(TChunkedBuffer&& other) {
+ while (!other.Items_.empty()) {
+ Items_.emplace_back(std::move(other.Items_.front()));
+ other.Items_.pop_front();
+ }
+ return *this;
+}
+
+TChunkedBuffer& TChunkedBuffer::Clear() {
+ Items_.clear();
+ return *this;
+}
+
+TChunkedBuffer& TChunkedBuffer::Erase(size_t size) {
+ while (size && !Items_.empty()) {
+ TStringBuf& buf = Items_.front().Buf;
+ size_t toErase = std::min(buf.size(), size);
+ buf.Skip(toErase);
+ size -= toErase;
+ if (buf.empty()) {
+ Items_.pop_front();
+ }
+ }
+ return *this;
+}
+
+TChunkedBufferOutput::TChunkedBufferOutput(TChunkedBuffer& dst)
+ : Dst_(dst)
+{
+}
+
+void TChunkedBufferOutput::DoWrite(const void* buf, size_t len) {
+ TString str(static_cast<const char*>(buf), len);
+ Dst_.Append(std::move(str));
+}
+
+TChunkedBuffer CopyData(const TChunkedBuffer& src) {
+ TChunkedBuffer result;
+ TChunkedBufferOutput out(result);
+ src.CopyTo(out);
+ return result;
+}
+
+TChunkedBuffer CopyData(TChunkedBuffer&& src) {
+ TChunkedBuffer result = CopyData(src);
+ src.Clear();
+ return result;
+}
+
+}