From 889761f538eda8feb7ee0ea224c98e6ea05f6246 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Wed, 30 Apr 2025 16:10:56 +0200 Subject: [PATCH] rename fs atomic impl and add simple testcase pre refactor --- src/CMakeLists.txt | 4 +- ...rage.cpp => filesystem_storage_atomic.cpp} | 18 ++--- ...rage.hpp => filesystem_storage_atomic.hpp} | 6 +- src/solanaceae/object_store/object_store.hpp | 2 +- test/CMakeLists.txt | 11 +++ test/test_backend_fs_atomic.cpp | 76 +++++++++++++++++++ 6 files changed, 102 insertions(+), 15 deletions(-) rename src/solanaceae/object_store/backends/{filesystem_storage.cpp => filesystem_storage_atomic.cpp} (97%) rename src/solanaceae/object_store/backends/{filesystem_storage.hpp => filesystem_storage_atomic.hpp} (86%) create mode 100644 test/test_backend_fs_atomic.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d647cc7..f9d4e13 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -40,8 +40,8 @@ target_link_libraries(solanaceae_file2_zstd PUBLIC add_library(solanaceae_object_store_backend_filesystem ./solanaceae/object_store/backends/file2_stack.hpp ./solanaceae/object_store/backends/file2_stack.cpp - ./solanaceae/object_store/backends/filesystem_storage.hpp - ./solanaceae/object_store/backends/filesystem_storage.cpp + ./solanaceae/object_store/backends/filesystem_storage_atomic.hpp + ./solanaceae/object_store/backends/filesystem_storage_atomic.cpp ) target_include_directories(solanaceae_object_store_backend_filesystem PUBLIC .) diff --git a/src/solanaceae/object_store/backends/filesystem_storage.cpp b/src/solanaceae/object_store/backends/filesystem_storage_atomic.cpp similarity index 97% rename from src/solanaceae/object_store/backends/filesystem_storage.cpp rename to src/solanaceae/object_store/backends/filesystem_storage_atomic.cpp index a16c41b..368591c 100644 --- a/src/solanaceae/object_store/backends/filesystem_storage.cpp +++ b/src/solanaceae/object_store/backends/filesystem_storage_atomic.cpp @@ -1,4 +1,4 @@ -#include "./filesystem_storage.hpp" +#include "./filesystem_storage_atomic.hpp" #include #include @@ -29,17 +29,17 @@ static const char* metaFileTypeSuffix(MetaFileType mft) { namespace Backends { -FilesystemStorage::FilesystemStorage( +FilesystemStorageAtomic::FilesystemStorageAtomic( ObjectStore2& os, std::string_view storage_path, MetaFileType mft_new ) : _os(os), _storage_path(storage_path), _mft_new(mft_new) { } -FilesystemStorage::~FilesystemStorage(void) { +FilesystemStorageAtomic::~FilesystemStorageAtomic(void) { } -ObjectHandle FilesystemStorage::newObject(ByteSpan id, bool throw_construct) { +ObjectHandle FilesystemStorageAtomic::newObject(ByteSpan id, bool throw_construct) { { // first check if id is already used (TODO: solve the multi obj/backend problem) auto exising_oh = _os.getOneObjectByID(id); if (static_cast(exising_oh)) { @@ -96,7 +96,7 @@ ObjectHandle FilesystemStorage::newObject(ByteSpan id, bool throw_construct) { return oh; } -bool FilesystemStorage::write(Object o, std::function& data_cb) { +bool FilesystemStorageAtomic::write(Object o, std::function& data_cb) { auto& reg = _os.registry(); if (!reg.valid(o)) { @@ -303,7 +303,7 @@ bool FilesystemStorage::write(Object o, std::function& data_cb) { +bool FilesystemStorageAtomic::read(Object o, std::function& data_cb) { auto& reg = _os.registry(); if (!reg.valid(o)) { @@ -355,11 +355,11 @@ bool FilesystemStorage::read(Object o, std::function +#include + +#include +#include + +#include + +#include +#include + +int main(void) { + const auto temp_dir_str = (std::filesystem::temp_directory_path() / "test_obj_store_tests").u8string(); + + assert(!std::filesystem::exists(temp_dir_str)); + + // test1, create store with 2 objects + { + ObjectStore2 os; + Backends::FilesystemStorageAtomic fsa{os, temp_dir_str}; + + // empty, does nothing + fsa.scanAsync(); + assert(!std::filesystem::exists(temp_dir_str)); + + { // o1 + // id1 + std::vector id{0x00, 0x13, 0x37, 0x01}; + auto o = fsa.newObject(ByteSpan{id}); + std::vector data{0x01, 0x01, 0x11, 0xf1, 0xae, 0x0b, 0x33}; + static_cast(fsa).write(o, ByteSpan{data}); + } + + { // o2 + // id2 + std::vector id{0x00, 0x13, 0x37, 0x02}; + auto o = fsa.newObject(ByteSpan{id}); + std::vector data{0x11, 0x11, 0x11, 0xff, 0xff, 0xee, 0xee}; + static_cast(fsa).write(o, ByteSpan{data}); + } + } + + assert(std::filesystem::exists(temp_dir_str)); + + // test2, load store created in test1 + { + ObjectStore2 os; + + Backends::FilesystemStorageAtomic fsa{os, temp_dir_str}; + + fsa.scanAsync(); + + assert(os.registry().storage().size() == 2); + + { // o1 + std::vector id{0x00, 0x13, 0x37, 0x01}; + auto o = os.getOneObjectByID(ByteSpan{id}); + assert(static_cast(o)); + + std::vector orig_data{0x01, 0x01, 0x11, 0xf1, 0xae, 0x0b, 0x33}; + // TODO: read and test content + + //static_cast(fsa).write(o, ByteSpan{data}); + } + + { // o2 + std::vector id{0x00, 0x13, 0x37, 0x02}; + auto o = os.getOneObjectByID(ByteSpan{id}); + assert(static_cast(o)); + } + } + + std::filesystem::remove_all(temp_dir_str); + + return 0; +}