feat/update_config #10

Merged
tqcq merged 26 commits from feat/update_config into master 2024-10-14 10:15:01 +08:00
12 changed files with 258 additions and 243 deletions
Showing only changes of commit 34f0461a68 - Show all commits

View File

@ -207,9 +207,9 @@ set(TILE_SRCS
"tile/rpc/protocol/http/buffer_io.cc"
"tile/rpc/protocol/message.cc"
# "tile/rpc/server.cc"
"tile/base/config/config.cc"
"tile/base/config/ini_file_config.cc"
"tile/base/config/layered_config.cc"
"tile/base/config/configuration.cc"
"tile/base/config/ini_file_configuration.cc"
"tile/base/config/layered_configuration.cc"
)
if((NOT TILE_HAVE_GETIFADDRS) OR (NOT TILE_HAVE_FREEIFADDRS))

View File

@ -1,26 +1,26 @@
#include "tile/base/config/config.h"
#include "tile/base/config/configuration.h"
#include "tile/base/thread/unique_lock.h"
namespace tile {
namespace util {
bool Config::Has(const Slice &key) const {
bool Configuration::Has(const Slice &key) const {
UniqueLock<tile::Mutex> lock(mutex_);
std::string value;
return GetRaw(key, &value);
}
void Config::Remove(const Slice &key) {}
void Configuration::Remove(const Slice &key) {}
void Config::EnableEvents(bool enable) { events_enabled_ = enable; }
bool Config::EventsEnabled() const { return events_enabled_; }
void Configuration::EnableEvents(bool enable) { events_enabled_ = enable; }
bool Configuration::EventsEnabled() const { return events_enabled_; }
Config::Keys Config::keys(const Slice &root) const {
Configuration::Keys Configuration::keys(const Slice &root) const {
UniqueLock<tile::Mutex> lock(mutex_);
return Enumerate(root);
}
#define TILE_DEFINE_CONFIG_GETTER(type, name) \
std::optional<type> Config::Get##name(const Slice &key) const { \
std::optional<type> Configuration::Get##name(const Slice &key) const { \
std::string value; \
if (GetRaw(key, &value)) { \
auto opt = TryParseTraits<type>::TryParse(value); \
@ -33,7 +33,7 @@ Config::Keys Config::keys(const Slice &root) const {
return std::nullopt; \
} \
} \
type Config::Get##name(const Slice &key, type default_value) const { \
type Configuration::Get##name(const Slice &key, type default_value) const { \
auto opt = Get##name(key); \
if (opt.has_value()) { \
return *opt; \
@ -43,11 +43,11 @@ Config::Keys Config::keys(const Slice &root) const {
}
#define TILE_DEFINE_CONFIG_SETTER(type, name) \
void Config::Set##name(const Slice &key, type value) { \
void Configuration::Set##name(const Slice &key, type value) { \
SetRawWithEvent(key, Format("{}", value)); \
}
std::optional<std::string> Config::GetString(const Slice &key) const {
std::optional<std::string> Configuration::GetString(const Slice &key) const {
std::string value;
UniqueLock<tile::Mutex> lock(mutex_);
if (GetRaw(key, &value)) {
@ -57,16 +57,16 @@ std::optional<std::string> Config::GetString(const Slice &key) const {
}
}
std::string Config::GetString(const Slice &key,
std::string Configuration::GetString(const Slice &key,
std::string default_value) const {
auto opt = GetString(key);
return opt.has_value() ? *opt : default_value;
}
void Config::SetString(const Slice &key, const std::string &value) {
void Configuration::SetString(const Slice &key, const std::string &value) {
SetRawWithEvent(key, value);
}
void Config::SetBool(const Slice &key, bool value) {
void Configuration::SetBool(const Slice &key, bool value) {
SetRawWithEvent(key, value ? "true" : "false");
}
@ -91,7 +91,8 @@ TILE_DEFINE_CONFIG_SETTER(uint32_t, UInt32)
TILE_DEFINE_CONFIG_SETTER(uint64_t, UInt64)
TILE_DEFINE_CONFIG_SETTER(double, Double)
void Config::SetRawWithEvent(const Slice &key, const std::string &value) {
void Configuration::SetRawWithEvent(const Slice &key,
const std::string &value) {
if (events_enabled_) {
OnChanging(key, value);
}
@ -106,7 +107,7 @@ void Config::SetRawWithEvent(const Slice &key, const std::string &value) {
}
}
Config::~Config() {}
Configuration::~Configuration() {}
} // namespace util

View File

@ -1,5 +1,5 @@
#ifndef TILE_BASE_CONFIG_CONFIG_H
#define TILE_BASE_CONFIG_CONFIG_H
#ifndef TILE_BASE_CONFIG_CONFIGURATION_H
#define TILE_BASE_CONFIG_CONFIGURATION_H
#pragma once
#include "tile/base/optional.h"
@ -12,10 +12,10 @@
namespace tile {
namespace util {
class Config : public RefCounted<Config> {
class Configuration : public RefCounted<Configuration> {
public:
using Keys = std::vector<std::string>;
using Ptr = RefPtr<Config>;
using Ptr = RefPtr<Configuration>;
// events
// Key, Value
@ -98,13 +98,13 @@ protected:
protected:
class ScopedLock {
public:
explicit ScopedLock(const Config &config) : config_(config) {
explicit ScopedLock(const Configuration &config) : config_(config) {
config_.mutex_.Lock();
}
~ScopedLock() { config_.mutex_.Unlock(); }
private:
const Config &config_;
const Configuration &config_;
};
virtual bool GetRaw(const Slice &key, std::string *value) const = 0;
@ -112,10 +112,10 @@ protected:
virtual void RemoveRaw(const Slice &key) = 0;
virtual Keys Enumerate(const Slice &range) const = 0;
void SetRawWithEvent(const Slice &key, const std::string &value);
virtual ~Config();
virtual ~Configuration();
friend class std::default_delete<Config>;
friend class LayeredConfig;
friend class std::default_delete<Configuration>;
friend class LayeredConfiguration;
private:
mutable Mutex mutex_;
@ -124,4 +124,4 @@ private:
} // namespace util
} // namespace tile
#endif // TILE_BASE_CONFIG_CONFIG_H
#endif // TILE_BASE_CONFIG_CONFIGURATION_H

View File

@ -1,16 +1,16 @@
#include "tile/base/config/ini_file_config.h"
#include "tile/base/config/ini_file_configuration.h"
#include "tile/base/thread/scoped_lock.h"
namespace tile {
namespace util {
IniFileConfig::IniFileConfig() {}
IniFileConfiguration::IniFileConfiguration() {}
// IniFileConfig::IniFileConfig(std::istream &istr) { load(istr); }
// IniFileConfig::IniFileConfig(const std::string &path) { load(path); }
IniFileConfig::~IniFileConfig() {}
IniFileConfiguration::~IniFileConfiguration() {}
bool IniFileConfig::load(std::istream &istr) {
Config::ScopedLock lock(*this);
bool IniFileConfiguration::load(std::istream &istr) {
Configuration::ScopedLock lock(*this);
map_.clear();
section_key_.clear();
while (!istr.eof()) {
@ -18,7 +18,7 @@ bool IniFileConfig::load(std::istream &istr) {
}
return true;
}
bool IniFileConfig::load(const std::string &path) {
bool IniFileConfiguration::load(const std::string &path) {
std::ifstream istr(path);
if (istr.good()) {
return load(istr);
@ -26,7 +26,7 @@ bool IniFileConfig::load(const std::string &path) {
return false;
}
}
bool IniFileConfig::GetRaw(const Slice &key, std::string *value) const {
bool IniFileConfiguration::GetRaw(const Slice &key, std::string *value) const {
auto iter = map_.find(key.ToString());
if (iter != map_.end()) {
*value = iter->second;
@ -35,12 +35,12 @@ bool IniFileConfig::GetRaw(const Slice &key, std::string *value) const {
return false;
}
}
bool IniFileConfig::SetRaw(const Slice &key, const Slice &value) {
bool IniFileConfiguration::SetRaw(const Slice &key, const Slice &value) {
map_[key] = value;
return true;
}
void IniFileConfig::RemoveRaw(const Slice &key) {
void IniFileConfiguration::RemoveRaw(const Slice &key) {
std::string prefix = key;
if (!prefix.empty()) {
prefix.push_back('.');
@ -57,8 +57,8 @@ void IniFileConfig::RemoveRaw(const Slice &key) {
}
}
Config::Keys IniFileConfig::Enumerate(const Slice &key) const {
Config::Keys range;
Configuration::Keys IniFileConfiguration::Enumerate(const Slice &key) const {
Configuration::Keys range;
std::set<Slice> keys;
std::string prefix = key.ToString();
if (prefix.empty()) {
@ -86,7 +86,7 @@ Config::Keys IniFileConfig::Enumerate(const Slice &key) const {
return range;
}
void IniFileConfig::ParseLine(std::istream &istr) {
void IniFileConfiguration::ParseLine(std::istream &istr) {
static const int eof = std::char_traits<char>::eof();
auto ReadLine = [&](std::string *line) {
line->clear();
@ -135,7 +135,7 @@ void IniFileConfig::ParseLine(std::istream &istr) {
}
}
}
bool IniFileConfig::ICompare::operator()(const std::string &s1,
bool IniFileConfiguration::ICompare::operator()(const std::string &s1,
const std::string &s2) const {
auto len = std::min(s1.size(), s2.size());
return strncmp(s1.c_str(), s2.c_str(), len) < 0;

View File

@ -3,17 +3,17 @@
#pragma once
#include "tile/base/config/config.h"
#include "tile/base/config/configuration.h"
#include <map>
namespace tile {
namespace util {
class IniFileConfig : public Config {
class IniFileConfiguration : public Configuration {
public:
using Ptr = RefPtr<IniFileConfig>;
using Ptr = RefPtr<IniFileConfiguration>;
IniFileConfig();
~IniFileConfig() override;
IniFileConfiguration();
~IniFileConfiguration() override;
// IniFileConfig(std::istream &istr);
// IniFileConfig(const std::string &path);
bool load(std::istream &istr);

View File

@ -1,4 +1,4 @@
#include "tile/base/config/ini_file_config.h"
#include "tile/base/config/ini_file_configuration.h"
#include "gtest/gtest.h"
const char *kIniFileConfig = R"(
@ -18,16 +18,17 @@ a=4
namespace tile {
namespace util {
static_assert(!detail::HasClassofImpl<IniFileConfig, Config>::value, "");
static_assert(
!detail::HasClassofImpl<IniFileConfiguration, Configuration>::value, "");
TEST(IniFileConfig, LoadFromIStream) {
std::stringstream ss(kIniFileConfig);
Config::Ptr config = MakeRefCounted<IniFileConfig>();
Configuration::Ptr config = MakeRefCounted<IniFileConfiguration>();
ASSERT_FALSE(config->Has("a"));
ASSERT_FALSE(config->Has("sec1.a"));
ASSERT_FALSE(config->Has("sec3.a"));
if (config.Is<IniFileConfig>()) {
IniFileConfig::Ptr ini = config.As<IniFileConfig>();
if (config.Is<IniFileConfiguration>()) {
IniFileConfiguration::Ptr ini = config.As<IniFileConfiguration>();
ASSERT_TRUE(ini->load(ss));
}
ASSERT_TRUE(config->Has("a"));

View File

@ -1,129 +0,0 @@
#include "tile/base/config/layered_config.h"
namespace tile {
namespace util {
LayeredConfig::LayeredConfig() {}
LayeredConfig::~LayeredConfig() {}
void LayeredConfig::Add(Config::Ptr cfg) { Add(cfg, highest(), false); }
void LayeredConfig::Add(Config::Ptr cfg, int priority) {
Add(cfg, priority, false);
}
void LayeredConfig::Add(Config::Ptr cfg, const std::string &label) {
Add(cfg, label, highest(), false);
}
void LayeredConfig::Add(Config::Ptr cfg, const std::string &label,
int priority) {
Add(cfg, label, priority, false);
}
void LayeredConfig::Add(Config::Ptr cfg, const std::string &label,
bool writeable) {
Add(cfg, label, highest(), writeable);
}
void LayeredConfig::Add(Config::Ptr cfg, const std::string &label, int priority,
bool writeable) {
Config::ScopedLock lock(*this);
ConfigItem item;
item.cfg = cfg;
item.priority = priority;
item.writeable = writeable;
item.label = label;
auto it = configs_.begin();
while (it != configs_.end() && it->priority < priority) {
++it;
}
configs_.insert(it, item);
}
void LayeredConfig::Add(Config::Ptr cfg, int priority, bool writeable) {
Add(cfg, std::string(), priority, writeable);
}
void LayeredConfig::AddWriteable(Config::Ptr cfg, int priority) {
Add(cfg, priority, true);
}
Config::Ptr LayeredConfig::Find(const Slice &label) const {
Config::ScopedLock lock(*this);
for (const auto &conf : configs_) {
if (conf.label == label) {
return conf.cfg;
}
}
return 0;
}
void LayeredConfig::RemoveConfig(Config::Ptr cfg) {
Config::ScopedLock lock(*this);
for (auto it = configs_.begin(); it != configs_.end();) {
if (it->cfg == cfg) {
it = configs_.erase(it);
} else {
++it;
}
}
}
bool LayeredConfig::GetRaw(const Slice &key, std::string *value) const {
for (const auto &conf : configs_) {
if (conf.cfg->GetRaw(key, value)) {
return true;
}
}
return false;
}
bool LayeredConfig::SetRaw(const Slice &key, const Slice &value) {
for (const auto &conf : configs_) {
if (conf.writeable && conf.cfg->SetRaw(key, value)) {
return true;
}
}
return false;
}
Config::Keys LayeredConfig::Enumerate(const Slice &key) const {
Config::Keys keys;
std::set<Slice> key_set;
for (const auto &conf : configs_) {
auto conf_keys = conf.cfg->Enumerate(key);
for (const auto &k : conf_keys) {
if (key_set.insert(k).second) {
keys.push_back(k);
}
}
}
return keys;
}
void LayeredConfig::RemoveRaw(const std::string &key) {
for (auto &conf : configs_) {
if (conf.writeable) {
conf.cfg->RemoveRaw(key);
}
}
}
int LayeredConfig::lowest() const {
if (configs_.empty()) {
return 0;
} else {
return configs_.front().priority - 1;
}
}
int LayeredConfig::highest() const {
if (configs_.empty()) {
return 0;
} else {
return configs_.back().priority + 1;
}
}
} // namespace util
} // namespace tile

View File

@ -1,57 +0,0 @@
#ifndef TILE_BASE_CONFIG_LAYERED_CONFIG_H
#define TILE_BASE_CONFIG_LAYERED_CONFIG_H
#pragma once
#include "tile/base/config/config.h"
#include <list>
namespace tile {
namespace util {
class LayeredConfig : public Config {
public:
using Ptr = RefPtr<LayeredConfig>;
LayeredConfig();
LayeredConfig(const LayeredConfig &) = delete;
LayeredConfig &operator=(const LayeredConfig &) = delete;
void Add(Config::Ptr cfg);
void Add(Config::Ptr cfg, int priority);
void Add(Config::Ptr cfg, const std::string &label);
void Add(Config::Ptr cfg, const std::string &label, int priority);
void Add(Config::Ptr cfg, const std::string &label, bool writeable);
void Add(Config::Ptr cfg, const std::string &label, int priority,
bool writeable);
void Add(Config::Ptr cfg, int priority, bool writeable);
void AddWriteable(Config::Ptr cfg, int priority);
Config::Ptr Find(const Slice &label) const;
void RemoveConfig(Config::Ptr cfg);
protected:
struct ConfigItem {
Config::Ptr cfg;
// ... > -2 > -1 > 0 > 1 > 2 > 3 > 4 ...
int priority;
// can remove or set new?
bool writeable;
std::string label;
};
bool GetRaw(const Slice &key, std::string *value) const override;
bool SetRaw(const Slice &key, const Slice &value) override;
Config::Keys Enumerate(const Slice &key) const override;
void RemoveRaw(const std::string &key);
int lowest() const;
int highest() const;
~LayeredConfig();
private:
using ConfigList = std::list<ConfigItem>;
ConfigList configs_;
};
} // namespace util
} // namespace tile
#endif // TILE_BASE_CONFIG_LAYERED_CONFIG_H

View File

@ -0,0 +1,133 @@
#include "tile/base/config/layered_configuration.h"
namespace tile {
namespace util {
LayeredConfiguration::LayeredConfiguration() {}
LayeredConfiguration::~LayeredConfiguration() {}
void LayeredConfiguration::Add(Configuration::Ptr cfg) {
Add(cfg, highest(), false);
}
void LayeredConfiguration::Add(Configuration::Ptr cfg, int priority) {
Add(cfg, priority, false);
}
void LayeredConfiguration::Add(Configuration::Ptr cfg,
const std::string &label) {
Add(cfg, label, highest(), false);
}
void LayeredConfiguration::Add(Configuration::Ptr cfg, const std::string &label,
int priority) {
Add(cfg, label, priority, false);
}
void LayeredConfiguration::Add(Configuration::Ptr cfg, const std::string &label,
bool writeable) {
Add(cfg, label, highest(), writeable);
}
void LayeredConfiguration::Add(Configuration::Ptr cfg, const std::string &label,
int priority, bool writeable) {
Configuration::ScopedLock lock(*this);
ConfigItem item;
item.cfg = cfg;
item.priority = priority;
item.writeable = writeable;
item.label = label;
auto it = configs_.begin();
while (it != configs_.end() && it->priority < priority) {
++it;
}
configs_.insert(it, item);
}
void LayeredConfiguration::Add(Configuration::Ptr cfg, int priority,
bool writeable) {
Add(cfg, std::string(), priority, writeable);
}
void LayeredConfiguration::AddWriteable(Configuration::Ptr cfg, int priority) {
Add(cfg, priority, true);
}
Configuration::Ptr LayeredConfiguration::Find(const Slice &label) const {
Configuration::ScopedLock lock(*this);
for (const auto &conf : configs_) {
if (conf.label == label) {
return conf.cfg;
}
}
return 0;
}
void LayeredConfiguration::RemoveConfig(Configuration::Ptr cfg) {
Configuration::ScopedLock lock(*this);
for (auto it = configs_.begin(); it != configs_.end();) {
if (it->cfg == cfg) {
it = configs_.erase(it);
} else {
++it;
}
}
}
bool LayeredConfiguration::GetRaw(const Slice &key, std::string *value) const {
for (const auto &conf : configs_) {
if (conf.cfg->GetRaw(key, value)) {
return true;
}
}
return false;
}
bool LayeredConfiguration::SetRaw(const Slice &key, const Slice &value) {
for (const auto &conf : configs_) {
if (conf.writeable && conf.cfg->SetRaw(key, value)) {
return true;
}
}
return false;
}
Configuration::Keys LayeredConfiguration::Enumerate(const Slice &key) const {
Configuration::Keys keys;
std::set<Slice> key_set;
for (const auto &conf : configs_) {
auto conf_keys = conf.cfg->Enumerate(key);
for (const auto &k : conf_keys) {
if (key_set.insert(k).second) {
keys.push_back(k);
}
}
}
return keys;
}
void LayeredConfiguration::RemoveRaw(const std::string &key) {
for (auto &conf : configs_) {
if (conf.writeable) {
conf.cfg->RemoveRaw(key);
}
}
}
int LayeredConfiguration::lowest() const {
if (configs_.empty()) {
return 0;
} else {
return configs_.front().priority - 1;
}
}
int LayeredConfiguration::highest() const {
if (configs_.empty()) {
return 0;
} else {
return configs_.back().priority + 1;
}
}
} // namespace util
} // namespace tile

View File

@ -0,0 +1,57 @@
#ifndef TILE_BASE_CONFIG_LAYERED_CONFIGURATION_H
#define TILE_BASE_CONFIG_LAYERED_CONFIGURATION_H
#pragma once
#include "tile/base/config/configuration.h"
#include <list>
namespace tile {
namespace util {
class LayeredConfiguration : public Configuration {
public:
using Ptr = RefPtr<LayeredConfiguration>;
LayeredConfiguration();
LayeredConfiguration(const LayeredConfiguration &) = delete;
LayeredConfiguration &operator=(const LayeredConfiguration &) = delete;
void Add(Configuration::Ptr cfg);
void Add(Configuration::Ptr cfg, int priority);
void Add(Configuration::Ptr cfg, const std::string &label);
void Add(Configuration::Ptr cfg, const std::string &label, int priority);
void Add(Configuration::Ptr cfg, const std::string &label, bool writeable);
void Add(Configuration::Ptr cfg, const std::string &label, int priority,
bool writeable);
void Add(Configuration::Ptr cfg, int priority, bool writeable);
void AddWriteable(Configuration::Ptr cfg, int priority);
Configuration::Ptr Find(const Slice &label) const;
void RemoveConfig(Configuration::Ptr cfg);
protected:
struct ConfigItem {
Configuration::Ptr cfg;
// ... > -2 > -1 > 0 > 1 > 2 > 3 > 4 ...
int priority;
// can remove or set new?
bool writeable;
std::string label;
};
bool GetRaw(const Slice &key, std::string *value) const override;
bool SetRaw(const Slice &key, const Slice &value) override;
Configuration::Keys Enumerate(const Slice &key) const override;
void RemoveRaw(const std::string &key);
int lowest() const;
int highest() const;
~LayeredConfiguration();
private:
using ConfigList = std::list<ConfigItem>;
ConfigList configs_;
};
} // namespace util
} // namespace tile
#endif // TILE_BASE_CONFIG_LAYERED_CONFIGURATION_H

11
tile/base/configuration.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef TILE_BASE_CONFIGURATION_H
#define TILE_BASE_CONFIGURATION_H
#pragma once
#include "tile/base/config/configurable.h"
#include "tile/base/config/configuration.h"
#include "tile/base/config/ini_file_configuration.h"
#include "tile/base/config/layered_configuration.h"
#endif // TILE_BASE_CONFIGURATION_H

View File

@ -9,7 +9,6 @@
#include "tile/base/casting.h"
#include "tile/base/chrono.h"
#include "tile/base/compression.h"
#include "tile/base/config/ini_file_config.h"
#include "tile/base/data.h"
#include "tile/base/deferred.h"
#include "tile/base/demangle.h"
@ -40,13 +39,12 @@
#include "tile/base/write_mostly.h"
// util
#include "tile/util/config.h"
#include "tile/util/ini_file_config.h"
#include "tile/base/configuration.h"
// init module
#include "tile/init/override_flag.h"
#include "sigslot/signal.h"
#include "sigslot/sigslot.h"
// Tile Init
#include "mustache.hpp"