aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrenadeen <renadeen@yandex-team.com>2023-09-26 16:06:04 +0300
committerrenadeen <renadeen@yandex-team.com>2023-09-26 18:12:17 +0300
commit0d364aa66774cd6a3ec4d3e6936e3c5f53a5a0c4 (patch)
tree4560912af3aa6894be7ffe7f49bc7c3fd20b556a
parent15644505fe64a4c7894f605ae6dd5dc7a9d49435 (diff)
downloadydb-0d364aa66774cd6a3ec4d3e6936e3c5f53a5a0c4.tar.gz
YT-20014: Public constructor for TYsonStructLite
-rw-r--r--yt/yt/client/api/client_common.cpp6
-rw-r--r--yt/yt/client/driver/driver.cpp10
-rw-r--r--yt/yt/client/hedging/config.h2
-rw-r--r--yt/yt/client/misc/workload.cpp7
-rw-r--r--yt/yt/client/table_client/schema_serialization_helpers.cpp8
-rw-r--r--yt/yt/core/ytree/unittests/yson_struct_ut.cpp14
-rw-r--r--yt/yt/core/ytree/yson_struct-inl.h48
-rw-r--r--yt/yt/core/ytree/yson_struct.cpp6
-rw-r--r--yt/yt/core/ytree/yson_struct.h17
9 files changed, 66 insertions, 52 deletions
diff --git a/yt/yt/client/api/client_common.cpp b/yt/yt/client/api/client_common.cpp
index ce15cfb9f72..87eac85edf7 100644
--- a/yt/yt/client/api/client_common.cpp
+++ b/yt/yt/client/api/client_common.cpp
@@ -50,7 +50,7 @@ struct TSerializableUserWorkloadDescriptor
public:
static TThis Wrap(const TUserWorkloadDescriptor& source)
{
- TThis result = Create();
+ TThis result;
result.Band = source.Band;
result.Category = source.Category;
return result;
@@ -72,14 +72,14 @@ void Serialize(const TUserWorkloadDescriptor& workloadDescriptor, NYson::IYsonCo
void Deserialize(TUserWorkloadDescriptor& workloadDescriptor, INodePtr node)
{
- auto serializableWorkloadDescriptor = TSerializableUserWorkloadDescriptor::Create();
+ TSerializableUserWorkloadDescriptor serializableWorkloadDescriptor;
NYTree::Deserialize(serializableWorkloadDescriptor, node);
workloadDescriptor = serializableWorkloadDescriptor.Unwrap();
}
void Deserialize(TUserWorkloadDescriptor& workloadDescriptor, NYson::TYsonPullParserCursor* cursor)
{
- auto serializableWorkloadDescriptor = TSerializableUserWorkloadDescriptor::Create();
+ TSerializableUserWorkloadDescriptor serializableWorkloadDescriptor;
NYTree::Deserialize(serializableWorkloadDescriptor, cursor);
workloadDescriptor = serializableWorkloadDescriptor.Unwrap();
}
diff --git a/yt/yt/client/driver/driver.cpp b/yt/yt/client/driver/driver.cpp
index 15b6fd527fd..011f1dbd67e 100644
--- a/yt/yt/client/driver/driver.cpp
+++ b/yt/yt/client/driver/driver.cpp
@@ -496,13 +496,8 @@ private:
TCommandEntry entry;
entry.Descriptor = descriptor;
entry.Execute = BIND_NO_PROPAGATE([] (ICommandContextPtr context) {
- if constexpr (std::is_convertible<TCommand*, TYsonStructLite*>::value) {
- TCommand command = TCommand::Create();
- command.Execute(context);
- } else {
- TCommand command;
- command.Execute(context);
- }
+ TCommand command;
+ command.Execute(context);
});
YT_VERIFY(CommandNameToEntry_.emplace(descriptor.CommandName, entry).second);
}
@@ -685,4 +680,3 @@ IDriverPtr CreateDriver(
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT::NDriver
-
diff --git a/yt/yt/client/hedging/config.h b/yt/yt/client/hedging/config.h
index b8e7c5dc2f8..bd21956f69f 100644
--- a/yt/yt/client/hedging/config.h
+++ b/yt/yt/client/hedging/config.h
@@ -59,7 +59,7 @@ struct THedgingClientOptions
// This parameter is set on postprocessor.
TVector<TClientOptions> Clients;
- REGISTER_YSON_STRUCT(THedgingClientOptions);
+ REGISTER_YSON_STRUCT_LITE(THedgingClientOptions);
static void Register(TRegistrar registrar);
};
diff --git a/yt/yt/client/misc/workload.cpp b/yt/yt/client/misc/workload.cpp
index 098e3df1824..82648cab850 100644
--- a/yt/yt/client/misc/workload.cpp
+++ b/yt/yt/client/misc/workload.cpp
@@ -118,21 +118,21 @@ struct TSerializableWorkloadDescriptor
void Serialize(const TWorkloadDescriptor& descriptor, IYsonConsumer* consumer)
{
- TSerializableWorkloadDescriptor wrapper = TSerializableWorkloadDescriptor::Create();
+ TSerializableWorkloadDescriptor wrapper;
static_cast<TWorkloadDescriptor&>(wrapper) = descriptor;
Serialize(static_cast<const TYsonStructLite&>(wrapper), consumer);
}
void Deserialize(TWorkloadDescriptor& descriptor, INodePtr node)
{
- TSerializableWorkloadDescriptor wrapper = TSerializableWorkloadDescriptor::Create();
+ TSerializableWorkloadDescriptor wrapper;
Deserialize(static_cast<TYsonStructLite&>(wrapper), node);
descriptor = static_cast<TWorkloadDescriptor&>(wrapper);
}
void Deserialize(TWorkloadDescriptor& descriptor, NYson::TYsonPullParserCursor* cursor)
{
- TSerializableWorkloadDescriptor wrapper = TSerializableWorkloadDescriptor::Create();
+ TSerializableWorkloadDescriptor wrapper;
Deserialize(static_cast<TYsonStructLite&>(wrapper), cursor);
descriptor = static_cast<TWorkloadDescriptor&>(wrapper);
}
@@ -185,4 +185,3 @@ TString ToString(const TWorkloadDescriptor& descriptor)
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
-
diff --git a/yt/yt/client/table_client/schema_serialization_helpers.cpp b/yt/yt/client/table_client/schema_serialization_helpers.cpp
index f9ac42f59c5..27da822c48a 100644
--- a/yt/yt/client/table_client/schema_serialization_helpers.cpp
+++ b/yt/yt/client/table_client/schema_serialization_helpers.cpp
@@ -9,14 +9,14 @@ namespace NYT::NTableClient {
void Deserialize(TMaybeDeletedColumnSchema& schema, NYson::TYsonPullParserCursor* cursor)
{
- TSerializableColumnSchema wrapper = TSerializableColumnSchema::Create();
+ TSerializableColumnSchema wrapper;
wrapper.DeserializeFromCursor(cursor);
schema = wrapper;
}
void Deserialize(TMaybeDeletedColumnSchema& schema, NYTree::INodePtr node)
{
- TSerializableColumnSchema wrapper = TSerializableColumnSchema::Create();
+ TSerializableColumnSchema wrapper;
Deserialize(static_cast<NYTree::TYsonStructLite&>(wrapper), node);
schema = static_cast<TMaybeDeletedColumnSchema>(wrapper);
}
@@ -210,7 +210,7 @@ void TSerializableColumnSchema::RunPostprocessor()
void Serialize(const TColumnSchema& schema, NYson::IYsonConsumer* consumer)
{
- TSerializableColumnSchema wrapper = TSerializableColumnSchema::Create();
+ TSerializableColumnSchema wrapper;
wrapper.SetColumnSchema(schema);
Serialize(static_cast<const NYTree::TYsonStructLite&>(wrapper), consumer);
}
@@ -260,7 +260,7 @@ void Deserialize(TTableSchema& schema, NYTree::INodePtr node)
std::vector<TDeletedColumn> deletedColumns;
for (auto childNode : childNodes) {
- auto wrapper = TSerializableColumnSchema::Create();
+ TSerializableColumnSchema wrapper;
Deserialize(static_cast<NYTree::TYsonStructLite&>(wrapper), childNode);
if (wrapper.Deleted() && *wrapper.Deleted()) {
deletedColumns.push_back(TDeletedColumn(wrapper.StableName()));
diff --git a/yt/yt/core/ytree/unittests/yson_struct_ut.cpp b/yt/yt/core/ytree/unittests/yson_struct_ut.cpp
index 7f3f33fd7ed..55e20d227e5 100644
--- a/yt/yt/core/ytree/unittests/yson_struct_ut.cpp
+++ b/yt/yt/core/ytree/unittests/yson_struct_ut.cpp
@@ -806,7 +806,7 @@ public:
TEST(TYsonStructTest, SaveLite)
{
- TTestConfigLite config = TTestConfigLite::Create();
+ TTestConfigLite config;
config.MyString = "hello!";
config.NullableInt = 42;
@@ -854,7 +854,7 @@ public:
TEST(TYsonStructTest, NewLiteInitedWithDefaults)
{
- TTestLiteWithDefaults config = TTestLiteWithDefaults::Create();
+ TTestLiteWithDefaults config;
EXPECT_EQ(config.MyString, "y");
EXPECT_EQ(config.MyInt, 10);
EXPECT_TRUE(config.Subconfig != nullptr);
@@ -1007,8 +1007,8 @@ public:
TEST(TYsonStructTest, ParameterTuplesAndContainers)
{
- TTestConfigWithContainers original = TTestConfigWithContainers::Create();
- TTestConfigWithContainers deserialized = TTestConfigWithContainers::Create();
+ TTestConfigWithContainers original;
+ TTestConfigWithContainers deserialized;
original.Vector = { "fceswf", "sadfcesa" };
original.Array = {{ "UYTUY", ":LL:a", "78678678" }};
@@ -1524,7 +1524,7 @@ TEST(TYsonStructTest, TestSimpleSerialization)
EXPECT_EQ(config->MyString, "TestString");
EXPECT_EQ(config->NullableInt, 42);
- auto liteConfig = TTestConfigLite::Create();
+ TTestConfigLite liteConfig;
initialize(liteConfig);
::Save(&stream, liteConfig);
@@ -1540,7 +1540,7 @@ TEST(TYsonStructTest, TestComplexSerialization)
{
TTestConfigPtr Config1;
TTestConfigPtr Config2;
- TTestConfigLite LiteConfig = TTestConfigLite::Create();
+ TTestConfigLite LiteConfig;
TString StructName;
Y_SAVELOAD_DEFINE(Config1, Config2, LiteConfig, StructName);
@@ -1549,7 +1549,7 @@ TEST(TYsonStructTest, TestComplexSerialization)
TComplexStruct toSerialize{
.Config1 = New<TTestConfig>(),
.Config2 = New<TTestConfig>(),
- .LiteConfig = TTestConfigLite::Create(),
+ .LiteConfig = TTestConfigLite(),
.StructName = "tmp",
};
toSerialize.Config1->Load(GetCompleteConfigNode());
diff --git a/yt/yt/core/ytree/yson_struct-inl.h b/yt/yt/core/ytree/yson_struct-inl.h
index 7e375d995f0..0d624df595f 100644
--- a/yt/yt/core/ytree/yson_struct-inl.h
+++ b/yt/yt/core/ytree/yson_struct-inl.h
@@ -309,6 +309,12 @@ private: \
#define YSON_STRUCT_IMPL__CTOR_BODY(TStruct) \
::NYT::NYTree::TYsonStructRegistry::Get()->InitializeStruct(this);
+#define YSON_STRUCT_LITE_IMPL__CTOR_BODY(TStruct) \
+ YSON_STRUCT_IMPL__CTOR_BODY(TStruct) \
+ if (std::type_index(typeid(TStruct)) == FinalType_ && !::NYT::NYTree::TYsonStructRegistry::Get()->InitializationInProgress()) { \
+ SetDefaults(); \
+ } \
+
#define DECLARE_YSON_STRUCT(TStruct) \
public: \
@@ -319,45 +325,39 @@ public: \
public: \
TStruct() \
{ \
+ static_assert(std::is_base_of_v<::NYT::NYTree::TYsonStruct, TStruct>, "Class must inherit from TYsonStruct"); \
YSON_STRUCT_IMPL__CTOR_BODY(TStruct) \
} \
YSON_STRUCT_IMPL__DECLARE_ALIASES(TStruct)
-#define REGISTER_YSON_STRUCT_LITE_BASE(TStruct) \
-public: \
- static TStruct Create() \
- { \
- static_assert(std::is_base_of_v<::NYT::NYTree::TYsonStructLite, TStruct>, "Class must inherit from TYsonStructLite"); \
- TStruct result; \
- result.SetDefaults(); \
- return result; \
- } \
- \
- template <class T> \
- friend const std::type_info& ::NYT::NYTree::CallCtor(); \
- \
- YSON_STRUCT_IMPL__DECLARE_ALIASES(TStruct) \
-
#define DECLARE_YSON_STRUCT_LITE(TStruct) \
- REGISTER_YSON_STRUCT_LITE_BASE(TStruct) \
- \
-protected: \
- TStruct();
+public: \
+ TStruct(); \
+ YSON_STRUCT_IMPL__DECLARE_ALIASES(TStruct)
#define REGISTER_YSON_STRUCT_LITE(TStruct) \
- REGISTER_YSON_STRUCT_LITE_BASE(TStruct) \
- \
-protected: \
+public: \
TStruct() \
+ : ::NYT::NYTree::TYsonStructFinalClassHolder(std::type_index(typeid(TStruct))) \
{ \
- YSON_STRUCT_IMPL__CTOR_BODY(TStruct) \
- }
+ static_assert(std::is_base_of_v<::NYT::NYTree::TYsonStructLite, TStruct>, "Class must inherit from TYsonStructLite"); \
+ YSON_STRUCT_LITE_IMPL__CTOR_BODY(TStruct) \
+ } \
+ YSON_STRUCT_IMPL__DECLARE_ALIASES(TStruct) \
+#define DEFINE_YSON_STRUCT_LITE(TStruct) \
+TStruct::TStruct() \
+ : ::NYT::NYTree::TYsonStructFinalClassHolder(std::type_index(typeid(TStruct))) \
+{ \
+ static_assert(std::is_base_of_v<::NYT::NYTree::TYsonStructLite, TStruct>, "Class must inherit from TYsonStructLite"); \
+ YSON_STRUCT_LITE_IMPL__CTOR_BODY(TStruct) \
+}
#define DEFINE_YSON_STRUCT(TStruct) \
TStruct::TStruct() \
{ \
+ static_assert(std::is_base_of_v<::NYT::NYTree::TYsonStruct, TStruct>, "Class must inherit from TYsonStruct"); \
YSON_STRUCT_IMPL__CTOR_BODY(TStruct) \
}
diff --git a/yt/yt/core/ytree/yson_struct.cpp b/yt/yt/core/ytree/yson_struct.cpp
index 2dfa8f5990b..b24fe076517 100644
--- a/yt/yt/core/ytree/yson_struct.cpp
+++ b/yt/yt/core/ytree/yson_struct.cpp
@@ -16,6 +16,12 @@ using namespace NYson;
////////////////////////////////////////////////////////////////////////////////
+TYsonStructFinalClassHolder::TYsonStructFinalClassHolder(std::type_index typeIndex)
+ : FinalType_(typeIndex)
+{ }
+
+////////////////////////////////////////////////////////////////////////////////
+
IMapNodePtr TYsonStructBase::GetLocalUnrecognized() const
{
return LocalUnrecognized_;
diff --git a/yt/yt/core/ytree/yson_struct.h b/yt/yt/core/ytree/yson_struct.h
index 12ec7f871d2..0b26ca40b04 100644
--- a/yt/yt/core/ytree/yson_struct.h
+++ b/yt/yt/core/ytree/yson_struct.h
@@ -124,8 +124,23 @@ public:
////////////////////////////////////////////////////////////////////////////////
+class TYsonStructFinalClassHolder
+{
+protected:
+ explicit TYsonStructFinalClassHolder(std::type_index typeIndex);
+
+ // This constructor is only declared but not defined as it never is called.
+ // If we delete it default constructor of TYsonStructLite will be implicitly deleted as well and compilation will fail.
+ TYsonStructFinalClassHolder();
+
+ std::type_index FinalType_;
+};
+
+////////////////////////////////////////////////////////////////////////////////
+
class TYsonStructLite
- : public TYsonStructBase
+ : public virtual TYsonStructFinalClassHolder
+ , public TYsonStructBase
{ };
////////////////////////////////////////////////////////////////////////////////