Skip to content

Commit 58af2d1

Browse files
author
Liubov Didkivska
authored
SDK should not cache data with FetchOption::OnlineOnly requests. (#705)
* SDK should not cache data with FetchOption::OnlineOnly requests. Add Check if fetch_option is not OnlineOnly, and cache only in that case. Update tests. Add CacheWithUpdate option. Resolves:OLPEDGE-1659 Signed-off-by: Liubov Didkivska <[email protected]>
1 parent 1ade147 commit 58af2d1

File tree

9 files changed

+40
-31
lines changed

9 files changed

+40
-31
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ ApiClientLookup::ApiClientResponse ApiClientLookup::LookupApi(
5757
client::OlpClientSettings settings) {
5858
repository::ApiCacheRepository repository(catalog, settings.cache);
5959

60-
if (options != OnlineOnly) {
60+
if (options != OnlineOnly && options != CacheWithUpdate) {
6161
auto url = repository.Get(service, service_version);
6262
if (url) {
6363
OLP_SDK_LOG_INFO_F(kLogTag, "LookupApi(%s, %s) -> from cache",
@@ -107,7 +107,9 @@ ApiClientLookup::ApiClientResponse ApiClientLookup::LookupApi(
107107
}
108108

109109
const auto& service_url = api_result.at(0).GetBaseUrl();
110-
repository.Put(service, service_version, service_url);
110+
if (options != OnlineOnly && options != CacheWithUpdate) {
111+
repository.Put(service, service_version, service_url);
112+
}
111113
OLP_SDK_LOG_INFO_F(kLogTag, "LookupApi(%s/%s): %s - OK, service_url=%s",
112114
service.c_str(), service_version.c_str(),
113115
catalog.partition.c_str(), service_url.c_str());

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ inline client::CancellationToken ScheduleFetch(TaskScheduler&& schedule_task,
4949
schedule_task(request.WithFetchOption(FetchOptions::CacheOnly),
5050
std::forward<Callback>(callback));
5151
auto online_token = schedule_task(
52-
std::move(request.WithFetchOption(FetchOptions::OnlineOnly)), nullptr);
52+
std::move(request.WithFetchOption(FetchOptions::CacheWithUpdate)),
53+
nullptr);
5354

5455
return client::CancellationToken([=]() {
5556
cache_token.Cancel();
@@ -92,4 +93,4 @@ inline client::CancellationToken AddTask(
9293

9394
} // namespace read
9495
} // namespace dataservice
95-
} // namespace olp
96+
} // namespace olp

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ CatalogResponse CatalogRepository::GetCatalog(
5454

5555
repository::CatalogCacheRepository repository{catalog, settings.cache};
5656

57-
if (fetch_options != OnlineOnly) {
57+
if (fetch_options != OnlineOnly && fetch_options != CacheWithUpdate) {
5858
auto cached = repository.Get();
5959
if (cached) {
6060
OLP_SDK_LOG_INFO_F(kLogTag, "cache catalog '%s' found!",
@@ -79,9 +79,10 @@ CatalogResponse CatalogRepository::GetCatalog(
7979
auto catalog_response =
8080
ConfigApi::GetCatalog(client, catalog.ToCatalogHRNString(),
8181
request.GetBillingTag(), cancellation_context);
82-
if (catalog_response.IsSuccessful()) {
82+
if (catalog_response.IsSuccessful() && fetch_options != OnlineOnly) {
8383
repository.Put(catalog_response.GetResult());
84-
} else {
84+
}
85+
if (!catalog_response.IsSuccessful()) {
8586
const auto& error = catalog_response.GetError();
8687
if (error.GetHttpStatusCode() == http::HttpStatusCode::FORBIDDEN) {
8788
repository.Clear();
@@ -99,7 +100,7 @@ CatalogVersionResponse CatalogRepository::GetLatestVersion(
99100
repository::CatalogCacheRepository repository(catalog, settings.cache);
100101

101102
auto fetch_option = request.GetFetchOption();
102-
if (fetch_option != OnlineOnly) {
103+
if (fetch_option != OnlineOnly && fetch_option != CacheWithUpdate) {
103104
auto cached_version = repository.GetVersion();
104105
if (cached_version) {
105106
OLP_SDK_LOG_INFO_F(kLogTag, "cache catalog '%s' found!",
@@ -127,9 +128,10 @@ CatalogVersionResponse CatalogRepository::GetLatestVersion(
127128
auto version_response = MetadataApi::GetLatestCatalogVersion(
128129
client, -1, request.GetBillingTag(), cancellation_context);
129130

130-
if (version_response.IsSuccessful()) {
131+
if (version_response.IsSuccessful() && fetch_option != OnlineOnly) {
131132
repository.PutVersion(version_response.GetResult());
132-
} else {
133+
}
134+
if (!version_response.IsSuccessful()) {
133135
const auto& error = version_response.GetError();
134136
if (error.GetHttpStatusCode() == http::HttpStatusCode::FORBIDDEN) {
135137
repository.Clear();

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ DataResponse DataRepository::GetBlobData(
141141

142142
repository::DataCacheRepository repository(catalog, settings.cache);
143143

144-
if (fetch_option != OnlineOnly) {
144+
if (fetch_option != OnlineOnly && fetch_option != CacheWithUpdate) {
145145
auto cached_data = repository.Get(layer, data_handle.value());
146146
if (cached_data) {
147147
OLP_SDK_LOG_INFO_F(kLogTag, "cache data '%s' found!",
@@ -174,9 +174,11 @@ DataResponse DataRepository::GetBlobData(
174174
data_request.GetBillingTag(), cancellation_context);
175175
}
176176

177-
if (blob_response.IsSuccessful()) {
177+
if (blob_response.IsSuccessful() && fetch_option != OnlineOnly) {
178178
repository.Put(blob_response.GetResult(), layer, data_handle.value());
179-
} else {
179+
}
180+
181+
if (!blob_response.IsSuccessful()) {
180182
const auto& error = blob_response.GetError();
181183
if (error.GetHttpStatusCode() == http::HttpStatusCode::FORBIDDEN) {
182184
OLP_SDK_LOG_INFO_F(kLogTag, "clear '%s' cache",

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ PartitionsResponse PartitionsRepository::GetPartitions(
124124

125125
repository::PartitionsCacheRepository repository(catalog, settings.cache);
126126

127-
if (fetch_option != OnlineOnly) {
127+
if (fetch_option != OnlineOnly && fetch_option != CacheWithUpdate) {
128128
auto cached_partitions = repository.Get(request, layer);
129129
if (cached_partitions) {
130130
OLP_SDK_LOG_INFO_F(kLogTag, "cache data '%s' found!",
@@ -173,12 +173,13 @@ PartitionsResponse PartitionsRepository::GetPartitions(
173173
// Save all partitions only when downloaded via metadata API
174174
const bool is_layer_metadata = partition_ids.empty();
175175

176-
if (response.IsSuccessful()) {
176+
if (response.IsSuccessful() && fetch_option != OnlineOnly) {
177177
OLP_SDK_LOG_INFO_F(kLogTag, "put '%s' to cache",
178178
request.CreateKey(layer).c_str());
179179
repository.Put(request, response.GetResult(), layer, expiry,
180180
is_layer_metadata);
181-
} else {
181+
}
182+
if (!response.IsSuccessful()) {
182183
const auto& error = response.GetError();
183184
if (error.GetHttpStatusCode() == http::HttpStatusCode::FORBIDDEN) {
184185
OLP_SDK_LOG_INFO_F(kLogTag, "clear '%s' cache",
@@ -211,7 +212,7 @@ PartitionsResponse PartitionsRepository::GetPartitionById(
211212

212213
auto fetch_option = data_request.GetFetchOption();
213214

214-
if (fetch_option != OnlineOnly) {
215+
if (fetch_option != OnlineOnly && fetch_option != CacheWithUpdate) {
215216
auto cached_partitions =
216217
repository.Get(partition_request, partitions, layer);
217218
if (cached_partitions.GetPartitions().size() == partitions.size()) {
@@ -240,12 +241,13 @@ PartitionsResponse PartitionsRepository::GetPartitionById(
240241
client, layer, partitions, version, {}, data_request.GetBillingTag(),
241242
cancellation_context);
242243

243-
if (query_response.IsSuccessful()) {
244+
if (query_response.IsSuccessful() && fetch_option != OnlineOnly) {
244245
OLP_SDK_LOG_INFO_F(kLogTag, "put '%s' to cache",
245246
data_request.CreateKey(layer).c_str());
246247
repository.Put(partition_request, query_response.GetResult(), layer,
247248
boost::none /* TODO: expiration */);
248-
} else {
249+
}
250+
if (!query_response.IsSuccessful()) {
249251
const auto& error = query_response.GetError();
250252
if (error.GetHttpStatusCode() == http::HttpStatusCode::FORBIDDEN) {
251253
OLP_SDK_LOG_INFO_F(kLogTag, "clear '%s' cache",
@@ -353,7 +355,7 @@ PartitionsResponse PartitionsRepository::QueryPartitionForVersionedTile(
353355
}
354356
}
355357

356-
if (fetch_option != OnlineOnly) {
358+
if (fetch_option != OnlineOnly && fetch_option != CacheWithUpdate) {
357359
// add partitions to cache
358360
repository::PartitionsCacheRepository repository(catalog, settings.cache);
359361
repository.Put(PartitionsRequest().WithVersion(version), partitions,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ TEST(ApiClientLookupTest, LookupApi) {
9898
olp::http::HttpStatusCode::OK),
9999
OLP_SDK_HTTP_RESPONSE_LOOKUP_CONFIG));
100100
EXPECT_CALL(*cache, Put(cache_key, _, _, _))
101-
.Times(1)
101+
.Times(0)
102102
.WillOnce(Return(true));
103103

104104
client::CancellationContext context;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ TEST_F(CatalogRepositoryTest, GetLatestVersionOnlineOnlyNotFound) {
170170
EXPECT_FALSE(response.IsSuccessful());
171171
}
172172

173-
TEST_F(CatalogRepositoryTest, GetLatestVersionOnlineOnlyFoundAndCacheWritten) {
173+
TEST_F(CatalogRepositoryTest, GetLatestVersionOnlineOnlyFound) {
174174
olp::client::CancellationContext context;
175175

176176
auto request = CatalogVersionRequest();
@@ -183,9 +183,9 @@ TEST_F(CatalogRepositoryTest, GetLatestVersionOnlineOnlyFoundAndCacheWritten) {
183183
return boost::any{};
184184
});
185185

186-
EXPECT_CALL(*cache_, Put(Eq(kLatestVersionCacheKey), _, _, _)).Times(1);
186+
EXPECT_CALL(*cache_, Put(Eq(kLatestVersionCacheKey), _, _, _)).Times(0);
187187

188-
EXPECT_CALL(*cache_, Put(Eq(kMetadataCacheKey), _, _, _)).Times(1);
188+
EXPECT_CALL(*cache_, Put(Eq(kMetadataCacheKey), _, _, _)).Times(0);
189189

190190
EXPECT_CALL(*network_,
191191
Send(IsGetRequest(OLP_SDK_URL_LOOKUP_METADATA), _, _, _, _))
@@ -339,7 +339,7 @@ TEST_F(CatalogRepositoryTest, GetLatestVersionTimeouted) {
339339
response.GetError().GetErrorCode());
340340
}
341341

342-
TEST_F(CatalogRepositoryTest, GetCatalogOnlineOnlyFoundAndCacheWritten) {
342+
TEST_F(CatalogRepositoryTest, GetCatalogOnlineOnlyFound) {
343343
olp::client::CancellationContext context;
344344

345345
auto request = CatalogRequest();
@@ -351,9 +351,9 @@ TEST_F(CatalogRepositoryTest, GetCatalogOnlineOnlyFoundAndCacheWritten) {
351351
return boost::any{};
352352
});
353353

354-
EXPECT_CALL(*cache_, Put(Eq(kCatalogCacheKey), _, _, _)).Times(1);
354+
EXPECT_CALL(*cache_, Put(Eq(kCatalogCacheKey), _, _, _)).Times(0);
355355

356-
EXPECT_CALL(*cache_, Put(Eq(kConfigCacheKey), _, _, _)).Times(1);
356+
EXPECT_CALL(*cache_, Put(Eq(kConfigCacheKey), _, _, _)).Times(0);
357357

358358
ON_CALL(*network_, Send(IsGetRequest(OLP_SDK_URL_LOOKUP_CONFIG), _, _, _, _))
359359
.WillByDefault(ReturnHttpResponse(olp::http::NetworkResponse().WithStatus(

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ TEST_F(PartitionsRepositoryTest, GetPartitionById) {
171171
olp::http::HttpStatusCode::OK),
172172
kOlpSdkHttpResponseLookupQuery));
173173

174-
EXPECT_CALL(*cache, Put(Eq(kCacheKeyMetadata), _, _, _)).Times(1);
174+
EXPECT_CALL(*cache, Put(Eq(kCacheKeyMetadata), _, _, _)).Times(0);
175175
};
176176

177177
{
@@ -246,7 +246,7 @@ TEST_F(PartitionsRepositoryTest, GetPartitionById) {
246246
olp::http::HttpStatusCode::OK),
247247
kOlpSdkHttpResponsePartitionById));
248248

249-
EXPECT_CALL(*cache, Put(Eq(cache_key), _, _, _)).Times(1);
249+
EXPECT_CALL(*cache, Put(Eq(cache_key), _, _, _)).Times(0);
250250

251251
auto response = repository::PartitionsRepository::GetPartitionById(
252252
catalog_hrn, kVersionedLayerId, context,
@@ -274,7 +274,7 @@ TEST_F(PartitionsRepositoryTest, GetPartitionById) {
274274
.WillOnce(ReturnHttpResponse(olp::http::NetworkResponse().WithStatus(
275275
olp::http::HttpStatusCode::OK),
276276
kOlpSdkHttpResponsePartitionById));
277-
EXPECT_CALL(*cache, Put(Eq(cache_key_no_version), _, _, _)).Times(1);
277+
EXPECT_CALL(*cache, Put(Eq(cache_key_no_version), _, _, _)).Times(0);
278278

279279
auto response = repository::PartitionsRepository::GetPartitionById(
280280
catalog_hrn, kVersionedLayerId, context,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ TEST_F(DataserviceReadVolatileLayerClientTest, GetPartitionsVersionIsIgnored) {
177177
"Online request with version in request. Version should be ignored.");
178178
auto request = olp::dataservice::read::PartitionsRequest();
179179
request.WithVersion(4);
180-
request.WithFetchOption(FetchOptions::OnlineOnly);
180+
request.WithFetchOption(FetchOptions::OnlineIfNotFound);
181181

182182
PartitionsResponse partitions_response;
183183
olp::client::Condition condition;

0 commit comments

Comments
 (0)