2019-01-23 19:42:20 +00:00
|
|
|
#include <Formats/FormatFactory.h>
|
2019-01-25 20:02:03 +00:00
|
|
|
|
|
|
|
#include <Common/config.h>
|
|
|
|
#if USE_PROTOBUF
|
|
|
|
|
2019-02-22 13:51:25 +00:00
|
|
|
#include "ProtobufRowOutputStream.h"
|
2019-01-25 20:02:03 +00:00
|
|
|
|
|
|
|
#include <Core/Block.h>
|
2019-02-22 13:51:25 +00:00
|
|
|
#include <Formats/BlockOutputStreamFromRowOutputStream.h>
|
2019-01-23 19:42:20 +00:00
|
|
|
#include <Formats/FormatSchemaInfo.h>
|
|
|
|
#include <Formats/ProtobufSchemas.h>
|
2019-01-27 09:15:32 +00:00
|
|
|
#include <google/protobuf/descriptor.h>
|
2019-01-23 19:42:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2019-02-26 13:59:17 +00:00
|
|
|
ProtobufRowOutputStream::ProtobufRowOutputStream(WriteBuffer & out, const Block & header, const FormatSchemaInfo & info)
|
|
|
|
: data_types(header.getDataTypes()), writer(out, ProtobufSchemas::instance().getMessageTypeForFormatSchema(info), header.getNames())
|
2019-01-23 19:42:20 +00:00
|
|
|
{
|
2019-02-22 13:51:25 +00:00
|
|
|
}
|
2019-01-23 19:42:20 +00:00
|
|
|
|
2019-02-22 13:51:25 +00:00
|
|
|
void ProtobufRowOutputStream::write(const Block & block, size_t row_num)
|
|
|
|
{
|
2019-02-26 13:59:17 +00:00
|
|
|
writer.startMessage();
|
|
|
|
size_t column_index;
|
|
|
|
while (writer.writeField(column_index))
|
|
|
|
data_types[column_index]->serializeProtobuf(*block.getByPosition(column_index).column, row_num, writer);
|
|
|
|
writer.endMessage();
|
2019-01-23 19:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void registerOutputFormatProtobuf(FormatFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerOutputFormat(
|
2019-02-22 13:51:25 +00:00
|
|
|
"Protobuf", [](WriteBuffer & buf, const Block & header, const Context & context, const FormatSettings &)
|
2019-01-23 19:42:20 +00:00
|
|
|
{
|
2019-02-22 13:51:25 +00:00
|
|
|
return std::make_shared<BlockOutputStreamFromRowOutputStream>(
|
2019-02-26 13:59:17 +00:00
|
|
|
std::make_shared<ProtobufRowOutputStream>(buf, header, FormatSchemaInfo(context, "proto")), header);
|
2019-01-23 19:42:20 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-01-25 20:02:03 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
class FormatFactory;
|
|
|
|
void registerOutputFormatProtobuf(FormatFactory &) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|