2019-01-23 19:39:19 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-09-28 12:57:31 +00:00
|
|
|
#include "config.h"
|
2019-02-22 14:22:12 +00:00
|
|
|
|
2020-04-16 12:31:57 +00:00
|
|
|
#if USE_PROTOBUF
|
2021-01-11 01:50:30 +00:00
|
|
|
# include <Core/Types.h>
|
|
|
|
# include <Common/PODArray.h>
|
2019-01-23 19:39:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2021-01-11 01:50:30 +00:00
|
|
|
class WriteBuffer;
|
2019-01-23 19:39:19 +00:00
|
|
|
|
2021-01-11 01:50:30 +00:00
|
|
|
/// Utility class for writing in the Protobuf format.
|
|
|
|
/// Knows nothing about protobuf schemas, just provides useful functions to serialize data.
|
|
|
|
class ProtobufWriter
|
2019-01-23 19:39:19 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-03-12 18:05:50 +00:00
|
|
|
explicit ProtobufWriter(WriteBuffer & out_);
|
2019-02-22 14:22:12 +00:00
|
|
|
~ProtobufWriter();
|
2019-01-23 19:39:19 +00:00
|
|
|
|
2019-02-26 13:59:17 +00:00
|
|
|
void startMessage();
|
2021-01-11 01:50:30 +00:00
|
|
|
void endMessage(bool with_length_delimiter);
|
2019-02-26 13:59:17 +00:00
|
|
|
|
2021-01-11 01:50:30 +00:00
|
|
|
void startNestedMessage();
|
|
|
|
void endNestedMessage(int field_number, bool is_group, bool skip_if_empty);
|
2019-02-26 13:59:17 +00:00
|
|
|
|
2021-01-11 01:50:30 +00:00
|
|
|
void writeInt(int field_number, Int64 value);
|
|
|
|
void writeUInt(int field_number, UInt64 value);
|
|
|
|
void writeSInt(int field_number, Int64 value);
|
|
|
|
template <typename T>
|
|
|
|
void writeFixed(int field_number, T value);
|
2022-07-14 16:11:35 +00:00
|
|
|
void writeString(int field_number, std::string_view str);
|
2020-08-19 11:52:17 +00:00
|
|
|
|
2021-01-11 01:50:30 +00:00
|
|
|
void startRepeatedPack();
|
|
|
|
void endRepeatedPack(int field_number, bool skip_if_empty);
|
2019-01-23 19:39:19 +00:00
|
|
|
|
|
|
|
private:
|
2021-01-11 01:50:30 +00:00
|
|
|
struct Piece
|
2019-02-26 13:59:17 +00:00
|
|
|
{
|
2021-01-11 01:50:30 +00:00
|
|
|
size_t start;
|
|
|
|
size_t end;
|
|
|
|
Piece(size_t start_, size_t end_) : start(start_), end(end_) {}
|
|
|
|
Piece() = default;
|
2019-02-26 13:59:17 +00:00
|
|
|
};
|
|
|
|
|
2021-01-11 01:50:30 +00:00
|
|
|
struct NestedInfo
|
2019-02-26 13:59:17 +00:00
|
|
|
{
|
2021-01-11 01:50:30 +00:00
|
|
|
size_t num_pieces_at_start;
|
|
|
|
size_t num_bytes_skipped_at_start;
|
|
|
|
NestedInfo(size_t num_pieces_at_start_, size_t num_bytes_skipped_at_start_)
|
|
|
|
: num_pieces_at_start(num_pieces_at_start_), num_bytes_skipped_at_start(num_bytes_skipped_at_start_)
|
2019-02-26 14:06:05 +00:00
|
|
|
{
|
|
|
|
}
|
2021-01-11 01:50:30 +00:00
|
|
|
};
|
2019-02-22 14:22:12 +00:00
|
|
|
|
2021-01-11 01:50:30 +00:00
|
|
|
WriteBuffer & out;
|
|
|
|
PODArray<UInt8> buffer;
|
|
|
|
std::vector<Piece> pieces;
|
|
|
|
size_t current_piece_start = 0;
|
|
|
|
size_t num_bytes_skipped = 0;
|
|
|
|
std::vector<NestedInfo> nested_infos;
|
|
|
|
bool in_repeated_pack = false;
|
2019-02-22 14:22:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2019-02-15 11:46:07 +00:00
|
|
|
#endif
|