feat add layered_config
Some checks failed
ci/push/linux-aarch64-gcc/1 Pipeline failed
ci/push/linux-x64-gcc/1 Pipeline was successful
ci/push/linux-aarch64-gcc/2 Pipeline was successful
ci/push/linux-x64-gcc/2 Pipeline was successful
ci/pr/linux-aarch64-gcc/1 Pipeline failed
ci/pr/linux-x64-gcc/1 Pipeline failed
ci/pr/linux-aarch64-gcc/2 Pipeline failed
ci/pr/linux-x64-gcc/2 Pipeline failed
Some checks failed
ci/push/linux-aarch64-gcc/1 Pipeline failed
ci/push/linux-x64-gcc/1 Pipeline was successful
ci/push/linux-aarch64-gcc/2 Pipeline was successful
ci/push/linux-x64-gcc/2 Pipeline was successful
ci/pr/linux-aarch64-gcc/1 Pipeline failed
ci/pr/linux-x64-gcc/1 Pipeline failed
ci/pr/linux-aarch64-gcc/2 Pipeline failed
ci/pr/linux-x64-gcc/2 Pipeline failed
This commit is contained in:
parent
f15f68b3f9
commit
398442037e
@ -212,6 +212,7 @@ set(TILE_SRCS
|
||||
# "tile/rpc/server.cc"
|
||||
"tile/util/config.cc"
|
||||
"tile/util/ini_file_config.cc"
|
||||
"tile/util/layered_config.cc"
|
||||
)
|
||||
|
||||
if((NOT TILE_HAVE_GETIFADDRS) OR (NOT TILE_HAVE_FREEIFADDRS))
|
||||
|
@ -109,13 +109,14 @@ protected:
|
||||
};
|
||||
|
||||
virtual bool GetRaw(const Slice &key, std::string *value) const = 0;
|
||||
virtual void SetRaw(const Slice &key, const Slice &value) = 0;
|
||||
virtual bool SetRaw(const Slice &key, const Slice &value) = 0;
|
||||
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();
|
||||
|
||||
friend class std::default_delete<Config>;
|
||||
friend class LayeredConfig;
|
||||
|
||||
private:
|
||||
mutable Mutex mutex_;
|
||||
|
@ -35,8 +35,9 @@ bool IniFileConfig::GetRaw(const Slice &key, std::string *value) const {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
void IniFileConfig::SetRaw(const Slice &key, const Slice &value) {
|
||||
bool IniFileConfig::SetRaw(const Slice &key, const Slice &value) {
|
||||
map_[key] = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
void IniFileConfig::RemoveRaw(const Slice &key) {
|
||||
|
@ -21,7 +21,7 @@ public:
|
||||
|
||||
protected:
|
||||
bool GetRaw(const Slice &key, std::string *value) const override;
|
||||
void SetRaw(const Slice &key, const Slice &value) override;
|
||||
bool SetRaw(const Slice &key, const Slice &value) override;
|
||||
void RemoveRaw(const Slice &key) override;
|
||||
Keys Enumerate(const Slice &range) const override;
|
||||
|
||||
|
@ -1,25 +0,0 @@
|
||||
#ifndef TILE_UTIL_LAYERD_CONFIG_H
|
||||
#define TILE_UTIL_LAYERD_CONFIG_H
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "tile/util/config.h"
|
||||
|
||||
namespace tile {
|
||||
namespace util {
|
||||
class LayerdConfig : public Config {
|
||||
public:
|
||||
protected:
|
||||
struct ConfigItem {
|
||||
Config::Ptr cfg;
|
||||
// 1 < 2 < 3 < 4 ...
|
||||
int priority;
|
||||
// can remove or set new?
|
||||
bool writeable;
|
||||
std::string label;
|
||||
};
|
||||
};
|
||||
} // namespace util
|
||||
} // namespace tile
|
||||
|
||||
#endif // TILE_UTIL_LAYERD_CONFIG_H
|
129
tile/util/layered_config.cc
Normal file
129
tile/util/layered_config.cc
Normal file
@ -0,0 +1,129 @@
|
||||
#include "tile/util/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
|
57
tile/util/layered_config.h
Normal file
57
tile/util/layered_config.h
Normal file
@ -0,0 +1,57 @@
|
||||
#ifndef TILE_UTIL_LAYERD_CONFIG_H
|
||||
#define TILE_UTIL_LAYERD_CONFIG_H
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "tile/util/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_UTIL_LAYERD_CONFIG_H
|
Loading…
Reference in New Issue
Block a user