diff options
author | attilamolnar <attilamolnar@hush.com> | 2013-04-03 20:05:49 +0200 |
---|---|---|
committer | attilamolnar <attilamolnar@hush.com> | 2013-04-08 23:13:25 +0200 |
commit | 98beba078bbc3138cea33d0b5b94cccffed84f2f (patch) | |
tree | d1d7d88ee5f681ce5f619733ce9919e10d74c368 | |
parent | 9de86c34d41fcbcedf7b332746b7f0e8c37c7c45 (diff) |
ListModeBase: Cache max items per channel
-rw-r--r-- | include/listmode.h | 34 | ||||
-rw-r--r-- | src/listmode.cpp | 70 |
2 files changed, 81 insertions, 23 deletions
diff --git a/include/listmode.h b/include/listmode.h index c21f9b93f..ee2b4118d 100644 --- a/include/listmode.h +++ b/include/listmode.h @@ -39,6 +39,15 @@ class ListModeBase : public ModeHandler typedef std::list<ListItem> ModeList; private: + class ChanData + { + public: + ModeList list; + int maxitems; + + ChanData() : maxitems(-1) { } + }; + /** The number of items a listmode's list may contain */ struct ListLimit @@ -46,12 +55,29 @@ class ListModeBase : public ModeHandler std::string mask; unsigned int limit; ListLimit(const std::string& Mask, unsigned int Limit) : mask(Mask), limit(Limit) { } + bool operator==(const ListLimit& other) const { return (this->mask == other.mask && this->limit == other.limit); } }; /** Max items per channel by name */ typedef std::vector<ListLimit> limitlist; + /** Finds the limit of modes that can be placed on the given channel name according to the config + * @param channame The channel name to find the limit for + * @return The maximum number of modes of this type that we allow to be set on the given channel name + */ + unsigned int FindLimit(const std::string& channame); + + /** Returns the limit on the given channel for this mode. + * If the limit is cached then the cached value is returned, + * otherwise the limit is determined using FindLimit() and cached + * for later queries before it is returned + * @param channame The channel name to find the limit for + * @param cd The ChanData associated with channel channame + * @return The maximum number of modes of this type that we allow to be set on the given channel + */ + unsigned int GetLimitInternal(const std::string& channame, ChanData* cd); + protected: /** Numeric to use when outputting the list */ @@ -75,7 +101,7 @@ class ListModeBase : public ModeHandler /** Storage key */ - SimpleExtItem<ModeList> extItem; + SimpleExtItem<ChanData> extItem; public: /** Constructor. @@ -183,5 +209,9 @@ class ListModeBase : public ModeHandler inline ListModeBase::ModeList* ListModeBase::GetList(Channel* channel) { - return extItem.get(channel); + ChanData* cd = extItem.get(channel); + if (!cd) + return NULL; + + return &cd->list; } diff --git a/src/listmode.cpp b/src/listmode.cpp index f45f1624f..555f75fa3 100644 --- a/src/listmode.cpp +++ b/src/listmode.cpp @@ -29,10 +29,10 @@ ListModeBase::ListModeBase(Module* Creator, const std::string& Name, char modech void ListModeBase::DisplayList(User* user, Channel* channel) { - ModeList* el = extItem.get(channel); - if (el) + ChanData* cd = extItem.get(channel); + if (cd) { - for (ModeList::reverse_iterator it = el->rbegin(); it != el->rend(); ++it) + for (ModeList::reverse_iterator it = cd->list.rbegin(); it != cd->list.rend(); ++it) { user->WriteNumeric(listnumeric, "%s %s %s %s %lu", user->nick.c_str(), channel->name.c_str(), it->mask.c_str(), (!it->setter.empty() ? it->setter.c_str() : ServerInstance->Config->ServerName.c_str()), (unsigned long) it->time); } @@ -47,12 +47,12 @@ void ListModeBase::DisplayEmptyList(User* user, Channel* channel) void ListModeBase::RemoveMode(Channel* channel, irc::modestacker* stack) { - ModeList* el = extItem.get(channel); - if (el) + ChanData* cd = extItem.get(channel); + if (cd) { irc::modestacker modestack(false); - for (ModeList::iterator it = el->begin(); it != el->end(); it++) + for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++) { if (stack) stack->Push(this->GetModeChar(), it->mask); @@ -83,6 +83,7 @@ void ListModeBase::DoRehash() { ConfigTagList tags = ServerInstance->Config->ConfTags(configtag); + limitlist oldlimits = chanlimits; chanlimits.clear(); for (ConfigIter i = tags.first; i != tags.second; i++) @@ -97,6 +98,17 @@ void ListModeBase::DoRehash() if (chanlimits.empty()) chanlimits.push_back(ListLimit("*", 64)); + + // Most of the time our settings are unchanged, so we can avoid iterating the chanlist + if (oldlimits == chanlimits) + return; + + for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); ++i) + { + ChanData* cd = extItem.get(i->second); + if (cd) + cd->maxitems = -1; + } } void ListModeBase::DoImplements(Module* m) @@ -107,11 +119,11 @@ void ListModeBase::DoImplements(Module* m) ServerInstance->Modules->Attach(eventlist, m, sizeof(eventlist)/sizeof(Implementation)); } -unsigned int ListModeBase::GetLimit(Channel* channel) +unsigned int ListModeBase::FindLimit(const std::string& channame) { for (limitlist::iterator it = chanlimits.begin(); it != chanlimits.end(); ++it) { - if (InspIRCd::Match(channel->name, it->mask)) + if (InspIRCd::Match(channame, it->mask)) { // We have a pattern matching the channel return it->limit; @@ -120,10 +132,26 @@ unsigned int ListModeBase::GetLimit(Channel* channel) return 64; } +unsigned int ListModeBase::GetLimitInternal(const std::string& channame, ChanData* cd) +{ + if (cd->maxitems < 0) + cd->maxitems = FindLimit(channame); + return cd->maxitems; +} + +unsigned int ListModeBase::GetLimit(Channel* channel) +{ + ChanData* cd = extItem.get(channel); + if (!cd) // just find the limit + return FindLimit(channel->name); + + return GetLimitInternal(channel->name, cd); +} + ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std::string ¶meter, bool adding) { // Try and grab the list - ModeList* el = extItem.get(channel); + ChanData* cd = extItem.get(channel); if (adding) { @@ -134,15 +162,15 @@ ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std return MODEACTION_DENY; // If there was no list - if (!el) + if (!cd) { // Make one - el = new ModeList; - extItem.set(channel, el); + cd = new ChanData; + extItem.set(channel, cd); } // Check if the item already exists in the list - for (ModeList::iterator it = el->begin(); it != el->end(); it++) + for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++) { if (parameter == it->mask) { @@ -154,7 +182,7 @@ ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std } } - if ((IS_LOCAL(source)) && (el->size() >= GetLimit(channel))) + if ((IS_LOCAL(source)) && (cd->list.size() >= GetLimitInternal(channel->name, cd))) { /* List is full, give subclass a chance to send a custom message */ TellListTooLong(source, channel, parameter); @@ -175,7 +203,7 @@ ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std if (ValidateParam(source, channel, parameter)) { // And now add the mask onto the list... - el->push_back(ListItem(parameter, source->nick, ServerInstance->Time())); + cd->list.push_back(ListItem(parameter, source->nick, ServerInstance->Time())); return MODEACTION_ALLOW; } else @@ -187,13 +215,13 @@ ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std else { // We're taking the mode off - if (el) + if (cd) { - for (ModeList::iterator it = el->begin(); it != el->end(); it++) + for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); ++it) { if (parameter == it->mask) { - el->erase(it); + cd->list.erase(it); return MODEACTION_ALLOW; } } @@ -208,8 +236,8 @@ ModeAction ListModeBase::OnModeChange(User* source, User*, Channel* channel, std void ListModeBase::DoSyncChannel(Channel* chan, Module* proto, void* opaque) { - ModeList* mlist = extItem.get(chan); - if (!mlist) + ChanData* cd = extItem.get(chan); + if (!cd) return; irc::modestacker modestack(true); @@ -217,7 +245,7 @@ void ListModeBase::DoSyncChannel(Channel* chan, Module* proto, void* opaque) std::vector<TranslateType> types; types.push_back(TR_TEXT); - for (ModeList::iterator it = mlist->begin(); it != mlist->end(); it++) + for (ModeList::iterator it = cd->list.begin(); it != cd->list.end(); it++) modestack.Push(mode, it->mask); while (modestack.GetStackedLine(stackresult)) |