forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubcolumnsTree.h
More file actions
207 lines (162 loc) · 5.95 KB
/
SubcolumnsTree.h
File metadata and controls
207 lines (162 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#pragma once
#include <DataTypes/Serializations/PathInData.h>
#include <Common/HashTable/HashMap.h>
namespace DB
{
/// Tree that represents paths in document
/// with additional data in nodes.
template <typename NodeData>
class SubcolumnsTree
{
public:
struct Node
{
enum Kind
{
TUPLE,
NESTED,
SCALAR,
};
explicit Node(Kind kind_) : kind(kind_) {}
Node(Kind kind_, const NodeData & data_) : kind(kind_), data(data_) {}
Node(Kind kind_, const NodeData & data_, const PathInData & path_)
: kind(kind_), data(data_), path(path_) {}
Kind kind = TUPLE;
const Node * parent = nullptr;
Arena strings_pool;
HashMapWithStackMemory<StringRef, std::shared_ptr<Node>, StringRefHash, 4> children;
NodeData data;
PathInData path;
bool isNested() const { return kind == NESTED; }
bool isScalar() const { return kind == SCALAR; }
void addChild(std::string_view key, std::shared_ptr<Node> next_node)
{
next_node->parent = this;
StringRef key_ref{strings_pool.insert(key.data(), key.length()), key.length()};
children[key_ref] = std::move(next_node);
}
};
using NodeKind = typename Node::Kind;
using NodePtr = std::shared_ptr<Node>;
SubcolumnsTree() : root(std::make_shared<Node>(Node::TUPLE)) {}
/// Add a leaf without any data in other nodes.
bool add(const PathInData & path, const NodeData & leaf_data)
{
return add(path, [&](NodeKind kind, bool exists) -> NodePtr
{
if (exists)
return nullptr;
if (kind == Node::SCALAR)
return std::make_shared<Node>(kind, leaf_data, path);
return std::make_shared<Node>(kind);
});
}
/// Callback for creation of node. Receives kind of node and
/// flag, which is true if node already exists.
using NodeCreator = std::function<NodePtr(NodeKind, bool)>;
bool add(const PathInData & path, const NodeCreator & node_creator)
{
const auto & parts = path.getParts();
if (parts.empty())
return false;
Node * current_node = root.get();
for (size_t i = 0; i < parts.size() - 1; ++i)
{
assert(current_node->kind != Node::SCALAR);
auto it = current_node->children.find(StringRef{parts[i].key});
if (it != current_node->children.end())
{
current_node = it->getMapped().get();
node_creator(current_node->kind, true);
if (current_node->isNested() != parts[i].is_nested)
return false;
}
else
{
auto next_kind = parts[i].is_nested ? Node::NESTED : Node::TUPLE;
auto next_node = node_creator(next_kind, false);
current_node->addChild(String(parts[i].key), next_node);
current_node = next_node.get();
}
}
auto it = current_node->children.find(StringRef{parts.back().key});
if (it != current_node->children.end())
return false;
auto next_node = node_creator(Node::SCALAR, false);
current_node->addChild(String(parts.back().key), next_node);
leaves.push_back(std::move(next_node));
return true;
}
/// Find node that matches the path the best.
const Node * findBestMatch(const PathInData & path) const
{
return findImpl(path, false);
}
/// Find node that matches the path exactly.
const Node * findExact(const PathInData & path) const
{
return findImpl(path, true);
}
/// Find leaf by path.
const Node * findLeaf(const PathInData & path) const
{
const auto * candidate = findExact(path);
if (!candidate || !candidate->isScalar())
return nullptr;
return candidate;
}
using NodePredicate = std::function<bool(const Node &)>;
/// Finds leaf that satisfies the predicate.
const Node * findLeaf(const NodePredicate & predicate)
{
return findLeaf(root.get(), predicate);
}
static const Node * findLeaf(const Node * node, const NodePredicate & predicate)
{
if (!node)
return nullptr;
if (node->isScalar())
return predicate(*node) ? node : nullptr;
for (const auto & [_, child] : node->children)
if (const auto * leaf = findLeaf(child.get(), predicate))
return leaf;
return nullptr;
}
/// Find first parent node that satisfies the predicate.
static const Node * findParent(const Node * node, const NodePredicate & predicate)
{
while (node && !predicate(*node))
node = node->parent;
return node;
}
bool empty() const { return root->children.empty(); }
size_t size() const { return leaves.size(); }
using Nodes = std::vector<NodePtr>;
const Nodes & getLeaves() const { return leaves; }
const Node & getRoot() const { return *root; }
using iterator = typename Nodes::iterator;
using const_iterator = typename Nodes::const_iterator;
iterator begin() { return leaves.begin(); }
iterator end() { return leaves.end(); }
const_iterator begin() const { return leaves.begin(); }
const_iterator end() const { return leaves.end(); }
private:
const Node * findImpl(const PathInData & path, bool find_exact) const
{
if (empty())
return nullptr;
const auto & parts = path.getParts();
const auto * current_node = root.get();
for (const auto & part : parts)
{
auto it = current_node->children.find(StringRef{part.key});
if (it == current_node->children.end())
return find_exact ? nullptr : current_node;
current_node = it->getMapped().get();
}
return current_node;
}
NodePtr root;
Nodes leaves;
};
}