| 1 | #include "duckdb/common/serializer/binary_serializer.hpp" |
| 2 | |
| 3 | namespace duckdb { |
| 4 | |
| 5 | void 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 | //===--------------------------------------------------------------------===// |
| 15 | void BinarySerializer::OnOptionalBegin(bool present) { |
| 16 | Write(element: present); |
| 17 | } |
| 18 | |
| 19 | void BinarySerializer::OnListBegin(idx_t count) { |
| 20 | Write(element: count); |
| 21 | } |
| 22 | |
| 23 | void BinarySerializer::OnListEnd(idx_t count) { |
| 24 | } |
| 25 | |
| 26 | // Serialize maps as arrays of objects with "key" and "value" properties. |
| 27 | void BinarySerializer::OnMapBegin(idx_t count) { |
| 28 | Write(element: count); |
| 29 | } |
| 30 | |
| 31 | void BinarySerializer::OnMapEntryBegin() { |
| 32 | } |
| 33 | |
| 34 | void BinarySerializer::OnMapKeyBegin() { |
| 35 | } |
| 36 | |
| 37 | void BinarySerializer::OnMapValueBegin() { |
| 38 | } |
| 39 | |
| 40 | void BinarySerializer::OnMapEntryEnd() { |
| 41 | } |
| 42 | |
| 43 | void BinarySerializer::OnMapEnd(idx_t count) { |
| 44 | } |
| 45 | |
| 46 | void 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 | |
| 52 | void 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 | |
| 61 | void BinarySerializer::OnPairBegin() { |
| 62 | } |
| 63 | |
| 64 | void BinarySerializer::OnPairKeyBegin() { |
| 65 | } |
| 66 | |
| 67 | void BinarySerializer::OnPairValueBegin() { |
| 68 | } |
| 69 | |
| 70 | void BinarySerializer::OnPairEnd() { |
| 71 | } |
| 72 | |
| 73 | //===--------------------------------------------------------------------===// |
| 74 | // Primitive types |
| 75 | //===--------------------------------------------------------------------===// |
| 76 | void BinarySerializer::WriteNull() { |
| 77 | // This should never be called, optional writes should be handled by OnOptionalBegin |
| 78 | } |
| 79 | |
| 80 | void BinarySerializer::WriteValue(uint8_t value) { |
| 81 | Write(element: value); |
| 82 | } |
| 83 | |
| 84 | void BinarySerializer::WriteValue(int8_t value) { |
| 85 | Write(element: value); |
| 86 | } |
| 87 | |
| 88 | void BinarySerializer::WriteValue(uint16_t value) { |
| 89 | Write(element: value); |
| 90 | } |
| 91 | |
| 92 | void BinarySerializer::WriteValue(int16_t value) { |
| 93 | Write(element: value); |
| 94 | } |
| 95 | |
| 96 | void BinarySerializer::WriteValue(uint32_t value) { |
| 97 | Write(element: value); |
| 98 | } |
| 99 | |
| 100 | void BinarySerializer::WriteValue(int32_t value) { |
| 101 | Write(element: value); |
| 102 | } |
| 103 | |
| 104 | void BinarySerializer::WriteValue(uint64_t value) { |
| 105 | Write(element: value); |
| 106 | } |
| 107 | |
| 108 | void BinarySerializer::WriteValue(int64_t value) { |
| 109 | Write(element: value); |
| 110 | } |
| 111 | |
| 112 | void BinarySerializer::WriteValue(hugeint_t value) { |
| 113 | Write(element: value); |
| 114 | } |
| 115 | |
| 116 | void BinarySerializer::WriteValue(float value) { |
| 117 | Write(element: value); |
| 118 | } |
| 119 | |
| 120 | void BinarySerializer::WriteValue(double value) { |
| 121 | Write(element: value); |
| 122 | } |
| 123 | |
| 124 | void BinarySerializer::WriteValue(interval_t value) { |
| 125 | Write(element: value); |
| 126 | } |
| 127 | |
| 128 | void 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 | |
| 136 | void 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 | |
| 144 | void 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 | |
| 152 | void BinarySerializer::WriteValue(bool value) { |
| 153 | Write(element: value); |
| 154 | } |
| 155 | |
| 156 | void BinarySerializer::WriteDataPtr(const_data_ptr_t ptr, idx_t count) { |
| 157 | WriteData(buffer: ptr, write_size: count); |
| 158 | } |
| 159 | |
| 160 | } // namespace duckdb |
| 161 | |