Skip to content

Commit e5e476d

Browse files
author
Liubov Didkivska
committed
Add caching versions infos
Add implementation to put/get versions list to catalog client cache. Implement versions infos cache tests. Relates-To: OLPEDGE-1606 Signed-off-by: Liubov Didkivska <ext-liubov.didkivska@here.com>
1 parent fd2d656 commit e5e476d

File tree

5 files changed

+194
-8
lines changed

5 files changed

+194
-8
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (C) 2020 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#include "VersionInfosSerializer.h"
21+
22+
#include <olp/core/generated/serializer/SerializerWrapper.h>
23+
24+
namespace olp {
25+
namespace serializer {
26+
27+
void to_json(const dataservice::read::model::VersionDependency& x,
28+
rapidjson::Value& value,
29+
rapidjson::Document::AllocatorType& allocator) {
30+
value.SetObject();
31+
serialize("hrn", x.GetHrn(), value, allocator);
32+
serialize("version", x.GetVersion(), value, allocator);
33+
serialize("direct", x.GetDirect(), value, allocator);
34+
}
35+
36+
void to_json(const dataservice::read::model::VersionInfo& x,
37+
rapidjson::Value& value,
38+
rapidjson::Document::AllocatorType& allocator) {
39+
value.SetObject();
40+
serialize("dependencies", x.GetDependencies(), value, allocator);
41+
serialize("timestamp", x.GetTimestamp(), value, allocator);
42+
serialize("version", x.GetVersion(), value, allocator);
43+
serialize("partitionCounts", x.GetPartitionCounts(), value, allocator);
44+
}
45+
46+
void to_json(const dataservice::read::model::VersionInfos& x,
47+
rapidjson::Value& value,
48+
rapidjson::Document::AllocatorType& allocator) {
49+
value.SetObject();
50+
serialize("versions", x.GetVersions(), value, allocator);
51+
}
52+
} // namespace serializer
53+
} // namespace olp
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (C) 2020 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#pragma once
21+
22+
#include <rapidjson/document.h>
23+
#include "olp/dataservice/read/model/VersionInfos.h"
24+
25+
namespace olp {
26+
namespace serializer {
27+
28+
void to_json(const dataservice::read::model::VersionDependency& x,
29+
rapidjson::Value& value,
30+
rapidjson::Document::AllocatorType& allocator);
31+
32+
void to_json(const dataservice::read::model::VersionInfo& x,
33+
rapidjson::Value& value,
34+
rapidjson::Document::AllocatorType& allocator);
35+
36+
void to_json(const dataservice::read::model::VersionInfos& x,
37+
rapidjson::Value& value,
38+
rapidjson::Document::AllocatorType& allocator);
39+
} // namespace serializer
40+
} // namespace olp

olp-cpp-sdk-dataservice-read/src/repositories/CatalogCacheRepository.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 HERE Europe B.V.
2+
* Copyright (C) 2019-2020 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,17 +19,21 @@
1919

2020
#include "CatalogCacheRepository.h"
2121

22+
#include <limits>
2223
#include <string>
24+
#include <vector>
2325

2426
#include <olp/core/cache/KeyValueCache.h>
2527
#include <olp/core/logging/Log.h>
2628

2729
// clang-format off
2830
#include "generated/parser/CatalogParser.h"
31+
#include "generated/parser/VersionInfosParser.h"
2932
#include "generated/parser/VersionResponseParser.h"
3033
#include <olp/core/generated/parser/JsonParser.h>
3134
#include "generated/serializer/CatalogSerializer.h"
3235
#include "generated/serializer/VersionResponseSerializer.h"
36+
#include "generated/serializer/VersionInfosSerializer.h"
3337
#include "generated/serializer/JsonSerializer.h"
3438
// clang-format on
3539

@@ -46,6 +50,11 @@ std::string CreateKey(const std::string& hrn) { return hrn + "::catalog"; }
4650
std::string VersionKey(const std::string& hrn) {
4751
return hrn + "::latestVersion";
4852
}
53+
std::string VersionInfosKey(const std::string& hrn, std::int64_t start,
54+
std::int64_t end) {
55+
return hrn + "::" + std::to_string(start) + "::" + std::to_string(end) +
56+
"::versionInfos";
57+
}
4958

5059
time_t ConvertTime(std::chrono::seconds time) {
5160
return time == kChronoSecondsMax ? kTimetMax : time.count();
@@ -111,6 +120,34 @@ boost::optional<model::VersionResponse> CatalogCacheRepository::GetVersion() {
111120
return boost::any_cast<model::VersionResponse>(cached_version);
112121
}
113122

123+
void CatalogCacheRepository::PutVersionInfos(
124+
std::int64_t start, std::int64_t end, const model::VersionInfos& versions) {
125+
std::string hrn(hrn_.ToCatalogHRNString());
126+
auto key = VersionInfosKey(hrn, start, end);
127+
OLP_SDK_LOG_DEBUG_F(kLogTag, "PutVersionInfos -> '%s'", key.c_str());
128+
auto list_versions = olp::serializer::serialize(versions);
129+
130+
auto versions_data = std::make_shared<std::vector<unsigned char>>(
131+
std::begin(list_versions), std::end(list_versions));
132+
133+
cache_->Put(VersionInfosKey(hrn, start, end), versions_data, default_expiry_);
134+
}
135+
136+
boost::optional<model::VersionInfos> CatalogCacheRepository::GetVersionInfos(
137+
std::int64_t start, std::int64_t end) {
138+
std::string hrn(hrn_.ToCatalogHRNString());
139+
auto key = VersionInfosKey(hrn, start, end);
140+
OLP_SDK_LOG_DEBUG_F(kLogTag, "GetVersionInfos -> '%s'", key.c_str());
141+
142+
auto value = cache_->Get(key);
143+
if (!value) {
144+
return boost::none;
145+
}
146+
147+
auto list_versions = std::string(value->begin(), value->end());
148+
return parser::parse<model::VersionInfos>(list_versions);
149+
}
150+
114151
void CatalogCacheRepository::Clear() {
115152
std::string hrn(hrn_.ToCatalogHRNString());
116153
OLP_SDK_LOG_INFO_F(kLogTag, "Clear -> '%s'", CreateKey(hrn).c_str());

olp-cpp-sdk-dataservice-read/src/repositories/CatalogCacheRepository.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 HERE Europe B.V.
2+
* Copyright (C) 2019-2020 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424

2525
#include <olp/core/client/HRN.h>
2626
#include <olp/dataservice/read/model/Catalog.h>
27+
#include <olp/dataservice/read/model/VersionInfos.h>
2728
#include <olp/dataservice/read/model/VersionResponse.h>
2829
#include <boost/optional.hpp>
2930

@@ -51,6 +52,12 @@ class CatalogCacheRepository final {
5152

5253
boost::optional<model::VersionResponse> GetVersion();
5354

55+
void PutVersionInfos(std::int64_t start, std::int64_t end,
56+
const model::VersionInfos& versions);
57+
58+
boost::optional<model::VersionInfos> GetVersionInfos(std::int64_t start,
59+
std::int64_t end);
60+
5461
void Clear();
5562

5663
private:

olp-cpp-sdk-dataservice-read/tests/CatalogCacheRepositoryTest.cpp

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,25 @@
2626

2727
namespace {
2828

29-
using namespace olp;
30-
using namespace olp::dataservice::read;
29+
namespace read = olp::dataservice::read;
30+
namespace client = olp::client;
31+
namespace cache = olp::cache;
3132

3233
constexpr auto kCatalog = "hrn:here:data::olp-here-test:catalog";
3334

34-
TEST(PartitionsCacheRepositoryTest, DefaultExpiry) {
35+
TEST(CatalogCacheRepositoryTest, DefaultExpiry) {
3536
const auto hrn = client::HRN::FromString(kCatalog);
3637

37-
model::Catalog model_catalog;
38+
read::model::Catalog model_catalog;
3839

3940
{
4041
SCOPED_TRACE("Disable expiration");
4142

4243
const auto default_expiry = std::chrono::seconds::max();
4344
std::shared_ptr<cache::KeyValueCache> cache =
4445
olp::client::OlpClientSettingsFactory::CreateDefaultCache({});
45-
repository::CatalogCacheRepository repository(hrn, cache, default_expiry);
46+
read::repository::CatalogCacheRepository repository(hrn, cache,
47+
default_expiry);
4648

4749
repository.Put(model_catalog);
4850
const auto result = repository.Get();
@@ -56,7 +58,8 @@ TEST(PartitionsCacheRepositoryTest, DefaultExpiry) {
5658
const auto default_expiry = std::chrono::seconds(-1);
5759
std::shared_ptr<cache::KeyValueCache> cache =
5860
olp::client::OlpClientSettingsFactory::CreateDefaultCache({});
59-
repository::CatalogCacheRepository repository(hrn, cache, default_expiry);
61+
read::repository::CatalogCacheRepository repository(hrn, cache,
62+
default_expiry);
6063

6164
repository.Put(model_catalog);
6265
const auto result = repository.Get();
@@ -65,4 +68,50 @@ TEST(PartitionsCacheRepositoryTest, DefaultExpiry) {
6568
}
6669
}
6770

71+
TEST(CatalogCacheRepositoryTest, VersionsList) {
72+
const auto hrn = client::HRN::FromString(kCatalog);
73+
74+
read::model::VersionInfos model_versions;
75+
model_versions.SetVersions(
76+
std::vector<olp::dataservice::read::model::VersionInfo>(1));
77+
const auto default_expiry = std::chrono::seconds::max();
78+
std::shared_ptr<cache::KeyValueCache> cache =
79+
olp::client::OlpClientSettingsFactory::CreateDefaultCache({});
80+
read::repository::CatalogCacheRepository repository(hrn, cache,
81+
default_expiry);
82+
83+
{
84+
SCOPED_TRACE("Put/get versions list");
85+
86+
repository.PutVersionInfos(3, 4, model_versions);
87+
const auto result = repository.GetVersionInfos(3, 4);
88+
89+
EXPECT_TRUE(result);
90+
EXPECT_EQ(1u, result->GetVersions().size());
91+
}
92+
93+
{
94+
SCOPED_TRACE("Get versions list wrong key");
95+
96+
const auto result = repository.GetVersionInfos(300, 3001);
97+
98+
EXPECT_FALSE(result);
99+
}
100+
101+
{
102+
SCOPED_TRACE("List versions expired");
103+
104+
const auto default_expiry = std::chrono::seconds(-1);
105+
std::shared_ptr<cache::KeyValueCache> cache_expiration =
106+
olp::client::OlpClientSettingsFactory::CreateDefaultCache({});
107+
read::repository::CatalogCacheRepository repository_expiration(
108+
hrn, cache_expiration, default_expiry);
109+
110+
repository_expiration.PutVersionInfos(3, 4, model_versions);
111+
const auto result = repository_expiration.GetVersionInfos(3, 4);
112+
113+
EXPECT_FALSE(result);
114+
}
115+
}
116+
68117
} // namespace

0 commit comments

Comments
 (0)