From 268cbe137e890e2131de488f8a12d0beedba14bc Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sat, 6 Apr 2024 13:21:26 +0200 Subject: [PATCH] move file stack creation to separate file, removing some scope --- src/CMakeLists.txt | 2 + src/fragment_store/file2_stack.cpp | 93 +++++++++++++++++++++++++++ src/fragment_store/file2_stack.hpp | 23 +++++++ src/fragment_store/fragment_store.cpp | 89 +------------------------ 4 files changed, 120 insertions(+), 87 deletions(-) create mode 100644 src/fragment_store/file2_stack.cpp create mode 100644 src/fragment_store/file2_stack.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 28c7938c..4643d2b7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,6 +13,8 @@ add_library(fragment_store ./fragment_store/meta_components.hpp ./fragment_store/meta_components_id.inl ./fragment_store/serializer.hpp + ./fragment_store/file2_stack.hpp + ./fragment_store/file2_stack.cpp ./fragment_store/fragment_store.hpp ./fragment_store/fragment_store.cpp diff --git a/src/fragment_store/file2_stack.cpp b/src/fragment_store/file2_stack.cpp new file mode 100644 index 00000000..70b599a9 --- /dev/null +++ b/src/fragment_store/file2_stack.cpp @@ -0,0 +1,93 @@ +#include "./file2_stack.hpp" + +#include +#include "./file2_zstd.hpp" + +#include + +#include + +// add enc and comp file layers +// assumes a file is already in the stack +bool buildStackRead(std::stack>& file_stack, Encryption encryption, Compression compression) { + assert(!file_stack.empty()); + + // TODO: decrypt here + assert(encryption == Encryption::NONE); + + // add layer based on enum + if (compression == Compression::ZSTD) { + file_stack.push(std::make_unique(*file_stack.top().get())); + } else { + // TODO: make error instead + assert(compression == Compression::NONE); + } + + if (!file_stack.top()->isGood()) { + std::cerr << "FS error: file failed to add " << (int)compression << " decompression layer\n"; + return false; + } + + return true; +} + +// do i need this? +std::stack> buildFileStackRead(std::string_view file_path, Encryption encryption, Compression compression) { + std::stack> file_stack; + file_stack.push(std::make_unique(file_path)); + + if (!file_stack.top()->isGood()) { + std::cerr << "FS error: opening file for reading '" << file_path << "'\n"; + return {}; + } + + if (!buildStackRead(file_stack, encryption, compression)) { + std::cerr << "FS error: file failed to add layers for '" << file_path << "'\n"; + return {}; + } + + return file_stack; +} + +// add enc and comp file layers +// assumes a file is already in the stack +bool buildStackWrite(std::stack>& file_stack, Encryption encryption, Compression compression) { + assert(!file_stack.empty()); + + // TODO: encrypt here + assert(encryption == Encryption::NONE); + + // add layer based on enum + if (compression == Compression::ZSTD) { + file_stack.push(std::make_unique(*file_stack.top().get())); + } else { + // TODO: make error instead + assert(compression == Compression::NONE); + } + + if (!file_stack.top()->isGood()) { + std::cerr << "FS error: file failed to add " << (int)compression << " compression layer\n"; + return false; + } + + return true; +} + +// do i need this? +std::stack> buildFileStackWrite(std::string_view file_path, Encryption encryption, Compression compression) { + std::stack> file_stack; + file_stack.push(std::make_unique(file_path)); + + if (!file_stack.top()->isGood()) { + std::cerr << "FS error: opening file for writing '" << file_path << "'\n"; + return {}; + } + + if (!buildStackWrite(file_stack, encryption, compression)) { + std::cerr << "FS error: file failed to add layers for '" << file_path << "'\n"; + return {}; + } + + return file_stack; +} + diff --git a/src/fragment_store/file2_stack.hpp b/src/fragment_store/file2_stack.hpp new file mode 100644 index 00000000..c1e878ca --- /dev/null +++ b/src/fragment_store/file2_stack.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "./types.hpp" + +#include + +#include +#include +#include + +// add enc and comp file layers +// assumes a file is already in the stack +[[nodiscard]] bool buildStackRead(std::stack>& file_stack, Encryption encryption, Compression compression); + +// do i need this? +[[nodiscard]] std::stack> buildFileStackRead(std::string_view file_path, Encryption encryption, Compression compression); + +// add enc and comp file layers +// assumes a file is already in the stack +[[nodiscard]] bool buildStackWrite(std::stack>& file_stack, Encryption encryption, Compression compression); + +// do i need this? +[[nodiscard]] std::stack> buildFileStackWrite(std::string_view file_path, Encryption encryption, Compression compression); diff --git a/src/fragment_store/fragment_store.cpp b/src/fragment_store/fragment_store.cpp index 963f67a8..d8fe80dc 100644 --- a/src/fragment_store/fragment_store.cpp +++ b/src/fragment_store/fragment_store.cpp @@ -10,7 +10,8 @@ #include #include -#include "./file2_zstd.hpp" + +#include "./file2_stack.hpp" #include @@ -51,92 +52,6 @@ static ByteSpan spanFromRead(const std::variant>& } } -// TODO: these stacks are file specific - -// add enc and comp file layers -// assumes a file is already in the stack -static bool buildStackRead(std::stack>& file_stack, Encryption encryption, Compression compression) { - assert(!file_stack.empty()); - - // TODO: decrypt here - assert(encryption == Encryption::NONE); - - // add layer based on enum - if (compression == Compression::ZSTD) { - file_stack.push(std::make_unique(*file_stack.top().get())); - } else { - // TODO: make error instead - assert(compression == Compression::NONE); - } - - if (!file_stack.top()->isGood()) { - std::cerr << "FS error: file failed to add " << (int)compression << " decompression layer\n"; - return false; - } - - return true; -} - -// do i need this? -static std::stack> buildFileStackRead(std::string_view file_path, Encryption encryption, Compression compression) { - std::stack> file_stack; - file_stack.push(std::make_unique(file_path)); - - if (!file_stack.top()->isGood()) { - std::cerr << "FS error: opening file for reading '" << file_path << "'\n"; - return {}; - } - - if (!buildStackRead(file_stack, encryption, compression)) { - std::cerr << "FS error: file failed to add layers for '" << file_path << "'\n"; - return {}; - } - - return file_stack; -} - -// add enc and comp file layers -// assumes a file is already in the stack -static bool buildStackWrite(std::stack>& file_stack, Encryption encryption, Compression compression) { - assert(!file_stack.empty()); - - // TODO: encrypt here - assert(encryption == Encryption::NONE); - - // add layer based on enum - if (compression == Compression::ZSTD) { - file_stack.push(std::make_unique(*file_stack.top().get())); - } else { - // TODO: make error instead - assert(compression == Compression::NONE); - } - - if (!file_stack.top()->isGood()) { - std::cerr << "FS error: file failed to add " << (int)compression << " compression layer\n"; - return false; - } - - return true; -} - -// do i need this? -static std::stack> buildFileStackWrite(std::string_view file_path, Encryption encryption, Compression compression) { - std::stack> file_stack; - file_stack.push(std::make_unique(file_path)); - - if (!file_stack.top()->isGood()) { - std::cerr << "FS error: opening file for writing '" << file_path << "'\n"; - return {}; - } - - if (!buildStackWrite(file_stack, encryption, compression)) { - std::cerr << "FS error: file failed to add layers for '" << file_path << "'\n"; - return {}; - } - - return file_stack; -} - FragmentStore::FragmentStore(void) { registerSerializers(); }