2019-02-20 04:28:02 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-10-27 23:10:39 +00:00
|
|
|
#include "config_formats.h"
|
2019-02-20 04:28:02 +00:00
|
|
|
|
2020-04-16 12:31:57 +00:00
|
|
|
#if USE_PROTOBUF
|
2021-01-11 01:50:30 +00:00
|
|
|
# include <Common/PODArray.h>
|
|
|
|
# include <IO/ReadBuffer.h>
|
2019-02-20 04:28:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
class ReadBuffer;
|
|
|
|
|
2021-01-11 01:50:30 +00:00
|
|
|
/// Utility class for reading in the Protobuf format.
|
|
|
|
/// Knows nothing about protobuf schemas, just provides useful functions to serialize data.
|
|
|
|
class ProtobufReader
|
2019-02-20 04:28:02 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-03-12 18:05:50 +00:00
|
|
|
explicit ProtobufReader(ReadBuffer & in_);
|
2019-02-20 04:28:02 +00:00
|
|
|
|
2021-01-11 01:50:30 +00:00
|
|
|
void startMessage(bool with_length_delimiter_);
|
|
|
|
void endMessage(bool ignore_errors);
|
|
|
|
void startNestedMessage();
|
|
|
|
void endNestedMessage();
|
2019-02-20 04:28:02 +00:00
|
|
|
|
2021-01-11 01:50:30 +00:00
|
|
|
bool readFieldNumber(int & field_number);
|
|
|
|
Int64 readInt();
|
|
|
|
Int64 readSInt();
|
|
|
|
UInt64 readUInt();
|
|
|
|
template<typename T> T readFixed();
|
2019-02-20 04:28:02 +00:00
|
|
|
|
2021-01-11 01:50:30 +00:00
|
|
|
void readString(String & str);
|
|
|
|
void readStringAndAppend(PaddedPODArray<UInt8> & str);
|
2019-02-20 04:28:02 +00:00
|
|
|
|
2021-01-11 01:50:30 +00:00
|
|
|
bool eof() const { return in.eof(); }
|
2019-02-20 04:28:02 +00:00
|
|
|
|
|
|
|
private:
|
2021-01-11 01:50:30 +00:00
|
|
|
void readBinary(void * data, size_t size);
|
|
|
|
void ignore(UInt64 num_bytes);
|
|
|
|
void ignoreAll();
|
|
|
|
void moveCursorBackward(UInt64 num_bytes);
|
2019-02-20 04:28:02 +00:00
|
|
|
|
2021-01-11 01:50:30 +00:00
|
|
|
UInt64 ALWAYS_INLINE readVarint()
|
2019-02-20 04:28:02 +00:00
|
|
|
{
|
2021-01-11 01:50:30 +00:00
|
|
|
char c;
|
|
|
|
in.readStrict(c);
|
|
|
|
UInt64 first_byte = static_cast<UInt8>(c);
|
|
|
|
++cursor;
|
|
|
|
if (likely(!(c & 0x80)))
|
|
|
|
return first_byte;
|
|
|
|
return continueReadingVarint(first_byte);
|
|
|
|
}
|
|
|
|
|
|
|
|
UInt64 continueReadingVarint(UInt64 first_byte);
|
|
|
|
void ignoreVarint();
|
|
|
|
void ignoreGroup();
|
|
|
|
[[noreturn]] void throwUnknownFormat() const;
|
|
|
|
|
|
|
|
ReadBuffer & in;
|
|
|
|
Int64 cursor = 0;
|
|
|
|
bool root_message_has_length_delimiter = false;
|
|
|
|
size_t current_message_level = 0;
|
|
|
|
Int64 current_message_end = 0;
|
|
|
|
std::vector<Int64> parent_message_ends;
|
|
|
|
int field_number = 0;
|
|
|
|
int next_field_number = 0;
|
|
|
|
Int64 field_end = 0;
|
2019-02-20 04:28:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|