Skip to content

Commit

Permalink
#447 Memory Storage Exception Normalization (#1297)
Browse files Browse the repository at this point in the history
Implementation of memory storage specific exceptions normalization. This
PR is a refactor of #584.
  • Loading branch information
muhammadhamzasajjad authored Feb 8, 2024
1 parent b4d417b commit da288af
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
7 changes: 5 additions & 2 deletions cpp/arcticdb/storage/memory/memory_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ namespace arcticdb::storage::memory {
for (auto &kv : group.values()) {
auto it = key_vec.find(kv.variant_key());

util::check_rte(opts.upsert_ || it != key_vec.end(), "update called with upsert=false but key does not exist");
if (!opts.upsert_ && it == key_vec.end()) {
std::string err_message = fmt::format("do_update called with upsert=false on non-existent key(s): {}", kv.variant_key());
throw KeyNotFoundException(std::move(kv.variant_key()), err_message);
}

if(it != key_vec.end()) {
key_vec.erase(it);
Expand Down Expand Up @@ -110,7 +113,7 @@ namespace arcticdb::storage::memory {
ARCTICDB_DEBUG(log::storage(), "Read key {}: {}, with {} bytes of data", variant_key_type(k), variant_key_view(k));
key_vec.erase(it);
} else if (!opts.ignores_missing_key_) {
util::raise_rte("Failed to find segment for key {}",variant_key_view(k));
throw KeyNotFoundException(std::move(k));
}
}
});
Expand Down
7 changes: 7 additions & 0 deletions cpp/arcticdb/storage/storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ class KeyNotFoundException : public ArcticSpecificException<ErrorCode::E_KEY_NOT
keys_(std::make_shared<Composite<VariantKey>>(std::move(keys))) {
}

explicit KeyNotFoundException(const VariantKey& single_key):
KeyNotFoundException(Composite<VariantKey>{VariantKey{single_key}}) {}

explicit KeyNotFoundException(const VariantKey& single_key, std::string err_output):
KeyNotFoundException(Composite<VariantKey>{VariantKey{single_key}}, err_output) {}


Composite<VariantKey>& keys() {
return *keys_;
}
Expand Down
15 changes: 14 additions & 1 deletion cpp/arcticdb/storage/test/test_storage_exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <arcticdb/codec/codec.hpp>
#include <arcticdb/storage/storage.hpp>
#include <arcticdb/storage/lmdb/lmdb_storage.hpp>
#include <arcticdb/storage/memory/memory_storage.hpp>
#include <arcticdb/util/buffer.hpp>

#include <filesystem>
Expand Down Expand Up @@ -61,6 +62,15 @@ class LMDBStorageFactory : public StorageFactory {
}
};

class MemoryStorageFactory : public StorageFactory {
std::unique_ptr<arcticdb::storage::Storage> create() override {
arcticdb::proto::memory_storage::Config cfg;
arcticdb::storage::LibraryPath library_path{"a", "b"};

return std::make_unique<arcticdb::storage::memory::MemoryStorage>(library_path, arcticdb::storage::OpenMode::WRITE, cfg);
}
};

// Generic tests that run with all types of storages

class GenericStorageTest : public ::testing::TestWithParam<std::shared_ptr<StorageFactory>> {
Expand Down Expand Up @@ -134,7 +144,10 @@ TEST_P(GenericStorageTest, RemoveKeyNotFoundException) {
INSTANTIATE_TEST_SUITE_P(
AllStoragesCommonTests,
GenericStorageTest,
::testing::Values(std::make_shared<LMDBStorageFactory>())
::testing::Values(
std::make_shared<LMDBStorageFactory>(),
std::make_shared<MemoryStorageFactory>()
)
);

// LMDB Storage specific tests
Expand Down

0 comments on commit da288af

Please sign in to comment.