1
0
mirror of https://github.com/anope/anope.git synced 2026-07-09 13:43:14 +02:00

Optimize much of the database code and serialize code.

This commit is contained in:
Adam
2012-12-13 06:12:56 -05:00
parent 76ba147c22
commit c1077faa28
60 changed files with 1203 additions and 1057 deletions
+1 -1
View File
@@ -87,7 +87,7 @@ class CoreExport ChanAccess : public Serializable
ChanAccess(AccessProvider *p);
virtual ~ChanAccess();
Serialize::Data Serialize() const anope_override;
void Serialize(Serialize::Data &data) const anope_override;
static Serializable* Unserialize(Serializable *obj, Serialize::Data &);
/** Check if this access entry matches the given user or account
+2 -2
View File
@@ -122,7 +122,7 @@ class CoreExport NickAlias : public Serializable, public Extensible, public Flag
NickAlias(const Anope::string &nickname, NickCore *nickcore);
~NickAlias();
Serialize::Data Serialize() const anope_override;
void Serialize(Serialize::Data &data) const anope_override;
static Serializable* Unserialize(Serializable *obj, Serialize::Data &);
/** Release a nick
@@ -227,7 +227,7 @@ class CoreExport NickCore : public Serializable, public Extensible, public Flags
NickCore(const Anope::string &nickdisplay);
~NickCore();
Serialize::Data Serialize() const anope_override;
void Serialize(Serialize::Data &data) const anope_override;
static Serializable* Unserialize(Serializable *obj, Serialize::Data &);
/** Changes the display for this account
+11
View File
@@ -13,6 +13,8 @@
#ifndef ANOPE_H
#define ANOPE_H
#include <signal.h>
#include "hashcomp.h"
namespace Anope
@@ -282,9 +284,12 @@ namespace Anope
* Stream insertion operator, must be friend because they cannot be inside the class.
*/
friend std::ostream &operator<<(std::ostream &os, const string &_str);
friend std::istream &operator>>(std::istream &is, string &_str);
};
inline std::ostream &operator<<(std::ostream &os, const string &_str) { return os << _str._string; }
/* This is not standard to make operator>> behave like operator<< in that it will allow extracting a whole line, not just one word */
inline std::istream &operator>>(std::istream &is, string &_str) { return std::getline(is, _str._string); }
inline const string operator+(char chr, const string &str) { string tmp(chr); tmp += str; return tmp; }
inline const string operator+(const char *_str, const string &str) { string tmp(_str); tmp += str; return tmp; }
@@ -318,6 +323,8 @@ namespace Anope
/** The value to return from main()
*/
extern int ReturnValue;
extern sig_atomic_t Signal;
extern bool Quitting;
extern bool Restarting;
extern Anope::string QuitReason;
@@ -376,6 +383,10 @@ namespace Anope
*/
extern void Fork();
/** Does something with the signal in Anope::Signal
*/
extern void HandleSignal();
/** One of the first functions called, does general initialization such as reading
* command line args, loading the configuration, doing the initial fork() if necessary,
* initializating language support, loading modules, and loading databases.
+1 -1
View File
@@ -17,7 +17,7 @@
class CoreExport Base
{
/* References to this base class */
std::set<ReferenceBase *> references;
std::set<ReferenceBase *> *references;
public:
Base();
virtual ~Base();
+1 -1
View File
@@ -63,7 +63,7 @@ class CoreExport BotInfo : public User, public Flags<BotFlag>, public Serializab
*/
virtual ~BotInfo();
Serialize::Data Serialize() const;
void Serialize(Serialize::Data &data) const;
static Serializable* Unserialize(Serializable *obj, Serialize::Data &);
void GenerateUID();
+29 -17
View File
@@ -38,22 +38,25 @@ class CoreExport Extensible
{
private:
typedef std::map<Anope::string, ExtensibleItem *> extensible_map;
extensible_map extension_items;
extensible_map *extension_items;
public:
/** Default constructor, does nothing
/** Default constructor
*/
Extensible() { }
Extensible() : extension_items(NULL) { }
/** Destructor, deletes all of the extensible items in this object
* then clears the map
*/
virtual ~Extensible()
{
for (extensible_map::iterator it = extension_items.begin(), it_end = extension_items.end(); it != it_end; ++it)
if (it->second)
it->second->OnDelete();
extension_items.clear();
if (extension_items)
{
for (extensible_map::iterator it = extension_items->begin(), it_end = extension_items->end(); it != it_end; ++it)
if (it->second)
it->second->OnDelete();
delete extension_items;
}
}
/** Extend an Extensible class.
@@ -70,7 +73,9 @@ class CoreExport Extensible
void Extend(const Anope::string &key, ExtensibleItem *p)
{
this->Shrink(key);
this->extension_items[key] = p;
if (!extension_items)
extension_items = new extensible_map();
(*this->extension_items)[key] = p;
}
/** Shrink an Extensible class.
@@ -83,8 +88,11 @@ class CoreExport Extensible
*/
bool Shrink(const Anope::string &key)
{
extensible_map::iterator it = this->extension_items.find(key);
if (it != this->extension_items.end())
if (!extension_items)
return false;
extensible_map::iterator it = this->extension_items->find(key);
if (it != this->extension_items->end())
{
if (it->second != NULL)
it->second->OnDelete();
@@ -92,7 +100,7 @@ class CoreExport Extensible
* returns the number of elements removed, std::map
* is single-associative so this should only be 0 or 1
*/
return this->extension_items.erase(key) > 0;
return this->extension_items->erase(key) > 0;
}
return false;
@@ -105,9 +113,12 @@ class CoreExport Extensible
*/
template<typename T> T GetExt(const Anope::string &key) const
{
extensible_map::const_iterator it = this->extension_items.find(key);
if (it != this->extension_items.end())
return anope_dynamic_static_cast<T>(it->second);
if (this->extension_items)
{
extensible_map::const_iterator it = this->extension_items->find(key);
if (it != this->extension_items->end())
return anope_dynamic_static_cast<T>(it->second);
}
return NULL;
}
@@ -119,7 +130,7 @@ class CoreExport Extensible
*/
bool HasExt(const Anope::string &key) const
{
return this->extension_items.count(key) > 0;
return this->extension_items != NULL && this->extension_items->count(key) > 0;
}
/** Get a list of all extension items names.
@@ -129,8 +140,9 @@ class CoreExport Extensible
*/
void GetExtList(std::deque<Anope::string> &list) const
{
for (extensible_map::const_iterator it = extension_items.begin(), it_end = extension_items.end(); it != it_end; ++it)
list.push_back(it->first);
if (extension_items)
for (extensible_map::const_iterator it = extension_items->begin(), it_end = extension_items->end(); it != it_end; ++it)
list.push_back(it->first);
}
};
+1 -1
View File
@@ -31,7 +31,7 @@ class CoreExport Memo : public Flags<MemoFlag>, public Serializable
public:
Memo();
Serialize::Data Serialize() const anope_override;
void Serialize(Serialize::Data &data) const anope_override;
static Serializable* Unserialize(Serializable *obj, Serialize::Data &);
Anope::string owner;
-1
View File
@@ -40,7 +40,6 @@
#include "servers.h"
#include "service.h"
#include "services.h"
#include "signals.h"
#include "socketengine.h"
#include "sockets.h"
#include "threadengine.h"
+5 -5
View File
@@ -143,7 +143,7 @@ struct CoreExport BadWord : Serializable
BadWordType type;
BadWord() : Serializable("BadWord") { }
Serialize::Data Serialize() const anope_override;
void Serialize(Serialize::Data &data) const anope_override;
static Serializable* Unserialize(Serializable *obj, Serialize::Data &);
};
@@ -172,7 +172,7 @@ class CoreExport AutoKick : public Flags<AutoKickFlag>, public Serializable
time_t last_used;
AutoKick();
Serialize::Data Serialize() const anope_override;
void Serialize(Serialize::Data &data) const anope_override;
static Serializable* Unserialize(Serializable *obj, Serialize::Data &);
};
@@ -188,7 +188,7 @@ struct CoreExport ModeLock : Serializable
ModeLock(ChannelInfo *ch, bool s, ChannelModeName n, const Anope::string &p, const Anope::string &se = "", time_t c = Anope::CurTime);
Serialize::Data Serialize() const anope_override;
void Serialize(Serialize::Data &data) const anope_override;
static Serializable* Unserialize(Serializable *obj, Serialize::Data &);
};
@@ -206,7 +206,7 @@ struct CoreExport LogSetting : Serializable
time_t created;
LogSetting() : Serializable("LogSetting") { }
Serialize::Data Serialize() const anope_override;
void Serialize(Serialize::Data &data) const anope_override;
static Serializable* Unserialize(Serializable *obj, Serialize::Data &);
};
@@ -263,7 +263,7 @@ class CoreExport ChannelInfo : public Serializable, public Extensible, public Fl
~ChannelInfo();
Serialize::Data Serialize() const anope_override;
void Serialize(Serialize::Data &data) const anope_override;
static Serializable* Unserialize(Serializable *obj, Serialize::Data &);
/** Change the founder of the channek
+20 -37
View File
@@ -20,42 +20,25 @@
namespace Serialize
{
enum DataType
class Data
{
DT_TEXT,
DT_INT
};
class CoreExport stringstream : public std::stringstream
{
private:
Serialize::DataType type;
unsigned _max;
public:
stringstream();
stringstream(const stringstream &ss);
Anope::string astr() const;
template<typename T> std::istream &operator>>(T &val)
enum Type
{
std::istringstream is(this->str());
is >> val;
return *this;
}
std::istream &operator>>(Anope::string &val);
DT_TEXT,
DT_INT
};
bool operator==(const stringstream &other) const;
bool operator!=(const stringstream &other) const;
virtual ~Data() { }
stringstream &SetType(Serialize::DataType t);
Serialize::DataType GetType() const;
stringstream &SetMax(unsigned m);
unsigned GetMax() const;
virtual std::iostream& operator[](const Anope::string &key) = 0;
virtual bool IsEqual(Data *other) { throw CoreException("Not supported"); }
virtual void SetType(const Anope::string &key, Type t) { }
virtual Type GetType(const Anope::string &key) const { return DT_TEXT; }
};
typedef std::map<Anope::string, stringstream> Data;
extern void RegisterTypes();
class Type;
@@ -64,7 +47,7 @@ namespace Serialize
}
/** A serialziable object. Serializable objects can be serialized into
* a map of stringstreams (Serialize::Data), and then reconstructed or
* abstract data types (Serialize::Data), and then reconstructed or
* updated later at any time.
*/
class CoreExport Serializable : public virtual Base
@@ -82,7 +65,7 @@ class CoreExport Serializable : public virtual Base
/* Iterator into serializable_items */
std::list<Serializable *>::iterator s_iter;
/* The last serialized form of this object commited to the database */
Serialize::Data last_commit;
Serialize::Data *last_commit;
/* The last time this object was commited to the database */
time_t last_commit_time;
@@ -108,8 +91,8 @@ class CoreExport Serializable : public virtual Base
*/
void QueueUpdate();
bool IsCached();
void UpdateCache();
bool IsCached(Serialize::Data *);
void UpdateCache(Serialize::Data *);
bool IsTSCached();
void UpdateTS();
@@ -117,9 +100,9 @@ class CoreExport Serializable : public virtual Base
/** Get the type of serializable object this is
* @return The serializable object type
*/
Serialize::Type* GetSerializableType() const;
Serialize::Type* GetSerializableType() const { return this->s_type; }
virtual Serialize::Data Serialize() const = 0;
virtual void Serialize(Serialize::Data &data) const = 0;
static const std::list<Serializable *> &GetItems();
};
@@ -164,7 +147,7 @@ class CoreExport Serialize::Type
/** Gets the name for this type
* @return The name, eg "NickAlias"
*/
const Anope::string &GetName();
const Anope::string &GetName() { return this->name; }
/** Unserialized an object.
* @param obj NULL if this object doesn't yet exist. If this isn't NULL, instead
@@ -187,7 +170,7 @@ class CoreExport Serialize::Type
*/
void UpdateTimestamp();
Module* GetOwner() const;
Module* GetOwner() const { return this->owner; }
static Serialize::Type *Find(const Anope::string &name);
-48
View File
@@ -1,48 +0,0 @@
/*
*
* (C) 2003-2012 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*
* Based on the original code of Epona by Lara.
* Based on the original code of Services by Andy Church.
*
*/
#ifndef SIGNAL_H
#define SIGNAL_H
#include <signal.h>
#include "sockets.h"
/** Represents a signal handler
*/
class Signal : public Pipe
{
static std::vector<Signal *> SignalHandlers;
static void SignalHandler(int signal);
struct sigaction action, old;
public:
int signal;
/** Constructor
* @param s The signal to listen for
*/
Signal(int s);
~Signal();
/**
* Called when the signal is received.
* Note this is not *immediatly* called when the signal is received,
* but it is saved and called at a later time when we are not doing something
* important. This is always called on the main thread, even on systems that
* spawn threads for signals, like Windows.
*/
virtual void OnNotify() anope_override = 0;
};
#endif
+1 -1
View File
@@ -18,7 +18,7 @@
class CoreExport SocketEngine
{
static const int DefaultSize = 8; // Uplink, DNS, Signal handlers, Mode stacker
static const int DefaultSize = 2; // Uplink, mode stacker
public:
/* Map of sockets */
static std::map<int, Socket *> Sockets;
+25 -8
View File
@@ -225,15 +225,11 @@ class CoreExport Socket : public Flags<SocketFlag>
*/
bool IsIPv6() const;
/** Mark a socket as blocking
/** Mark a socket as (non)blocking
* @param state true to enable blocking, false to disable blocking
* @return true if the socket is now blocking
*/
bool SetBlocking();
/** Mark a socket as non-blocking
* @return true if the socket is now non-blocking
*/
bool SetNonBlocking();
bool SetBlocking(bool state);
/** Bind the socket to an ip and port
* @param ip The ip
@@ -456,7 +452,7 @@ class CoreExport ClientSocket : public virtual Socket
class CoreExport Pipe : public Socket
{
public:
/** The FD of the write pipe (if this isn't evenfd)
/** The FD of the write pipe
* this->sock is the readfd
*/
int write_pipe;
@@ -468,7 +464,28 @@ class CoreExport Pipe : public Socket
*/
bool ProcessRead() anope_override;
/** Write data to this pipe
* @param data The data to write
* @param sz The amount of data to wirite
*/
void Write(const char *data, size_t sz);
inline void Write(const Anope::string &data) { this->Write(data.c_str(), data.length() + 1); }
/** Read data from this pipe
* @param data A buffer to read data into
* @param sz The size of the buffer
* @return The amount of data read
*/
int Read(char *data, size_t sz);
/** Mark the write end of this pipe (non)blocking
* @param state true to enable blocking, false to disable blocking
* @return true if the socket is now blocking
*/
bool SetWriteBlocking(bool state);
/** Called when this pipe needs to be woken up
* Is the same as Write("\0", 1)
*/
void Notify();
+1 -1
View File
@@ -42,7 +42,7 @@ class CoreExport XLine : public Serializable
bool HasNickOrReal() const;
bool IsRegex() const;
Serialize::Data Serialize() const anope_override;
void Serialize(Serialize::Data &data) const anope_override;
static Serializable* Unserialize(Serializable *obj, Serialize::Data &data);
};