aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Processors/Formats/Impl/CapnProtoRowOutputFormat.cpp
blob: 92680a2c9ddd0d2be7340445ce82c9e6e53ad9e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <Processors/Formats/Impl/CapnProtoRowOutputFormat.h>
#if USE_CAPNP

#include <Formats/CapnProtoSchema.h>
#include <Formats/FormatSettings.h>
#include <Formats/CapnProtoSerializer.h>
#include <IO/WriteBuffer.h>
#error #include <capnp/dynamic.h>
#error #include <capnp/serialize-packed.h>

namespace DB
{

CapnProtoOutputStream::CapnProtoOutputStream(WriteBuffer & out_) : out(out_)
{
}

void CapnProtoOutputStream::write(const void * buffer, size_t size)
{
    out.write(reinterpret_cast<const char *>(buffer), size);
}

CapnProtoRowOutputFormat::CapnProtoRowOutputFormat(
    WriteBuffer & out_,
    const Block & header_,
    const CapnProtoSchemaInfo & info,
    const FormatSettings & format_settings)
    : IRowOutputFormat(header_, out_)
    , column_names(header_.getNames())
    , column_types(header_.getDataTypes())
    , output_stream(std::make_unique<CapnProtoOutputStream>(out_))
{
    schema = schema_parser.getMessageSchema(info.getSchemaInfo());
    const auto & header = getPort(PortKind::Main).getHeader();
    serializer = std::make_unique<CapnProtoSerializer>(header.getDataTypes(), header.getNames(), schema, format_settings.capn_proto);
    capnp::MallocMessageBuilder message;
}

void CapnProtoRowOutputFormat::write(const Columns & columns, size_t row_num)
{
    capnp::MallocMessageBuilder message;
    capnp::DynamicStruct::Builder root = message.initRoot<capnp::DynamicStruct>(schema);
    serializer->writeRow(columns, std::move(root), row_num);
    capnp::writeMessage(*output_stream, message);

}

void registerOutputFormatCapnProto(FormatFactory & factory)
{
    factory.registerOutputFormat("CapnProto", [](
        WriteBuffer & buf,
        const Block & sample,
        const FormatSettings & format_settings)
    {
            return std::make_shared<CapnProtoRowOutputFormat>(
                buf,
                sample,
                CapnProtoSchemaInfo(format_settings, "CapnProto", sample, format_settings.capn_proto.use_autogenerated_schema),
                format_settings);
    });
}

}

#else

namespace DB
{
class FormatFactory;
void registerOutputFormatCapnProto(FormatFactory &) {}
}

#endif // USE_CAPNP