1#pragma once
2
3#include "duckdb/common/serializer/format_serializer.hpp"
4
5namespace duckdb {
6
7struct BinarySerializer : public FormatSerializer {
8
9private:
10 struct State {
11 // how many fields are present in the object
12 uint32_t field_count;
13 // the size of the object
14 uint64_t size;
15 // the offset of the object start in the buffer
16 uint64_t offset;
17 };
18
19 const char *current_tag;
20
21 vector<data_t> data;
22 vector<State> stack;
23
24 template <class T>
25 void Write(T element) {
26 static_assert(std::is_trivially_destructible<T>(), "Write element must be trivially destructible");
27 WriteData(const_data_ptr_cast(&element), sizeof(T));
28 }
29 void WriteData(const_data_ptr_t buffer, idx_t write_size) {
30 data.insert(position: data.end(), first: buffer, last: buffer + write_size);
31 stack.back().size += write_size;
32 }
33 void WriteData(const char *ptr, idx_t write_size) {
34 WriteData(buffer: const_data_ptr_cast(src: ptr), write_size);
35 }
36
37 explicit BinarySerializer() {
38 serialize_enum_as_string = false;
39 }
40
41public:
42 template <class T>
43 static vector<data_t> Serialize(T &obj) {
44 BinarySerializer serializer;
45 serializer.OnObjectBegin();
46 obj.FormatSerialize(serializer);
47 serializer.OnObjectEnd();
48 return std::move(serializer.data);
49 }
50
51 void SetTag(const char *tag) final;
52
53 //===--------------------------------------------------------------------===//
54 // Nested Types Hooks
55 //===--------------------------------------------------------------------===//
56 void OnOptionalBegin(bool present) final;
57 void OnListBegin(idx_t count) final;
58 void OnListEnd(idx_t count) final;
59 void OnMapBegin(idx_t count) final;
60 void OnMapEntryBegin() final;
61 void OnMapEntryEnd() final;
62 void OnMapKeyBegin() final;
63 void OnMapValueBegin() final;
64 void OnMapEnd(idx_t count) final;
65 void OnObjectBegin() final;
66 void OnObjectEnd() final;
67 void OnPairBegin() final;
68 void OnPairKeyBegin() final;
69 void OnPairValueBegin() final;
70 void OnPairEnd() final;
71
72 //===--------------------------------------------------------------------===//
73 // Primitive Types
74 //===--------------------------------------------------------------------===//
75 void WriteNull() final;
76 void WriteValue(uint8_t value) final;
77 void WriteValue(int8_t value) final;
78 void WriteValue(uint16_t value) final;
79 void WriteValue(int16_t value) final;
80 void WriteValue(uint32_t value) final;
81 void WriteValue(int32_t value) final;
82 void WriteValue(uint64_t value) final;
83 void WriteValue(int64_t value) final;
84 void WriteValue(hugeint_t value) final;
85 void WriteValue(float value) final;
86 void WriteValue(double value) final;
87 void WriteValue(interval_t value) final;
88 void WriteValue(const string_t value) final;
89 void WriteValue(const string &value) final;
90 void WriteValue(const char *value) final;
91 void WriteValue(bool value) final;
92 void WriteDataPtr(const_data_ptr_t ptr, idx_t count) final;
93};
94
95} // namespace duckdb
96