From b222a8a12ed91fd954b3e9623518e6345bb6fa97 Mon Sep 17 00:00:00 2001 From: Arsentiy Milchakov Date: Thu, 5 Oct 2017 11:27:34 +0300 Subject: [PATCH] [ugc] added version number into mwm section --- ugc/binary/serdes.cpp | 11 +++++++++ ugc/binary/serdes.hpp | 35 ++++++++++++++++++++++++--- ugc/ugc_tests/serdes_binary_tests.cpp | 6 ++--- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/ugc/binary/serdes.cpp b/ugc/binary/serdes.cpp index b239b0edce..70643d0e1d 100644 --- a/ugc/binary/serdes.cpp +++ b/ugc/binary/serdes.cpp @@ -101,5 +101,16 @@ void UGCSeriaizer::CollectTexts() collector(p.m_ugc); } } + +std::string DebugPrint(Version v) +{ + switch (v) + { + case Version::V0: return "Version 0"; + default: ASSERT(false, ("Unknown version", static_cast(v))); + } + + return {}; +} } // namespace binary } // namespace ugc diff --git a/ugc/binary/serdes.hpp b/ugc/binary/serdes.hpp index 10826a7064..3af82a0cdd 100644 --- a/ugc/binary/serdes.hpp +++ b/ugc/binary/serdes.hpp @@ -27,6 +27,12 @@ namespace binary using FeatureIndex = uint32_t; using UGCOffset = uint64_t; +enum class Version : uint64_t +{ + V0 = 0, + Latest = V0 +}; + class UGCSeriaizer { public: @@ -49,6 +55,8 @@ public: template void Serialize(Sink & sink) { + WriteToSink(sink, Version::Latest); + auto const startPos = sink.Pos(); HeaderV0 header; @@ -152,11 +160,28 @@ private: // efficient to keep it alive between accesses. The instances of // |reader| for Deserialize() may differ between calls, but all // instances must be set to the beginning of the UGC section -class UGCDeserializerV0 +class UGCDeserializer { public: template bool Deserialize(R & reader, FeatureIndex index, UGC & ugc) + { + NonOwningReaderSource source(reader); + Version v = ReadPrimitiveFromSource(source); + + auto subReader = reader.CreateSubReader(source.Pos(), source.Size()); + + switch (v) + { + case Version::V0: return DeserializeV0(*subReader, index, ugc); + default: ASSERT(false, ("Cannot deserialize ugc for version", v)); + } + + return false; + } + + template + bool DeserializeV0(R & reader, FeatureIndex index, UGC & ugc) { InitializeIfNeeded(reader); @@ -188,6 +213,9 @@ public: return true; } + std::vector const & GetTranslationKeys() const { return m_keys; } + +private: template void InitializeIfNeeded(Reader & reader) { @@ -231,9 +259,6 @@ public: } } - std::vector const & GetTranslationKeys() const { return m_keys; } - -private: uint64_t GetNumUGCs() { ASSERT(m_initialized, ()); @@ -294,5 +319,7 @@ private: bool m_initialized = false; }; + +std::string DebugPrint(Version v); } // namespace binary } // namespace ugc diff --git a/ugc/ugc_tests/serdes_binary_tests.cpp b/ugc/ugc_tests/serdes_binary_tests.cpp index 74f6b02c02..3080f16e4d 100644 --- a/ugc/ugc_tests/serdes_binary_tests.cpp +++ b/ugc/ugc_tests/serdes_binary_tests.cpp @@ -73,14 +73,12 @@ UNIT_TEST(BinarySerDes_Smoke) ser.Serialize(sink); } - UGCDeserializerV0 des; + UGCDeserializer des; { MemReader reader(buffer.data(), buffer.size()); TEST(des.GetTranslationKeys().empty(), ()); - des.InitializeIfNeeded(reader); - TEST_EQUAL(GetExpectedTranslationKeys(), des.GetTranslationKeys(), ()); UGC ugc; TEST(!des.Deserialize(reader, 0 /* index */, ugc), ()); @@ -90,6 +88,8 @@ UNIT_TEST(BinarySerDes_Smoke) TEST(des.Deserialize(reader, 31337 /* index */, ugc), ()); TEST_EQUAL(ugc, expectedUGC1, ()); + + TEST_EQUAL(GetExpectedTranslationKeys(), des.GetTranslationKeys(), ()); } {