Skip to content

Commit d21cf38

Browse files
authored
Add cache compaction to protected cache example. (#1210)
If protected cache contains level 0 files leveldb will consume to much RAM while trying to compact in read env. To prevent such situations level 0 files treatened as error. Thus, protected cache example must be updated. Relates-To: OLPSUP-14367 Signed-off-by: Kostiantyn Zvieriev <[email protected]>
1 parent 795f596 commit d21cf38

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

examples/ProtectedCacheExample.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#include <olp/authentication/TokenProvider.h>
2222
#include <olp/core/cache/CacheSettings.h>
23-
#include <olp/core/cache/KeyValueCache.h>
23+
#include <olp/core/cache/DefaultCache.h>
2424
#include <olp/core/client/HRN.h>
2525
#include <olp/core/client/OlpClientSettingsFactory.h>
2626
#include <olp/core/logging/Log.h>
@@ -95,13 +95,16 @@ int RunExampleReadWithCache(const AccessKey& access_key,
9595
auth_settings.provider =
9696
olp::authentication::TokenProviderDefault(std::move(settings));
9797

98+
// Create and initialize cache
99+
auto cache = std::make_shared<olp::cache::DefaultCache>(cache_settings);
100+
cache->Open();
101+
98102
// Setup OlpClientSettings and provide it to the CatalogClient.
99103
olp::client::OlpClientSettings client_settings;
100104
client_settings.authentication_settings = auth_settings;
101105
client_settings.task_scheduler = std::move(task_scheduler);
102106
client_settings.network_request_handler = std::move(http_client);
103-
client_settings.cache =
104-
olp::client::OlpClientSettingsFactory::CreateDefaultCache(cache_settings);
107+
client_settings.cache = cache;
105108

106109
// Create appropriate layer client with HRN, layer name and settings.
107110
olp::dataservice::read::VersionedLayerClient layer_client(
@@ -112,8 +115,9 @@ int RunExampleReadWithCache(const AccessKey& access_key,
112115
auto request = olp::dataservice::read::DataRequest()
113116
.WithPartitionId(first_partition_id)
114117
.WithBillingTag(boost::none);
115-
if (cache_settings.disk_path_protected.is_initialized())
118+
if (cache_settings.disk_path_protected.is_initialized()) {
116119
request.WithFetchOption(olp::dataservice::read::FetchOptions::CacheOnly);
120+
}
117121

118122
// Run the DataRequest
119123
auto future = layer_client.GetData(request);
@@ -122,6 +126,9 @@ int RunExampleReadWithCache(const AccessKey& access_key,
122126
olp::dataservice::read::DataResponse data_response =
123127
future.GetFuture().get();
124128

129+
// Compact mutable cache, so it can be used as protected cache
130+
cache->Compact();
131+
125132
// Retrieve data from the response
126133
return (HandleDataResponse(data_response) ? 0 : -1);
127134
}

0 commit comments

Comments
 (0)