Skip to content

Commit fc00797

Browse files
authored
RemoveFromCache API for VersionedLayerClient. (#719)
Added new RemoveFromCache method. It's now possible to clear all cache data for specific partition. Added integration test for the new functionality. Resolves: OLPEDGE-1567 Signed-off-by: Kostiantyn Zvieriev <[email protected]>
1 parent 61f8626 commit fc00797

File tree

10 files changed

+223
-45
lines changed

10 files changed

+223
-45
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,15 @@ class DATASERVICE_READ_API VersionedLayerClient final {
318318
client::CancellableFuture<PrefetchTilesResponse> PrefetchTiles(
319319
PrefetchTilesRequest request);
320320

321+
/**
322+
* @brief Removes the partition from the mutable disk cache.
323+
*
324+
* @param partition_id The partition ID that should be removed.
325+
*
326+
* @return True if partition data is removed successfully; false otherwise.
327+
*/
328+
bool RemoveFromCache(const std::string& partition_id);
329+
321330
private:
322331
std::unique_ptr<VersionedLayerClientImpl> impl_;
323332
};

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ client::CancellableFuture<DataResponse> VersionedLayerClient::GetData(
9494
return impl_->GetData(std::move(request));
9595
}
9696

97+
bool VersionedLayerClient::RemoveFromCache(const std::string& partition_id) {
98+
return impl_->RemoveFromCache(partition_id);
99+
}
100+
97101
} // namespace read
98102
} // namespace dataservice
99103
} // namespace olp

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <olp/dataservice/read/CatalogVersionRequest.h>
3030
#include "Common.h"
3131
#include "repositories/CatalogRepository.h"
32+
#include "repositories/DataCacheRepository.h"
3233
#include "repositories/DataRepository.h"
3334
#include "repositories/PartitionsRepository.h"
3435
#include "repositories/PrefetchTilesRepository.h"
@@ -452,6 +453,24 @@ client::CancellableFuture<DataResponse> VersionedLayerClientImpl::GetData(
452453
std::move(promise));
453454
}
454455

456+
bool VersionedLayerClientImpl::RemoveFromCache(
457+
const std::string& partition_id) {
458+
repository::PartitionsCacheRepository cache_repository(catalog_,
459+
settings_.cache);
460+
boost::optional<model::Partition> partition;
461+
if (!cache_repository.ClearPartitionMetadata(
462+
catalog_version_.load(), partition_id, layer_id_, partition)) {
463+
return false;
464+
}
465+
466+
if (!partition) {
467+
return true;
468+
}
469+
470+
repository::DataCacheRepository data_repository(catalog_, settings_.cache);
471+
return data_repository.Clear(layer_id_, partition.get().GetDataHandle());
472+
}
473+
455474
PORTING_POP_WARNINGS()
456475
} // namespace read
457476
} // namespace dataservice

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class VersionedLayerClientImpl {
8181
virtual client::CancellableFuture<PrefetchTilesResponse> PrefetchTiles(
8282
PrefetchTilesRequest request);
8383

84+
virtual bool RemoveFromCache(const std::string& partition_id);
85+
8486
private:
8587
CatalogVersionResponse GetVersion(boost::optional<std::string> billing_tag,
8688
const FetchOptions& fetch_options,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ boost::optional<model::Data> DataCacheRepository::Get(
6262
return std::move(cachedData);
6363
}
6464

65-
void DataCacheRepository::Clear(const std::string& layer_id,
65+
bool DataCacheRepository::Clear(const std::string& layer_id,
6666
const std::string& data_handle) {
6767
std::string hrn(hrn_.ToCatalogHRNString());
6868
auto key = CreateKey(hrn, layer_id, data_handle);
69-
OLP_SDK_LOG_TRACE_F(kLogTag, "Clear '%s'", key.c_str());
70-
cache_->RemoveKeysWithPrefix(key);
69+
OLP_SDK_LOG_INFO_F(kLogTag, "Clear '%s'", key.c_str());
70+
return cache_->RemoveKeysWithPrefix(key);
7171
}
7272

7373
} // namespace repository

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class DataCacheRepository final {
4646
boost::optional<model::Data> Get(const std::string& layer_id,
4747
const std::string& data_handle);
4848

49-
void Clear(const std::string& layer_id, const std::string& data_handle);
49+
bool Clear(const std::string& layer_id, const std::string& data_handle);
5050

5151
private:
5252
client::HRN hrn_;

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,25 @@ void PartitionsCacheRepository::ClearPartitions(
206206
}
207207
}
208208

209+
bool PartitionsCacheRepository::ClearPartitionMetadata(
210+
int64_t catalog_version, const std::string& partition_id,
211+
const std::string& layer_id,
212+
boost::optional<model::Partition>& out_partition) {
213+
std::string hrn(hrn_.ToCatalogHRNString());
214+
auto key = CreateKey(hrn, layer_id, partition_id, catalog_version);
215+
OLP_SDK_LOG_INFO_F(kLogTag, "ClearPartitionMetadata '%s'", key.c_str());
216+
auto cached_partition =
217+
cache_->Get(key, [](const std::string& serialized_object) {
218+
return parser::parse<model::Partition>(serialized_object);
219+
});
220+
if (cached_partition.empty()) {
221+
return true;
222+
}
223+
224+
out_partition = boost::any_cast<model::Partition>(cached_partition);
225+
return cache_->RemoveKeysWithPrefix(key);
226+
}
227+
209228
PORTING_POP_WARNINGS()
210229
} // namespace repository
211230
} // namespace read

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ class PartitionsCacheRepository final {
6363
const std::vector<std::string>& partitionIds,
6464
const std::string& layer_id);
6565

66+
bool ClearPartitionMetadata(int64_t catalog_version,
67+
const std::string& partition_id,
68+
const std::string& layer_id,
69+
boost::optional<model::Partition>& out_partition);
70+
6671
private:
6772
client::HRN hrn_;
6873
std::shared_ptr<cache::KeyValueCache> cache_;

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#include <mocks/CacheMock.h>
2222
#include <mocks/NetworkMock.h>
2323

24+
#include <olp/core/cache/CacheSettings.h>
25+
#include <olp/core/cache/KeyValueCache.h>
26+
#include <olp/core/client/OlpClientSettingsFactory.h>
2427
#include <olp/dataservice/read/VersionedLayerClient.h>
2528

2629
namespace {
@@ -33,6 +36,7 @@ const std::string kCatalog =
3336
const std::string kLayerId = "testlayer";
3437
const auto kHrn = olp::client::HRN::FromString(kCatalog);
3538
const auto kPartitionId = "269";
39+
const auto kCatalogVersion = 108;
3640
const auto kTimeout = std::chrono::seconds(5);
3741

3842
constexpr auto kBlobDataHandle = R"(4eed6ed1-0d32-43b9-ae79-043cb4256432)";
@@ -72,4 +76,66 @@ TEST(VersionedLayerClientTest, GetData) {
7276
}
7377
}
7478

79+
TEST(VersionedLayerClientTest, RemoveFromCache) {
80+
olp::client::OlpClientSettings settings;
81+
std::shared_ptr<CacheMock> cache_mock = std::make_shared<CacheMock>();
82+
settings.cache = cache_mock;
83+
84+
// successfull mock cache calls
85+
auto found_cache_response = [](const std::string& key,
86+
const olp::cache::Decoder& encoder) {
87+
auto partition = model::Partition();
88+
partition.SetPartition(kPartitionId);
89+
partition.SetDataHandle(kBlobDataHandle);
90+
return partition;
91+
};
92+
auto partition_cache_remove = [&](const std::string& prefix) {
93+
std::string expected_prefix =
94+
kHrn.ToCatalogHRNString() + "::" + kLayerId + "::" + kPartitionId +
95+
"::" + std::to_string(kCatalogVersion) + "::partition";
96+
EXPECT_EQ(prefix, expected_prefix);
97+
return true;
98+
};
99+
auto data_cache_remove = [&](const std::string& prefix) {
100+
std::string expected_prefix = kHrn.ToCatalogHRNString() + "::" + kLayerId +
101+
"::" + kBlobDataHandle + "::Data";
102+
EXPECT_EQ(prefix, expected_prefix);
103+
return true;
104+
};
105+
106+
VersionedLayerClient client(kHrn, kLayerId, kCatalogVersion, settings);
107+
{
108+
SCOPED_TRACE("Successfull remove partition from cache");
109+
110+
EXPECT_CALL(*cache_mock, Get(_, _)).WillOnce(found_cache_response);
111+
EXPECT_CALL(*cache_mock, RemoveKeysWithPrefix(_))
112+
.WillOnce(partition_cache_remove)
113+
.WillOnce(data_cache_remove);
114+
ASSERT_TRUE(client.RemoveFromCache(kPartitionId));
115+
}
116+
{
117+
SCOPED_TRACE("Remove not existing partition from cache");
118+
EXPECT_CALL(*cache_mock, Get(_, _))
119+
.WillOnce([](const std::string&, const olp::cache::Decoder&) {
120+
return boost::any();
121+
});
122+
ASSERT_TRUE(client.RemoveFromCache(kPartitionId));
123+
}
124+
{
125+
SCOPED_TRACE("Partition cache failure");
126+
EXPECT_CALL(*cache_mock, Get(_, _)).WillOnce(found_cache_response);
127+
EXPECT_CALL(*cache_mock, RemoveKeysWithPrefix(_))
128+
.WillOnce([](const std::string&) { return false; });
129+
ASSERT_FALSE(client.RemoveFromCache(kPartitionId));
130+
}
131+
{
132+
SCOPED_TRACE("Data cache failure");
133+
EXPECT_CALL(*cache_mock, Get(_, _)).WillOnce(found_cache_response);
134+
EXPECT_CALL(*cache_mock, RemoveKeysWithPrefix(_))
135+
.WillOnce(partition_cache_remove)
136+
.WillOnce([](const std::string&) { return false; });
137+
ASSERT_FALSE(client.RemoveFromCache(kPartitionId));
138+
}
139+
}
140+
75141
} // namespace

0 commit comments

Comments
 (0)