2019-01-23 19:42:20 +00:00
|
|
|
#include <Formats/FormatFactory.h>
|
2019-01-25 20:02:03 +00:00
|
|
|
|
2019-06-20 09:12:49 +00:00
|
|
|
#include "config_formats.h"
|
2019-01-25 20:02:03 +00:00
|
|
|
#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 19:51:42 +00:00
|
|
|
ProtobufRowOutputStream::ProtobufRowOutputStream(WriteBuffer & out, const Block & header, const FormatSchemaInfo & format_schema)
|
|
|
|
: data_types(header.getDataTypes()), writer(out, ProtobufSchemas::instance().getMessageTypeForFormatSchema(format_schema), header.getNames())
|
2019-01-23 19:42:20 +00:00
|
|
|
{
|
2019-02-26 14:06:05 +00:00
|
|
|
value_indices.resize(header.columns());
|
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();
|
2019-02-26 14:06:05 +00:00
|
|
|
std::fill(value_indices.begin(), value_indices.end(), 0);
|
2019-02-26 13:59:17 +00:00
|
|
|
size_t column_index;
|
|
|
|
while (writer.writeField(column_index))
|
2019-02-26 14:06:05 +00:00
|
|
|
data_types[column_index]->serializeProtobuf(
|
|
|
|
*block.getByPosition(column_index).column, row_num, writer, value_indices[column_index]);
|
2019-02-26 13:59:17 +00:00
|
|
|
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-03-29 14:37:08 +00:00
|
|
|
std::make_shared<ProtobufRowOutputStream>(buf, header, FormatSchemaInfo(context, "Protobuf")), 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
|