#pragma once #include "data/LedgerCacheInterface.hpp" #include "util/config/ConfigDefinition.hpp" #include #include #include #include #include namespace data { /** * @brief A concept for a class that can save ledger cache asynchronously. * * This concept defines the interface requirements for any type that manages * asynchronous saving of ledger cache to persistent storage. */ template concept SomeLedgerCacheSaver = requires(T a) { { a.save() } -> std::same_as; { a.waitToFinish() } -> std::same_as; }; /** * @brief Manages asynchronous saving of ledger cache to a file. * * This class provides functionality to save the ledger cache to a file in a separate thread, * allowing the main application to continue without blocking. The file path is configured * through the application's configuration system. */ class LedgerCacheSaver { std::optional cacheFilePath_; std::reference_wrapper cache_; std::optional savingThread_; bool isAsync_; public: /** * @brief Constructs a LedgerCacheSaver instance. * * @param config The configuration object containing the cache file path setting * @param cache Reference to the ledger cache interface to be saved */ LedgerCacheSaver( util::config::ClioConfigDefinition const& config, LedgerCacheInterface const& cache ); /** * @brief Destructor that ensures the saving thread is properly joined. * * Waits for any ongoing save operation to complete before destruction. */ ~LedgerCacheSaver(); /** * @brief Initiates an asynchronous save operation of the ledger cache. * * Spawns a new thread that saves the ledger cache to the configured file path. * If no file path is configured, the operation is skipped. Logs the progress * and result of the save operation. */ void save(); /** * @brief Waits for the saving thread to complete. * * Blocks until the saving operation finishes if a thread is currently active. * Safe to call multiple times or when no save operation is in progress. */ void waitToFinish(); }; } // namespace data