diff options
author | Alexander Smirnov <alex@ydb.tech> | 2024-04-16 09:11:59 +0000 |
---|---|---|
committer | Alexander Smirnov <alex@ydb.tech> | 2024-04-16 09:11:59 +0000 |
commit | 25de1d521ca218e2b040739fea77a39e9fc543e9 (patch) | |
tree | 21521d8866cf1462dbd52c071cf369974c29650e /library/cpp/clickhouse/client/base | |
parent | bf444b8ed4d0f6bf17fd753e2cf88f9440012e87 (diff) | |
parent | 0a63d9ddc516f206f2b8745ce5e5dfa60190d755 (diff) | |
download | ydb-25de1d521ca218e2b040739fea77a39e9fc543e9.tar.gz |
Merge branch 'rightlib' into mergelibs-240416-0910
Diffstat (limited to 'library/cpp/clickhouse/client/base')
-rw-r--r-- | library/cpp/clickhouse/client/base/coded.cpp | 101 | ||||
-rw-r--r-- | library/cpp/clickhouse/client/base/coded.h | 64 | ||||
-rw-r--r-- | library/cpp/clickhouse/client/base/compressed.cpp | 88 | ||||
-rw-r--r-- | library/cpp/clickhouse/client/base/compressed.h | 27 | ||||
-rw-r--r-- | library/cpp/clickhouse/client/base/wire_format.h | 103 | ||||
-rw-r--r-- | library/cpp/clickhouse/client/base/ya.make | 9 |
6 files changed, 0 insertions, 392 deletions
diff --git a/library/cpp/clickhouse/client/base/coded.cpp b/library/cpp/clickhouse/client/base/coded.cpp deleted file mode 100644 index 5a5d56d158..0000000000 --- a/library/cpp/clickhouse/client/base/coded.cpp +++ /dev/null @@ -1,101 +0,0 @@ -#include "coded.h" - -#include <memory.h> - -namespace NClickHouse { - static const int MAX_VARINT_BYTES = 10; - - TCodedInputStream::TCodedInputStream(IZeroCopyInput* input) - : Input_(input) - { - } - - bool TCodedInputStream::ReadRaw(void* buffer, size_t size) { - ui8* p = static_cast<ui8*>(buffer); - - while (size > 0) { - const void* ptr; - - if (size_t len = Input_->Next(&ptr, size)) { - memcpy(p, ptr, len); - - p += len; - size -= len; - } else { - break; - } - } - - return size == 0; - } - - bool TCodedInputStream::Skip(size_t count) { - while (count > 0) { - const void* ptr; - size_t len = Input_->Next(&ptr, count); - - if (len == 0) { - return false; - } - - count -= len; - } - - return true; - } - - bool TCodedInputStream::ReadVarint64(ui64* value) { - *value = 0; - - for (size_t i = 0; i < 9; ++i) { - ui8 byte; - - if (!Input_->Read(&byte, sizeof(byte))) { - return false; - } else { - *value |= (byte & 0x7F) << (7 * i); - - if (!(byte & 0x80)) { - return true; - } - } - } - - // TODO skip invalid - return false; - } - - TCodedOutputStream::TCodedOutputStream(IOutputStream* output) - : Output_(output) - { - } - - void TCodedOutputStream::Flush() { - Output_->Flush(); - } - - void TCodedOutputStream::WriteRaw(const void* buffer, int size) { - Output_->Write(buffer, size); - } - - void TCodedOutputStream::WriteVarint64(ui64 value) { - ui8 bytes[MAX_VARINT_BYTES]; - int size = 0; - - for (size_t i = 0; i < 9; ++i) { - ui8 byte = value & 0x7F; - if (value > 0x7F) - byte |= 0x80; - - bytes[size++] = byte; - - value >>= 7; - if (!value) { - break; - } - } - - WriteRaw(bytes, size); - } - -} diff --git a/library/cpp/clickhouse/client/base/coded.h b/library/cpp/clickhouse/client/base/coded.h deleted file mode 100644 index 486cfc8165..0000000000 --- a/library/cpp/clickhouse/client/base/coded.h +++ /dev/null @@ -1,64 +0,0 @@ -#pragma once - -#include <util/generic/string.h> -#include <util/stream/output.h> -#include <util/stream/zerocopy.h> - -namespace NClickHouse { - /** - * Class which reads and decodes binary data which is composed of varint- - * encoded integers and fixed-width pieces. - */ - class TCodedInputStream { - public: - TCodedInputStream() = default; - /// Create a CodedInputStream that reads from the given ZeroCopyInput. - explicit TCodedInputStream(IZeroCopyInput* input); - - // Read an unsigned integer with Varint encoding, truncating to 32 bits. - // Reading a 32-bit value is equivalent to reading a 64-bit one and casting - // it to uint32, but may be more efficient. - bool ReadVarint32(ui32* value); - - // Read an unsigned integer with Varint encoding. - bool ReadVarint64(ui64* value); - - // Read raw bytes, copying them into the given buffer. - bool ReadRaw(void* buffer, size_t size); - - // Like ReadRaw, but reads into a string. - // - // Implementation Note: ReadString() grows the string gradually as it - // reads in the data, rather than allocating the entire requested size - // upfront. This prevents denial-of-service attacks in which a client - // could claim that a string is going to be MAX_INT bytes long in order to - // crash the server because it can't allocate this much space at once. - bool ReadString(TString* buffer, int size); - - // Skips a number of bytes. Returns false if an underlying read error - // occurs. - bool Skip(size_t count); - - private: - IZeroCopyInput* Input_; - }; - - class TCodedOutputStream { - public: - TCodedOutputStream() = default; - /// Create a CodedInputStream that writes to the given ZeroCopyOutput. - explicit TCodedOutputStream(IOutputStream* output); - - void Flush(); - - // Write raw bytes, copying them from the given buffer. - void WriteRaw(const void* buffer, int size); - - /// Write an unsigned integer with Varint encoding. - void WriteVarint64(const ui64 value); - - private: - IOutputStream* Output_; - }; - -} diff --git a/library/cpp/clickhouse/client/base/compressed.cpp b/library/cpp/clickhouse/client/base/compressed.cpp deleted file mode 100644 index b883d534ee..0000000000 --- a/library/cpp/clickhouse/client/base/compressed.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include "compressed.h" -#include "wire_format.h" - -#include <util/generic/yexception.h> - -#include <contrib/libs/lz4/lz4.h> -#include <contrib/restricted/cityhash-1.0.2/city.h> - -#define DBMS_MAX_COMPRESSED_SIZE 0x40000000ULL // 1GB - -namespace NClickHouse { - TCompressedInput::TCompressedInput(TCodedInputStream* input) - : Input_(input) - { - } - - TCompressedInput::~TCompressedInput() { - if (!Mem_.Exhausted()) { - Y_ABORT("some data was not read"); - } - } - - size_t TCompressedInput::DoNext(const void** ptr, size_t len) { - if (Mem_.Exhausted()) { - if (!Decompress()) { - return 0; - } - } - - return Mem_.Next(ptr, len); - } - - bool TCompressedInput::Decompress() { - CityHash_v1_0_2::uint128 hash; - ui32 compressed = 0; - ui32 original = 0; - ui8 method = 0; - - if (!TWireFormat::ReadFixed(Input_, &hash)) { - return false; - } - if (!TWireFormat::ReadFixed(Input_, &method)) { - return false; - } - - if (method != 0x82) { - ythrow yexception() << "unsupported compression method " - << int(method); - } else { - if (!TWireFormat::ReadFixed(Input_, &compressed)) { - return false; - } - if (!TWireFormat::ReadFixed(Input_, &original)) { - return false; - } - - if (compressed > DBMS_MAX_COMPRESSED_SIZE) { - ythrow yexception() << "compressed data too big"; - } - - TTempBuf tmp(compressed); - - // Заполнить заголовок сжатых данных. - tmp.Append(&method, sizeof(method)); - tmp.Append(&compressed, sizeof(compressed)); - tmp.Append(&original, sizeof(original)); - - if (!TWireFormat::ReadBytes(Input_, tmp.Data() + 9, compressed - 9)) { - return false; - } else { - if (hash != CityHash_v1_0_2::CityHash128(tmp.Data(), compressed)) { - ythrow yexception() << "data was corrupted"; - } - } - - Data_ = TTempBuf(original); - - if (LZ4_decompress_fast(tmp.Data() + 9, Data_.Data(), original) < 0) { - ythrow yexception() << "can't decompress data"; - } else { - Mem_.Reset(Data_.Data(), original); - } - } - - return true; - } - -} diff --git a/library/cpp/clickhouse/client/base/compressed.h b/library/cpp/clickhouse/client/base/compressed.h deleted file mode 100644 index d7c628ebb7..0000000000 --- a/library/cpp/clickhouse/client/base/compressed.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include "coded.h" - -#include <util/memory/tempbuf.h> -#include <util/stream/zerocopy.h> -#include <util/stream/mem.h> - -namespace NClickHouse { - class TCompressedInput: public IZeroCopyInput { - public: - TCompressedInput(TCodedInputStream* input); - ~TCompressedInput(); - - protected: - size_t DoNext(const void** ptr, size_t len) override; - - bool Decompress(); - - private: - TCodedInputStream* const Input_; - - TTempBuf Data_; - TMemoryInput Mem_; - }; - -} diff --git a/library/cpp/clickhouse/client/base/wire_format.h b/library/cpp/clickhouse/client/base/wire_format.h deleted file mode 100644 index 805a2d3212..0000000000 --- a/library/cpp/clickhouse/client/base/wire_format.h +++ /dev/null @@ -1,103 +0,0 @@ -#pragma once - -#include "coded.h" - -#include <util/generic/string.h> -#include <util/memory/tempbuf.h> - -namespace NClickHouse { - class TWireFormat { - public: - template <typename T> - static bool ReadFixed(TCodedInputStream* input, T* value); - - static bool ReadString(TCodedInputStream* input, TString* value); - - static bool ReadBytes(TCodedInputStream* input, void* buf, size_t len); - - static bool ReadUInt64(TCodedInputStream* input, ui64* value); - - template <typename T> - static void WriteFixed(TCodedOutputStream* output, const T& value); - - static void WriteBytes(TCodedOutputStream* output, const void* buf, size_t len); - - static void WriteString(TCodedOutputStream* output, const TString& value); - - static void WriteStringBuf(TCodedOutputStream* output, const TStringBuf value); - - static void WriteUInt64(TCodedOutputStream* output, const ui64 value); - }; - - template <typename T> - inline bool TWireFormat::ReadFixed( - TCodedInputStream* input, - T* value) { - return input->ReadRaw(value, sizeof(T)); - } - - inline bool TWireFormat::ReadString( - TCodedInputStream* input, - TString* value) { - ui64 len; - - if (input->ReadVarint64(&len)) { - if (len > 0x00FFFFFFULL) { - return false; - } - TTempBuf buf(len); - if (input->ReadRaw(buf.Data(), (size_t)len)) { - value->assign(buf.Data(), len); - return true; - } - } - - return false; - } - - inline bool TWireFormat::ReadBytes( - TCodedInputStream* input, void* buf, size_t len) { - return input->ReadRaw(buf, len); - } - - inline bool TWireFormat::ReadUInt64( - TCodedInputStream* input, - ui64* value) { - return input->ReadVarint64(value); - } - - template <typename T> - inline void TWireFormat::WriteFixed( - TCodedOutputStream* output, - const T& value) { - output->WriteRaw(&value, sizeof(T)); - } - - inline void TWireFormat::WriteBytes( - TCodedOutputStream* output, - const void* buf, - size_t len) { - output->WriteRaw(buf, len); - } - - inline void TWireFormat::WriteString( - TCodedOutputStream* output, - const TString& value) { - output->WriteVarint64(value.size()); - output->WriteRaw(value.data(), value.size()); - } - - inline void TWireFormat::WriteStringBuf( - TCodedOutputStream* output, - const TStringBuf value) { - output->WriteVarint64(value.size()); - output->WriteRaw(value.data(), value.size()); - } - - inline void TWireFormat::WriteUInt64( - TCodedOutputStream* output, - const ui64 value) { - output->WriteVarint64(value); - } - -} diff --git a/library/cpp/clickhouse/client/base/ya.make b/library/cpp/clickhouse/client/base/ya.make deleted file mode 100644 index 8c3a1f6552..0000000000 --- a/library/cpp/clickhouse/client/base/ya.make +++ /dev/null @@ -1,9 +0,0 @@ -LIBRARY() - -SRCS( - coded.cpp - compressed.cpp - wire_format.h -) - -END() |