Skip to content

Commit f4add0a

Browse files
authored
Expose cache keys creation. (#1265)
Added new class to create keys used to store partitions, bundles, quadtrees etc. Now user can modify and rewrite data in the cache. All *CacheRepository classes now use the API instead of internal hidden scattered methods. Integration testing done via adding new checks in existing tests. The change doesn't add new functionality, but we must be sure clients write data with same cache keys. Resolves: OLPEDGE-2649 Signed-off-by: Kostiantyn Zvieriev <[email protected]>
1 parent c0990a1 commit f4add0a

22 files changed

+612
-124
lines changed

olp-cpp-sdk-core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ include(cmake/ios.cmake)
6262
set(OLP_SDK_CACHE_HEADERS
6363
./include/olp/core/cache/CacheSettings.h
6464
./include/olp/core/cache/DefaultCache.h
65+
./include/olp/core/cache/KeyGenerator.h
6566
./include/olp/core/cache/KeyValueCache.h
6667
)
6768

@@ -234,6 +235,7 @@ set(OLP_SDK_CACHE_SOURCES
234235
./src/cache/DiskCacheSizeLimitEnv.h
235236
./src/cache/DiskCacheSizeLimitWritableFile.cpp
236237
./src/cache/DiskCacheSizeLimitWritableFile.h
238+
./src/cache/KeyGenerator.cpp
237239
./src/cache/ProtectedKeyList.cpp
238240
./src/cache/ProtectedKeyList.h
239241
./src/cache/InMemoryCache.cpp
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Copyright (C) 2021 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 <string>
23+
24+
#include <olp/core/CoreApi.h>
25+
#include <olp/core/client/HRN.h>
26+
#include <olp/core/geo/tiling/TileKey.h>
27+
#include <boost/optional.hpp>
28+
29+
namespace olp {
30+
namespace cache {
31+
32+
/**
33+
* @brief Helper class to generate cache keys for different entities.
34+
*/
35+
class CORE_API KeyGenerator {
36+
public:
37+
/**
38+
* @brief Generates cache key for service API.
39+
*
40+
* @param hrn The HRN of the catalog.
41+
* @param service The service.
42+
* @param version The version of the service.
43+
*
44+
* @return A key used to store the API in cache.
45+
*/
46+
static std::string CreateApiKey(const std::string& hrn,
47+
const std::string& service,
48+
const std::string& version);
49+
50+
/**
51+
* @brief Generates cache key for catalog data.
52+
*
53+
* @param hrn The HRN of the catalog.
54+
*
55+
* @return A key used to store the catalog data in cache.
56+
*/
57+
static std::string CreateCatalogKey(const std::string& hrn);
58+
59+
/**
60+
* @brief Generates cache key to store latest catalog version.
61+
*
62+
* @param hrn The HRN of the catalog.
63+
*
64+
* @return A key used to store the version in cache.
65+
*/
66+
static std::string CreateLatestVersionKey(const std::string& hrn);
67+
68+
/**
69+
* @brief Generates cache key for storing partition data.
70+
*
71+
* @param hrn The HRN of the catalog.
72+
* @param layer_id The layer of the partition.
73+
* @param partition_id The partition name.
74+
* @param version The version of the catalog.
75+
*
76+
* @return A key used to store the patition data in cache.
77+
*/
78+
static std::string CreatePartitionKey(
79+
const std::string& hrn, const std::string& layer_id,
80+
const std::string& partition_id, const boost::optional<int64_t>& version);
81+
82+
/**
83+
* @brief Generates cache key for storing list of partitions.
84+
*
85+
* @param hrn The HRN of the catalog.
86+
* @param layer_id The layer of the partition.
87+
* @param version The version of the catalog.
88+
*
89+
* @return A key used to store the list of patitions in cache.
90+
*/
91+
static std::string CreatePartitionsKey(
92+
const std::string& hrn, const std::string& layer_id,
93+
const boost::optional<int64_t>& version);
94+
95+
/**
96+
* @brief Generates cache key for storing list of available layer versions.
97+
*
98+
* @param hrn The HRN of the catalog.
99+
* @param version The version of the catalog.
100+
*
101+
* @return A key used to store the list layer versions in cache.
102+
*/
103+
static std::string CreateLayerVersionsKey(const std::string& hrn,
104+
const int64_t version);
105+
106+
/**
107+
* @brief Generates cache key for storing quadtree metadata.
108+
*
109+
* @param hrn The HRN of the catalog.
110+
* @param layer_id The layer of the quadtree.
111+
* @param root The root tile of the quadtree.
112+
* @param version The version of the catalog.
113+
* @param depth The quadtree depth.
114+
*
115+
* @return A key used to store the quadtree in cache.
116+
*/
117+
static std::string CreateQuadTreeKey(const std::string& hrn,
118+
const std::string& layer_id,
119+
olp::geo::TileKey root,
120+
const boost::optional<int64_t>& version,
121+
int32_t depth);
122+
123+
/**
124+
* @brief Generates cache key for data handle entities.
125+
*
126+
* @param hrn The HRN of the catalog.
127+
* @param layer_id The layer of the data handle.
128+
* @param data_handle The data handle.
129+
*
130+
* @return A key used to store the data handle in cache.
131+
*/
132+
static std::string CreateDataHandleKey(const std::string& hrn,
133+
const std::string& layer_id,
134+
const std::string& data_handle);
135+
};
136+
137+
} // namespace cache
138+
} // namespace olp
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (C) 2021 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 "olp/core/cache/KeyGenerator.h"
21+
22+
namespace olp {
23+
namespace cache {
24+
std::string KeyGenerator::CreateApiKey(const std::string& hrn,
25+
const std::string& service,
26+
const std::string& version) {
27+
return hrn + "::" + service + "::" + version + "::api";
28+
}
29+
30+
std::string KeyGenerator::CreateCatalogKey(const std::string& hrn) {
31+
return hrn + "::catalog";
32+
}
33+
34+
std::string KeyGenerator::CreateLatestVersionKey(const std::string& hrn) {
35+
return hrn + "::latestVersion";
36+
}
37+
38+
std::string KeyGenerator::CreatePartitionKey(
39+
const std::string& hrn, const std::string& layer_id,
40+
const std::string& partition_id, const boost::optional<int64_t>& version) {
41+
return hrn + "::" + layer_id + "::" + partition_id +
42+
"::" + (version ? std::to_string(*version) + "::" : "") + "partition";
43+
}
44+
45+
std::string KeyGenerator::CreatePartitionsKey(
46+
const std::string& hrn, const std::string& layer_id,
47+
const boost::optional<int64_t>& version) {
48+
return hrn + "::" + layer_id +
49+
"::" + (version ? std::to_string(*version) + "::" : "") + "partitions";
50+
}
51+
52+
std::string KeyGenerator::CreateLayerVersionsKey(const std::string& hrn,
53+
const int64_t version) {
54+
return hrn + "::" + std::to_string(version) + "::layerVersions";
55+
}
56+
57+
std::string KeyGenerator::CreateQuadTreeKey(
58+
const std::string& hrn, const std::string& layer_id, olp::geo::TileKey root,
59+
const boost::optional<int64_t>& version, int32_t depth) {
60+
return hrn + "::" + layer_id + "::" + root.ToHereTile() +
61+
"::" + (version ? std::to_string(*version) + "::" : "") +
62+
std::to_string(depth) + "::quadtree";
63+
}
64+
65+
std::string KeyGenerator::CreateDataHandleKey(const std::string& hrn,
66+
const std::string& layer_id,
67+
const std::string& data_handle) {
68+
return hrn + "::" + layer_id + "::" + data_handle + "::Data";
69+
}
70+
71+
} // namespace cache
72+
} // namespace olp

olp-cpp-sdk-core/src/client/repository/ApiCacheRepository.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 HERE Europe B.V.
2+
* Copyright (C) 2020-2021 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,13 @@
1919

2020
#include "ApiCacheRepository.h"
2121

22+
#include <olp/core/cache/KeyGenerator.h>
2223
#include <olp/core/cache/KeyValueCache.h>
2324
#include <olp/core/logging/Log.h>
2425

2526
namespace {
2627
constexpr auto kLogTag = "ApiCacheRepository";
2728
constexpr time_t kLookupApiExpiryTime = 3600;
28-
29-
std::string CreateKey(const std::string& hrn, const std::string& service,
30-
const std::string& serviceVersion) {
31-
return hrn + "::" + service + "::" + serviceVersion + "::api";
32-
}
3329
} // namespace
3430

3531
namespace olp {
@@ -42,7 +38,7 @@ ApiCacheRepository::ApiCacheRepository(
4238
void ApiCacheRepository::Put(const std::string& service,
4339
const std::string& version, const std::string& url,
4440
boost::optional<time_t> expiry) {
45-
auto key = CreateKey(hrn_, service, version);
41+
const auto key = cache::KeyGenerator::CreateApiKey(hrn_, service, version);
4642
OLP_SDK_LOG_DEBUG_F(kLogTag, "Put -> '%s'", key.c_str());
4743

4844
cache_->Put(key, url, [&]() { return url; },
@@ -51,7 +47,7 @@ void ApiCacheRepository::Put(const std::string& service,
5147

5248
boost::optional<std::string> ApiCacheRepository::Get(
5349
const std::string& service, const std::string& version) {
54-
auto key = CreateKey(hrn_, service, version);
50+
const auto key = cache::KeyGenerator::CreateApiKey(hrn_, service, version);
5551
OLP_SDK_LOG_DEBUG_F(kLogTag, "Get -> '%s'", key.c_str());
5652

5753
auto url = cache_->Get(key, [](const std::string& value) { return value; });

olp-cpp-sdk-core/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ set(OLP_CPP_SDK_CORE_TESTS_SOURCES
2121
./cache/Helpers.cpp
2222
./cache/Helpers.h
2323
./cache/InMemoryCacheTest.cpp
24+
./cache/KeyGeneratorTest.cpp
2425
./cache/ProtectedKeyListTest.cpp
2526

2627
./client/ApiLookupClientImplTest.cpp

0 commit comments

Comments
 (0)