Kùzu C++ API
Loading...
Searching...
No Matches
deserializer.h
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <string>
5#include <unordered_map>
6#include <unordered_set>
7#include <vector>
8
9#include "assert.h"
10#include "reader.h"
11
12namespace kuzu {
13namespace common {
14
16public:
17 explicit Deserializer(std::unique_ptr<Reader> reader) : reader(std::move(reader)) {}
18
19 bool finished() const { return reader->finished(); }
20
21 template<typename T>
22 requires std::is_trivially_destructible_v<T> || std::is_same_v<std::string, T>
23 void deserializeValue(T& value) {
24 reader->read(reinterpret_cast<uint8_t*>(&value), sizeof(T));
25 }
26
27 void read(uint8_t* data, uint64_t size) { reader->read(data, size); }
28
29 void validateDebuggingInfo(std::string& value, std::string expectedVal);
30
31 template<typename T>
32 void deserializeOptionalValue(std::unique_ptr<T>& value) {
33 bool isNull;
34 deserializeValue(isNull);
35 if (!isNull) {
36 value = T::deserialize(*this);
37 }
38 }
39
40 template<typename T1, typename T2>
41 void deserializeUnorderedMap(std::unordered_map<T1, std::unique_ptr<T2>>& values) {
42 uint64_t mapSize;
44 values.reserve(mapSize);
45 for (auto i = 0u; i < mapSize; i++) {
46 T1 key;
48 auto val = T2::deserialize(*this);
49 values.emplace(key, std::move(val));
50 }
51 }
52
53 template<typename T>
54 void deserializeVector(std::vector<T>& values) {
55 uint64_t vectorSize;
56 deserializeValue(vectorSize);
57 values.resize(vectorSize);
58 for (auto& value : values) {
59 if constexpr (requires(Deserializer& deser) { T::deserialize(deser); }) {
60 value = T::deserialize(*this);
61 } else {
62 deserializeValue(value);
63 }
64 }
65 }
66
67 template<typename T, uint64_t ARRAY_SIZE>
68 void deserializeArray(std::array<T, ARRAY_SIZE>& values) {
69 KU_ASSERT(values.size() == ARRAY_SIZE);
70 for (auto& value : values) {
71 if constexpr (requires(Deserializer& deser) { T::deserialize(deser); }) {
72 value = T::deserialize(*this);
73 } else {
74 deserializeValue(value);
75 }
76 }
77 }
78
79 template<typename T>
80 void deserializeVectorOfPtrs(std::vector<std::unique_ptr<T>>& values) {
81 uint64_t vectorSize;
82 deserializeValue(vectorSize);
83 values.resize(vectorSize);
84 for (auto i = 0u; i < vectorSize; i++) {
85 values[i] = T::deserialize(*this);
86 }
87 }
88
89 template<typename T>
90 void deserializeUnorderedSet(std::unordered_set<T>& values) {
91 uint64_t setSize;
92 deserializeValue(setSize);
93 for (auto i = 0u; i < setSize; i++) {
94 T value;
96 values.insert(value);
97 }
98 }
99
100private:
101 std::unique_ptr<Reader> reader;
102};
103
104template<>
105void Deserializer::deserializeValue(std::string& value);
106
107} // namespace common
108} // namespace kuzu
#define KU_ASSERT(condition)
Definition assert.h:19
Definition deserializer.h:15
bool finished() const
Definition deserializer.h:19
Deserializer(std::unique_ptr< Reader > reader)
Definition deserializer.h:17
void deserializeArray(std::array< T, ARRAY_SIZE > &values)
Definition deserializer.h:68
void validateDebuggingInfo(std::string &value, std::string expectedVal)
void deserializeOptionalValue(std::unique_ptr< T > &value)
Definition deserializer.h:32
void deserializeUnorderedMap(std::unordered_map< T1, std::unique_ptr< T2 > > &values)
Definition deserializer.h:41
void read(uint8_t *data, uint64_t size)
Definition deserializer.h:27
void deserializeVectorOfPtrs(std::vector< std::unique_ptr< T > > &values)
Definition deserializer.h:80
void deserializeValue(T &value)
Definition deserializer.h:23
void deserializeUnorderedSet(std::unordered_set< T > &values)
Definition deserializer.h:90
void deserializeVector(std::vector< T > &values)
Definition deserializer.h:54
Definition alter_type.h:5