-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathPartitionsCacheRepository.cpp
More file actions
397 lines (336 loc) · 13.7 KB
/
PartitionsCacheRepository.cpp
File metadata and controls
397 lines (336 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Copyright (C) 2019-2025 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
#include "PartitionsCacheRepository.h"
#include <algorithm>
#include <limits>
#include <string>
#include <utility>
#include <vector>
#include <olp/core/cache/KeyGenerator.h>
#include <olp/core/cache/KeyValueCache.h>
#include <olp/core/logging/Log.h>
// clang-format off
#include "generated/parser/PartitionsParser.h"
#include "generated/parser/LayerVersionsParser.h"
#include "JsonResultParser.h"
#include <olp/core/generated/parser/JsonParser.h>
#include <olp/core/generated/serializer/SerializerWrapper.h>
#include "generated/serializer/PartitionsSerializer.h"
#include "generated/serializer/LayerVersionsSerializer.h"
#include "generated/serializer/JsonSerializer.h"
// clang-format on
namespace {
constexpr auto kLogTag = "PartitionsCacheRepository";
constexpr auto kChronoSecondsMax = std::chrono::seconds::max();
constexpr auto kTimetMax = std::numeric_limits<time_t>::max();
constexpr auto kMaxQuadTreeIndexDepth = 4u;
time_t ConvertTime(std::chrono::seconds time) {
return time == kChronoSecondsMax ? kTimetMax : time.count();
}
} // namespace
namespace olp {
namespace dataservice {
namespace read {
namespace repository {
PartitionsCacheRepository::PartitionsCacheRepository(
const client::HRN& catalog, const std::string& layer_id,
std::shared_ptr<cache::KeyValueCache> cache,
std::chrono::seconds default_expiry)
: catalog_(catalog.ToCatalogHRNString()),
layer_id_(layer_id),
cache_(std::move(cache)),
default_expiry_(ConvertTime(default_expiry)) {}
client::ApiNoResponse PartitionsCacheRepository::Put(
const model::Partitions& partitions,
const porting::optional<int64_t>& version,
const porting::optional<time_t>& expiry, bool layer_metadata) {
const auto& partitions_list = partitions.GetPartitions();
std::vector<std::string> partition_ids;
partition_ids.reserve(partitions_list.size());
for (const auto& partition : partitions_list) {
const auto key = cache::KeyGenerator::CreatePartitionKey(
catalog_, layer_id_, partition.GetPartition(), version);
OLP_SDK_LOG_TRACE_F(kLogTag, "Put -> '%s'", key.c_str());
const auto put_result =
cache_->Write(key, serializer::serialize_bytes(partition),
porting::value_or(expiry, default_expiry_));
if (!put_result) {
OLP_SDK_LOG_ERROR_F(kLogTag, "Failed to write -> '%s'", key.c_str());
return put_result.GetError();
}
if (layer_metadata) {
partition_ids.push_back(partition.GetPartition());
}
}
if (layer_metadata) {
const auto key =
cache::KeyGenerator::CreatePartitionsKey(catalog_, layer_id_, version);
OLP_SDK_LOG_TRACE_F(kLogTag, "Put -> '%s'", key.c_str());
const auto put_result =
cache_->Write(key, serializer::serialize_bytes(partition_ids),
porting::value_or(expiry, default_expiry_));
if (!put_result) {
OLP_SDK_LOG_ERROR_F(kLogTag, "Failed to write -> '%s'", key.c_str());
return put_result.GetError();
}
}
return {client::ApiNoResult{}};
}
model::Partitions PartitionsCacheRepository::Get(
const std::vector<std::string>& partition_ids,
const porting::optional<int64_t>& version) {
model::Partitions cached_partitions_model;
auto& cached_partitions = cached_partitions_model.GetMutablePartitions();
cached_partitions.reserve(partition_ids.size());
for (const auto& partition_id : partition_ids) {
const auto key = cache::KeyGenerator::CreatePartitionKey(
catalog_, layer_id_, partition_id, version);
OLP_SDK_LOG_TRACE_F(kLogTag, "Get '%s'", key.c_str());
auto read_response = cache_->Read(key);
if (read_response) {
auto partition =
parser::parse<model::Partition>(read_response.GetResult());
cached_partitions.emplace_back(std::move(partition));
}
}
cached_partitions.shrink_to_fit();
return cached_partitions_model;
}
porting::optional<model::Partitions> PartitionsCacheRepository::Get(
const PartitionsRequest& request,
const porting::optional<int64_t>& version) {
const auto key =
cache::KeyGenerator::CreatePartitionsKey(catalog_, layer_id_, version);
porting::optional<model::Partitions> partitions;
const auto& partition_ids = request.GetPartitionIds();
if (partition_ids.empty()) {
auto read_response = cache_->Read(key);
if (read_response) {
const auto cached_ids =
parser::parse<std::vector<std::string>>(read_response.GetResult());
partitions = Get(cached_ids, version);
} else {
partitions = olp::porting::none;
}
} else {
auto available_partitions = Get(partition_ids, version);
// In the case when not all partitions are available, we fail the cache
// lookup. This can be enhanced in the future.
if (available_partitions.GetPartitions().size() != partition_ids.size()) {
partitions = olp::porting::none;
} else {
partitions = std::move(available_partitions);
}
}
return partitions;
}
bool PartitionsCacheRepository::Put(
int64_t catalog_version, const model::LayerVersions& layer_versions) {
const auto key =
cache::KeyGenerator::CreateLayerVersionsKey(catalog_, catalog_version);
OLP_SDK_LOG_TRACE_F(kLogTag, "Put -> '%s'", key.c_str());
return cache_->Put(key, layer_versions,
[&]() { return serializer::serialize(layer_versions); },
default_expiry_);
}
porting::optional<model::LayerVersions> PartitionsCacheRepository::Get(
int64_t catalog_version) {
const auto key =
cache::KeyGenerator::CreateLayerVersionsKey(catalog_, catalog_version);
OLP_SDK_LOG_TRACE_F(kLogTag, "Get -> '%s'", key.c_str());
auto cached_layer_versions =
cache_->Get(key, [](const std::string& serialized_object) {
return parser::parse<model::LayerVersions>(serialized_object);
});
if (cached_layer_versions.empty()) {
return olp::porting::none;
}
return std::move(
olp::porting::any_cast<model::LayerVersions&&>(cached_layer_versions));
}
client::ApiNoResponse PartitionsCacheRepository::Put(
geo::TileKey tile_key, int32_t depth, const QuadTreeIndex& quad_tree,
const porting::optional<int64_t>& version) {
const auto key = cache::KeyGenerator::CreateQuadTreeKey(
catalog_, layer_id_, tile_key, version, depth);
if (quad_tree.IsNull()) {
OLP_SDK_LOG_WARNING_F(kLogTag, "Put: invalid QuadTreeIndex -> '%s'",
key.c_str());
return {client::ApiNoResult{}};
}
OLP_SDK_LOG_TRACE_F(kLogTag, "Put -> '%s'", key.c_str());
auto write_response =
cache_->Write(key, quad_tree.GetRawData(), default_expiry_);
if (!write_response) {
OLP_SDK_LOG_WARNING_F(kLogTag, "Failed to write -> '%s'", key.c_str());
return write_response.GetError();
}
return {client::ApiNoResult{}};
}
bool PartitionsCacheRepository::Get(geo::TileKey tile_key, int32_t depth,
const porting::optional<int64_t>& version,
QuadTreeIndex& tree) {
const auto key = cache::KeyGenerator::CreateQuadTreeKey(
catalog_, layer_id_, tile_key, version, depth);
OLP_SDK_LOG_TRACE_F(kLogTag, "Get -> '%s'", key.c_str());
auto read_response = cache_->Read(key);
if (read_response) {
tree = QuadTreeIndex(read_response.GetResult());
return true;
}
return false;
}
bool PartitionsCacheRepository::Clear() {
auto key = catalog_ + "::" + layer_id_ + "::";
OLP_SDK_LOG_TRACE_F(kLogTag, "Clear -> '%s'", key.c_str());
return cache_->RemoveKeysWithPrefix(key);
}
bool PartitionsCacheRepository::ClearPartitions(
const std::vector<std::string>& partition_ids,
const porting::optional<int64_t>& version) {
OLP_SDK_LOG_TRACE_F(kLogTag, "ClearPartitions -> '%s'", catalog_.c_str());
auto cached_partitions = Get(partition_ids, version);
bool passed = true;
// Partitions not processed here are not cached to begin with.
for (const auto& partition : cached_partitions.GetPartitions()) {
passed = cache_->RemoveKeysWithPrefix(catalog_ + "::" + layer_id_ +
"::" + partition.GetDataHandle()) &&
passed;
passed = cache_->RemoveKeysWithPrefix(catalog_ + "::" + layer_id_ +
"::" + partition.GetPartition()) &&
passed;
}
return passed;
}
client::ApiNoResponse PartitionsCacheRepository::ClearQuadTree(
geo::TileKey tile_key, int32_t depth,
const porting::optional<int64_t>& version) {
const auto key = cache::KeyGenerator::CreateQuadTreeKey(
catalog_, layer_id_, tile_key, version, depth);
OLP_SDK_LOG_TRACE_F(kLogTag, "ClearQuadTree -> '%s'", key.c_str());
return cache_->DeleteByPrefix(key);
}
client::ApiNoResponse PartitionsCacheRepository::ClearPartitionMetadata(
const std::string& partition_id,
const porting::optional<int64_t>& catalog_version,
porting::optional<model::Partition>& out_partition) {
const auto key = cache::KeyGenerator::CreatePartitionKey(
catalog_, layer_id_, partition_id, catalog_version);
OLP_SDK_LOG_TRACE_F(kLogTag, "ClearPartitionMetadata -> '%s'", key.c_str());
auto read_response = cache_->Read(key);
if (!read_response) {
if (read_response.GetError().GetErrorCode() == client::ErrorCode::NotFound)
return client::ApiNoResponse{client::ApiNoResult{}};
return client::ApiNoResponse{read_response.GetError()};
}
out_partition = parser::parse<model::Partition>(read_response.GetResult());
return cache_->DeleteByPrefix(key);
}
bool PartitionsCacheRepository::GetPartitionHandle(
const std::string& partition_id,
const porting::optional<int64_t>& catalog_version,
std::string& data_handle) {
const auto key = cache::KeyGenerator::CreatePartitionKey(
catalog_, layer_id_, partition_id, catalog_version);
OLP_SDK_LOG_TRACE_F(kLogTag, "IsPartitionCached -> '%s'", key.c_str());
auto read_response = cache_->Read(key);
if (!read_response) {
return false;
}
auto partition = parser::parse<model::Partition>(read_response.GetResult());
data_handle = std::move(partition.GetMutableDataHandle());
return true;
}
bool PartitionsCacheRepository::FindQuadTree(geo::TileKey key,
porting::optional<int64_t> version,
read::QuadTreeIndex& tree) {
auto max_depth = std::min<std::uint32_t>(key.Level(), kMaxQuadTreeIndexDepth);
for (int32_t i = max_depth; i >= 0; i--) {
const auto& root_tile_key = key.ChangedLevelBy(-i);
QuadTreeIndex cached_tree;
if (Get(root_tile_key, kMaxQuadTreeIndexDepth, version, cached_tree)) {
OLP_SDK_LOG_TRACE_F(kLogTag,
"FindQuadTree found in cache, tile='%s', "
"root='%s', depth='%" PRId32 "'",
key.ToHereTile().c_str(),
root_tile_key.ToHereTile().c_str(),
kMaxQuadTreeIndexDepth);
tree = std::move(cached_tree);
return true;
}
}
return false;
}
bool PartitionsCacheRepository::ContainsTree(
geo::TileKey key, int32_t depth,
const porting::optional<int64_t>& version) const {
return cache_->Contains(cache::KeyGenerator::CreateQuadTreeKey(
catalog_, layer_id_, key, version, depth));
}
cache::KeyValueCache::KeyListType
PartitionsCacheRepository::CreatePartitionKeys(
const std::string& partition_id,
const porting::optional<int64_t>& version) {
std::string handle;
if (GetPartitionHandle(partition_id, version, handle)) {
return cache::KeyValueCache::KeyListType{
cache::KeyGenerator::CreatePartitionKey(catalog_, layer_id_,
partition_id, version),
cache::KeyGenerator::CreateDataHandleKey(catalog_, layer_id_, handle)};
}
return {};
}
bool PartitionsCacheRepository::Protect(
const std::vector<std::string>& partition_ids,
const porting::optional<int64_t>& version) {
cache::KeyValueCache::KeyListType keys;
keys.reserve(partition_ids.size() * 2u);
std::for_each(partition_ids.cbegin(), partition_ids.cend(),
[&](const std::string& partition_id) {
auto partition_keys =
CreatePartitionKeys(partition_id, version);
std::move(partition_keys.begin(), partition_keys.end(),
std::back_inserter(keys));
});
if (keys.empty()) {
return false;
}
return cache_->Protect(keys);
}
bool PartitionsCacheRepository::Release(
const std::vector<std::string>& partition_ids,
const porting::optional<int64_t>& version) {
cache::KeyValueCache::KeyListType keys;
keys.reserve(partition_ids.size() * 2u);
std::for_each(partition_ids.cbegin(), partition_ids.cend(),
[&](const std::string& partition_id) {
auto partition_keys =
CreatePartitionKeys(partition_id, version);
std::move(partition_keys.begin(), partition_keys.end(),
std::back_inserter(keys));
});
if (keys.empty()) {
return false;
}
return cache_->Release(keys);
}
} // namespace repository
} // namespace read
} // namespace dataservice
} // namespace olp