From b640b5a06b4caaf776684bbdb9180a0cbab8a4b2 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Mon, 1 Apr 2024 12:43:21 +0200 Subject: [PATCH] variant to span helper --- src/fragment_store/fragment_store.cpp | 33 ++++++++++++++++----------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/fragment_store/fragment_store.cpp b/src/fragment_store/fragment_store.cpp index 5d41853..3af7f69 100644 --- a/src/fragment_store/fragment_store.cpp +++ b/src/fragment_store/fragment_store.cpp @@ -37,6 +37,19 @@ static const char* metaFileTypeSuffix(MetaFileType mft) { return ""; // .unk? } +// TODO: move to ... somewhere. (span? file2i?) +static ByteSpan spanFromRead(const std::variant>& data_var) { + if (std::holds_alternative>(data_var)) { + auto& vec = std::get>(data_var); + return {vec.data(), vec.size()}; + } else if (std::holds_alternative(data_var)) { + return std::get(data_var); + } else { + assert(false); + return {}; + } +} + FragmentStore::FragmentStore(void) { registerSerializers(); } @@ -445,27 +458,21 @@ bool FragmentStore::loadFromStorage(FragmentID fid, std::function(*data_file_stack.top().get())); - if (!data_file_stack.top()->isGood()) { - std::cerr << "FS error: fragment data file failed to add zstd decompression layer '" << frag_path << "'\n"; - return false; - } } else { + // TODO: make error instead assert(data_comp == Compression::NONE); } + if (!data_file_stack.top()->isGood()) { + std::cerr << "FS error: fragment data file failed to add " << (int)data_comp << " decompression layer '" << frag_path << "'\n"; + return false; + } + // TODO: make it read in a single chunk instead? static constexpr int64_t chunk_size {1024 * 1024}; // 1MiB should be good for read do { auto data_var = data_file_stack.top()->read(chunk_size); - ByteSpan data; - if (std::holds_alternative>(data_var)) { - auto& vec = std::get>(data_var); - data = {vec.data(), vec.size()}; - } else if (std::holds_alternative(data_var)) { - data = std::get(data_var); - } else { - assert(false); - } + ByteSpan data = spanFromRead(data_var); if (data.empty()) { // error or probably eof