Skip to content

Commit 63c69d5

Browse files
authored
RemoveFromCache by TileKey for VersionedLayerClient (#727)
Overloading RemoveFromCache() method with TileKey input. Its reusing previous method implementation, so added integration and unit tests are similar. Resolves: OLPEDGE-1567 Signed-off-by: Kostiantyn Zvieriev <[email protected]>
1 parent 6738052 commit 63c69d5

File tree

6 files changed

+145
-2
lines changed

6 files changed

+145
-2
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
@@ -331,6 +331,15 @@ class DATASERVICE_READ_API VersionedLayerClient final {
331331
*/
332332
bool RemoveFromCache(const std::string& partition_id);
333333

334+
/**
335+
* @brief Removes the tile from the mutable disk cache.
336+
*
337+
* @param tile The tile key that should be removed.
338+
*
339+
* @return True if tile data is removed successfully; false otherwise.
340+
*/
341+
bool RemoveFromCache(const geo::TileKey& tile);
342+
334343
private:
335344
std::unique_ptr<VersionedLayerClientImpl> impl_;
336345
};

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ bool VersionedLayerClient::RemoveFromCache(const std::string& partition_id) {
9898
return impl_->RemoveFromCache(partition_id);
9999
}
100100

101+
bool VersionedLayerClient::RemoveFromCache(const geo::TileKey& tile) {
102+
return impl_->RemoveFromCache(tile);
103+
}
104+
105+
101106
} // namespace read
102107
} // namespace dataservice
103108
} // namespace olp

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,11 @@ bool VersionedLayerClientImpl::RemoveFromCache(
498498
return data_repository.Clear(layer_id_, partition.get().GetDataHandle());
499499
}
500500

501+
bool VersionedLayerClientImpl::RemoveFromCache(const geo::TileKey& tile) {
502+
auto partition_id = tile.ToHereTile();
503+
return RemoveFromCache(partition_id);
504+
}
505+
501506
PORTING_POP_WARNINGS()
502507
} // namespace read
503508
} // namespace dataservice

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ class VersionedLayerClientImpl {
8383

8484
virtual bool RemoveFromCache(const std::string& partition_id);
8585

86+
virtual bool RemoveFromCache(const geo::TileKey& tile);
87+
8688
private:
8789
CatalogVersionResponse GetVersion(boost::optional<std::string> billing_tag,
8890
const FetchOptions& fetch_options,

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

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ TEST(VersionedLayerClientTest, GetData) {
7676
}
7777
}
7878

79-
TEST(VersionedLayerClientTest, RemoveFromCache) {
79+
TEST(VersionedLayerClientTest, RemoveFromCachePartition) {
8080
olp::client::OlpClientSettings settings;
8181
std::shared_ptr<CacheMock> cache_mock = std::make_shared<CacheMock>();
8282
settings.cache = cache_mock;
@@ -138,4 +138,67 @@ TEST(VersionedLayerClientTest, RemoveFromCache) {
138138
}
139139
}
140140

141+
TEST(VersionedLayerClientTest, RemoveFromCacheTileKey) {
142+
olp::client::OlpClientSettings settings;
143+
std::shared_ptr<CacheMock> cache_mock = std::make_shared<CacheMock>();
144+
settings.cache = cache_mock;
145+
146+
// successfull mock cache calls
147+
auto found_cache_response = [](const std::string& key,
148+
const olp::cache::Decoder& encoder) {
149+
auto partition = model::Partition();
150+
partition.SetPartition(kPartitionId);
151+
partition.SetDataHandle(kBlobDataHandle);
152+
return partition;
153+
};
154+
auto partition_cache_remove = [&](const std::string& prefix) {
155+
std::string expected_prefix =
156+
kHrn.ToCatalogHRNString() + "::" + kLayerId + "::" + kPartitionId +
157+
"::" + std::to_string(kCatalogVersion) + "::partition";
158+
EXPECT_EQ(prefix, expected_prefix);
159+
return true;
160+
};
161+
auto data_cache_remove = [&](const std::string& prefix) {
162+
std::string expected_prefix = kHrn.ToCatalogHRNString() + "::" + kLayerId +
163+
"::" + kBlobDataHandle + "::Data";
164+
EXPECT_EQ(prefix, expected_prefix);
165+
return true;
166+
};
167+
168+
auto tile_key = olp::geo::TileKey::FromHereTile(kPartitionId);
169+
VersionedLayerClient client(kHrn, kLayerId, kCatalogVersion, settings);
170+
{
171+
SCOPED_TRACE("Successfull remove tile from cache");
172+
173+
EXPECT_CALL(*cache_mock, Get(_, _)).WillOnce(found_cache_response);
174+
EXPECT_CALL(*cache_mock, RemoveKeysWithPrefix(_))
175+
.WillOnce(partition_cache_remove)
176+
.WillOnce(data_cache_remove);
177+
ASSERT_TRUE(client.RemoveFromCache(tile_key));
178+
}
179+
{
180+
SCOPED_TRACE("Remove not existing tile from cache");
181+
EXPECT_CALL(*cache_mock, Get(_, _))
182+
.WillOnce([](const std::string&, const olp::cache::Decoder&) {
183+
return boost::any();
184+
});
185+
ASSERT_TRUE(client.RemoveFromCache(tile_key));
186+
}
187+
{
188+
SCOPED_TRACE("Partition cache failure");
189+
EXPECT_CALL(*cache_mock, Get(_, _)).WillOnce(found_cache_response);
190+
EXPECT_CALL(*cache_mock, RemoveKeysWithPrefix(_))
191+
.WillOnce([](const std::string&) { return false; });
192+
ASSERT_FALSE(client.RemoveFromCache(tile_key));
193+
}
194+
{
195+
SCOPED_TRACE("Data cache failure");
196+
EXPECT_CALL(*cache_mock, Get(_, _)).WillOnce(found_cache_response);
197+
EXPECT_CALL(*cache_mock, RemoveKeysWithPrefix(_))
198+
.WillOnce(partition_cache_remove)
199+
.WillOnce([](const std::string&) { return false; });
200+
ASSERT_FALSE(client.RemoveFromCache(tile_key));
201+
}
202+
}
203+
141204
} // namespace

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

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2795,7 +2795,7 @@ TEST_F(DataserviceReadVersionedLayerClientTest, GetTileTwoSequentialCalls) {
27952795
}
27962796
}
27972797

2798-
TEST_F(DataserviceReadVersionedLayerClientTest, RemoveFromCache) {
2798+
TEST_F(DataserviceReadVersionedLayerClientTest, RemoveFromCachePartition) {
27992799
EXPECT_CALL(*network_mock_, Send(_, _, _, _, _))
28002800
.WillOnce(ReturnHttpResponse(olp::http::NetworkResponse().WithStatus(
28012801
olp::http::HttpStatusCode::OK),
@@ -2853,4 +2853,63 @@ TEST_F(DataserviceReadVersionedLayerClientTest, RemoveFromCache) {
28532853
ASSERT_FALSE(response.IsSuccessful());
28542854
}
28552855

2856+
TEST_F(DataserviceReadVersionedLayerClientTest, RemoveFromCacheTileKey) {
2857+
EXPECT_CALL(*network_mock_, Send(_, _, _, _, _))
2858+
.WillOnce(ReturnHttpResponse(olp::http::NetworkResponse().WithStatus(
2859+
olp::http::HttpStatusCode::OK),
2860+
kHttpResponseLookupQuery))
2861+
.WillOnce(ReturnHttpResponse(olp::http::NetworkResponse().WithStatus(
2862+
olp::http::HttpStatusCode::OK),
2863+
kHttpResponsePartition_269))
2864+
.WillOnce(ReturnHttpResponse(olp::http::NetworkResponse().WithStatus(
2865+
olp::http::HttpStatusCode::OK),
2866+
kHttpResponseLookupBlob))
2867+
.WillOnce(ReturnHttpResponse(olp::http::NetworkResponse().WithStatus(
2868+
olp::http::HttpStatusCode::OK),
2869+
kHttpResponseBlobData_269));
2870+
2871+
auto catalog = olp::client::HRN::FromString(
2872+
GetArgument("dataservice_read_test_catalog"));
2873+
auto layer = GetArgument("dataservice_read_test_layer");
2874+
auto version = std::stoi(GetArgument("dataservice_read_test_layer_version"));
2875+
2876+
auto client = std::make_unique<olp::dataservice::read::VersionedLayerClient>(
2877+
catalog, layer, version, *settings_);
2878+
ASSERT_TRUE(client);
2879+
2880+
// load and cache some data
2881+
auto promise = std::make_shared<std::promise<DataResponse>>();
2882+
auto future = promise->get_future();
2883+
auto partition = GetArgument("dataservice_read_test_partition");
2884+
auto data_request =
2885+
olp::dataservice::read::DataRequest().WithPartitionId(partition);
2886+
auto token = client->GetData(data_request, [promise](DataResponse response) {
2887+
promise->set_value(response);
2888+
});
2889+
2890+
ASSERT_NE(future.wait_for(kWaitTimeout), std::future_status::timeout);
2891+
DataResponse response = future.get();
2892+
2893+
ASSERT_TRUE(response.IsSuccessful()) << response.GetError().GetMessage();
2894+
ASSERT_NE(response.GetResult(), nullptr);
2895+
ASSERT_NE(response.GetResult()->size(), 0u);
2896+
2897+
// remove the data from cache
2898+
auto tile_key = olp::geo::TileKey::FromHereTile(partition);
2899+
ASSERT_TRUE(client->RemoveFromCache(tile_key));
2900+
2901+
// check the data is not available in cache
2902+
promise = std::make_shared<std::promise<DataResponse>>();
2903+
future = promise->get_future();
2904+
data_request.WithFetchOption(CacheOnly);
2905+
token = client->GetData(data_request, [promise](DataResponse response) {
2906+
promise->set_value(response);
2907+
});
2908+
2909+
ASSERT_NE(future.wait_for(kWaitTimeout), std::future_status::timeout);
2910+
response = future.get();
2911+
2912+
ASSERT_FALSE(response.IsSuccessful());
2913+
}
2914+
28562915
} // namespace

0 commit comments

Comments
 (0)