diff options
Diffstat (limited to 'modules/database/db_redis.cpp')
-rw-r--r-- | modules/database/db_redis.cpp | 233 |
1 files changed, 102 insertions, 131 deletions
diff --git a/modules/database/db_redis.cpp b/modules/database/db_redis.cpp index 55843beac..4c633d684 100644 --- a/modules/database/db_redis.cpp +++ b/modules/database/db_redis.cpp @@ -14,116 +14,118 @@ using namespace Redis; class DatabaseRedis; static DatabaseRedis *me; -class Data : public Serialize::Data +class Data final + : public Serialize::Data { - public: +public: std::map<Anope::string, std::stringstream *> data; - ~Data() + ~Data() override { - for (std::map<Anope::string, std::stringstream *>::iterator it = data.begin(), it_end = data.end(); it != it_end; ++it) - delete it->second; + for (auto &[_, stream] : data) + delete stream; } - std::iostream& operator[](const Anope::string &key) anope_override + std::iostream &operator[](const Anope::string &key) override { - std::stringstream* &stream = data[key]; + std::stringstream *&stream = data[key]; if (!stream) stream = new std::stringstream(); return *stream; } - std::set<Anope::string> KeySet() const anope_override - { - std::set<Anope::string> keys; - for (std::map<Anope::string, std::stringstream *>::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it) - keys.insert(it->first); - return keys; - } - - size_t Hash() const anope_override + size_t Hash() const override { size_t hash = 0; - for (std::map<Anope::string, std::stringstream *>::const_iterator it = this->data.begin(), it_end = this->data.end(); it != it_end; ++it) - if (!it->second->str().empty()) - hash ^= Anope::hash_cs()(it->second->str()); + for (const auto &[_, value] : this->data) + if (!value->str().empty()) + hash ^= Anope::hash_cs()(value->str()); return hash; } }; -class TypeLoader : public Interface +class TypeLoader final + : public Interface { Anope::string type; - public: +public: TypeLoader(Module *creator, const Anope::string &t) : Interface(creator), type(t) { } - void OnResult(const Reply &r) anope_override; + void OnResult(const Reply &r) override; }; -class ObjectLoader : public Interface +class ObjectLoader final + : public Interface { Anope::string type; int64_t id; - public: +public: ObjectLoader(Module *creator, const Anope::string &t, int64_t i) : Interface(creator), type(t), id(i) { } - void OnResult(const Reply &r) anope_override; + void OnResult(const Reply &r) override; }; -class IDInterface : public Interface +class IDInterface final + : public Interface { Reference<Serializable> o; - public: +public: IDInterface(Module *creator, Serializable *obj) : Interface(creator), o(obj) { } - void OnResult(const Reply &r) anope_override; + void OnResult(const Reply &r) override; }; -class Deleter : public Interface +class Deleter final + : public Interface { Anope::string type; int64_t id; - public: +public: Deleter(Module *creator, const Anope::string &t, int64_t i) : Interface(creator), type(t), id(i) { } - void OnResult(const Reply &r) anope_override; + void OnResult(const Reply &r) override; }; -class Updater : public Interface +class Updater final + : public Interface { Anope::string type; int64_t id; - public: +public: Updater(Module *creator, const Anope::string &t, int64_t i) : Interface(creator), type(t), id(i) { } - void OnResult(const Reply &r) anope_override; + void OnResult(const Reply &r) override; }; -class ModifiedObject : public Interface +class ModifiedObject final + : public Interface { Anope::string type; int64_t id; - public: +public: ModifiedObject(Module *creator, const Anope::string &t, int64_t i) : Interface(creator), type(t), id(i) { } - void OnResult(const Reply &r) anope_override; + void OnResult(const Reply &r) override; }; -class SubscriptionListener : public Interface +class SubscriptionListener final + : public Interface { - public: +public: SubscriptionListener(Module *creator) : Interface(creator) { } - void OnResult(const Reply &r) anope_override; + void OnResult(const Reply &r) override; }; -class DatabaseRedis : public Module, public Pipe +class DatabaseRedis final + : public Module + , public Pipe { SubscriptionListener sl; std::set<Serializable *> updated_items; - public: +public: ServiceReference<Provider> redis; DatabaseRedis(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, DATABASE | VENDOR), sl(this) @@ -151,33 +153,31 @@ class DatabaseRedis : public Module, public Pipe obj->UpdateCache(data); std::vector<Anope::string> args; - args.push_back("HGETALL"); - args.push_back("hash:" + t->GetName() + ":" + stringify(obj->id)); + args.emplace_back("HGETALL"); + args.push_back("hash:" + t->GetName() + ":" + Anope::ToString(obj->id)); /* Get object attrs to clear before updating */ redis->SendCommand(new Updater(this, t->GetName(), obj->id), args); } } - void OnNotify() anope_override + void OnNotify() override { - for (std::set<Serializable *>::iterator it = this->updated_items.begin(), it_end = this->updated_items.end(); it != it_end; ++it) + for (auto *obj : this->updated_items) { - Serializable *s = *it; - - this->InsertObject(s); + this->InsertObject(obj); } this->updated_items.clear(); } - void OnReload(Configuration::Conf *conf) anope_override + void OnReload(Configuration::Conf *conf) override { Configuration::Block *block = conf->GetModule(this); this->redis = ServiceReference<Provider>("Redis::Provider", block->Get<const Anope::string>("engine", "redis/main")); } - EventReturn OnLoadDatabase() anope_override + EventReturn OnLoadDatabase() override { if (!redis) { @@ -185,10 +185,9 @@ class DatabaseRedis : public Module, public Pipe return EVENT_CONTINUE; } - const std::vector<Anope::string> type_order = Serialize::Type::GetTypeOrder(); - for (unsigned i = 0; i < type_order.size(); ++i) + for (const auto &type_order : Serialize::Type::GetTypeOrder()) { - Serialize::Type *sb = Serialize::Type::Find(type_order[i]); + Serialize::Type *sb = Serialize::Type::Find(type_order); this->OnSerializeTypeCreate(sb); } @@ -205,25 +204,25 @@ class DatabaseRedis : public Module, public Pipe return EVENT_STOP; } - void OnSerializeTypeCreate(Serialize::Type *sb) anope_override + void OnSerializeTypeCreate(Serialize::Type *sb) override { if (!redis) return; std::vector<Anope::string> args; - args.push_back("SMEMBERS"); + args.emplace_back("SMEMBERS"); args.push_back("ids:" + sb->GetName()); redis->SendCommand(new TypeLoader(this, sb->GetName()), args); } - void OnSerializableConstruct(Serializable *obj) anope_override + void OnSerializableConstruct(Serializable *obj) override { this->updated_items.insert(obj); this->Notify(); } - void OnSerializableDestruct(Serializable *obj) anope_override + void OnSerializableDestruct(Serializable *obj) override { Serialize::Type *t = obj->GetSerializableType(); @@ -240,8 +239,8 @@ class DatabaseRedis : public Module, public Pipe } std::vector<Anope::string> args; - args.push_back("HGETALL"); - args.push_back("hash:" + t->GetName() + ":" + stringify(obj->id)); + args.emplace_back("HGETALL"); + args.push_back("hash:" + t->GetName() + ":" + Anope::ToString(obj->id)); /* Get all of the attributes for this object */ redis->SendCommand(new Deleter(this, t->GetName(), obj->id), args); @@ -251,7 +250,7 @@ class DatabaseRedis : public Module, public Pipe this->Notify(); } - void OnSerializableUpdate(Serializable *obj) anope_override + void OnSerializableUpdate(Serializable *obj) override { this->updated_items.insert(obj); this->Notify(); @@ -266,26 +265,19 @@ void TypeLoader::OnResult(const Reply &r) return; } - for (unsigned i = 0; i < r.multi_bulk.size(); ++i) + for (auto *reply : r.multi_bulk) { - const Reply *reply = r.multi_bulk[i]; - if (reply->type != Reply::BULK) continue; - int64_t id; - try - { - id = convertTo<int64_t>(reply->bulk); - } - catch (const ConvertException &) - { + auto i = Anope::TryConvert<int64_t>(reply->bulk); + if (!i) continue; - } + auto id = i.value(); std::vector<Anope::string> args; - args.push_back("HGETALL"); - args.push_back("hash:" + this->type + ":" + stringify(id)); + args.emplace_back("HGETALL"); + args.push_back("hash:" + this->type + ":" + Anope::ToString(id)); me->redis->SendCommand(new ObjectLoader(me, this->type, id), args); } @@ -313,7 +305,7 @@ void ObjectLoader::OnResult(const Reply &r) data[key->bulk] << value->bulk; } - Serializable* &obj = st->objects[this->id]; + Serializable *&obj = st->objects[this->id]; obj = st->Unserialize(obj, data); if (obj) { @@ -332,7 +324,7 @@ void IDInterface::OnResult(const Reply &r) return; } - Serializable* &obj = o->GetSerializableType()->objects[r.i]; + Serializable *&obj = o->GetSerializableType()->objects[r.i]; if (obj) /* This shouldn't be possible */ obj->id = 0; @@ -358,16 +350,16 @@ void Deleter::OnResult(const Reply &r) me->redis->StartTransaction(); std::vector<Anope::string> args; - args.push_back("DEL"); - args.push_back("hash:" + this->type + ":" + stringify(this->id)); + args.emplace_back("DEL"); + args.push_back("hash:" + this->type + ":" + Anope::ToString(this->id)); /* Delete hash object */ me->redis->SendCommand(NULL, args); args.clear(); - args.push_back("SREM"); + args.emplace_back("SREM"); args.push_back("ids:" + this->type); - args.push_back(stringify(this->id)); + args.push_back(Anope::ToString(this->id)); /* Delete id from ids set */ me->redis->SendCommand(NULL, args); @@ -378,9 +370,9 @@ void Deleter::OnResult(const Reply &r) *value = r.multi_bulk[i + 1]; args.clear(); - args.push_back("SREM"); + args.emplace_back("SREM"); args.push_back("value:" + this->type + ":" + key->bulk + ":" + value->bulk); - args.push_back(stringify(this->id)); + args.push_back(Anope::ToString(this->id)); /* Delete value -> object id */ me->redis->SendCommand(NULL, args); @@ -421,9 +413,9 @@ void Updater::OnResult(const Reply &r) *value = r.multi_bulk[i + 1]; std::vector<Anope::string> args; - args.push_back("SREM"); + args.emplace_back("SREM"); args.push_back("value:" + this->type + ":" + key->bulk + ":" + value->bulk); - args.push_back(stringify(this->id)); + args.push_back(Anope::ToString(this->id)); /* Delete value -> object id */ me->redis->SendCommand(NULL, args); @@ -431,29 +423,25 @@ void Updater::OnResult(const Reply &r) /* Add object id to id set for this type */ std::vector<Anope::string> args; - args.push_back("SADD"); + args.emplace_back("SADD"); args.push_back("ids:" + this->type); - args.push_back(stringify(obj->id)); + args.push_back(Anope::ToString(obj->id)); me->redis->SendCommand(NULL, args); args.clear(); - args.push_back("HMSET"); - args.push_back("hash:" + this->type + ":" + stringify(obj->id)); + args.emplace_back("HMSET"); + args.push_back("hash:" + this->type + ":" + Anope::ToString(obj->id)); - typedef std::map<Anope::string, std::stringstream *> items; - for (items::iterator it = data.data.begin(), it_end = data.data.end(); it != it_end; ++it) + for (const auto &[key, value] : data.data) { - const Anope::string &key = it->first; - std::stringstream *value = it->second; - args.push_back(key); - args.push_back(value->str()); + args.emplace_back(value->str()); std::vector<Anope::string> args2; - args2.push_back("SADD"); + args2.emplace_back("SADD"); args2.push_back("value:" + this->type + ":" + key + ":" + value->str()); - args2.push_back(stringify(obj->id)); + args2.push_back(Anope::ToString(obj->id)); /* Add to value -> object id set */ me->redis->SendCommand(NULL, args2); @@ -504,16 +492,11 @@ void SubscriptionListener::OnResult(const Reply &r) if (s_type == NULL) return; - uint64_t obj_id; - try - { - obj_id = convertTo<uint64_t>(id); - } - catch (const ConvertException &) - { + auto oid = Anope::TryConvert<uint64_t>(id); + if (!oid.has_value()) return; - } + auto obj_id = oid.value(); if (op == "hset" || op == "hdel") { Serializable *s = s_type->objects[obj_id]; @@ -528,7 +511,7 @@ void SubscriptionListener::OnResult(const Reply &r) Log(LOG_DEBUG) << "redis: notify: got modify for object id " << obj_id << " of type " << type; std::vector<Anope::string> args; - args.push_back("HGETALL"); + args.emplace_back("HGETALL"); args.push_back("hash:" + type + ":" + id); me->redis->SendCommand(new ModifiedObject(me, type, obj_id), args); @@ -536,7 +519,7 @@ void SubscriptionListener::OnResult(const Reply &r) } else if (op == "del") { - Serializable* &s = s_type->objects[obj_id]; + Serializable *&s = s_type->objects[obj_id]; if (s == NULL) return; @@ -549,14 +532,10 @@ void SubscriptionListener::OnResult(const Reply &r) /* Transaction start */ me->redis->StartTransaction(); - typedef std::map<Anope::string, std::stringstream *> items; - for (items::iterator it = data.data.begin(), it_end = data.data.end(); it != it_end; ++it) + for (const auto &[k, value] : data.data) { - const Anope::string &k = it->first; - std::stringstream *value = it->second; - std::vector<Anope::string> args; - args.push_back("SREM"); + args.emplace_back("SREM"); args.push_back("value:" + type + ":" + k + ":" + value->str()); args.push_back(id); @@ -565,9 +544,9 @@ void SubscriptionListener::OnResult(const Reply &r) } std::vector<Anope::string> args; - args.push_back("SREM"); + args.emplace_back("SREM"); args.push_back("ids:" + type); - args.push_back(stringify(s->id)); + args.push_back(Anope::ToString(s->id)); /* Delete object from id set */ me->redis->SendCommand(NULL, args); @@ -590,7 +569,7 @@ void ModifiedObject::OnResult(const Reply &r) return; } - Serializable* &obj = st->objects[this->id]; + Serializable *&obj = st->objects[this->id]; /* Transaction start */ me->redis->StartTransaction(); @@ -602,16 +581,12 @@ void ModifiedObject::OnResult(const Reply &r) obj->Serialize(data); - typedef std::map<Anope::string, std::stringstream *> items; - for (items::iterator it = data.data.begin(), it_end = data.data.end(); it != it_end; ++it) + for (auto &[key, value] : data.data) { - const Anope::string &key = it->first; - std::stringstream *value = it->second; - std::vector<Anope::string> args; - args.push_back("SREM"); + args.emplace_back("SREM"); args.push_back("value:" + st->GetName() + ":" + key + ":" + value->str()); - args.push_back(stringify(this->id)); + args.push_back(Anope::ToString(this->id)); /* Delete value -> object id */ me->redis->SendCommand(NULL, args); @@ -635,25 +610,21 @@ void ModifiedObject::OnResult(const Reply &r) obj->UpdateCache(data); /* Insert new object values */ - typedef std::map<Anope::string, std::stringstream *> items; - for (items::iterator it = data.data.begin(), it_end = data.data.end(); it != it_end; ++it) + for (const auto &[key, value] : data.data) { - const Anope::string &key = it->first; - std::stringstream *value = it->second; - std::vector<Anope::string> args; - args.push_back("SADD"); + args.emplace_back("SADD"); args.push_back("value:" + st->GetName() + ":" + key + ":" + value->str()); - args.push_back(stringify(obj->id)); + args.push_back(Anope::ToString(obj->id)); /* Add to value -> object id set */ me->redis->SendCommand(NULL, args); } std::vector<Anope::string> args; - args.push_back("SADD"); + args.emplace_back("SADD"); args.push_back("ids:" + st->GetName()); - args.push_back(stringify(obj->id)); + args.push_back(Anope::ToString(obj->id)); /* Add to type -> id set */ me->redis->SendCommand(NULL, args); |