Skip to content

Commit 2460ec1

Browse files
authored
Make DiskCache create db if folder is an empty. (#1103)
Now db and folder creation is independent for protected cache. Also added a simple test for the case. Resolves: OLPSUP-12312 Signed-off-by: Kostiantyn Zvieriev <[email protected]>
1 parent 785876c commit 2460ec1

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

olp-cpp-sdk-core/src/cache/DiskCache.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,10 @@ OpenResult DiskCache::Open(const std::string& data_path,
165165
StorageSettings settings, OpenOptions options) {
166166
disk_cache_path_ = data_path;
167167
bool is_read_only = (options & ReadOnly) == ReadOnly;
168-
bool is_created = false;
169168
if (!olp::utils::Dir::exists(disk_cache_path_)) {
170169
if (!olp::utils::Dir::create(disk_cache_path_)) {
171170
return OpenResult::Fail;
172171
}
173-
174-
is_created = true;
175172
}
176173

177174
max_size_ = settings.max_disk_storage;
@@ -191,13 +188,6 @@ OpenResult DiskCache::Open(const std::string& data_path,
191188
open_options.env = environment_.get();
192189
}
193190
} else {
194-
if (is_created) {
195-
auto status = InitializeDB(settings, versioned_data_path);
196-
if (!status.ok()) {
197-
return OpenResult::Fail;
198-
}
199-
}
200-
201191
environment_ = std::make_unique<ReadOnlyEnv>(leveldb::Env::Default());
202192
open_options.env = environment_.get();
203193
}
@@ -207,11 +197,21 @@ OpenResult DiskCache::Open(const std::string& data_path,
207197

208198
// First attempt in opening the db
209199
auto status = leveldb::DB::Open(open_options, versioned_data_path, &db);
210-
OLP_SDK_LOG_WARNING(
211-
kLogTag, "Open: failed, attempting repair, error=" << status.ToString());
212-
if (!status.ok() && !is_read_only)
200+
201+
if (!status.ok() && !is_read_only) {
213202
OLP_SDK_LOG_WARNING(kLogTag, "Open: failed, attempting repair, error="
214203
<< status.ToString());
204+
}
205+
206+
if (status.IsInvalidArgument() && is_read_only) {
207+
// Maybe folder with cache is an empty, so trying to create db and reopen it
208+
status = InitializeDB(settings, versioned_data_path);
209+
if (!status.ok()) {
210+
return OpenResult::Fail;
211+
}
212+
213+
status = leveldb::DB::Open(open_options, versioned_data_path, &db);
214+
}
215215

216216
// If the database is r/w and corrupted, attempt to repair & reopen
217217
if ((status.IsCorruption() || status.IsIOError()) &&

olp-cpp-sdk-core/tests/cache/DefaultCacheTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,21 @@ TEST(DefaultCacheTest, ProtectedCacheTest) {
485485
ASSERT_EQ(olp::cache::DefaultCache::Success, cache.Open());
486486
ASSERT_TRUE(olp::utils::Dir::Exists(protected_path));
487487
}
488+
489+
{
490+
SCOPED_TRACE("Open empty folder");
491+
492+
// create an empty folder without db
493+
olp::utils::Dir::Remove(protected_path);
494+
olp::utils::Dir::Create(protected_path);
495+
496+
olp::cache::CacheSettings settings;
497+
settings.disk_path_protected = protected_path;
498+
499+
olp::cache::DefaultCache cache(settings);
500+
ASSERT_EQ(olp::cache::DefaultCache::Success, cache.Open());
501+
ASSERT_TRUE(olp::utils::Dir::Exists(protected_path));
502+
}
488503
}
489504

490505
TEST(DefaultCacheTest, AlreadyInUsePath) {

0 commit comments

Comments
 (0)