feat/update_config #10

Merged
tqcq merged 26 commits from feat/update_config into master 2024-10-14 10:15:01 +08:00
3 changed files with 38 additions and 4 deletions
Showing only changes of commit 28c22939ce - Show all commits

View File

@ -8,7 +8,16 @@ bool Configuration::Has(const Slice &key) const {
return GetRaw(key, &value); return GetRaw(key, &value);
} }
void Configuration::Remove(const Slice &key) {} void Configuration::Remove(const Slice &key) {
UniqueLock<tile::Mutex> lock(mutex_);
if (events_enabled_) {
OnRemoving(key);
}
RemoveRaw(key);
if (events_enabled_) {
OnRemoved(key);
}
}
void Configuration::EnableEvents(bool enable) { events_enabled_ = enable; } void Configuration::EnableEvents(bool enable) { events_enabled_ = enable; }
bool Configuration::EventsEnabled() const { return events_enabled_; } bool Configuration::EventsEnabled() const { return events_enabled_; }

View File

@ -47,7 +47,7 @@ void JSONConfiguration::RemoveRaw(const Slice &key) {
if (!FindStart(key, &last_part, &root)) { if (!FindStart(key, &last_part, &root)) {
return; return;
} }
root->removeMember(last_part.ToString()); root->removeMember(last_part);
} }
std::string JSONConfiguration::Dump() const { std::string JSONConfiguration::Dump() const {
Configuration::ScopedLock _(*this); Configuration::ScopedLock _(*this);
@ -82,8 +82,11 @@ bool JSONConfiguration::SetRaw(const Slice &key, const Slice &value) {
} }
Configuration::Keys JSONConfiguration::Enumerate(const Slice &range) const { Configuration::Keys JSONConfiguration::Enumerate(const Slice &range) const {
Configuration::ScopedLock _(*this);
Configuration::Keys key_set; Configuration::Keys key_set;
std::string prefix = range;
if (!prefix.empty()) {
prefix += ".";
}
auto keys = Split(range, '.'); auto keys = Split(range, '.');
auto root = object_; auto root = object_;
@ -100,7 +103,7 @@ Configuration::Keys JSONConfiguration::Enumerate(const Slice &range) const {
} }
for (const auto &key : root.getMemberNames()) { for (const auto &key : root.getMemberNames()) {
key_set.push_back(key); key_set.push_back(prefix + key);
} }
return key_set; return key_set;
} }

View File

@ -73,4 +73,26 @@ TEST(JSONConfiguration, LayeredSet) {
ASSERT_FALSE(*config.GetBool("key5.key52")) << config.Dump(); ASSERT_FALSE(*config.GetBool("key5.key52")) << config.Dump();
} }
TEST(json_configuration, Enumerate) {
JSONConfiguration config;
std::istringstream istr(kJsonConfig);
ASSERT_TRUE(config.load(istr));
auto keys = config.keys("");
ASSERT_EQ(5, keys.size());
ASSERT_EQ("key1", keys[0]);
ASSERT_EQ("key2", keys[1]);
ASSERT_EQ("key3", keys[2]);
ASSERT_EQ("key4", keys[3]);
ASSERT_EQ("key5", keys[4]);
keys = config.keys("key5");
ASSERT_EQ(2, keys.size());
ASSERT_EQ("key5.key51", keys[0]);
ASSERT_EQ("key5.key52", keys[1]);
config.Remove("key5.key51");
keys = config.keys("key5");
ASSERT_EQ("key5.key52", keys[0]);
}
} // namespace tile } // namespace tile