2019-02-22 14:22:12 +00:00
|
|
|
#include <Common/config.h>
|
2019-01-25 20:02:03 +00:00
|
|
|
#if USE_PROTOBUF
|
|
|
|
|
2019-01-23 19:39:19 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <math.h>
|
2019-02-22 14:22:12 +00:00
|
|
|
#include <optional>
|
2019-01-23 19:39:19 +00:00
|
|
|
#include <AggregateFunctions/IAggregateFunction.h>
|
|
|
|
#include <boost/numeric/conversion/cast.hpp>
|
|
|
|
#include <google/protobuf/descriptor.h>
|
|
|
|
#include <google/protobuf/descriptor.pb.h>
|
2019-02-22 14:22:12 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include "ProtobufWriter.h"
|
2019-01-23 19:39:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NOT_IMPLEMENTED;
|
|
|
|
extern const int NO_DATA_FOR_REQUIRED_PROTOBUF_FIELD;
|
2019-02-20 04:28:02 +00:00
|
|
|
extern const int PROTOBUF_BAD_CAST;
|
2019-01-23 19:39:19 +00:00
|
|
|
extern const int PROTOBUF_FIELD_NOT_REPEATED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-22 14:19:07 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
// Note: We cannot simply use writeVarUInt() from IO/VarInt.h here because there is one small difference:
|
|
|
|
// Google protobuf's representation of 64-bit integer contains from 1 to 10 bytes, whileas writeVarUInt() writes from 1 to 9 bytes
|
|
|
|
// because it omits the tenth byte (which is not necessary to decode actually).
|
|
|
|
void writePbVarUInt(UInt64 value, WriteBuffer & buf)
|
|
|
|
{
|
|
|
|
while (value >= 0x80)
|
|
|
|
{
|
|
|
|
buf.write(static_cast<char>(value | 0x80));
|
|
|
|
value >>= 7;
|
|
|
|
}
|
|
|
|
buf.write(static_cast<char>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void writePbVarInt(Int64 value, WriteBuffer & buf)
|
|
|
|
{
|
|
|
|
writePbVarUInt((static_cast<UInt64>(value) << 1) ^ static_cast<UInt64>(value >> 63), buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void writePbVarIntNoZigZagEncoding(Int64 value, WriteBuffer & buf) { writePbVarUInt(static_cast<UInt64>(value), buf); }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
enum ProtobufWriter::SimpleWriter::WireType : UInt32
|
|
|
|
{
|
|
|
|
VARINT = 0,
|
|
|
|
BITS64 = 1,
|
|
|
|
LENGTH_DELIMITED = 2,
|
|
|
|
BITS32 = 5
|
|
|
|
};
|
|
|
|
|
|
|
|
ProtobufWriter::SimpleWriter::SimpleWriter(WriteBuffer & out_) : out(out_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ProtobufWriter::SimpleWriter::~SimpleWriter()
|
|
|
|
{
|
|
|
|
finishCurrentMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::newMessage()
|
|
|
|
{
|
|
|
|
finishCurrentMessage();
|
|
|
|
were_messages = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::finishCurrentMessage()
|
|
|
|
{
|
|
|
|
if (!were_messages)
|
|
|
|
return;
|
|
|
|
finishCurrentField();
|
|
|
|
current_field_number = 0;
|
|
|
|
StringRef str = message_buffer.stringRef();
|
|
|
|
writePbVarUInt(str.size, out);
|
|
|
|
out.write(str.data, str.size);
|
|
|
|
message_buffer.restart();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::setCurrentField(UInt32 field_number)
|
|
|
|
{
|
|
|
|
finishCurrentField();
|
|
|
|
assert(current_field_number < field_number);
|
|
|
|
current_field_number = field_number;
|
|
|
|
num_normal_values = 0;
|
|
|
|
num_packed_values = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::finishCurrentField()
|
|
|
|
{
|
|
|
|
if (num_packed_values)
|
|
|
|
{
|
|
|
|
assert(!num_normal_values);
|
|
|
|
StringRef str = repeated_packing_buffer.stringRef();
|
|
|
|
if (str.size)
|
|
|
|
{
|
|
|
|
writeKey(LENGTH_DELIMITED, message_buffer);
|
|
|
|
writePbVarUInt(str.size, message_buffer);
|
|
|
|
message_buffer.write(str.data, str.size);
|
|
|
|
repeated_packing_buffer.restart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeKey(WireType wire_type, WriteBuffer & buf)
|
|
|
|
{
|
|
|
|
writePbVarUInt((current_field_number << 3) | wire_type, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeInt32(Int32 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writeKey(VARINT, message_buffer);
|
|
|
|
writePbVarIntNoZigZagEncoding(value, message_buffer);
|
|
|
|
++num_normal_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeUInt32(UInt32 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writeKey(VARINT, message_buffer);
|
|
|
|
writePbVarUInt(value, message_buffer);
|
|
|
|
++num_normal_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeSInt32(Int32 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writeKey(VARINT, message_buffer);
|
|
|
|
writePbVarInt(value, message_buffer);
|
|
|
|
++num_normal_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeInt64(Int64 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writeKey(VARINT, message_buffer);
|
|
|
|
writePbVarIntNoZigZagEncoding(value, message_buffer);
|
|
|
|
++num_normal_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeUInt64(UInt64 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writeKey(VARINT, message_buffer);
|
|
|
|
writePbVarUInt(value, message_buffer);
|
|
|
|
++num_normal_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeSInt64(Int64 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writeKey(VARINT, message_buffer);
|
|
|
|
writePbVarInt(value, message_buffer);
|
|
|
|
++num_normal_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeFixed32(UInt32 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writeKey(BITS32, message_buffer);
|
|
|
|
writePODBinary(value, message_buffer);
|
|
|
|
++num_normal_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeSFixed32(Int32 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writeKey(BITS32, message_buffer);
|
|
|
|
writePODBinary(value, message_buffer);
|
|
|
|
++num_normal_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeFloat(float value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writeKey(BITS32, message_buffer);
|
|
|
|
writePODBinary(value, message_buffer);
|
|
|
|
++num_normal_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeFixed64(UInt64 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writeKey(BITS64, message_buffer);
|
|
|
|
writePODBinary(value, message_buffer);
|
|
|
|
++num_normal_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeSFixed64(Int64 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writeKey(BITS64, message_buffer);
|
|
|
|
writePODBinary(value, message_buffer);
|
|
|
|
++num_normal_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeDouble(double value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writeKey(BITS64, message_buffer);
|
|
|
|
writePODBinary(value, message_buffer);
|
|
|
|
++num_normal_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeString(const StringRef & str)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
++num_normal_values;
|
|
|
|
writeKey(LENGTH_DELIMITED, message_buffer);
|
|
|
|
writePbVarUInt(str.size, message_buffer);
|
|
|
|
message_buffer.write(str.data, str.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeInt32IfNonZero(Int32 value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
writeInt32(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeUInt32IfNonZero(UInt32 value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
writeUInt32(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeSInt32IfNonZero(Int32 value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
writeSInt32(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeInt64IfNonZero(Int64 value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
writeInt64(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeUInt64IfNonZero(UInt64 value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
writeUInt64(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeSInt64IfNonZero(Int64 value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
writeSInt64(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeFixed32IfNonZero(UInt32 value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
writeFixed32(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeSFixed32IfNonZero(Int32 value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
writeSFixed32(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeFloatIfNonZero(float value)
|
|
|
|
{
|
|
|
|
if (value != 0)
|
|
|
|
writeFloat(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeFixed64IfNonZero(UInt64 value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
writeFixed64(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeSFixed64IfNonZero(Int64 value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
writeSFixed64(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeDoubleIfNonZero(double value)
|
|
|
|
{
|
|
|
|
if (value != 0)
|
|
|
|
writeDouble(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::writeStringIfNotEmpty(const StringRef & str)
|
|
|
|
{
|
|
|
|
if (str.size)
|
|
|
|
writeString(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::packRepeatedInt32(Int32 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writePbVarIntNoZigZagEncoding(value, repeated_packing_buffer);
|
|
|
|
++num_packed_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::packRepeatedUInt32(UInt32 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writePbVarUInt(value, repeated_packing_buffer);
|
|
|
|
++num_packed_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::packRepeatedSInt32(Int32 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writePbVarInt(value, repeated_packing_buffer);
|
|
|
|
++num_packed_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::packRepeatedInt64(Int64 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writePbVarIntNoZigZagEncoding(value, repeated_packing_buffer);
|
|
|
|
++num_packed_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::packRepeatedUInt64(UInt64 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writePbVarUInt(value, repeated_packing_buffer);
|
|
|
|
++num_packed_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::packRepeatedSInt64(Int64 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writePbVarInt(value, repeated_packing_buffer);
|
|
|
|
++num_packed_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::packRepeatedFixed32(UInt32 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writePODBinary(value, repeated_packing_buffer);
|
|
|
|
++num_packed_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::packRepeatedSFixed32(Int32 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writePODBinary(value, repeated_packing_buffer);
|
|
|
|
++num_packed_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::packRepeatedFloat(float value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writePODBinary(value, repeated_packing_buffer);
|
|
|
|
++num_packed_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::packRepeatedFixed64(UInt64 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writePODBinary(value, repeated_packing_buffer);
|
|
|
|
++num_packed_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::packRepeatedSFixed64(Int64 value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writePODBinary(value, repeated_packing_buffer);
|
|
|
|
++num_packed_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::SimpleWriter::packRepeatedDouble(double value)
|
|
|
|
{
|
|
|
|
assert(current_field_number);
|
|
|
|
writePODBinary(value, repeated_packing_buffer);
|
|
|
|
++num_packed_values;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-01-23 19:39:19 +00:00
|
|
|
class ProtobufWriter::Converter : private boost::noncopyable
|
|
|
|
{
|
|
|
|
public:
|
2019-02-22 14:19:07 +00:00
|
|
|
Converter(SimpleWriter & simple_writer_, const google::protobuf::FieldDescriptor * field_)
|
2019-01-23 19:39:19 +00:00
|
|
|
: simple_writer(simple_writer_), field(field_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~Converter() = default;
|
|
|
|
|
|
|
|
virtual void writeString(const StringRef &) { cannotConvertType("String"); }
|
|
|
|
|
|
|
|
virtual void writeInt8(Int8) { cannotConvertType("Int8"); }
|
|
|
|
virtual void writeUInt8(UInt8) { cannotConvertType("UInt8"); }
|
|
|
|
virtual void writeInt16(Int16) { cannotConvertType("Int16"); }
|
|
|
|
virtual void writeUInt16(UInt16) { cannotConvertType("UInt16"); }
|
|
|
|
virtual void writeInt32(Int32) { cannotConvertType("Int32"); }
|
|
|
|
virtual void writeUInt32(UInt32) { cannotConvertType("UInt32"); }
|
|
|
|
virtual void writeInt64(Int64) { cannotConvertType("Int64"); }
|
|
|
|
virtual void writeUInt64(UInt64) { cannotConvertType("UInt64"); }
|
|
|
|
virtual void writeUInt128(const UInt128 &) { cannotConvertType("UInt128"); }
|
|
|
|
virtual void writeFloat32(Float32) { cannotConvertType("Float32"); }
|
|
|
|
virtual void writeFloat64(Float64) { cannotConvertType("Float64"); }
|
|
|
|
|
|
|
|
virtual void prepareEnumMappingInt8(const std::vector<std::pair<std::string, Int8>> &) {}
|
|
|
|
virtual void prepareEnumMappingInt16(const std::vector<std::pair<std::string, Int16>> &) {}
|
|
|
|
virtual void writeEnumInt8(Int8) { cannotConvertType("Enum"); }
|
|
|
|
virtual void writeEnumInt16(Int16) { cannotConvertType("Enum"); }
|
|
|
|
|
|
|
|
virtual void writeUUID(const UUID &) { cannotConvertType("UUID"); }
|
|
|
|
virtual void writeDate(DayNum) { cannotConvertType("Date"); }
|
|
|
|
virtual void writeDateTime(time_t) { cannotConvertType("DateTime"); }
|
|
|
|
|
|
|
|
virtual void writeDecimal32(Decimal32, UInt32) { cannotConvertType("Decimal32"); }
|
|
|
|
virtual void writeDecimal64(Decimal64, UInt32) { cannotConvertType("Decimal64"); }
|
|
|
|
virtual void writeDecimal128(const Decimal128 &, UInt32) { cannotConvertType("Decimal128"); }
|
|
|
|
|
|
|
|
virtual void writeAggregateFunction(const AggregateFunctionPtr &, ConstAggregateDataPtr) { cannotConvertType("AggregateFunction"); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void cannotConvertType(const String & type_name)
|
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
"Could not convert data type '" + type_name + "' to protobuf type '" + field->type_name() + "' (field: " + field->name() + ")",
|
2019-02-20 04:28:02 +00:00
|
|
|
ErrorCodes::PROTOBUF_BAD_CAST);
|
2019-01-23 19:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cannotConvertValue(const String & value)
|
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
"Could not convert value '" + value + "' to protobuf type '" + field->type_name() + "' (field: " + field->name() + ")",
|
2019-02-20 04:28:02 +00:00
|
|
|
ErrorCodes::PROTOBUF_BAD_CAST);
|
2019-01-23 19:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename To, typename From>
|
|
|
|
To numericCast(From value)
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<To, From>)
|
|
|
|
return value;
|
|
|
|
To result;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
result = boost::numeric_cast<To>(value);
|
|
|
|
}
|
|
|
|
catch (boost::numeric::bad_numeric_cast &)
|
|
|
|
{
|
|
|
|
cannotConvertValue(toString(value));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename To>
|
|
|
|
To parseFromString(const StringRef & str)
|
|
|
|
{
|
|
|
|
To result;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
result = ::DB::parse<To>(str.data, str.size);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
cannotConvertValue(str.toString());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool packRepeated() const
|
|
|
|
{
|
|
|
|
if (!field->is_repeated())
|
|
|
|
return false;
|
|
|
|
if (field->options().has_packed())
|
|
|
|
return field->options().packed();
|
|
|
|
return field->file()->syntax() == google::protobuf::FileDescriptor::SYNTAX_PROTO3;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool skipNullValue() const
|
|
|
|
{
|
|
|
|
return field->is_optional() && (field->file()->syntax() == google::protobuf::FileDescriptor::SYNTAX_PROTO3);
|
|
|
|
}
|
|
|
|
|
2019-02-22 14:19:07 +00:00
|
|
|
SimpleWriter & simple_writer;
|
2019-01-23 19:39:19 +00:00
|
|
|
const google::protobuf::FieldDescriptor * field;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ProtobufWriter::ToStringConverter : public Converter
|
|
|
|
{
|
|
|
|
public:
|
2019-02-22 14:19:07 +00:00
|
|
|
ToStringConverter(SimpleWriter & simple_writer_, const google::protobuf::FieldDescriptor * field_)
|
2019-01-23 19:39:19 +00:00
|
|
|
: Converter(simple_writer_, field_)
|
|
|
|
{
|
|
|
|
initWriteFieldFunction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeString(const StringRef & str) override { writeField(str); }
|
|
|
|
|
|
|
|
void writeInt8(Int8 value) override { convertToStringAndWriteField(value); }
|
|
|
|
void writeUInt8(UInt8 value) override { convertToStringAndWriteField(value); }
|
|
|
|
void writeInt16(Int16 value) override { convertToStringAndWriteField(value); }
|
|
|
|
void writeUInt16(UInt16 value) override { convertToStringAndWriteField(value); }
|
|
|
|
void writeInt32(Int32 value) override { convertToStringAndWriteField(value); }
|
|
|
|
void writeUInt32(UInt32 value) override { convertToStringAndWriteField(value); }
|
|
|
|
void writeInt64(Int64 value) override { convertToStringAndWriteField(value); }
|
|
|
|
void writeUInt64(UInt64 value) override { convertToStringAndWriteField(value); }
|
|
|
|
void writeFloat32(Float32 value) override { convertToStringAndWriteField(value); }
|
|
|
|
void writeFloat64(Float64 value) override { convertToStringAndWriteField(value); }
|
|
|
|
|
|
|
|
void prepareEnumMappingInt8(const std::vector<std::pair<String, Int8>> & name_value_pairs) override
|
|
|
|
{
|
|
|
|
prepareEnumValueToNameMap(name_value_pairs);
|
|
|
|
}
|
|
|
|
void prepareEnumMappingInt16(const std::vector<std::pair<String, Int16>> & name_value_pairs) override
|
|
|
|
{
|
|
|
|
prepareEnumValueToNameMap(name_value_pairs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeEnumInt8(Int8 value) override { writeEnumInt16(value); }
|
|
|
|
|
|
|
|
void writeEnumInt16(Int16 value) override
|
|
|
|
{
|
|
|
|
auto it = enum_value_to_name_map->find(value);
|
|
|
|
if (it == enum_value_to_name_map->end())
|
|
|
|
cannotConvertValue(toString(value));
|
|
|
|
writeField(it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeUUID(const UUID & uuid) override { convertToStringAndWriteField(uuid); }
|
|
|
|
void writeDate(DayNum date) override { convertToStringAndWriteField(date); }
|
|
|
|
|
|
|
|
void writeDateTime(time_t tm) override
|
|
|
|
{
|
|
|
|
writeDateTimeText(tm, text_buffer);
|
|
|
|
writeField(text_buffer.stringRef());
|
|
|
|
text_buffer.restart();
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeDecimal32(Decimal32 decimal, UInt32 scale) override { writeDecimal(decimal, scale); }
|
|
|
|
void writeDecimal64(Decimal64 decimal, UInt32 scale) override { writeDecimal(decimal, scale); }
|
|
|
|
void writeDecimal128(const Decimal128 & decimal, UInt32 scale) override { writeDecimal(decimal, scale); }
|
|
|
|
|
|
|
|
void writeAggregateFunction(const AggregateFunctionPtr & function, ConstAggregateDataPtr place) override
|
|
|
|
{
|
|
|
|
function->serialize(place, text_buffer);
|
|
|
|
writeField(text_buffer.stringRef());
|
|
|
|
text_buffer.restart();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename T>
|
|
|
|
void convertToStringAndWriteField(T value)
|
|
|
|
{
|
|
|
|
writeText(value, text_buffer);
|
|
|
|
writeField(text_buffer.stringRef());
|
|
|
|
text_buffer.restart();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void writeDecimal(const Decimal<T> & decimal, UInt32 scale)
|
|
|
|
{
|
|
|
|
writeText(decimal, scale, text_buffer);
|
|
|
|
writeField(text_buffer.stringRef());
|
|
|
|
text_buffer.restart();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void prepareEnumValueToNameMap(const std::vector<std::pair<String, T>> & name_value_pairs)
|
|
|
|
{
|
|
|
|
if (enum_value_to_name_map.has_value())
|
|
|
|
return;
|
|
|
|
enum_value_to_name_map.emplace();
|
|
|
|
for (const auto & name_value_pair : name_value_pairs)
|
|
|
|
enum_value_to_name_map->emplace(name_value_pair.second, name_value_pair.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeField(const StringRef & str) { (simple_writer.*write_field_function)(str); }
|
|
|
|
|
|
|
|
void initWriteFieldFunction()
|
|
|
|
{
|
2019-02-22 14:19:07 +00:00
|
|
|
write_field_function = skipNullValue() ? &SimpleWriter::writeStringIfNotEmpty : &SimpleWriter::writeString;
|
2019-01-23 19:39:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 14:19:07 +00:00
|
|
|
void (SimpleWriter::*write_field_function)(const StringRef & str);
|
2019-01-23 19:39:19 +00:00
|
|
|
WriteBufferFromOwnString text_buffer;
|
|
|
|
std::optional<std::unordered_map<Int16, String>> enum_value_to_name_map;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class ProtobufWriter::ToNumberConverter : public Converter
|
|
|
|
{
|
|
|
|
public:
|
2019-02-22 14:19:07 +00:00
|
|
|
ToNumberConverter(SimpleWriter & simple_writer_, const google::protobuf::FieldDescriptor * field_)
|
2019-01-23 19:39:19 +00:00
|
|
|
: Converter(simple_writer_, field_)
|
|
|
|
{
|
|
|
|
initWriteFieldFunction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeString(const StringRef & str) override { writeField(parseFromString<T>(str)); }
|
|
|
|
|
|
|
|
void writeInt8(Int8 value) override { castNumericAndWriteField(value); }
|
|
|
|
void writeUInt8(UInt8 value) override { castNumericAndWriteField(value); }
|
|
|
|
void writeInt16(Int16 value) override { castNumericAndWriteField(value); }
|
|
|
|
void writeUInt16(UInt16 value) override { castNumericAndWriteField(value); }
|
|
|
|
void writeInt32(Int32 value) override { castNumericAndWriteField(value); }
|
|
|
|
void writeUInt32(UInt32 value) override { castNumericAndWriteField(value); }
|
|
|
|
void writeInt64(Int64 value) override { castNumericAndWriteField(value); }
|
|
|
|
void writeUInt64(UInt64 value) override { castNumericAndWriteField(value); }
|
|
|
|
void writeFloat32(Float32 value) override { castNumericAndWriteField(value); }
|
|
|
|
void writeFloat64(Float64 value) override { castNumericAndWriteField(value); }
|
|
|
|
|
|
|
|
void writeEnumInt8(Int8 value) override { writeEnumInt16(value); }
|
|
|
|
|
|
|
|
void writeEnumInt16(Int16 value) override
|
|
|
|
{
|
|
|
|
if constexpr (!std::is_integral_v<T>)
|
|
|
|
cannotConvertType("Enum"); // It's not correct to convert enum to floating point.
|
|
|
|
castNumericAndWriteField(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeDate(DayNum date) override { castNumericAndWriteField(static_cast<UInt16>(date)); }
|
|
|
|
void writeDateTime(time_t tm) override { castNumericAndWriteField(tm); }
|
|
|
|
|
|
|
|
void writeDecimal32(Decimal32 decimal, UInt32 scale) override { writeDecimal(decimal, scale); }
|
|
|
|
void writeDecimal64(Decimal64 decimal, UInt32 scale) override { writeDecimal(decimal, scale); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename From>
|
|
|
|
void castNumericAndWriteField(From value)
|
|
|
|
{
|
|
|
|
writeField(numericCast<T>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename S>
|
|
|
|
void writeDecimal(const Decimal<S> & decimal, UInt32 scale)
|
|
|
|
{
|
|
|
|
if constexpr (std::is_integral_v<T>)
|
|
|
|
castNumericAndWriteField(decimal.value / decimalScaleMultiplier<S>(scale));
|
|
|
|
else
|
|
|
|
castNumericAndWriteField(double(decimal.value) * pow(10., -double(scale)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeField(T value) { (simple_writer.*write_field_function)(value); }
|
|
|
|
|
|
|
|
void initWriteFieldFunction()
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<T, Int32>)
|
|
|
|
{
|
|
|
|
switch (field->type())
|
|
|
|
{
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_INT32:
|
|
|
|
write_field_function = packRepeated()
|
2019-02-22 14:19:07 +00:00
|
|
|
? &SimpleWriter::packRepeatedInt32
|
|
|
|
: (skipNullValue() ? &SimpleWriter::writeInt32IfNonZero : &SimpleWriter::writeInt32);
|
2019-01-23 19:39:19 +00:00
|
|
|
break;
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_SINT32:
|
|
|
|
write_field_function = packRepeated()
|
2019-02-22 14:19:07 +00:00
|
|
|
? &SimpleWriter::packRepeatedSInt32
|
|
|
|
: (skipNullValue() ? &SimpleWriter::writeSInt32IfNonZero : &SimpleWriter::writeSInt32);
|
2019-01-23 19:39:19 +00:00
|
|
|
break;
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_SFIXED32:
|
|
|
|
write_field_function = packRepeated()
|
2019-02-22 14:19:07 +00:00
|
|
|
? &SimpleWriter::packRepeatedSFixed32
|
|
|
|
: (skipNullValue() ? &SimpleWriter::writeSFixed32IfNonZero : &SimpleWriter::writeSFixed32);
|
2019-01-23 19:39:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<T, UInt32>)
|
|
|
|
{
|
|
|
|
switch (field->type())
|
|
|
|
{
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_UINT32:
|
|
|
|
write_field_function = packRepeated()
|
2019-02-22 14:19:07 +00:00
|
|
|
? &SimpleWriter::packRepeatedUInt32
|
|
|
|
: (skipNullValue() ? &SimpleWriter::writeUInt32IfNonZero : &SimpleWriter::writeUInt32);
|
2019-01-23 19:39:19 +00:00
|
|
|
break;
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_FIXED32:
|
|
|
|
write_field_function = packRepeated()
|
2019-02-22 14:19:07 +00:00
|
|
|
? &SimpleWriter::packRepeatedFixed32
|
|
|
|
: (skipNullValue() ? &SimpleWriter::writeFixed32IfNonZero : &SimpleWriter::writeFixed32);
|
2019-01-23 19:39:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<T, Int64>)
|
|
|
|
{
|
|
|
|
switch (field->type())
|
|
|
|
{
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_INT64:
|
|
|
|
write_field_function = packRepeated()
|
2019-02-22 14:19:07 +00:00
|
|
|
? &SimpleWriter::packRepeatedInt64
|
|
|
|
: (skipNullValue() ? &SimpleWriter::writeInt64IfNonZero : &SimpleWriter::writeInt64);
|
2019-01-23 19:39:19 +00:00
|
|
|
break;
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_SINT64:
|
|
|
|
write_field_function = packRepeated()
|
2019-02-22 14:19:07 +00:00
|
|
|
? &SimpleWriter::packRepeatedSInt64
|
|
|
|
: (skipNullValue() ? &SimpleWriter::writeSInt64IfNonZero : &SimpleWriter::writeSInt64);
|
2019-01-23 19:39:19 +00:00
|
|
|
break;
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_SFIXED64:
|
|
|
|
write_field_function = packRepeated()
|
2019-02-22 14:19:07 +00:00
|
|
|
? &SimpleWriter::packRepeatedSFixed64
|
|
|
|
: (skipNullValue() ? &SimpleWriter::writeSFixed64IfNonZero : &SimpleWriter::writeSFixed64);
|
2019-01-23 19:39:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<T, UInt64>)
|
|
|
|
{
|
|
|
|
switch (field->type())
|
|
|
|
{
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_UINT64:
|
|
|
|
write_field_function = packRepeated()
|
2019-02-22 14:19:07 +00:00
|
|
|
? &SimpleWriter::packRepeatedUInt64
|
|
|
|
: (skipNullValue() ? &SimpleWriter::writeUInt64IfNonZero : &SimpleWriter::writeUInt64);
|
2019-01-23 19:39:19 +00:00
|
|
|
break;
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_FIXED64:
|
|
|
|
write_field_function = packRepeated()
|
2019-02-22 14:19:07 +00:00
|
|
|
? &SimpleWriter::packRepeatedFixed64
|
|
|
|
: (skipNullValue() ? &SimpleWriter::writeFixed64IfNonZero : &SimpleWriter::writeFixed64);
|
2019-01-23 19:39:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<T, float>)
|
|
|
|
{
|
|
|
|
write_field_function = packRepeated()
|
2019-02-22 14:19:07 +00:00
|
|
|
? &SimpleWriter::packRepeatedFloat
|
|
|
|
: (skipNullValue() ? &SimpleWriter::writeFloatIfNonZero : &SimpleWriter::writeFloat);
|
2019-01-23 19:39:19 +00:00
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<T, double>)
|
|
|
|
{
|
|
|
|
write_field_function = packRepeated()
|
2019-02-22 14:19:07 +00:00
|
|
|
? &SimpleWriter::packRepeatedDouble
|
|
|
|
: (skipNullValue() ? &SimpleWriter::writeDoubleIfNonZero : &SimpleWriter::writeDouble);
|
2019-01-23 19:39:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 14:19:07 +00:00
|
|
|
void (SimpleWriter::*write_field_function)(T value);
|
2019-01-23 19:39:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ProtobufWriter::ToBoolConverter : public Converter
|
|
|
|
{
|
|
|
|
public:
|
2019-02-22 14:19:07 +00:00
|
|
|
ToBoolConverter(SimpleWriter & simple_writer_, const google::protobuf::FieldDescriptor * field_)
|
2019-01-23 19:39:19 +00:00
|
|
|
: Converter(simple_writer_, field_)
|
|
|
|
{
|
|
|
|
initWriteFieldFunction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeString(const StringRef & str) override
|
|
|
|
{
|
|
|
|
if (str == "true")
|
|
|
|
writeField(true);
|
|
|
|
else if (str == "false")
|
|
|
|
writeField(false);
|
|
|
|
else
|
|
|
|
cannotConvertValue(str.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeInt8(Int8 value) override { convertToBoolAndWriteField(value); }
|
|
|
|
void writeUInt8(UInt8 value) override { convertToBoolAndWriteField(value); }
|
|
|
|
void writeInt16(Int16 value) override { convertToBoolAndWriteField(value); }
|
|
|
|
void writeUInt16(UInt16 value) override { convertToBoolAndWriteField(value); }
|
|
|
|
void writeInt32(Int32 value) override { convertToBoolAndWriteField(value); }
|
|
|
|
void writeUInt32(UInt32 value) override { convertToBoolAndWriteField(value); }
|
|
|
|
void writeInt64(Int64 value) override { convertToBoolAndWriteField(value); }
|
|
|
|
void writeUInt64(UInt64 value) override { convertToBoolAndWriteField(value); }
|
|
|
|
void writeFloat32(Float32 value) override { convertToBoolAndWriteField(value); }
|
|
|
|
void writeFloat64(Float64 value) override { convertToBoolAndWriteField(value); }
|
|
|
|
void writeDecimal32(Decimal32 decimal, UInt32) override { convertToBoolAndWriteField(decimal.value); }
|
|
|
|
void writeDecimal64(Decimal64 decimal, UInt32) override { convertToBoolAndWriteField(decimal.value); }
|
|
|
|
void writeDecimal128(const Decimal128 & decimal, UInt32) override { convertToBoolAndWriteField(decimal.value); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename T>
|
|
|
|
void convertToBoolAndWriteField(T value)
|
|
|
|
{
|
|
|
|
writeField(static_cast<bool>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeField(bool b) { (simple_writer.*write_field_function)(b); }
|
|
|
|
|
|
|
|
void initWriteFieldFunction()
|
|
|
|
{
|
|
|
|
write_field_function = packRepeated()
|
2019-02-22 14:19:07 +00:00
|
|
|
? &SimpleWriter::packRepeatedUInt32
|
|
|
|
: (skipNullValue() ? &SimpleWriter::writeUInt32IfNonZero : &SimpleWriter::writeUInt32);
|
2019-01-23 19:39:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 14:19:07 +00:00
|
|
|
void (SimpleWriter::*write_field_function)(UInt32 b);
|
2019-01-23 19:39:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ProtobufWriter::ToEnumConverter : public Converter
|
|
|
|
{
|
|
|
|
public:
|
2019-02-22 14:19:07 +00:00
|
|
|
ToEnumConverter(SimpleWriter & simple_writer_, const google::protobuf::FieldDescriptor * field_)
|
2019-01-23 19:39:19 +00:00
|
|
|
: Converter(simple_writer_, field_)
|
|
|
|
{
|
|
|
|
initWriteFieldFunction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeString(const StringRef & str) override
|
|
|
|
{
|
|
|
|
prepareEnumNameToPbNumberMap();
|
|
|
|
auto it = enum_name_to_pbnumber_map->find(str);
|
|
|
|
if (it == enum_name_to_pbnumber_map->end())
|
|
|
|
cannotConvertValue(str.toString());
|
|
|
|
writeField(it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeInt8(Int8 value) override { convertToEnumAndWriteField(value); }
|
|
|
|
void writeUInt8(UInt8 value) override { convertToEnumAndWriteField(value); }
|
|
|
|
void writeInt16(Int16 value) override { convertToEnumAndWriteField(value); }
|
|
|
|
void writeUInt16(UInt16 value) override { convertToEnumAndWriteField(value); }
|
|
|
|
void writeInt32(Int32 value) override { convertToEnumAndWriteField(value); }
|
|
|
|
void writeUInt32(UInt32 value) override { convertToEnumAndWriteField(value); }
|
|
|
|
void writeInt64(Int64 value) override { convertToEnumAndWriteField(value); }
|
|
|
|
void writeUInt64(UInt64 value) override { convertToEnumAndWriteField(value); }
|
|
|
|
|
|
|
|
void prepareEnumMappingInt8(const std::vector<std::pair<String, Int8>> & name_value_pairs) override
|
|
|
|
{
|
|
|
|
prepareEnumValueToPbNumberMap(name_value_pairs);
|
|
|
|
}
|
|
|
|
void prepareEnumMappingInt16(const std::vector<std::pair<String, Int16>> & name_value_pairs) override
|
|
|
|
{
|
|
|
|
prepareEnumValueToPbNumberMap(name_value_pairs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeEnumInt8(Int8 value) override { writeEnumInt16(value); }
|
|
|
|
|
|
|
|
void writeEnumInt16(Int16 value) override
|
|
|
|
{
|
|
|
|
auto it = enum_value_to_pbnumber_map->find(value);
|
|
|
|
if (it == enum_value_to_pbnumber_map->end())
|
|
|
|
cannotConvertValue(toString(value));
|
|
|
|
writeField(it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename T>
|
|
|
|
void convertToEnumAndWriteField(T value)
|
|
|
|
{
|
|
|
|
const auto * enum_descriptor = field->enum_type()->FindValueByNumber(numericCast<int>(value));
|
|
|
|
if (!enum_descriptor)
|
|
|
|
cannotConvertValue(toString(value));
|
|
|
|
writeField(enum_descriptor->number());
|
|
|
|
}
|
|
|
|
|
|
|
|
void prepareEnumNameToPbNumberMap()
|
|
|
|
{
|
|
|
|
if (enum_name_to_pbnumber_map.has_value())
|
|
|
|
return;
|
|
|
|
enum_name_to_pbnumber_map.emplace();
|
|
|
|
const auto * enum_type = field->enum_type();
|
|
|
|
for (int i = 0; i != enum_type->value_count(); ++i)
|
|
|
|
{
|
|
|
|
const auto * enum_value = enum_type->value(i);
|
|
|
|
enum_name_to_pbnumber_map->emplace(enum_value->name(), enum_value->number());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void prepareEnumValueToPbNumberMap(const std::vector<std::pair<String, T>> & name_value_pairs)
|
|
|
|
{
|
|
|
|
if (enum_value_to_pbnumber_map.has_value())
|
|
|
|
return;
|
|
|
|
enum_value_to_pbnumber_map.emplace();
|
|
|
|
for (const auto & name_value_pair : name_value_pairs)
|
|
|
|
{
|
|
|
|
Int16 value = name_value_pair.second;
|
|
|
|
const auto * enum_descriptor = field->enum_type()->FindValueByName(name_value_pair.first);
|
|
|
|
if (enum_descriptor)
|
|
|
|
enum_value_to_pbnumber_map->emplace(value, enum_descriptor->number());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeField(int enum_number) { (simple_writer.*write_field_function)(enum_number); }
|
|
|
|
|
|
|
|
void initWriteFieldFunction()
|
|
|
|
{
|
|
|
|
write_field_function = packRepeated()
|
2019-02-22 14:19:07 +00:00
|
|
|
? &SimpleWriter::packRepeatedUInt32
|
|
|
|
: (skipNullValue() ? &SimpleWriter::writeUInt32IfNonZero : &SimpleWriter::writeUInt32);
|
2019-01-23 19:39:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 14:19:07 +00:00
|
|
|
void (SimpleWriter::*write_field_function)(UInt32 enum_number);
|
2019-01-23 19:39:19 +00:00
|
|
|
std::optional<std::unordered_map<StringRef, int>> enum_name_to_pbnumber_map;
|
|
|
|
std::optional<std::unordered_map<Int16, int>> enum_value_to_pbnumber_map;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ProtobufWriter::ProtobufWriter(WriteBuffer & out, const google::protobuf::Descriptor * message_type) : simple_writer(out)
|
|
|
|
{
|
|
|
|
enumerateFieldsInWriteOrder(message_type);
|
|
|
|
createConverters();
|
|
|
|
}
|
|
|
|
|
|
|
|
ProtobufWriter::~ProtobufWriter()
|
|
|
|
{
|
|
|
|
finishCurrentMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::enumerateFieldsInWriteOrder(const google::protobuf::Descriptor * message_type)
|
|
|
|
{
|
|
|
|
assert(fields_in_write_order.empty());
|
|
|
|
fields_in_write_order.reserve(message_type->field_count());
|
|
|
|
for (int i = 0; i < message_type->field_count(); ++i)
|
|
|
|
fields_in_write_order.emplace_back(message_type->field(i));
|
|
|
|
|
|
|
|
std::sort(
|
|
|
|
fields_in_write_order.begin(),
|
|
|
|
fields_in_write_order.end(),
|
|
|
|
[](const google::protobuf::FieldDescriptor * left, const google::protobuf::FieldDescriptor * right)
|
|
|
|
{
|
|
|
|
return left->number() < right->number();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::createConverters()
|
|
|
|
{
|
|
|
|
assert(converters.empty());
|
|
|
|
converters.reserve(fields_in_write_order.size());
|
|
|
|
for (size_t i = 0; i != fields_in_write_order.size(); ++i)
|
|
|
|
{
|
|
|
|
const auto * field = fields_in_write_order[i];
|
|
|
|
std::unique_ptr<Converter> converter;
|
|
|
|
switch (field->type())
|
|
|
|
{
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_INT32:
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_SINT32:
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_SFIXED32:
|
|
|
|
converter = std::make_unique<ToNumberConverter<Int32>>(simple_writer, field);
|
|
|
|
break;
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_UINT32:
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_FIXED32:
|
|
|
|
converter = std::make_unique<ToNumberConverter<UInt32>>(simple_writer, field);
|
|
|
|
break;
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_INT64:
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_SFIXED64:
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_SINT64:
|
|
|
|
converter = std::make_unique<ToNumberConverter<Int64>>(simple_writer, field);
|
|
|
|
break;
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_UINT64:
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_FIXED64:
|
|
|
|
converter = std::make_unique<ToNumberConverter<UInt64>>(simple_writer, field);
|
|
|
|
break;
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_FLOAT:
|
|
|
|
converter = std::make_unique<ToNumberConverter<float>>(simple_writer, field);
|
|
|
|
break;
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_DOUBLE:
|
|
|
|
converter = std::make_unique<ToNumberConverter<double>>(simple_writer, field);
|
|
|
|
break;
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_BOOL:
|
|
|
|
converter = std::make_unique<ToBoolConverter>(simple_writer, field);
|
|
|
|
break;
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_ENUM:
|
|
|
|
converter = std::make_unique<ToEnumConverter>(simple_writer, field);
|
|
|
|
break;
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_STRING:
|
|
|
|
case google::protobuf::FieldDescriptor::TYPE_BYTES:
|
|
|
|
converter = std::make_unique<ToStringConverter>(simple_writer, field);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw Exception(String("Protobuf type '") + field->type_name() + "' isn't supported", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
converters.emplace_back(std::move(converter));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<const google::protobuf::FieldDescriptor *> & ProtobufWriter::fieldsInWriteOrder() const
|
|
|
|
{
|
|
|
|
return fields_in_write_order;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::newMessage()
|
|
|
|
{
|
|
|
|
finishCurrentMessage();
|
|
|
|
simple_writer.newMessage();
|
|
|
|
if (fields_in_write_order.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
current_field_index = 0;
|
|
|
|
current_field = fields_in_write_order[current_field_index];
|
|
|
|
current_converter = converters[current_field_index].get();
|
|
|
|
simple_writer.setCurrentField(current_field->number());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::finishCurrentMessage()
|
|
|
|
{
|
|
|
|
if (current_field)
|
|
|
|
{
|
|
|
|
assert(current_field_index == fields_in_write_order.size() - 1);
|
|
|
|
finishCurrentField();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProtobufWriter::nextField()
|
|
|
|
{
|
|
|
|
if (current_field_index == fields_in_write_order.size() - 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
finishCurrentField();
|
|
|
|
|
|
|
|
++current_field_index;
|
|
|
|
current_field = fields_in_write_order[current_field_index];
|
|
|
|
current_converter = converters[current_field_index].get();
|
|
|
|
simple_writer.setCurrentField(current_field->number());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::finishCurrentField()
|
|
|
|
{
|
|
|
|
assert(current_field);
|
|
|
|
size_t num_values = simple_writer.numValues();
|
|
|
|
if (num_values == 0)
|
|
|
|
{
|
|
|
|
if (current_field->is_required())
|
|
|
|
throw Exception(
|
|
|
|
"No data for the required field '" + current_field->name() + "'", ErrorCodes::NO_DATA_FOR_REQUIRED_PROTOBUF_FIELD);
|
|
|
|
}
|
|
|
|
else if (num_values > 1 && !current_field->is_repeated())
|
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
"Cannot write more than single value to the non-repeated field '" + current_field->name() + "'",
|
|
|
|
ErrorCodes::PROTOBUF_FIELD_NOT_REPEATED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeNumber(Int8 value)
|
|
|
|
{
|
|
|
|
current_converter->writeInt8(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeNumber(UInt8 value)
|
|
|
|
{
|
|
|
|
current_converter->writeUInt8(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeNumber(Int16 value)
|
|
|
|
{
|
|
|
|
current_converter->writeInt16(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeNumber(UInt16 value)
|
|
|
|
{
|
|
|
|
current_converter->writeUInt16(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeNumber(Int32 value)
|
|
|
|
{
|
|
|
|
current_converter->writeInt32(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeNumber(UInt32 value)
|
|
|
|
{
|
|
|
|
current_converter->writeUInt32(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeNumber(Int64 value)
|
|
|
|
{
|
|
|
|
current_converter->writeInt64(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeNumber(UInt64 value)
|
|
|
|
{
|
|
|
|
current_converter->writeUInt64(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeNumber(UInt128 value)
|
|
|
|
{
|
|
|
|
current_converter->writeUInt128(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeNumber(Float32 value)
|
|
|
|
{
|
|
|
|
current_converter->writeFloat32(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeNumber(Float64 value)
|
|
|
|
{
|
|
|
|
current_converter->writeFloat64(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeString(const StringRef & str)
|
|
|
|
{
|
|
|
|
current_converter->writeString(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::prepareEnumMapping(const std::vector<std::pair<std::string, Int8>> & enum_values)
|
|
|
|
{
|
|
|
|
current_converter->prepareEnumMappingInt8(enum_values);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::prepareEnumMapping(const std::vector<std::pair<std::string, Int16>> & enum_values)
|
|
|
|
{
|
|
|
|
current_converter->prepareEnumMappingInt16(enum_values);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeEnum(Int8 value)
|
|
|
|
{
|
|
|
|
current_converter->writeEnumInt8(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeEnum(Int16 value)
|
|
|
|
{
|
|
|
|
current_converter->writeEnumInt16(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeUUID(const UUID & uuid)
|
|
|
|
{
|
|
|
|
current_converter->writeUUID(uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeDate(DayNum date)
|
|
|
|
{
|
|
|
|
current_converter->writeDate(date);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeDateTime(time_t tm)
|
|
|
|
{
|
|
|
|
current_converter->writeDateTime(tm);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeDecimal(Decimal32 decimal, UInt32 scale)
|
|
|
|
{
|
|
|
|
current_converter->writeDecimal32(decimal, scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeDecimal(Decimal64 decimal, UInt32 scale)
|
|
|
|
{
|
|
|
|
current_converter->writeDecimal64(decimal, scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeDecimal(const Decimal128 & decimal, UInt32 scale)
|
|
|
|
{
|
|
|
|
current_converter->writeDecimal128(decimal, scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtobufWriter::writeAggregateFunction(const AggregateFunctionPtr & function, ConstAggregateDataPtr place)
|
|
|
|
{
|
|
|
|
current_converter->writeAggregateFunction(function, place);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-01-25 20:02:03 +00:00
|
|
|
#endif
|