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
|
#include "tree_builder.h"
#include "attribute_consumer.h"
#include "helpers.h"
#include "attributes.h"
#include "attribute_consumer.h"
#include "node.h"
#include <yt/yt/core/actions/bind.h>
#include <yt/yt/core/yson/forwarding_consumer.h>
#include <library/cpp/yt/assert/assert.h>
#include <stack>
namespace NYT::NYTree {
////////////////////////////////////////////////////////////////////////////////
class TTreeBuilder
: public NYson::TForwardingYsonConsumer
, public ITreeBuilder
{
public:
explicit TTreeBuilder(INodeFactory* factory)
: Factory_(factory)
{
YT_ASSERT(Factory_);
}
void BeginTree() override
{
YT_VERIFY(NodeStack_.size() == 0);
}
INodePtr EndTree() override
{
// Failure here means that the tree is not fully constructed yet.
YT_VERIFY(NodeStack_.size() == 0);
YT_VERIFY(ResultNode_);
return ResultNode_;
}
void OnNode(INodePtr node) override
{
AddNode(node, false);
}
void OnMyStringScalar(TStringBuf value) override
{
auto node = Factory_->CreateString();
node->SetValue(TString(value));
AddNode(node, false);
}
void OnMyInt64Scalar(i64 value) override
{
auto node = Factory_->CreateInt64();
node->SetValue(value);
AddNode(node, false);
}
void OnMyUint64Scalar(ui64 value) override
{
auto node = Factory_->CreateUint64();
node->SetValue(value);
AddNode(node, false);
}
void OnMyDoubleScalar(double value) override
{
auto node = Factory_->CreateDouble();
node->SetValue(value);
AddNode(node, false);
}
void OnMyBooleanScalar(bool value) override
{
auto node = Factory_->CreateBoolean();
node->SetValue(value);
AddNode(node, false);
}
void OnMyEntity() override
{
AddNode(Factory_->CreateEntity(), false);
}
void OnMyBeginList() override
{
AddNode(Factory_->CreateList(), true);
}
void OnMyListItem() override
{
YT_ASSERT(!Key_);
}
void OnMyEndList() override
{
NodeStack_.pop();
}
void OnMyBeginMap() override
{
AddNode(Factory_->CreateMap(), true);
}
void OnMyKeyedItem(TStringBuf key) override
{
Key_ = TString(key);
}
void OnMyEndMap() override
{
NodeStack_.pop();
}
void OnMyBeginAttributes() override
{
YT_ASSERT(!AttributeConsumer_);
Attributes_ = CreateEphemeralAttributes();
AttributeConsumer_ = std::make_unique<TAttributeConsumer>(Attributes_.Get());
Forward(AttributeConsumer_.get(), nullptr, NYson::EYsonType::MapFragment);
}
void OnMyEndAttributes() override
{
AttributeConsumer_.reset();
YT_ASSERT(Attributes_);
}
private:
INodeFactory* const Factory_;
//! Contains nodes forming the current path in the tree.
std::stack<INodePtr> NodeStack_;
std::optional<TString> Key_;
INodePtr ResultNode_;
std::unique_ptr<TAttributeConsumer> AttributeConsumer_;
IAttributeDictionaryPtr Attributes_;
void AddNode(INodePtr node, bool push)
{
if (Attributes_) {
node->MutableAttributes()->MergeFrom(*Attributes_);
Attributes_ = nullptr;
}
if (NodeStack_.empty()) {
ResultNode_ = node;
} else {
auto collectionNode = NodeStack_.top();
if (Key_) {
if (!collectionNode->AsMap()->AddChild(*Key_, node)) {
THROW_ERROR_EXCEPTION("Duplicate key %Qv", *Key_);
}
Key_.reset();
} else {
collectionNode->AsList()->AddChild(node);
}
}
if (push) {
NodeStack_.push(node);
}
}
};
std::unique_ptr<ITreeBuilder> CreateBuilderFromFactory(INodeFactory* factory)
{
return std::unique_ptr<ITreeBuilder>(new TTreeBuilder(std::move(factory)));
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT::NYTree
|