1#include "duckdb/common/serializer/binary_serializer.hpp"
2
3namespace duckdb {
4
5void BinarySerializer::SetTag(const char *tag) {
6 current_tag = tag;
7
8 // Increment the number of fields
9 stack.back().field_count++;
10}
11
12//===--------------------------------------------------------------------===//
13// Nested types
14//===--------------------------------------------------------------------===//
15void BinarySerializer::OnOptionalBegin(bool present) {
16 Write(element: present);
17}
18
19void BinarySerializer::OnListBegin(idx_t count) {
20 Write(element: count);
21}
22
23void BinarySerializer::OnListEnd(idx_t count) {
24}
25
26// Serialize maps as arrays of objects with "key" and "value" properties.
27void BinarySerializer::OnMapBegin(idx_t count) {
28 Write(element: count);
29}
30
31void BinarySerializer::OnMapEntryBegin() {
32}
33
34void BinarySerializer::OnMapKeyBegin() {
35}
36
37void BinarySerializer::OnMapValueBegin() {
38}
39
40void BinarySerializer::OnMapEntryEnd() {
41}
42
43void BinarySerializer::OnMapEnd(idx_t count) {
44}
45
46void BinarySerializer::OnObjectBegin() {
47 stack.push_back(x: State({.field_count: 0, .size: 0, .offset: data.size()}));
48 Write<uint32_t>(element: 0); // Placeholder for the field count
49 Write<uint64_t>(element: 0); // Placeholder for the size
50}
51
52void BinarySerializer::OnObjectEnd() {
53 auto &frame = stack.back();
54 // Patch the field count and size
55 auto message_start = &data[frame.offset];
56 Store<uint32_t>(val: frame.field_count, ptr: message_start);
57 Store<uint64_t>(val: frame.size, ptr: message_start + sizeof(uint32_t));
58 stack.pop_back();
59}
60
61void BinarySerializer::OnPairBegin() {
62}
63
64void BinarySerializer::OnPairKeyBegin() {
65}
66
67void BinarySerializer::OnPairValueBegin() {
68}
69
70void BinarySerializer::OnPairEnd() {
71}
72
73//===--------------------------------------------------------------------===//
74// Primitive types
75//===--------------------------------------------------------------------===//
76void BinarySerializer::WriteNull() {
77 // This should never be called, optional writes should be handled by OnOptionalBegin
78}
79
80void BinarySerializer::WriteValue(uint8_t value) {
81 Write(element: value);
82}
83
84void BinarySerializer::WriteValue(int8_t value) {
85 Write(element: value);
86}
87
88void BinarySerializer::WriteValue(uint16_t value) {
89 Write(element: value);
90}
91
92void BinarySerializer::WriteValue(int16_t value) {
93 Write(element: value);
94}
95
96void BinarySerializer::WriteValue(uint32_t value) {
97 Write(element: value);
98}
99
100void BinarySerializer::WriteValue(int32_t value) {
101 Write(element: value);
102}
103
104void BinarySerializer::WriteValue(uint64_t value) {
105 Write(element: value);
106}
107
108void BinarySerializer::WriteValue(int64_t value) {
109 Write(element: value);
110}
111
112void BinarySerializer::WriteValue(hugeint_t value) {
113 Write(element: value);
114}
115
116void BinarySerializer::WriteValue(float value) {
117 Write(element: value);
118}
119
120void BinarySerializer::WriteValue(double value) {
121 Write(element: value);
122}
123
124void BinarySerializer::WriteValue(interval_t value) {
125 Write(element: value);
126}
127
128void BinarySerializer::WriteValue(const string &value) {
129 auto len = value.length();
130 Write<uint32_t>(element: (uint32_t)len);
131 if (len > 0) {
132 WriteData(ptr: value.c_str(), write_size: len);
133 }
134}
135
136void BinarySerializer::WriteValue(const string_t value) {
137 auto len = value.GetSize();
138 Write<uint32_t>(element: (uint32_t)len);
139 if (len > 0) {
140 WriteData(ptr: value.GetDataUnsafe(), write_size: len);
141 }
142}
143
144void BinarySerializer::WriteValue(const char *value) {
145 auto len = strlen(s: value);
146 Write<uint32_t>(element: (uint32_t)len);
147 if (len > 0) {
148 WriteData(ptr: value, write_size: len);
149 }
150}
151
152void BinarySerializer::WriteValue(bool value) {
153 Write(element: value);
154}
155
156void BinarySerializer::WriteDataPtr(const_data_ptr_t ptr, idx_t count) {
157 WriteData(buffer: ptr, write_size: count);
158}
159
160} // namespace duckdb
161