1
0

feat: finish binstore configuration and partial storage

This commit is contained in:
2025-12-10 22:17:38 +08:00
parent d64c6669b4
commit 79e8af89fe
11 changed files with 162 additions and 6 deletions

View File

@ -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
)

View File

@ -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

View File

@ -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

View File

@ -5,6 +5,7 @@
#include <string>
#include <limits>
#include <concepts>
#include <type_traits>
#define NS_YYCC_BINSTORE_TYPES ::yycc::carton::binstore::types

View File

@ -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

View File

@ -1,5 +1,87 @@
#pragma once
#include "../../macro/class_copy_move.hpp"
#include "types.hpp"
#include "configuration.hpp"
#include "serializer.hpp"
#include <map>
#include <optional>
#include <utility>
#include <filesystem>
#include <istream>
#include <ostream>
#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<NS_YYCC_BINSTORE_TYPES::Token, NS_YYCC_BINSTORE_TYPES::ByteArray> raws;
public:
Storage();
~Storage();
public:
NS_YYCC_BINSTORE_TYPES::BinstoreResult<void> load_from_file(const std::filesystem::path& fpath, LoadStrategy strategy);
NS_YYCC_BINSTORE_TYPES::BinstoreResult<void> load_from_io(const std::istream& s, LoadStrategy strategy);
NS_YYCC_BINSTORE_TYPES::BinstoreResult<void> save_into_file(const std::filesystem::path& fpath);
NS_YYCC_BINSTORE_TYPES::BinstoreResult<void> save_into_io(const std::ostream& s);
void reset();
private:
bool has_value(NS_YYCC_BINSTORE_TYPES::Token token) const;
std::optional<NS_YYCC_BINSTORE_TYPES::ByteArray> 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_SERIALIZER::SerDes T>
NS_YYCC_BINSTORE_TYPES::BinstoreResult<std::pair<GetValueState, NS_YYCC_BINSTORE_SERIALIZER::SerDesValueType<T>>> 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_SERIALIZER::SerDes T>
NS_YYCC_BINSTORE_TYPES::BinstoreResult<SetValueState> set_value(NS_YYCC_BINSTORE_TYPES::Token token,
const NS_YYCC_BINSTORE_SERIALIZER::SerDesValueType<T>& value,
const T& serdes = T()) {
}
};
} // namespace yycc::carton::binstore::storage

View File

@ -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<uint8_t> datas; ///< The internal buffer.
};
}
} // namespace yycc::carton::binstore::types

View File

@ -1,5 +1,4 @@
#include "parser.hpp"
#include "types.hpp"
#include "../../env.hpp"
#include <stdexcept>
#include <ranges>

View File

@ -1,5 +1,6 @@
#pragma once
#include "../../macro/class_copy_move.hpp"
#include "types.hpp"
#include "application.hpp"
#include "validator.hpp"
#include <vector>

View File

@ -1,5 +1,4 @@
#include "resolver.hpp"
#include "types.hpp"
#include "../../env.hpp"
#include <stdexcept>
#include <ranges>
@ -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

View File

@ -1,5 +1,6 @@
#pragma once
#include "../../macro/class_copy_move.hpp"
#include "types.hpp"
#include "application.hpp"
#include "validator.hpp"
#include <vector>