1 | #pragma once |
2 | #include "duckdb/common/serializer/format_deserializer.hpp" |
3 | |
4 | namespace duckdb { |
5 | |
6 | class BinaryDeserializer : public FormatDeserializer { |
7 | public: |
8 | template <class T> |
9 | static unique_ptr<T> Deserialize(data_ptr_t ptr, idx_t length) { |
10 | BinaryDeserializer deserializer(ptr, length); |
11 | deserializer.OnObjectBegin(); |
12 | auto result = T::FormatDeserialize(deserializer); |
13 | deserializer.OnObjectEnd(); |
14 | return result; |
15 | } |
16 | |
17 | private: |
18 | explicit BinaryDeserializer(data_ptr_t ptr, idx_t length) : ptr(ptr), end_ptr(ptr + length) { |
19 | deserialize_enum_from_string = false; |
20 | } |
21 | struct State { |
22 | uint32_t expected_field_count; |
23 | idx_t expected_size; |
24 | uint32_t read_field_count; |
25 | State(uint32_t expected_field_count, idx_t expected_size) |
26 | : expected_field_count(expected_field_count), expected_size(expected_size), read_field_count(0) { |
27 | } |
28 | }; |
29 | |
30 | const char *current_tag = nullptr; |
31 | data_ptr_t ptr; |
32 | data_ptr_t end_ptr; |
33 | vector<State> stack; |
34 | |
35 | template <class T> |
36 | T ReadPrimitive() { |
37 | T value; |
38 | ReadData(buffer: data_ptr_cast(&value), read_size: sizeof(T)); |
39 | return value; |
40 | } |
41 | |
42 | void ReadData(data_ptr_t buffer, idx_t read_size) { |
43 | if (ptr + read_size > end_ptr) { |
44 | throw SerializationException("Failed to deserialize: not enough data in buffer to fulfill read request" ); |
45 | } |
46 | memcpy(dest: buffer, src: ptr, n: read_size); |
47 | ptr += read_size; |
48 | } |
49 | |
50 | // Set the 'tag' of the property to read |
51 | void SetTag(const char *tag) final; |
52 | |
53 | //===--------------------------------------------------------------------===// |
54 | // Nested Types Hooks |
55 | //===--------------------------------------------------------------------===// |
56 | void OnObjectBegin() final; |
57 | void OnObjectEnd() final; |
58 | idx_t OnListBegin() final; |
59 | void OnListEnd() final; |
60 | idx_t OnMapBegin() final; |
61 | void OnMapEnd() final; |
62 | void OnMapEntryBegin() final; |
63 | void OnMapEntryEnd() final; |
64 | void OnMapKeyBegin() final; |
65 | void OnMapValueBegin() final; |
66 | bool OnOptionalBegin() final; |
67 | |
68 | void OnPairBegin() final; |
69 | void OnPairKeyBegin() final; |
70 | void OnPairValueBegin() final; |
71 | void OnPairEnd() final; |
72 | |
73 | //===--------------------------------------------------------------------===// |
74 | // Primitive Types |
75 | //===--------------------------------------------------------------------===// |
76 | bool ReadBool() final; |
77 | int8_t ReadSignedInt8() final; |
78 | uint8_t ReadUnsignedInt8() final; |
79 | int16_t ReadSignedInt16() final; |
80 | uint16_t ReadUnsignedInt16() final; |
81 | int32_t ReadSignedInt32() final; |
82 | uint32_t ReadUnsignedInt32() final; |
83 | int64_t ReadSignedInt64() final; |
84 | uint64_t ReadUnsignedInt64() final; |
85 | float ReadFloat() final; |
86 | double ReadDouble() final; |
87 | string ReadString() final; |
88 | interval_t ReadInterval() final; |
89 | hugeint_t ReadHugeInt() final; |
90 | void ReadDataPtr(data_ptr_t &ptr, idx_t count) final; |
91 | }; |
92 | |
93 | } // namespace duckdb |
94 | |