diff options
author | yurial <yurial@yandex-team.com> | 2023-11-16 13:59:02 +0300 |
---|---|---|
committer | yurial <yurial@yandex-team.com> | 2023-11-16 14:43:37 +0300 |
commit | a2fd00a24fcb20211418b797cb6191c4f1afa6ea (patch) | |
tree | 68e33195baef03090a66c0e3868c89bfd2268480 /library | |
parent | cab414f768af417824e8def2b045b5f13c5e7931 (diff) | |
download | ydb-a2fd00a24fcb20211418b797cb6191c4f1afa6ea.tar.gz |
Support Save/Load TYsonString
Diffstat (limited to 'library')
-rw-r--r-- | library/cpp/yt/memory/ref.h | 4 | ||||
-rw-r--r-- | library/cpp/yt/memory/shared_range.h | 2 | ||||
-rw-r--r-- | library/cpp/yt/memory/unittests/ref_ut.cpp | 22 | ||||
-rw-r--r-- | library/cpp/yt/memory/unittests/ya.make | 1 | ||||
-rw-r--r-- | library/cpp/yt/yson_string/string.cpp | 22 | ||||
-rw-r--r-- | library/cpp/yt/yson_string/string.h | 4 | ||||
-rw-r--r-- | library/cpp/yt/yson_string/unittests/saveload_ut.cpp | 46 | ||||
-rw-r--r-- | library/cpp/yt/yson_string/unittests/ya.make | 1 |
8 files changed, 102 insertions, 0 deletions
diff --git a/library/cpp/yt/memory/ref.h b/library/cpp/yt/memory/ref.h index 2b2a0792ab..2177778a8f 100644 --- a/library/cpp/yt/memory/ref.h +++ b/library/cpp/yt/memory/ref.h @@ -403,3 +403,7 @@ size_t GetByteSize(const std::vector<T>& parts); #define REF_INL_H_ #include "ref-inl.h" #undef REF_INL_H_ + +//! Serialize TSharedRef like vector<char>. Useful for ::Save, ::Load serialization/deserialization. See util/ysaveload.h. +template <> +class TSerializer<NYT::TSharedRef>: public TVectorSerializer<NYT::TSharedRange<char>> {}; diff --git a/library/cpp/yt/memory/shared_range.h b/library/cpp/yt/memory/shared_range.h index e1c262f418..de5fc354ea 100644 --- a/library/cpp/yt/memory/shared_range.h +++ b/library/cpp/yt/memory/shared_range.h @@ -7,6 +7,8 @@ #include <library/cpp/yt/assert/assert.h> +#include <util/ysaveload.h> + #include <optional> namespace NYT { diff --git a/library/cpp/yt/memory/unittests/ref_ut.cpp b/library/cpp/yt/memory/unittests/ref_ut.cpp new file mode 100644 index 0000000000..8ecd57e15d --- /dev/null +++ b/library/cpp/yt/memory/unittests/ref_ut.cpp @@ -0,0 +1,22 @@ +#include <library/cpp/testing/gtest/gtest.h> + +#include <library/cpp/testing/gtest_extensions/assertions.h> + +#include <library/cpp/yt/memory/ref.h> + +namespace NYT::NYson { +namespace { + +//////////////////////////////////////////////////////////////////////////////// + +TEST(TSharedRefTest, Save) +{ + const TSharedRef expected = TSharedRef::FromString("My tests data"); + TStringStream s; + ::Save(&s, expected); // only Save supported for TSharedRef. You can ::Load serialized data to vector. +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace +} // namespace NYT::NYson diff --git a/library/cpp/yt/memory/unittests/ya.make b/library/cpp/yt/memory/unittests/ya.make index 514caa93a3..3658eefce1 100644 --- a/library/cpp/yt/memory/unittests/ya.make +++ b/library/cpp/yt/memory/unittests/ya.make @@ -15,6 +15,7 @@ SRCS( intrusive_ptr_ut.cpp shared_range_ut.cpp weak_ptr_ut.cpp + ref_ut.cpp ) IF (NOT OS_WINDOWS) diff --git a/library/cpp/yt/yson_string/string.cpp b/library/cpp/yt/yson_string/string.cpp index dec428c31e..164e450b72 100644 --- a/library/cpp/yt/yson_string/string.cpp +++ b/library/cpp/yt/yson_string/string.cpp @@ -173,6 +173,28 @@ size_t TYsonString::ComputeHash() const return THash<TStringBuf>()(TStringBuf(Begin_, Begin_ + Size_)); } +void TYsonString::Save(IOutputStream* s) const +{ + EYsonType type = Type_; + if (*this) { + ::SaveMany(s, type, ToSharedRef()); + } else { + ::SaveMany(s, type, TString()); + } +} + +void TYsonString::Load(IInputStream* s) +{ + EYsonType type; + TString data; + ::LoadMany(s, type, data); + if (data) { + *this = TYsonString(data, type); + } else { + *this = TYsonString(); + } +} + //////////////////////////////////////////////////////////////////////////////// TString ToString(const TYsonString& yson) diff --git a/library/cpp/yt/yson_string/string.h b/library/cpp/yt/yson_string/string.h index 29845b70ae..d9f036569f 100644 --- a/library/cpp/yt/yson_string/string.h +++ b/library/cpp/yt/yson_string/string.h @@ -107,6 +107,10 @@ public: //! Computes the hash code. size_t ComputeHash() const; + //! Allow to serialize/deserialize using the ::Save ::Load functions. See util/ysaveload.h. + void Save(IOutputStream* s) const; + void Load(IInputStream* s); + private: struct TNullPayload { }; diff --git a/library/cpp/yt/yson_string/unittests/saveload_ut.cpp b/library/cpp/yt/yson_string/unittests/saveload_ut.cpp new file mode 100644 index 0000000000..33a98a30ea --- /dev/null +++ b/library/cpp/yt/yson_string/unittests/saveload_ut.cpp @@ -0,0 +1,46 @@ +#include <library/cpp/testing/gtest/gtest.h> + +#include <library/cpp/testing/gtest_extensions/assertions.h> + +#include <library/cpp/yt/yson_string/string.h> + +namespace NYT::NYson { +namespace { + +//////////////////////////////////////////////////////////////////////////////// + +TEST(TYsonStringTest, SaveLoadNull) +{ + const TYsonString expected; + TStringStream s; + ::Save(&s, expected); + TYsonString result; + ::Load(&s, result); + EXPECT_EQ(expected, result); +} + +TEST(TYsonStringTest, SaveLoadString) +{ + const TYsonString expected(TString("My tests data")); + TStringStream s; + ::Save(&s, expected); + TYsonString result; + ::Load(&s, result); + EXPECT_EQ(expected, result); +} + +TEST(TYsonStringTest, SaveLoadSharedRef) +{ + TSharedRef ref = TSharedRef::FromString("My tests data"); + const TYsonString expected(ref); + TStringStream s; + ::Save(&s, expected); + TYsonString result; + ::Load(&s, result); + EXPECT_EQ(expected, result); +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace +} // namespace NYT::NYson diff --git a/library/cpp/yt/yson_string/unittests/ya.make b/library/cpp/yt/yson_string/unittests/ya.make index f301e5b438..b584119f78 100644 --- a/library/cpp/yt/yson_string/unittests/ya.make +++ b/library/cpp/yt/yson_string/unittests/ya.make @@ -4,6 +4,7 @@ INCLUDE(${ARCADIA_ROOT}/library/cpp/yt/ya_cpp.make.inc) SRCS( convert_ut.cpp + saveload_ut.cpp ) PEERDIR( |