summaryrefslogtreecommitdiff
path: root/plugins/Dbx_tree/src/Events.h
blob: 7a3d50ff19fd77e6bd00f267c4d0d32ed77260fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*

dbx_tree: tree database driver for Miranda IM

Copyright 2007-2010 Michael "Protogenes" Kunz,

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#pragma once

#include "Interface.h"
#include "BTree.h"
#include "FileBTree.h"
#include "BlockManager.h"
#include "IterationHeap.h"
#include "Entities.h"
#include "Settings.h"
#include "Hash.h"
#include "EncryptionManager.h"
#include "sigslot.h"

#ifdef _MSC_VER
#include <hash_map>
#include <hash_set>
#else
#include <ext/hash_map>
#include <ext/hash_set>
#endif
#include <queue>
#include <time.h>
#include <windows.h>

#pragma pack(push, 1)  // push current alignment to stack, set alignment to 1 byte boundary

/**
	\brief Key Type of the EventsBTree

	The Key consists of a timestamp, seconds elapsed since 1.1.1970
	and an Index, which makes it possible to store multiple events with the same timestamp
**/
typedef struct TEventKey {
	uint32_t        TimeStamp; /// timestamp at which the event occoured
	TDBTEventHandle Event;

	bool operator <  (const TEventKey & Other) const
	{
		if (TimeStamp != Other.TimeStamp) return TimeStamp < Other.TimeStamp;
		if (Event != Other.Event) return Event < Other.Event;
		return false;
	}
	//bool operator <= (const TEventKey & Other);
	bool operator == (const TEventKey & Other) const
	{
		return (TimeStamp == Other.TimeStamp) && (Event == Other.Event);
	}

	//bool operator >= (const TEventKey & Other);
	bool operator >  (const TEventKey & Other) const
	{
		if (TimeStamp != Other.TimeStamp) return TimeStamp > Other.TimeStamp;
		if (Event != Other.Event) return Event > Other.Event;
		return false;
	}

} TEventKey;

/**
	\brief The data of an Event

	A event's data is variable length. The data is a TDBTEvent-structure followed by varaible length data.
	- fixed data
	- blob data (mostly UTF8 message body)
**/
typedef struct TEvent {
	uint32_t Flags;				       /// Flags
	uint32_t TimeStamp;          /// Timestamp of the event (seconds elapsed since 1.1.1970) used as key element
	uint32_t Type;               /// Eventtype
	TDBTEntityHandle Entity;     /// hEntity which owns this event
	uint32_t DataLength;         /// Length of the stored data in bytes

	uint8_t Reserved[8];         /// reserved storage
} TEvent;

#pragma pack(pop)


static const uint32_t cEventSignature = 0x365A7E92;
static const uint16_t cEventNodeSignature = 0x195C;

/**
	\brief Manages the Events Index in the Database
**/
class CEventsTree : public CFileBTree<TEventKey, 16>
{
private:
	TDBTEntityHandle m_Entity;

public:
	CEventsTree(CBlockManager & BlockManager, TNodeRef RootNode, TDBTEntityHandle Entity)
		:	CFileBTree<TEventKey, 16>::CFileBTree(BlockManager, RootNode, cEventNodeSignature),
			m_Entity(Entity)
		{	};
	~CEventsTree()
		{	};

	TDBTEntityHandle Entity()
		{
			return m_Entity;
		};
	void Entity(TDBTEntityHandle NewEntity)
		{
			m_Entity = NewEntity;
		};
};

/**
	\brief Manages the Virtual Events Index
	Sorry for duplicating code...
**/
class CVirtualEventsTree : public CBTree<TEventKey, 16>
{
private:
	TDBTEntityHandle m_Entity;

public:
	CVirtualEventsTree(TDBTEntityHandle Entity)
		:	CBTree<TEventKey, 16>::CBTree(0),
			m_Entity(Entity)
		{	};

	~CVirtualEventsTree()
		{	};

	TDBTEntityHandle Entity()
		{
			return m_Entity;
		};
	void Entity(TDBTEntityHandle NewEntity)
		{
			m_Entity = NewEntity;
		};
};


class CEventsTypeManager
{
public:
	CEventsTypeManager(CEntities & Entities, CSettings & Settings);
	~CEventsTypeManager();

	uint32_t MakeGlobalID(char* Module, uint32_t EventType);
	bool GetType(uint32_t GlobalID, char * & Module, uint32_t & EventType);
	uint32_t EnsureIDExists(char* Module, uint32_t EventType);

private:
	typedef struct TEventType {
		char *   ModuleName;
		uint32_t EventType;
	} TEventType, *PEventType;
	#ifdef _MSC_VER
	typedef stdext::hash_map<uint32_t, PEventType> TTypeMap;
	#else
    typedef __gnu_cxx::hash_map<uint32_t, PEventType> TTypeMap;
	#endif

	CEntities & m_Entities;
	CSettings & m_Settings;
	TTypeMap m_Map;

};


class CEvents : public sigslot::has_slots<>
{
public:

	CEvents(
		CBlockManager & BlockManager,
		CEncryptionManager & EncryptionManager,
		CEntities & Entities,
		CSettings & Settings
		);
	~CEvents();

	//compatibility
	TDBTEventHandle compFirstEvent(TDBTEntityHandle hEntity);
	TDBTEventHandle compFirstUnreadEvent(TDBTEntityHandle hEntity);
	TDBTEventHandle compLastEvent(TDBTEntityHandle hEntity);
	TDBTEventHandle compNextEvent(TDBTEventHandle hEvent);
	TDBTEventHandle compPrevEvent(TDBTEventHandle hEvent);

	//services
	unsigned int GetBlobSize(TDBTEventHandle hEvent);
	unsigned int Get(TDBTEventHandle hEvent, TDBTEvent & Event);
	unsigned int GetCount(TDBTEntityHandle hEntity);
	unsigned int Delete(TDBTEventHandle hEvent);
	TDBTEventHandle Add(TDBTEntityHandle hEntity, TDBTEvent & Event);
	unsigned int MarkRead(TDBTEventHandle hEvent);
	unsigned int WriteToDisk(TDBTEventHandle hEvent);

	TDBTEntityHandle getEntity(TDBTEventHandle hEvent);

	TDBTEventIterationHandle IterationInit(TDBTEventIterFilter & Filter);
	TDBTEventHandle IterationNext(TDBTEventIterationHandle Iteration);
	unsigned int IterationClose(TDBTEventIterationHandle Iteration);


private:
	typedef CBTree<TEventKey, 16> TEventBase;
	typedef struct  
	{
		CEventsTree * RealTree;
		CVirtualEventsTree * VirtualTree;
		uint32_t VirtualCount;
		TEventKey FirstVirtualUnread;
	} TEntityEventsRecord, *PEntityEventsRecord;
	#ifdef _MSC_VER
		typedef stdext::hash_map<TDBTEntityHandle, TEntityEventsRecord*> TEntityEventsMap;
	#else
		typedef __gnu_cxx::hash_map<TDBTEntityHandle, TEntityEventsRecord*> TEntityEventsMap;
  #endif
  typedef CIterationHeap<TEventBase::iterator> TEventsHeap;

	CBlockManager & m_BlockManager;
	CEncryptionManager & m_EncryptionManager;

	CEntities & m_Entities;
	CEventsTypeManager m_Types;

	TEntityEventsMap m_EntityEventsMap;

	typedef struct TEventIteration {
		TDBTEventIterFilter Filter;
		TEventsHeap * Heap;
		TDBTEventHandle LastEvent;
	} TEventIteration, *PEventIteration;

	void onRootChanged(void* EventsTree, CEventsTree::TNodeRef NewRoot);

	void onDeleteEventCallback(void * Tree, const TEventKey & Key, uint32_t Param);
	void onDeleteVirtualEventCallback(void * Tree, const TEventKey & Key, uint32_t Param);
	void onDeleteEvents(CEntities * Entities, TDBTEntityHandle hEntity);
	void onTransferEvents(CEntities * Entities, TDBTEntityHandle Source, TDBTEntityHandle Dest);

	PEntityEventsRecord getEntityRecord(TDBTEntityHandle hEntity);
	uint32_t adjustVirtualEventCount(PEntityEventsRecord Record, int32_t Adjust);
	bool MarkEventsTree(TEventBase::iterator Iterator, TDBTEventHandle FirstUnread);
	void FindNextUnreadEvent(TEventBase::iterator & Iterator);
};