Skip to content

Commit 6f9d80e

Browse files
Add multi partitions Protect and Release (#1464)
The main goal is to decrease number of calls and number of log messages Relates-To: OLPEDGE-2855 Signed-off-by: Rustam Gamidov <ext-rustam.gamidov@here.com>
1 parent c2621d0 commit 6f9d80e

File tree

7 files changed

+126
-22
lines changed

7 files changed

+126
-22
lines changed

olp-cpp-sdk-dataservice-read/include/olp/dataservice/read/VersionedLayerClient.h

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2023 HERE Europe B.V.
2+
* Copyright (C) 2019-2024 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.
@@ -286,7 +286,7 @@ class DATASERVICE_READ_API VersionedLayerClient final {
286286
* @brief Fetches a list of partitions of the given generic layer
287287
* asynchronously. Client does not cache the partitions, instead every
288288
* partition is passed to the provided callback.
289-
*
289+
*
290290
* @note API is considered experimental and a subject to change.
291291
*
292292
* @param partitions_request The `PartitionsRequest` instance that contains
@@ -549,6 +549,29 @@ class DATASERVICE_READ_API VersionedLayerClient final {
549549
*/
550550
bool Protect(const std::string& partition_id);
551551

552+
/**
553+
* @brief Protect partitions from eviction.
554+
*
555+
* Protecting partitions means that its data and metadata keys are added to
556+
* the protected list and stored in the cache. These keys are removed from the
557+
* LRU cache, so they could not be evicted. Also, they do not expire.
558+
*
559+
* @note Before calling the API, specify a catalog version. You can set it
560+
* using the constructor or after the first online request.
561+
*
562+
* @note You can only protect partitions which data handles are present in the
563+
* cache at the time of the call.
564+
*
565+
* @note Please do not call `Protect` while the `Release` call for the same
566+
* catalog and layer is in progress.
567+
*
568+
* @param partition_ids Partition ids to be protected.
569+
*
570+
* @return True if partition keys were successfully added to the protected
571+
* list; false otherwise.
572+
*/
573+
bool Protect(const std::vector<std::string>& partition_ids);
574+
552575
/**
553576
* @brief Removes a list of tiles from protection.
554577
*
@@ -591,6 +614,27 @@ class DATASERVICE_READ_API VersionedLayerClient final {
591614
*/
592615
bool Release(const std::string& partition_id);
593616

617+
/**
618+
* @brief Removes partitions from protection.
619+
*
620+
* Releasing partition ids removes data handle and metadata keys from the
621+
* protected list. The keys are added to the LRU cache, so they could be
622+
* evicted. Expiration value is restored, and related to partition keys can
623+
* expire.
624+
*
625+
* @note Before calling the API, specify a catalog version. You can set it
626+
* using the constructor or after the first online request.
627+
*
628+
* @note Please make sure that `Protect` will not be called for the same
629+
* catalog and layer while the `Release` call is in progress.
630+
*
631+
* @param partition_ids Partition ids to be removed from protection.
632+
*
633+
* @return True if keys related to partitions were successfully removed from
634+
* the protected list; false otherwise.
635+
*/
636+
bool Release(const std::vector<std::string>& partition_ids);
637+
594638
private:
595639
std::unique_ptr<VersionedLayerClientImpl> impl_;
596640
};

olp-cpp-sdk-dataservice-read/src/VersionedLayerClient.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2023 HERE Europe B.V.
2+
* Copyright (C) 2019-2024 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.
@@ -156,11 +156,21 @@ bool VersionedLayerClient::Release(const TileKeys& tiles) {
156156
}
157157

158158
bool VersionedLayerClient::Protect(const std::string& partition_id) {
159-
return impl_->Protect(partition_id);
159+
return impl_->Protect({partition_id});
160160
}
161161

162162
bool VersionedLayerClient::Release(const std::string& partition_id) {
163-
return impl_->Release(partition_id);
163+
return impl_->Release({partition_id});
164+
}
165+
166+
bool VersionedLayerClient::Protect(
167+
const std::vector<std::string>& partition_ids) {
168+
return impl_->Protect(partition_ids);
169+
}
170+
171+
bool VersionedLayerClient::Release(
172+
const std::vector<std::string>& partition_ids) {
173+
return impl_->Release(partition_ids);
164174
}
165175

166176
} // namespace read

olp-cpp-sdk-dataservice-read/src/VersionedLayerClientImpl.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2023 HERE Europe B.V.
2+
* Copyright (C) 2019-2024 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.
@@ -910,7 +910,8 @@ bool VersionedLayerClientImpl::Release(const TileKeys& tiles) {
910910
return settings_.cache->Release(keys_to_release);
911911
}
912912

913-
bool VersionedLayerClientImpl::Protect(const std::string& partition_id) {
913+
bool VersionedLayerClientImpl::Protect(
914+
const std::vector<std::string>& partition_ids) {
914915
if (!settings_.cache) {
915916
return {};
916917
}
@@ -925,10 +926,11 @@ bool VersionedLayerClientImpl::Protect(const std::string& partition_id) {
925926
repository::PartitionsCacheRepository repository(catalog_, layer_id_,
926927
settings_.cache);
927928

928-
return repository.Protect(partition_id, version);
929+
return repository.Protect(partition_ids, version);
929930
}
930931

931-
bool VersionedLayerClientImpl::Release(const std::string& partition_id) {
932+
bool VersionedLayerClientImpl::Release(
933+
const std::vector<std::string>& partition_ids) {
932934
if (!settings_.cache) {
933935
return {};
934936
}
@@ -943,7 +945,7 @@ bool VersionedLayerClientImpl::Release(const std::string& partition_id) {
943945
repository::PartitionsCacheRepository repository(catalog_, layer_id_,
944946
settings_.cache);
945947

946-
return repository.Release(partition_id, version);
948+
return repository.Release(partition_ids, version);
947949
}
948950

949951
} // namespace read

olp-cpp-sdk-dataservice-read/src/VersionedLayerClientImpl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2023 HERE Europe B.V.
2+
* Copyright (C) 2019-2024 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.
@@ -119,9 +119,9 @@ class VersionedLayerClientImpl {
119119

120120
virtual bool Release(const TileKeys& tiles);
121121

122-
virtual bool Protect(const std::string& partition_id);
122+
virtual bool Protect(const std::vector<std::string>& partition_ids);
123123

124-
virtual bool Release(const std::string& partition_id);
124+
virtual bool Release(const std::vector<std::string>& partition_ids);
125125

126126
private:
127127
CatalogVersionResponse GetVersion(boost::optional<std::string> billing_tag,

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2023 HERE Europe B.V.
2+
* Copyright (C) 2019-2024 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.
@@ -357,8 +357,18 @@ PartitionsCacheRepository::CreatePartitionKeys(
357357
}
358358

359359
bool PartitionsCacheRepository::Protect(
360-
const std::string& partition_id, const boost::optional<int64_t>& version) {
361-
const auto keys = CreatePartitionKeys(partition_id, version);
360+
const std::vector<std::string>& partition_ids,
361+
const boost::optional<int64_t>& version) {
362+
cache::KeyValueCache::KeyListType keys;
363+
keys.reserve(partition_ids.size() * 2u);
364+
std::for_each(partition_ids.cbegin(), partition_ids.cend(),
365+
[&](const std::string& partition_id) {
366+
auto partition_keys =
367+
CreatePartitionKeys(partition_id, version);
368+
std::move(partition_keys.begin(), partition_keys.end(),
369+
std::back_inserter(keys));
370+
});
371+
362372
if (keys.empty()) {
363373
return false;
364374
}
@@ -367,8 +377,18 @@ bool PartitionsCacheRepository::Protect(
367377
}
368378

369379
bool PartitionsCacheRepository::Release(
370-
const std::string& partition_id, const boost::optional<int64_t>& version) {
371-
const auto keys = CreatePartitionKeys(partition_id, version);
380+
const std::vector<std::string>& partition_ids,
381+
const boost::optional<int64_t>& version) {
382+
cache::KeyValueCache::KeyListType keys;
383+
keys.reserve(partition_ids.size() * 2u);
384+
std::for_each(partition_ids.cbegin(), partition_ids.cend(),
385+
[&](const std::string& partition_id) {
386+
auto partition_keys =
387+
CreatePartitionKeys(partition_id, version);
388+
std::move(partition_keys.begin(), partition_keys.end(),
389+
std::back_inserter(keys));
390+
});
391+
372392
if (keys.empty()) {
373393
return false;
374394
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2021 HERE Europe B.V.
2+
* Copyright (C) 2019-2024 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.
@@ -92,10 +92,10 @@ class PartitionsCacheRepository final {
9292
bool ContainsTree(geo::TileKey key, int32_t depth,
9393
const boost::optional<int64_t>& version) const;
9494

95-
bool Protect(const std::string& partition_id,
95+
bool Protect(const std::vector<std::string>& partition_ids,
9696
const boost::optional<int64_t>& version);
9797

98-
bool Release(const std::string& partition_id,
98+
bool Release(const std::vector<std::string>& partition_ids,
9999
const boost::optional<int64_t>& version);
100100

101101
private:

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2022 HERE Europe B.V.
2+
* Copyright (C) 2019-2024 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.
@@ -384,6 +384,20 @@ TEST(VersionedLayerClientTest, ProtectThenReleasePartition) {
384384
ASSERT_TRUE(client.Protect(kPartitionId));
385385
}
386386

387+
{
388+
SCOPED_TRACE("Successful protect partitions");
389+
390+
EXPECT_CALL(*cache_mock, Get(_, _)).WillOnce(found_cache_response);
391+
EXPECT_CALL(*cache_mock, Protect(_)).WillOnce(partition_keys);
392+
ASSERT_TRUE(client.Protect(std::vector<std::string>{kPartitionId}));
393+
}
394+
395+
{
396+
SCOPED_TRACE("Protect empty partitions");
397+
398+
ASSERT_FALSE(client.Protect(std::vector<std::string>{}));
399+
}
400+
387401
{
388402
SCOPED_TRACE("Successful release partition");
389403

@@ -392,6 +406,20 @@ TEST(VersionedLayerClientTest, ProtectThenReleasePartition) {
392406
ASSERT_TRUE(client.Release(kPartitionId));
393407
}
394408

409+
{
410+
SCOPED_TRACE("Successful release partitions");
411+
412+
EXPECT_CALL(*cache_mock, Get(_, _)).WillOnce(found_cache_response);
413+
EXPECT_CALL(*cache_mock, Release(_)).WillOnce(partition_keys);
414+
ASSERT_TRUE(client.Release(std::vector<std::string>{kPartitionId}));
415+
}
416+
417+
{
418+
SCOPED_TRACE("Release empty partitions");
419+
420+
ASSERT_FALSE(client.Release(std::vector<std::string>{}));
421+
}
422+
395423
{
396424
SCOPED_TRACE("Protect not existing partition");
397425
EXPECT_CALL(*cache_mock, Get(_, _))

0 commit comments

Comments
 (0)