From 25d10e3877fe85cc9d3ee0f5596337797b0a0505 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sat, 24 Jun 2023 20:57:54 +0200 Subject: [PATCH] that was all the fixes (hopefully) --- mission/tmtc/PersistentTmStore.cpp | 50 +++++++++++++++++------------- mission/tmtc/PersistentTmStore.h | 4 +-- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/mission/tmtc/PersistentTmStore.cpp b/mission/tmtc/PersistentTmStore.cpp index f68299a0..825f9d73 100644 --- a/mission/tmtc/PersistentTmStore.cpp +++ b/mission/tmtc/PersistentTmStore.cpp @@ -79,8 +79,8 @@ ReturnValue_t PersistentTmStore::buildDumpSet(uint32_t fromUnixSeconds, uint32_t } DumpIndex dumpIndex; dumpIndex.epoch = fileEpoch; - // Multiple files for the same time are supported via a special suffix. We smply count the - // number of copies and later try to dump the same number of files with the additonal + // Multiple files for the same time are supported via a special suffix. We simply count the + // number of copies and later try to dump the same number of files with the additional // suffixes auto iter = dumpParams.orderedDumpFilestamps.find(dumpIndex); if (iter != dumpParams.orderedDumpFilestamps.end()) { @@ -295,6 +295,7 @@ ReturnValue_t PersistentTmStore::startDumpFromUpTo(uint32_t fromUnixSeconds, return DUMP_DONE; } dumpParams.dumpIter = dumpParams.orderedDumpFilestamps.begin(); + dumpParams.currentSameFileIdx = std::nullopt; state = State::DUMPING; return loadNextDumpFile(); } @@ -303,7 +304,25 @@ ReturnValue_t PersistentTmStore::loadNextDumpFile() { using namespace std::filesystem; dumpParams.currentSize = 0; std::error_code e; - for (; dumpParams.dumpIter != dumpParams.orderedDumpFilestamps.end(); dumpParams.dumpIter++) { + // Handle iteration, which does not necessarily have to increment the set iterator + // if there are multiple files for a given timestamp. + auto handleIteration = [&](DumpIndex& dumpIndex) { + if (dumpIndex.additionalFiles > 0) { + if (not dumpParams.currentSameFileIdx.has_value()) { + // Initialize the file index and stay on same file + dumpParams.currentSameFileIdx = 0; + return; + } else if (dumpParams.currentSameFileIdx.value() < dumpIndex.additionalFiles - 1) { + dumpParams.currentSameFileIdx = dumpParams.currentSameFileIdx.value() + 1; + return; + } + } + // File will change, reset this field for correct state-keeping. + dumpParams.currentSameFileIdx = std::nullopt; + // Increment iterator for next cycle. + dumpParams.dumpIter++; + }; + while (dumpParams.dumpIter != dumpParams.orderedDumpFilestamps.end()) { DumpIndex dumpIndex = *dumpParams.dumpIter; timeval tv{}; tv.tv_sec = dumpIndex.epoch; @@ -311,38 +330,27 @@ ReturnValue_t PersistentTmStore::loadNextDumpFile() { createFileName(tv, dumpParams.currentSameFileIdx, fullPathLength); dumpParams.currentFile = path(std::string(reinterpret_cast(filePathBuf.data()), fullPathLength)); + if (DEBUG_DUMPS) { + sif::debug << baseName << " dump: Loading " << dumpParams.currentFile << std::endl; + } dumpParams.fileSize = std::filesystem::file_size(dumpParams.currentFile, e); if (e) { // TODO: Event? sif::error << "PersistentTmStore: Could not load next dump file: " << e.message() << std::endl; + handleIteration(dumpIndex); continue; } std::ifstream ifile(dumpParams.currentFile, std::ios::binary); if (ifile.bad()) { sif::error << "PersistentTmStore: File is bad. Loading next file" << std::endl; + handleIteration(dumpIndex); continue; } - if (DEBUG_DUMPS) { - sif::debug << baseName << " dump: Loading " << dumpParams.currentFile << std::endl; - } ifile.read(reinterpret_cast(fileBuf.data()), static_cast(dumpParams.fileSize)); - if (dumpIndex.additionalFiles > 0 and not dumpParams.currentSameFileIdx.has_value()) { - if (not dumpParams.currentSameFileIdx.has_value()) { - // Initialze the file index and stay on same file - dumpParams.currentSameFileIdx = 0; - continue; - - } else if (dumpParams.currentSameFileIdx.value() < dumpIndex.additionalFiles) { - dumpParams.currentSameFileIdx = dumpParams.currentSameFileIdx.value() + 1; - continue; - } - } - // File will change, reset this field for correct state-keeping. - dumpParams.currentSameFileIdx = std::nullopt; - // Increment iterator for next cycle. - dumpParams.dumpIter++; + // Next file is loaded, exit the loop. + handleIteration(dumpIndex); return returnvalue::OK; } // Directory iterator was consumed and we are done. diff --git a/mission/tmtc/PersistentTmStore.h b/mission/tmtc/PersistentTmStore.h index 54b87a42..e86acaaf 100644 --- a/mission/tmtc/PersistentTmStore.h +++ b/mission/tmtc/PersistentTmStore.h @@ -39,9 +39,9 @@ struct PersistentTmStoreArgs { }; struct DumpIndex { - uint32_t epoch; + uint32_t epoch = 0; // Number of additional files with a suffix like .0, .1 etc. - uint8_t additionalFiles; + uint8_t additionalFiles = 0; // Define a custom comparison function based on the epoch variable bool operator<(const DumpIndex& other) const { return epoch < other.epoch; } };