diff options
Diffstat (limited to 'search')
-rw-r--r-- | search/query_saver.cpp | 40 | ||||
-rw-r--r-- | search/query_saver.hpp | 8 | ||||
-rw-r--r-- | search/search_tests/query_saver_tests.cpp | 36 |
3 files changed, 41 insertions, 43 deletions
diff --git a/search/query_saver.cpp b/search/query_saver.cpp index 2cf765d87e..c5a4acce7c 100644 --- a/search/query_saver.cpp +++ b/search/query_saver.cpp @@ -10,6 +10,8 @@ namespace { size_t constexpr kMaxSuggestCount = 10; char constexpr kSettingsKey[] = "UserQueries"; +using TLength = uint16_t; +size_t constexpr kLengthTypeSize = sizeof(TLength); } // namespace namespace search @@ -19,7 +21,7 @@ QuerySaver::QuerySaver() Load(); } -void QuerySaver::SaveNewQuery(string const & query) +void QuerySaver::Add(string const & query) { if (query.empty()) return; @@ -42,46 +44,43 @@ void QuerySaver::Clear() Settings::Delete(kSettingsKey); } -void QuerySaver::Serialize(vector<char> & data) const +void QuerySaver::Serialize(vector<uint8_t> & data) const { data.clear(); - MemWriter<vector<char>> writer(data); - uint16_t size = m_topQueries.size(); - writer.Write(&size, 2); + MemWriter<vector<uint8_t>> writer(data); + TLength size = m_topQueries.size(); + writer.Write(&size, kLengthTypeSize); for (auto const & query : m_topQueries) { size = query.size(); - writer.Write(&size, 2); + writer.Write(&size, kLengthTypeSize); writer.Write(query.c_str(), size); } } void QuerySaver::Deserialize(string const & data) { - MemReader reader(data.c_str(), data.size()); + MemReader rawReader(data.c_str(), data.size()); + ReaderSource<MemReader> reader(rawReader); - uint16_t queriesCount; - reader.Read(0 /* pos */, &queriesCount, 2); + TLength queriesCount; + reader.Read(&queriesCount, kLengthTypeSize); - size_t pos = 2; - for (int i = 0; i < queriesCount; ++i) + for (TLength i = 0; i < queriesCount; ++i) { - uint16_t stringLength; - reader.Read(pos, &stringLength, 2); - pos += 2; + TLength stringLength; + reader.Read(&stringLength, kLengthTypeSize); vector<char> str(stringLength); - reader.Read(pos, &str[0], stringLength); - pos += stringLength; + reader.Read(&str[0], stringLength); m_topQueries.emplace_back(&str[0], stringLength); } } void QuerySaver::Save() { - vector<char> data; + vector<uint8_t> data; Serialize(data); - string hexData = ToHex(&data[0], data.size()); - Settings::Set(kSettingsKey, hexData); + Settings::Set(kSettingsKey, ToHex(&data[0], data.size())); } void QuerySaver::Load() @@ -90,7 +89,6 @@ void QuerySaver::Load() Settings::Get(kSettingsKey, hexData); if (hexData.empty()) return; - string rawData = FromHex(hexData); - Deserialize(rawData); + Deserialize(FromHex(hexData)); } } // namesapce search diff --git a/search/query_saver.hpp b/search/query_saver.hpp index 2d4071d8f0..78d858c113 100644 --- a/search/query_saver.hpp +++ b/search/query_saver.hpp @@ -9,16 +9,16 @@ class QuerySaver { public: QuerySaver(); - void SaveNewQuery(string const & query); + void Add(string const & query); /// Returns several last saved queries from newest to oldest query. /// @see kMaxSuggestCount in implementation file. - list<string> const & GetTopQueries() const { return m_topQueries; } + list<string> const & Get() const { return m_topQueries; } /// Clear last queries storage. All data will be lost. void Clear(); private: friend void UnitTest_QuerySaverSerializerTest(); - void Serialize(vector<char> & data) const; + void Serialize(vector<uint8_t> & data) const; void Deserialize(string const & data); void Save(); @@ -26,4 +26,4 @@ private: list<string> m_topQueries; }; -} // namespace seatch +} // namespace search diff --git a/search/search_tests/query_saver_tests.cpp b/search/search_tests/query_saver_tests.cpp index 55c5eb3bd0..169cd5df54 100644 --- a/search/search_tests/query_saver_tests.cpp +++ b/search/search_tests/query_saver_tests.cpp @@ -17,8 +17,8 @@ UNIT_TEST(QuerySaverFogTest) { QuerySaver saver; saver.Clear(); - saver.SaveNewQuery(record1); - list<string> const & result = saver.GetTopQueries(); + saver.Add(record1); + list<string> const & result = saver.Get(); TEST_EQUAL(result.size(), 1, ()); TEST_EQUAL(result.front(), record1, ()); saver.Clear(); @@ -28,27 +28,27 @@ UNIT_TEST(QuerySaverClearTest) { QuerySaver saver; saver.Clear(); - saver.SaveNewQuery(record1); - TEST_GREATER(saver.GetTopQueries().size(), 0, ()); + saver.Add(record1); + TEST_GREATER(saver.Get().size(), 0, ()); saver.Clear(); - TEST_EQUAL(saver.GetTopQueries().size(), 0, ()); + TEST_EQUAL(saver.Get().size(), 0, ()); } UNIT_TEST(QuerySaverOrderingTest) { QuerySaver saver; saver.Clear(); - saver.SaveNewQuery(record1); - saver.SaveNewQuery(record2); + saver.Add(record1); + saver.Add(record2); { - list<string> const & result = saver.GetTopQueries(); + list<string> const & result = saver.Get(); TEST_EQUAL(result.size(), 2, ()); TEST_EQUAL(result.back(), record1, ()); TEST_EQUAL(result.front(), record2, ()); } - saver.SaveNewQuery(record1); + saver.Add(record1); { - list<string> const & result = saver.GetTopQueries(); + list<string> const & result = saver.Get(); TEST_EQUAL(result.size(), 2, ()); TEST_EQUAL(result.front(), record1, ()); TEST_EQUAL(result.back(), record2, ()); @@ -60,16 +60,16 @@ UNIT_TEST(QuerySaverSerializerTest) { QuerySaver saver; saver.Clear(); - saver.SaveNewQuery(record1); - saver.SaveNewQuery(record2); - vector<char> data; + saver.Add(record1); + saver.Add(record2); + vector<uint8_t> data; saver.Serialize(data); TEST_GREATER(data.size(), 0, ()); saver.Clear(); - TEST_EQUAL(saver.GetTopQueries().size(), 0, ()); + TEST_EQUAL(saver.Get().size(), 0, ()); saver.Deserialize(string(data.begin(), data.end())); - list<string> const & result = saver.GetTopQueries(); + list<string> const & result = saver.Get(); TEST_EQUAL(result.size(), 2, ()); TEST_EQUAL(result.back(), record1, ()); TEST_EQUAL(result.front(), record2, ()); @@ -80,12 +80,12 @@ UNIT_TEST(QuerySaverPersistanceStore) { QuerySaver saver; saver.Clear(); - saver.SaveNewQuery(record1); - saver.SaveNewQuery(record2); + saver.Add(record1); + saver.Add(record2); } { QuerySaver saver; - list<string> const & result = saver.GetTopQueries(); + list<string> const & result = saver.Get(); TEST_EQUAL(result.size(), 2, ()); TEST_EQUAL(result.back(), record1, ()); TEST_EQUAL(result.front(), record2, ()); |