Repository navigation
Expand file tree
/
Copy pathPathInData.cpp
More file actions
156 lines (130 loc) · 3.67 KB
/
Copy pathPathInData.cpp
File metadata and controls
156 lines (130 loc) · 3.67 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
#include <DataTypes/Serializations/PathInData.h>
#include <DataTypes/NestedUtils.h>
#include <DataTypes/DataTypeTuple.h>
#include <DataTypes/DataTypeArray.h>
#include <Columns/ColumnTuple.h>
#include <Columns/ColumnArray.h>
#include <Common/SipHash.h>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>
namespace DB
{
PathInData::PathInData(std::string_view path_)
: path(path_)
{
const char * begin = path.data();
const char * end = path.data() + path.size();
for (const char * it = path.data(); it != end; ++it)
{
if (*it == '.')
{
size_t size = static_cast<size_t>(it - begin);
parts.emplace_back(std::string_view{begin, size}, false, 0);
begin = it + 1;
}
}
size_t size = static_cast<size_t>(end - begin);
parts.emplace_back(std::string_view{begin, size}, false, 0.);
}
PathInData::PathInData(const Parts & parts_)
{
buildPath(parts_);
buildParts(parts_);
}
PathInData::PathInData(const PathInData & other)
: path(other.path)
{
buildParts(other.getParts());
}
PathInData & PathInData::operator=(const PathInData & other)
{
if (this != &other)
{
path = other.path;
buildParts(other.parts);
}
return *this;
}
UInt128 PathInData::getPartsHash(const Parts::const_iterator & begin, const Parts::const_iterator & end)
{
SipHash hash;
hash.update(std::distance(begin, end));
for (auto part_it = begin; part_it != end; ++part_it)
{
hash.update(part_it->key.data(), part_it->key.length());
hash.update(part_it->is_nested);
hash.update(part_it->anonymous_array_level);
}
return hash.get128();
}
void PathInData::buildPath(const Parts & other_parts)
{
if (other_parts.empty())
return;
path.clear();
auto it = other_parts.begin();
path += it->key;
++it;
for (; it != other_parts.end(); ++it)
{
path += ".";
path += it->key;
}
}
void PathInData::buildParts(const Parts & other_parts)
{
if (other_parts.empty())
return;
parts.clear();
parts.reserve(other_parts.size());
const char * begin = path.data();
for (const auto & part : other_parts)
{
has_nested |= part.is_nested;
parts.emplace_back(std::string_view{begin, part.key.length()}, part.is_nested, part.anonymous_array_level);
begin += part.key.length() + 1;
}
}
size_t PathInData::Hash::operator()(const PathInData & value) const
{
auto hash = getPartsHash(value.parts.begin(), value.parts.end());
return hash.items[0] ^ hash.items[1];
}
PathInDataBuilder & PathInDataBuilder::append(std::string_view key, bool is_array)
{
if (parts.empty())
current_anonymous_array_level += is_array;
if (!key.empty())
{
if (!parts.empty())
parts.back().is_nested = is_array;
parts.emplace_back(key, false, current_anonymous_array_level);
current_anonymous_array_level = 0;
}
return *this;
}
PathInDataBuilder & PathInDataBuilder::append(const PathInData::Parts & path, bool is_array)
{
if (parts.empty())
current_anonymous_array_level += is_array;
if (!path.empty())
{
if (!parts.empty())
parts.back().is_nested = is_array;
auto it = parts.insert(parts.end(), path.begin(), path.end());
for (; it != parts.end(); ++it)
it->anonymous_array_level += current_anonymous_array_level;
current_anonymous_array_level = 0;
}
return *this;
}
void PathInDataBuilder::popBack()
{
parts.pop_back();
}
void PathInDataBuilder::popBack(size_t n)
{
assert(n <= parts.size());
parts.resize(parts.size() - n);
}
}