Skip to content

Commit 6e9fe23

Browse files
Add DeleteFromCache API to layer clients (#1535)
New API allows to get exact error instead of cryptic boolean result. Old API is not deprecated yet so warnings number stays unaffected. Relates-To: DATASDK-34 Signed-off-by: Andrey Kashcheev <ext-andrey.kashcheev@here.com>
1 parent 748fd32 commit 6e9fe23

14 files changed

+227
-90
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ class VersionedLayerClientImpl;
5959
* first request made, based on this rules which apply for each specific FetchOptions:
6060
* - OnlineOnly - version is resolved from online.
6161
* - CacheOnly - version is resolved from cache.
62-
* - OnlineIfNotFound - retrieve from online first, then checks the cache in
63-
* case of error. Update cache version if online version is higher then the
62+
* - OnlineIfNotFound - retrieve from online first, then checks the cache in
63+
* case of error. Update cache version if online version is higher then the
6464
* cache version.
6565
*
6666
* An example with the catalog version provided that saves one network request:
@@ -475,6 +475,30 @@ class DATASERVICE_READ_API VersionedLayerClient final {
475475
*/
476476
bool RemoveFromCache(const geo::TileKey& tile);
477477

478+
/**
479+
* @brief Removes the partition from the mutable disk cache.
480+
*
481+
* @param partition_id The partition ID that should be removed.
482+
*
483+
* @note Before calling the API, specify a catalog version. You can set it
484+
* using the constructor or after the first online request.
485+
*
486+
* @return An error if the partition data could not be removed from the cache.
487+
*/
488+
client::ApiNoResponse DeleteFromCache(const std::string& partition_id);
489+
490+
/**
491+
* @brief Removes the tile from the mutable disk cache.
492+
*
493+
* @param tile The tile key that should be removed.
494+
*
495+
* @note Before calling the API, specify a catalog version. You can set it
496+
* using the constructor or after the first online request.
497+
*
498+
* @return An error if the tile data could not be removed from the cache.
499+
*/
500+
client::ApiNoResponse DeleteFromCache(const geo::TileKey& tile);
501+
478502
/**
479503
* @brief Checks whether the partition is cached.
480504
*

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

Lines changed: 19 additions & 1 deletion
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.
@@ -202,6 +202,24 @@ class DATASERVICE_READ_API VolatileLayerClient final {
202202
*/
203203
bool RemoveFromCache(const geo::TileKey& tile);
204204

205+
/**
206+
* @brief Removes the partition from the mutable disk cache.
207+
*
208+
* @param partition_id The partition ID that should be removed.
209+
*
210+
* @return An error if the partition data could not be removed from the cache.
211+
*/
212+
client::ApiNoResponse DeleteFromCache(const std::string& partition_id);
213+
214+
/**
215+
* @brief Removes the tile from the mutable disk cache.
216+
*
217+
* @param tile The tile key that should be removed.
218+
*
219+
* @return An error if the tile data could not be removed from the cache.
220+
*/
221+
client::ApiNoResponse DeleteFromCache(const geo::TileKey& tile);
222+
205223
/**
206224
* @brief Prefetches a set of tiles asynchronously.
207225
*

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ bool VersionedLayerClient::RemoveFromCache(const geo::TileKey& tile) {
128128
return impl_->RemoveFromCache(tile);
129129
}
130130

131+
client::ApiNoResponse VersionedLayerClient::DeleteFromCache(
132+
const std::string& partition_id) {
133+
return impl_->DeleteFromCache(partition_id);
134+
}
135+
136+
client::ApiNoResponse VersionedLayerClient::DeleteFromCache(
137+
const geo::TileKey& tile) {
138+
return impl_->DeleteFromCache(tile);
139+
}
140+
131141
client::CancellationToken VersionedLayerClient::GetAggregatedData(
132142
TileRequest request, AggregatedDataResponseCallback callback) {
133143
return impl_->GetAggregatedData(std::move(request), std::move(callback));

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -684,24 +684,34 @@ client::CancellableFuture<DataResponse> VersionedLayerClientImpl::GetData(
684684

685685
bool VersionedLayerClientImpl::RemoveFromCache(
686686
const std::string& partition_id) {
687+
return DeleteFromCache(partition_id).IsSuccessful();
688+
}
689+
690+
bool VersionedLayerClientImpl::RemoveFromCache(const geo::TileKey& tile) {
691+
return DeleteFromCache(tile).IsSuccessful();
692+
}
693+
694+
client::ApiNoResponse VersionedLayerClientImpl::DeleteFromCache(
695+
const std::string& partition_id) {
687696
auto version = catalog_version_.load();
688697
if (version == kInvalidVersion) {
689698
OLP_SDK_LOG_WARNING(
690-
kLogTag, "Method RemoveFromCache failed, version is not initialized");
691-
return false;
699+
kLogTag, "Method DeleteFromCache failed, version is not initialized");
700+
return client::ApiError::PreconditionFailed("Version is not initialized");
692701
}
693702

694703
boost::optional<model::Partition> partition;
695704

696705
repository::PartitionsCacheRepository partitions_cache_repository(
697706
catalog_, layer_id_, settings_.cache);
698-
if (!partitions_cache_repository.ClearPartitionMetadata(partition_id, version,
699-
partition)) {
700-
return false;
707+
auto clear_response = partitions_cache_repository.ClearPartitionMetadata(
708+
partition_id, version, partition);
709+
if (!clear_response) {
710+
return clear_response;
701711
}
702712

703713
if (!partition) {
704-
return true;
714+
return client::ApiNoResult{};
705715
}
706716

707717
repository::DataCacheRepository data_cache_repository(catalog_,
@@ -710,37 +720,38 @@ bool VersionedLayerClientImpl::RemoveFromCache(
710720
partition.get().GetDataHandle());
711721
}
712722

713-
bool VersionedLayerClientImpl::RemoveFromCache(const geo::TileKey& tile) {
723+
client::ApiNoResponse VersionedLayerClientImpl::DeleteFromCache(
724+
const geo::TileKey& tile) {
714725
read::QuadTreeIndex cached_tree;
715726
repository::PartitionsCacheRepository partitions_cache_repository(
716727
catalog_, layer_id_, settings_.cache);
717728
auto version = catalog_version_.load();
718729
if (version == kInvalidVersion) {
719730
OLP_SDK_LOG_WARNING(
720-
kLogTag, "Method RemoveFromCache failed, version is not initialized");
721-
return false;
731+
kLogTag, "Method DeleteFromCache failed, version is not initialized");
732+
return client::ApiError::PreconditionFailed("Version is not initialized");
722733
}
723734

724735
if (!partitions_cache_repository.FindQuadTree(tile, version, cached_tree)) {
725-
return true;
736+
return client::ApiNoResult{};
726737
}
727738

728739
auto data = cached_tree.Find(tile, false);
729740
if (!data) {
730-
return true;
741+
return client::ApiNoResult{};
731742
}
732743
repository::DataCacheRepository data_cache_repository(catalog_,
733744
settings_.cache);
734745
auto result = data_cache_repository.Clear(layer_id_, data->data_handle);
735746
if (!result) {
736-
return false;
747+
return result;
737748
}
738749

739750
auto index_data = cached_tree.GetIndexData();
740751
for (const auto& ind : index_data) {
741752
if (ind.tile_key != tile &&
742753
data_cache_repository.IsCached(layer_id_, ind.data_handle)) {
743-
return true;
754+
return client::ApiNoResult{};
744755
}
745756
}
746757

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ class VersionedLayerClientImpl {
105105

106106
virtual bool RemoveFromCache(const geo::TileKey& tile);
107107

108+
virtual client::ApiNoResponse DeleteFromCache(
109+
const std::string& partition_id);
110+
111+
virtual client::ApiNoResponse DeleteFromCache(const geo::TileKey& tile);
112+
108113
virtual bool IsCached(const std::string& partition_id);
109114

110115
virtual bool IsCached(const geo::TileKey& tile, bool aggregated = false);

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

Lines changed: 11 additions & 1 deletion
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.
@@ -72,6 +72,16 @@ bool VolatileLayerClient::RemoveFromCache(const geo::TileKey& tile) {
7272
return impl_->RemoveFromCache(tile);
7373
}
7474

75+
client::ApiNoResponse VolatileLayerClient::DeleteFromCache(
76+
const std::string& partition_id) {
77+
return impl_->DeleteFromCache(partition_id);
78+
}
79+
80+
client::ApiNoResponse VolatileLayerClient::DeleteFromCache(
81+
const geo::TileKey& tile) {
82+
return impl_->DeleteFromCache(tile);
83+
}
84+
7585
client::CancellationToken VolatileLayerClient::PrefetchTiles(
7686
PrefetchTilesRequest request, PrefetchTilesResponseCallback callback) {
7787
return impl_->PrefetchTiles(std::move(request), std::move(callback));

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,26 +131,37 @@ client::CancellableFuture<DataResponse> VolatileLayerClientImpl::GetData(
131131
}
132132

133133
bool VolatileLayerClientImpl::RemoveFromCache(const std::string& partition_id) {
134+
return DeleteFromCache(partition_id).IsSuccessful();
135+
}
136+
137+
bool VolatileLayerClientImpl::RemoveFromCache(const geo::TileKey& tile) {
138+
return DeleteFromCache(tile).IsSuccessful();
139+
}
140+
141+
client::ApiNoResponse VolatileLayerClientImpl::DeleteFromCache(
142+
const std::string& partition_id) {
134143
repository::PartitionsCacheRepository cache_repository(catalog_, layer_id_,
135144
settings_.cache);
136145
boost::optional<model::Partition> partition;
137-
if (!cache_repository.ClearPartitionMetadata(partition_id, boost::none,
138-
partition)) {
139-
return false;
146+
auto clear_response = cache_repository.ClearPartitionMetadata(
147+
partition_id, boost::none, partition);
148+
if (!clear_response) {
149+
return clear_response;
140150
}
141151

142152
if (!partition) {
143153
// partition are not stored in cache
144-
return true;
154+
return client::ApiNoResult{};
145155
}
146156

147157
repository::DataCacheRepository data_repository(catalog_, settings_.cache);
148158
return data_repository.Clear(layer_id_, partition.get().GetDataHandle());
149159
}
150160

151-
bool VolatileLayerClientImpl::RemoveFromCache(const geo::TileKey& tile) {
161+
client::ApiNoResponse VolatileLayerClientImpl::DeleteFromCache(
162+
const geo::TileKey& tile) {
152163
auto partition_id = tile.ToHereTile();
153-
return RemoveFromCache(partition_id);
164+
return DeleteFromCache(partition_id);
154165
}
155166

156167
client::CancellationToken VolatileLayerClientImpl::PrefetchTiles(

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

Lines changed: 6 additions & 1 deletion
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.
@@ -68,6 +68,11 @@ class VolatileLayerClientImpl {
6868

6969
virtual bool RemoveFromCache(const geo::TileKey& tile);
7070

71+
virtual client::ApiNoResponse DeleteFromCache(
72+
const std::string& partition_id);
73+
74+
virtual client::ApiNoResponse DeleteFromCache(const geo::TileKey& tile);
75+
7176
virtual client::CancellationToken PrefetchTiles(
7277
PrefetchTilesRequest request, PrefetchTilesResponseCallback callback);
7378

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ bool DataCacheRepository::IsCached(const std::string& layer_id,
8383
cache::KeyGenerator::CreateDataHandleKey(hrn_, layer_id, data_handle));
8484
}
8585

86-
bool DataCacheRepository::Clear(const std::string& layer_id,
87-
const std::string& data_handle) {
86+
client::ApiNoResponse DataCacheRepository::Clear(
87+
const std::string& layer_id, const std::string& data_handle) {
8888
const auto key =
8989
cache::KeyGenerator::CreateDataHandleKey(hrn_, layer_id, data_handle);
9090
OLP_SDK_LOG_DEBUG_F(kLogTag, "Clear -> '%s'", key.c_str());
91-
return cache_->RemoveKeysWithPrefix(key);
91+
return cache_->DeleteByPrefix(key);
9292
}
9393
void DataCacheRepository::PromoteInCache(const std::string& layer_id,
9494
const std::string& data_handle) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class DataCacheRepository final {
5555
void PromoteInCache(const std::string& layer_id,
5656
const std::string& data_handle);
5757

58-
bool Clear(const std::string& layer_id, const std::string& data_handle);
58+
client::ApiNoResponse Clear(const std::string& layer_id,
59+
const std::string& data_handle);
5960

6061
private:
6162
const std::string hrn_;

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,17 @@ bool PartitionsCacheRepository::ClearPartitions(
258258
return passed;
259259
}
260260

261-
bool PartitionsCacheRepository::ClearQuadTree(
261+
client::ApiNoResponse PartitionsCacheRepository::ClearQuadTree(
262262
geo::TileKey tile_key, int32_t depth,
263263
const boost::optional<int64_t>& version) {
264264
const auto key = cache::KeyGenerator::CreateQuadTreeKey(
265265
catalog_, layer_id_, tile_key, version, depth);
266266
OLP_SDK_LOG_DEBUG_F(kLogTag, "ClearQuadTree -> '%s'", key.c_str());
267267

268-
return cache_->RemoveKeysWithPrefix(key);
268+
return cache_->DeleteByPrefix(key);
269269
}
270270

271-
bool PartitionsCacheRepository::ClearPartitionMetadata(
271+
client::ApiNoResponse PartitionsCacheRepository::ClearPartitionMetadata(
272272
const std::string& partition_id,
273273
const boost::optional<int64_t>& catalog_version,
274274
boost::optional<model::Partition>& out_partition) {
@@ -278,12 +278,13 @@ bool PartitionsCacheRepository::ClearPartitionMetadata(
278278

279279
auto read_response = cache_->Read(key);
280280
if (!read_response) {
281-
return read_response.GetError().GetErrorCode() ==
282-
client::ErrorCode::NotFound;
281+
if (read_response.GetError().GetErrorCode() == client::ErrorCode::NotFound)
282+
return client::ApiNoResponse{client::ApiNoResult{}};
283+
return client::ApiNoResponse{read_response.GetError()};
283284
}
284285

285286
out_partition = parser::parse<model::Partition>(read_response.GetResult());
286-
return cache_->RemoveKeysWithPrefix(key);
287+
return cache_->DeleteByPrefix(key);
287288
}
288289

289290
bool PartitionsCacheRepository::GetPartitionHandle(

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,13 @@ class PartitionsCacheRepository final {
7575
bool ClearPartitions(const std::vector<std::string>& partition_ids,
7676
const boost::optional<int64_t>& version);
7777

78-
bool ClearQuadTree(geo::TileKey tile_key, int32_t depth,
79-
const boost::optional<int64_t>& version);
78+
client::ApiNoResponse ClearQuadTree(geo::TileKey tile_key, int32_t depth,
79+
const boost::optional<int64_t>& version);
8080

81-
bool ClearPartitionMetadata(const std::string& partition_id,
82-
const boost::optional<int64_t>& catalog_version,
83-
boost::optional<model::Partition>& out_partition);
81+
client::ApiNoResponse ClearPartitionMetadata(
82+
const std::string& partition_id,
83+
const boost::optional<int64_t>& catalog_version,
84+
boost::optional<model::Partition>& out_partition);
8485

8586
bool GetPartitionHandle(const std::string& partition_id,
8687
const boost::optional<int64_t>& catalog_version,

0 commit comments

Comments
 (0)