From 79e8af89fe4868bf801f8b3425dd595dc128d1fe Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Wed, 10 Dec 2025 22:17:38 +0800 Subject: [PATCH] feat: finish binstore configuration and partial storage --- src/CMakeLists.txt | 2 + src/yycc/carton/binstore/configuration.cpp | 21 ++++++ src/yycc/carton/binstore/configuration.hpp | 29 ++++++++ src/yycc/carton/binstore/serializer.hpp | 1 + src/yycc/carton/binstore/storage.cpp | 12 +++- src/yycc/carton/binstore/storage.hpp | 84 +++++++++++++++++++++- src/yycc/carton/binstore/types.hpp | 14 +++- src/yycc/carton/clap/parser.cpp | 1 - src/yycc/carton/clap/parser.hpp | 1 + src/yycc/carton/clap/resolver.cpp | 2 +- src/yycc/carton/clap/resolver.hpp | 1 + 11 files changed, 162 insertions(+), 6 deletions(-) create mode 100644 src/yycc/carton/binstore/configuration.cpp create mode 100644 src/yycc/carton/binstore/configuration.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 868e47f..097d879 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -40,6 +40,7 @@ PRIVATE yycc/carton/clap/resolver.cpp yycc/carton/binstore/types.cpp yycc/carton/binstore/setting.cpp + yycc/carton/binstore/configuration.cpp yycc/carton/binstore/storage.cpp ) target_sources(YYCCommonplace @@ -108,6 +109,7 @@ FILES yycc/carton/binstore/types.hpp yycc/carton/binstore/serializer.hpp yycc/carton/binstore/setting.hpp + yycc/carton/binstore/configuration.hpp yycc/carton/binstore/storage.hpp yycc/carton/fft.hpp ) diff --git a/src/yycc/carton/binstore/configuration.cpp b/src/yycc/carton/binstore/configuration.cpp new file mode 100644 index 0000000..526093d --- /dev/null +++ b/src/yycc/carton/binstore/configuration.cpp @@ -0,0 +1,21 @@ +#include "configuration.hpp" + +#define BINSTORE ::yycc::carton::binstore +#define TYPES ::yycc::carton::binstore::types + +namespace yycc::carton::binstore::configuration { + + Configuration::Configuration(TYPES::VersionIdentifier version, BINSTORE::setting::SettingCollection&& settings) : + version(version), settings(std::move(settings)) {} + + Configuration::~Configuration() {} + + TYPES::VersionIdentifier Configuration::get_version() const { + return this->version; + } + + const BINSTORE::setting::SettingCollection& Configuration::get_settings() const { + return this->settings; + } + +} // namespace yycc::carton::binstore::configuration diff --git a/src/yycc/carton/binstore/configuration.hpp b/src/yycc/carton/binstore/configuration.hpp new file mode 100644 index 0000000..a19ecbe --- /dev/null +++ b/src/yycc/carton/binstore/configuration.hpp @@ -0,0 +1,29 @@ +#pragma once +#include "../../macro/class_copy_move.hpp" +#include "types.hpp" +#include "setting.hpp" + +#define NS_YYCC_BINSTORE ::yycc::carton::binstore +#define NS_YYCC_BINSTORE_TYPES ::yycc::carton::binstore::types + +namespace yycc::carton::binstore::configuration { + + class Configuration { + public: + Configuration(NS_YYCC_BINSTORE_TYPES::VersionIdentifier version, NS_YYCC_BINSTORE::setting::SettingCollection&& settings); + ~Configuration(); + YYCC_DEFAULT_COPY_MOVE(Configuration) + + public: + NS_YYCC_BINSTORE_TYPES::VersionIdentifier get_version() const; + const NS_YYCC_BINSTORE::setting::SettingCollection& get_settings() const; + + private: + NS_YYCC_BINSTORE_TYPES::VersionIdentifier version; ///< The version of current configuration struct. + NS_YYCC_BINSTORE::setting::SettingCollection settings; ///< All registered settings. + }; + +} // namespace yycc::carton::binstore::configuration + +#undef NS_YYCC_BINSTORE_TYPES +#undef NS_YYCC_BINSTORE diff --git a/src/yycc/carton/binstore/serializer.hpp b/src/yycc/carton/binstore/serializer.hpp index 02f681c..0e50da2 100644 --- a/src/yycc/carton/binstore/serializer.hpp +++ b/src/yycc/carton/binstore/serializer.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #define NS_YYCC_BINSTORE_TYPES ::yycc::carton::binstore::types diff --git a/src/yycc/carton/binstore/storage.cpp b/src/yycc/carton/binstore/storage.cpp index 06e7967..aa5858d 100644 --- a/src/yycc/carton/binstore/storage.cpp +++ b/src/yycc/carton/binstore/storage.cpp @@ -1,3 +1,13 @@ #include "storage.hpp" -namespace yycc::carton::binstore::storage {} +namespace yycc::carton::binstore::storage { + +#pragma region Storage Class + + Storage::Storage() {} + + Storage::~Storage() {} + +#pragma endregion + +} // namespace yycc::carton::binstore::storage diff --git a/src/yycc/carton/binstore/storage.hpp b/src/yycc/carton/binstore/storage.hpp index 324e01b..8e1dc9e 100644 --- a/src/yycc/carton/binstore/storage.hpp +++ b/src/yycc/carton/binstore/storage.hpp @@ -1,5 +1,87 @@ #pragma once +#include "../../macro/class_copy_move.hpp" +#include "types.hpp" +#include "configuration.hpp" +#include "serializer.hpp" +#include +#include +#include +#include +#include +#include + +#define NS_YYCC_BINSTORE_TYPES ::yycc::carton::binstore::types +#define NS_YYCC_BINSTORE_SERIALIZER ::yycc::carton::binstore::serializer namespace yycc::carton::binstore::storage { -} + /// @brief The strategy when loading from storage. + enum class LoadStrategy { + OnlyCurrent, ///< Only the file with exactly matched version identifier can be loaded. Any other version will cause load error. + LegacyAndCurrent, ///< Accept all old version and current version. Any other version will cause load error. + AcceptAll, ///< Accept all versions. + }; + + /// @brief The operation result state when getting setting's value. + enum class GetValueState { + Ok, ///< Successfully load value from raw part. We return it. + Default, ///< Given setting is not presented. We create it from its default value and return. + Reset, ///< Can not read from raw part. We reset it in its default value and return. + }; + + /// @brief The operation result state when setting setting's value. + enum class SetValueState { + Ok, ///< Successfully set value. + Reset, ///< Given value is not meet the requirement of this setting and its value was reset into default value. + }; + + class Storage { + private: + /** + * @brief All stored settings in raw format. + * @details Key is the token to already registered settings. + * Valus is its stored value in raw data format. + */ + std::map raws; + + public: + Storage(); + ~Storage(); + + public: + NS_YYCC_BINSTORE_TYPES::BinstoreResult load_from_file(const std::filesystem::path& fpath, LoadStrategy strategy); + NS_YYCC_BINSTORE_TYPES::BinstoreResult load_from_io(const std::istream& s, LoadStrategy strategy); + NS_YYCC_BINSTORE_TYPES::BinstoreResult save_into_file(const std::filesystem::path& fpath); + NS_YYCC_BINSTORE_TYPES::BinstoreResult save_into_io(const std::ostream& s); + void reset(); + + private: + bool has_value(NS_YYCC_BINSTORE_TYPES::Token token) const; + std::optional get_raw_value(NS_YYCC_BINSTORE_TYPES::Token token) const; + void set_raw_value(NS_YYCC_BINSTORE_TYPES::Token token, NS_YYCC_BINSTORE_TYPES::ByteArray&& ba); + + public: + template + NS_YYCC_BINSTORE_TYPES::BinstoreResult>> get_value( + NS_YYCC_BINSTORE_TYPES::Token token, const T& serdes = T()) { + // Fetch from raw value. + auto rv_raw_value = this->get_raw_value(token); + if (!rv_raw_value.has_value()) return std::unexpected(rv_raw_value.error()); + auto& [state, raw_value] = rv_raw_value.value(); + + // Pass into deserializer. + auto rv_des = serdes.deserialize(raw_value); + if (rv_des.has_value()) return rv_des.value(); + + // If we can not set it, we force reset it and try again. + this->set_raw_value() + } + template + NS_YYCC_BINSTORE_TYPES::BinstoreResult set_value(NS_YYCC_BINSTORE_TYPES::Token token, + const NS_YYCC_BINSTORE_SERIALIZER::SerDesValueType& value, + const T& serdes = T()) { + + } + }; + +} // namespace yycc::carton::binstore::storage diff --git a/src/yycc/carton/binstore/types.hpp b/src/yycc/carton/binstore/types.hpp index 6a36bce..c8fd6ae 100644 --- a/src/yycc/carton/binstore/types.hpp +++ b/src/yycc/carton/binstore/types.hpp @@ -8,7 +8,9 @@ namespace yycc::carton::binstore::types { /// @brief All possible error kind occurs in this module. enum class BinstoreError { - + NoSuchSetting, ///< Given token is invalid for setting. + BadVersion, ///< The version provided in given file or IO is rejected by version strategy. + Io, ///< C++ IO error. }; /// @brief The result type used in this module. @@ -28,6 +30,14 @@ namespace yycc::carton::binstore::types { */ using Token = size_t; + /** + * @brief The identifier reprsenting version in binstore. + * @details Higher number in identifier means that the newer version. + * In most cases, new version can accept old version's configurations. + * Once the struct of configurations was changed, this identifier should be bumped up. + */ + using VersionIdentifier = uint64_t; + /// @brief The raw data of setting. class ByteArray { public: @@ -67,4 +77,4 @@ namespace yycc::carton::binstore::types { std::vector datas; ///< The internal buffer. }; -} +} // namespace yycc::carton::binstore::types diff --git a/src/yycc/carton/clap/parser.cpp b/src/yycc/carton/clap/parser.cpp index dad2b16..58be3b1 100644 --- a/src/yycc/carton/clap/parser.cpp +++ b/src/yycc/carton/clap/parser.cpp @@ -1,5 +1,4 @@ #include "parser.hpp" -#include "types.hpp" #include "../../env.hpp" #include #include diff --git a/src/yycc/carton/clap/parser.hpp b/src/yycc/carton/clap/parser.hpp index aea936b..771821a 100644 --- a/src/yycc/carton/clap/parser.hpp +++ b/src/yycc/carton/clap/parser.hpp @@ -1,5 +1,6 @@ #pragma once #include "../../macro/class_copy_move.hpp" +#include "types.hpp" #include "application.hpp" #include "validator.hpp" #include diff --git a/src/yycc/carton/clap/resolver.cpp b/src/yycc/carton/clap/resolver.cpp index 910f21a..777d6f9 100644 --- a/src/yycc/carton/clap/resolver.cpp +++ b/src/yycc/carton/clap/resolver.cpp @@ -1,5 +1,4 @@ #include "resolver.hpp" -#include "types.hpp" #include "../../env.hpp" #include #include @@ -7,6 +6,7 @@ #define TYPES ::yycc::carton::clap::types #define APPLICATION ::yycc::carton::clap::application #define ENV ::yycc::env + namespace yycc::carton::clap::resolver { #pragma region Core diff --git a/src/yycc/carton/clap/resolver.hpp b/src/yycc/carton/clap/resolver.hpp index 49ee25b..b3a8987 100644 --- a/src/yycc/carton/clap/resolver.hpp +++ b/src/yycc/carton/clap/resolver.hpp @@ -1,5 +1,6 @@ #pragma once #include "../../macro/class_copy_move.hpp" +#include "types.hpp" #include "application.hpp" #include "validator.hpp" #include