Skip to content

Commit 8f11f60

Browse files
committed
Intermediate changes
1 parent a10178d commit 8f11f60

File tree

5 files changed

+159
-78
lines changed

5 files changed

+159
-78
lines changed

yt/yt/core/concurrency/unittests/profiled_fair_share_invoker_pool_ut.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include <yt/yt/library/profiling/solomon/exporter.h>
1616

17+
#include <library/cpp/json/yson/json2yson.h>
18+
1719
#include <util/datetime/base.h>
1820

1921
#include <algorithm>
@@ -666,15 +668,7 @@ class TProfiledFairShareInvokerPoolProfilingTest
666668

667669
auto GetSensors(TString json)
668670
{
669-
for (auto& c : json) {
670-
if (c == ':') {
671-
c = '=';
672-
} else if (c == ',') {
673-
c = ';';
674-
}
675-
}
676-
677-
auto yson = NYson::TYsonString(json);
671+
auto yson = NYson::TYsonString(NJson2Yson::SerializeJsonValueAsYson(NJson::ReadJsonFastTree(json)));
678672

679673
auto list = NYTree::ConvertToNode(yson)->AsMap()->FindChild("sensors");
680674

@@ -730,7 +724,7 @@ class TProfiledFairShareInvokerPoolProfilingTest
730724

731725
THashMap<TString, int> invokerNameToDequeued = invokerNameToEnqueued;
732726

733-
for (const auto& entry : GetSensors(json)) {
727+
for (const auto& entry : GetSensors(std::move(json))) {
734728
auto mapEntry = entry->AsMap();
735729
auto labels = mapEntry->FindChild("labels")->AsMap();
736730

yt/yt/core/concurrency/unittests/ya.make

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ INCLUDE(${ARCADIA_ROOT}/yt/opensource.inc)
4848
PEERDIR(
4949
yt/yt/core
5050
yt/yt/core/test_framework
51+
52+
library/cpp/json/yson
5153
)
5254

5355
REQUIREMENTS(

yt/yt/core/ytree/polymorphic_yson_struct-inl.h

Lines changed: 82 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,44 @@ namespace NYT::NYTree {
1010

1111
namespace NDetail {
1212

13-
template <class TEnum>
14-
requires TEnumTraits<TEnum>::IsEnum
15-
template <class TBase, class TDerived>
16-
TIntrusivePtr<TBase> TEnumTraitsExt<TEnum>::ConcreteFactory()
13+
template <class TEnum, TEnum Value, class TBase, class TDerived>
14+
TIntrusivePtr<TBase> TMappingLeaf<TEnum, Value, TBase, TDerived>::CreateInstance()
1715
{
1816
return New<TDerived>();
1917
}
2018

21-
template <class TEnum>
22-
requires TEnumTraits<TEnum>::IsEnum
23-
template <class TBase, class... TDerived>
24-
TInstanceFactory<TEnum, TBase> TEnumTraitsExt<TEnum>::MakeFactory()
19+
template <class TEnum, TEnum BaseValue, CYsonStructDerived TBase, TEnum... Values, class... TDerived>
20+
requires (CHierarchy<TBase, TDerived...>)
21+
TIntrusivePtr<TBase>
22+
TPolymorphicMapping<TEnum, TLeafTag<BaseValue, TBase>, TLeafTag<Values, TDerived>...>::
23+
CreateInstance(TEnum value)
2524
{
26-
static constexpr auto keys = TTraits::GetDomainValues();
27-
using TTuple = std::tuple<TBase, TDerived...>;
25+
if (value == BaseValue) {
26+
return TLeaf<BaseValue, TBase>::CreateInstance();
27+
}
2828

29-
TInstanceFactory<TEnum, TBase> mapping;
29+
TIntrusivePtr<TBase> ret;
3030

31-
[&] <size_t... Idx> (std::index_sequence<Idx...>) {
32-
([&] {
33-
mapping[keys[Idx]] = &TEnumTraitsExt<TEnum>::ConcreteFactory<TBase, std::tuple_element_t<Idx, TTuple>>;
34-
} (), ...);
35-
} (std::make_index_sequence<sizeof...(TDerived) + 1>());
31+
([&ret, value] {
32+
if (value == Values) {
33+
ret = TLeaf<Values, TDerived>::CreateInstance();
34+
return false;
35+
}
36+
return true;
37+
} () && ...);
3638

37-
return mapping;
38-
}
39-
40-
////////////////////////////////////////////////////////////////////////////////
41-
42-
template <class TEnum, class TB, class... TD>
43-
TIntrusivePtr<TB> TPolymorphicEnumMapping<TEnum, TB, TD...>::MakeInstance(TEnum e)
44-
{
45-
static auto factory =
46-
TEnumTraitsExt<TEnum>::template MakeFactory<TB, TD...>();
47-
48-
return factory[e]();
39+
return ret;
4940
}
5041

5142
} // namespace NDetail
5243

5344
////////////////////////////////////////////////////////////////////////////////
5445

46+
template <CPolymorphicEnumMapping TMapping>
47+
TPolymorphicYsonStruct<TMapping>::TPolymorphicYsonStruct(TKey key)
48+
: TPolymorphicYsonStruct(key, TMapping::CreateInstance(key))
49+
{ }
50+
5551
template <CPolymorphicEnumMapping TMapping>
5652
TPolymorphicYsonStruct<TMapping>::TPolymorphicYsonStruct(TKey key, TIntrusivePtr<TBase> ptr) noexcept
5753
: Storage_(std::move(ptr))
@@ -77,6 +73,11 @@ void TPolymorphicYsonStruct<TMapping>::Load(
7773
// parse (unless we want to slice which we don't).
7874
IMapNodePtr map = TTraits::AsNode(source)->AsMap();
7975

76+
if (!map || map->GetChildCount() == 0) {
77+
// Empty struct.
78+
return;
79+
}
80+
8081
auto key = map->FindChildValue<TKey>("type");
8182
THROW_ERROR_EXCEPTION_UNLESS(
8283
key.has_value(),
@@ -86,7 +87,7 @@ void TPolymorphicYsonStruct<TMapping>::Load(
8687
if (!Storage_ || HeldType_ != type) {
8788
// NB: We will try to merge configs if types match.
8889
HeldType_ = type;
89-
Storage_ = TMapping::MakeInstance(HeldType_);
90+
Storage_ = TMapping::CreateInstance(HeldType_);
9091
}
9192

9293
if (recursiveUnrecognizedStrategy) {
@@ -97,33 +98,62 @@ void TPolymorphicYsonStruct<TMapping>::Load(
9798
// therefore we must delete it prior to |Load| call.
9899
map->RemoveChild("type");
99100
Storage_->Load(map, postprocess, setDefaults, path);
101+
102+
// NB(arkady-e1ppa): We must not actually remove contents of the node as a postcondition
103+
// since it mutates serialized data which might be used for config validation.
104+
map->AddChild("type", ConvertToNode(HeldType_));
100105
}
101106

102107
template <CPolymorphicEnumMapping TMapping>
103108
void TPolymorphicYsonStruct<TMapping>::Save(NYson::IYsonConsumer* consumer) const
104109
{
105110
consumer->OnBeginMap();
106111

107-
consumer->OnKeyedItem("type");
108-
consumer->OnStringScalar(FormatEnum(HeldType_));
112+
if (Storage_) {
113+
consumer->OnKeyedItem("type");
114+
consumer->OnStringScalar(FormatEnum(HeldType_));
115+
116+
Storage_->SaveAsMapFragment(consumer);
117+
}
109118

110-
Storage_->SaveAsMapFragment(consumer);
111119
consumer->OnEndMap();
112120
}
113121

114122
template <CPolymorphicEnumMapping TMapping>
115-
template <std::derived_from<typename TMapping::TBase> TConcrete>
123+
template <std::derived_from<typename TMapping::TBaseClass> TConcrete>
116124
TIntrusivePtr<TConcrete> TPolymorphicYsonStruct<TMapping>::TryGetConcrete() const
117125
{
118126
return DynamicPointerCast<TConcrete>(Storage_);
119127
}
120128

129+
template <CPolymorphicEnumMapping TMapping>
130+
template <typename TMapping::TKey Value>
131+
TIntrusivePtr<typename TMapping::template TDerivedToEnum<Value>> TPolymorphicYsonStruct<TMapping>::TryGetConcrete() const
132+
{
133+
if (Value != HeldType_) {
134+
return {};
135+
}
136+
return TryGetConcrete<typename TMapping::template TDerivedToEnum<Value>>();
137+
}
138+
121139
template <CPolymorphicEnumMapping TMapping>
122140
typename TPolymorphicYsonStruct<TMapping>::TKey TPolymorphicYsonStruct<TMapping>::GetCurrentType() const
123141
{
124142
return HeldType_;
125143
}
126144

145+
template <CPolymorphicEnumMapping TMapping>
146+
typename TPolymorphicYsonStruct<TMapping>::TBase* TPolymorphicYsonStruct<TMapping>::operator->()
147+
{
148+
return Storage_.Get();
149+
}
150+
151+
template <CPolymorphicEnumMapping TMapping>
152+
const typename TPolymorphicYsonStruct<TMapping>::TBase* TPolymorphicYsonStruct<TMapping>::operator->() const
153+
{
154+
return Storage_.Get();
155+
}
156+
127157
////////////////////////////////////////////////////////////////////////////////
128158

129159
template <CPolymorphicEnumMapping TMapping>
@@ -141,6 +171,7 @@ void Deserialize(TPolymorphicYsonStruct<TMapping>& value, TSource source)
141171
////////////////////////////////////////////////////////////////////////////////
142172

143173
#undef DEFINE_POLYMORPHIC_YSON_STRUCT
174+
#undef DEFINE_POLYMORPHIC_YSON_STRUCT_FOR_ENUM
144175

145176
#define POLYMORPHIC_YSON_STRUCT_IMPL__GET_ENUM_SEQ_ELEM(item) \
146177
PP_LEFT_PARENTHESIS PP_ELEMENT(item, 0) PP_RIGHT_PARENTHESIS
@@ -151,23 +182,37 @@ void Deserialize(TPolymorphicYsonStruct<TMapping>& value, TSource source)
151182
#define POLYMORPHIC_YSON_STRUCT_IMPL__ENUM_NAME(Struct) \
152183
E##Struct##Type
153184

154-
#define POLYMORPHIC_YSON_STRUCT_IMPL__MAKE_MAPPING_ENUM(Struct, seq) \
185+
#define POLYMORPHIC_YSON_STRUCT_IMPL__MAKE_MAPPING_ENUM(seq) \
155186
DEFINE_ENUM(EType, POLYMORPHIC_YSON_STRUCT_IMPL__GET_ENUM_SEQ(seq))
156187

188+
#define POLYMORPHIC_YSON_STRUCT_IMPL__MAKE_MAPPING_ENUM_ALIAS(EnumName) \
189+
using EType = EnumName;
190+
157191
#define POLYMORPHIC_YSON_STRUCT_IMPL__GET_CLASS_ELEM(item) \
158192
PP_COMMA() PP_ELEMENT(item, 1)
159193

194+
#define POLYMORPHIC_YSON_STRUCT_IMPL__MAKE_LEAF_FROM_ETYPE(item) \
195+
PP_COMMA() ::NYT::NYTree::NDetail::TLeafTag<EType:: PP_ELEMENT(item, 0), PP_ELEMENT(item, 1)>
196+
160197
#define POLYMORPHIC_YSON_STRUCT_IMPL__MAKE_MAPPING_CLASS(Struct, seq) \
161-
using TMapping = TPolymorphicEnumMapping<EType PP_FOR_EACH(POLYMORPHIC_YSON_STRUCT_IMPL__GET_CLASS_ELEM, seq)>
198+
using TMapping = ::NYT::NYTree::TPolymorphicEnumMapping<EType PP_FOR_EACH(POLYMORPHIC_YSON_STRUCT_IMPL__MAKE_LEAF_FROM_ETYPE, seq)>
162199

163200
#define DEFINE_POLYMORPHIC_YSON_STRUCT(name, seq) \
164201
namespace NPolymorphicYsonStructFor##name { \
165202
\
166-
POLYMORPHIC_YSON_STRUCT_IMPL__MAKE_MAPPING_ENUM(name, seq); \
203+
POLYMORPHIC_YSON_STRUCT_IMPL__MAKE_MAPPING_ENUM(seq); \
167204
POLYMORPHIC_YSON_STRUCT_IMPL__MAKE_MAPPING_CLASS(name, seq); \
168205
} /*NPolymorphicYsonStructFor##name*/ \
169206
using POLYMORPHIC_YSON_STRUCT_IMPL__ENUM_NAME(name) = NPolymorphicYsonStructFor##name::EType; \
170-
using T##name = TPolymorphicYsonStruct<NPolymorphicYsonStructFor##name::TMapping>; \
207+
using T##name = ::NYT::NYTree::TPolymorphicYsonStruct<NPolymorphicYsonStructFor##name::TMapping>; \
208+
static_assert(true)
209+
210+
#define DEFINE_POLYMORPHIC_YSON_STRUCT_FOR_ENUM(name, enum, seq) \
211+
namespace NPolymorphicYsonStructFor##name { \
212+
POLYMORPHIC_YSON_STRUCT_IMPL__MAKE_MAPPING_ENUM_ALIAS(enum); \
213+
POLYMORPHIC_YSON_STRUCT_IMPL__MAKE_MAPPING_CLASS(name, seq); \
214+
} /*NPolymorphicYsonStructFor##name*/ \
215+
using T##name = ::NYT::NYTree::TPolymorphicYsonStruct<NPolymorphicYsonStructFor##name::TMapping>; \
171216
static_assert(true)
172217

173218
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)