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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
|
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2010 PrimeBase Technologies GmbH, Germany
*
* 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; version 2 of the License.
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Barry Leslie
*
* 2010-05-12
*/
#include "config.h"
#include <string>
#include <vector>
#include "drizzled/session.h"
#include "drizzled/table_list.h"
#include "drizzled/definition/table.h"
#include "drizzled/module/registry.h"
#include "drizzled/plugin/event_observer.h"
#include <drizzled/util/functors.h>
#include <algorithm>
using namespace std;
namespace drizzled
{
namespace plugin
{
/*============================*/
// Basic plugin registration stuff.
EventObserverVector all_event_plugins;
const EventObserverVector &EventObserver::getEventObservers(void)
{
return all_event_plugins;
}
//---------
bool EventObserver::addPlugin(EventObserver *handler)
{
if (handler != NULL)
all_event_plugins.push_back(handler);
return false;
}
//---------
void EventObserver::removePlugin(EventObserver *handler)
{
if (handler != NULL)
all_event_plugins.erase(find(all_event_plugins.begin(), all_event_plugins.end(), handler));
}
/*
* The Event Observer list class in which plugins register which events they
* are interested in.
*
* Each table share for example, will have one of these hung on it to store
* a list off all event observers interested in it and which events they are
* interested in.
*/
class EventObserverList
{
public:
typedef multimap<uint32_t, EventObserver *> ObserverMap;
private:
/* A list of lists indexed by event type. */
vector<ObserverMap *> event_observer_lists;
public:
EventObserverList()
{
// Initialize the list with NULL pointers.
event_observer_lists.assign(EventObserver::MAX_EVENT_COUNT, NULL);
}
~EventObserverList()
{
for_each(event_observer_lists.begin(),
event_observer_lists.end(),
SafeDeletePtr());
event_observer_lists.clear();
}
/* Add the observer to the observer list for the even, positioning it if required.
*
* Note: Event observers are storted in a multimap object so that the order in which
* they are called can be sorted based on the requested position. Lookups are never done
* on the multimap, once filled it is used as a vector.
*/
void addObserver(EventObserver *eventObserver, enum EventObserver::EventType event, int32_t position)
{
uint32_t event_pos;
ObserverMap *observers;
observers= event_observer_lists[event];
if (observers == NULL)
{
observers= new ObserverMap();
event_observer_lists[event]= observers;
}
if (position == 0)
event_pos= INT32_MAX; // Set the event position to be in the middle.
else
event_pos= (uint32_t) position;
/* If positioned then check if the position is already taken. */
if (position)
{
if (observers->find(event_pos) != observers->end())
{
errmsg_printf(ERRMSG_LVL_WARN,
_("EventObserverList::addEventObserver() Duplicate event position %d for event '%s' from EventObserver plugin '%s'"),
position,
EventObserver::eventName(event),
eventObserver->getName().c_str());
}
}
observers->insert(pair<uint32_t, EventObserver *>(event_pos, eventObserver) );
}
/* Get the observer list for an event type. Will return NULL if no observer exists.*/
ObserverMap *getObservers(enum EventObserver::EventType event)
{
return event_observer_lists[event];
}
};
//---------
/* registerEvent() is called from the event observer plugins to add themselves to
* the event observer list to be notified when the specified event takes place.
*/
void EventObserver::registerEvent(EventObserverList &observers, EventType event, int32_t position)
{
observers.addObserver(this, event, position);
}
/*========================================================*/
/* Table Event Observer handling: */
/*========================================================*/
//----------
/* For each EventObserver plugin call its registerTableEventsDo() meathod so that it can
* register what events, if any, it is interested in on this table.
*/
class RegisterTableEventsIterate : public unary_function<EventObserver *, void>
{
TableShare &table_share;
EventObserverList &observers;
public:
RegisterTableEventsIterate(TableShare &table_share_arg, EventObserverList &observers_arg):
table_share(table_share_arg), observers(observers_arg) {}
inline result_type operator() (argument_type eventObserver)
{
eventObserver->registerTableEventsDo(table_share, observers);
}
};
//----------
/*
* registerTableEvents() is called by drizzle to register all plugins that
* may be interested in table events on the newly created TableShare object.
*/
void EventObserver::registerTableEvents(TableShare &table_share)
{
if (all_event_plugins.empty())
return;
EventObserverList *observers;
observers= table_share.getTableObservers();
if (observers != NULL)
{
errmsg_printf(ERRMSG_LVL_WARN,
_("EventObserver::registerTableEvents(): Table already has events registered on it: probable programming error."));
table_share.setTableObservers(NULL);
delete observers;
}
observers= new EventObserverList();
table_share.setTableObservers(observers);
for_each(all_event_plugins.begin(), all_event_plugins.end(),
RegisterTableEventsIterate(table_share, *observers));
}
//----------
/* Cleanup before freeing the TableShare object. */
void EventObserver::deregisterTableEvents(TableShare &table_share)
{
if (all_event_plugins.empty())
return;
EventObserverList *observers;
observers= table_share.getTableObservers();
if (observers)
{
table_share.setTableObservers(NULL);
delete observers;
}
}
/*========================================================*/
/* Schema Event Observer handling: */
/*========================================================*/
//----------
/* For each EventObserver plugin call its registerSchemaEventsDo() meathod so that it can
* register what events, if any, it is interested in on the schema.
*/
class RegisterSchemaEventsIterate : public unary_function<EventObserver *, void>
{
const std::string &db;
EventObserverList &observers;
public:
RegisterSchemaEventsIterate(const std::string &db_arg, EventObserverList &observers_arg) :
db(db_arg),
observers(observers_arg){}
inline result_type operator() (argument_type eventObserver)
{
eventObserver->registerSchemaEventsDo(db, observers);
}
};
//----------
/*
* registerSchemaEvents() is called by drizzle to register all plugins that
* may be interested in schema events on the database.
*/
void EventObserver::registerSchemaEvents(Session &session, const std::string &db)
{
if (all_event_plugins.empty())
return;
EventObserverList *observers;
observers= session.getSchemaObservers(db);
if (observers == NULL)
{
observers= new EventObserverList();
session.setSchemaObservers(db, observers);
}
for_each(all_event_plugins.begin(), all_event_plugins.end(),
RegisterSchemaEventsIterate(db, *observers));
}
//----------
/* Cleanup before freeing the Session object. */
void EventObserver::deregisterSchemaEvents(Session &session, const std::string &db)
{
if (all_event_plugins.empty())
return;
EventObserverList *observers;
observers= session.getSchemaObservers(db);
if (observers)
{
session.setSchemaObservers(db, NULL);
delete observers;
}
}
/*========================================================*/
/* Session Event Observer handling: */
/*========================================================*/
//----------
/* For each EventObserver plugin call its registerSessionEventsDo() meathod so that it can
* register what events, if any, it is interested in on this session.
*/
class RegisterSessionEventsIterate : public unary_function<EventObserver *, void>
{
Session &session;
EventObserverList &observers;
public:
RegisterSessionEventsIterate(Session &session_arg, EventObserverList &observers_arg) :
session(session_arg), observers(observers_arg) {}
inline result_type operator() (argument_type eventObserver)
{
eventObserver->registerSessionEventsDo(session, observers);
}
};
//----------
/*
* registerSessionEvents() is called by drizzle to register all plugins that
* may be interested in session events on the newly created session.
*/
void EventObserver::registerSessionEvents(Session &session)
{
if (all_event_plugins.empty())
return;
EventObserverList *observers;
observers= session.getSessionObservers();
if (observers) { // This should not happed
errmsg_printf(ERRMSG_LVL_WARN,
_("EventObserver::registerSessionEvents(): Session already has events registered on it: probable programming error."));
session.setSessionObservers(NULL);
delete observers;
}
observers= new EventObserverList();
session.setSessionObservers(observers);
for_each(all_event_plugins.begin(), all_event_plugins.end(),
RegisterSessionEventsIterate(session, *observers));
}
//----------
/* Cleanup before freeing the session object. */
void EventObserver::deregisterSessionEvents(Session &session)
{
if (all_event_plugins.empty())
return;
EventObserverList *observers;
observers= session.getSessionObservers();
if (observers)
{
session.setSessionObservers(NULL);
delete observers;
}
}
/* Event observer list iterator: */
//----------
class EventIterate : public unary_function<pair<uint32_t, EventObserver *>, bool>
{
EventData &data;
public:
EventIterate(EventData &data_arg) :
unary_function<pair<uint32_t, EventObserver *>, bool>(),
data(data_arg)
{}
inline result_type operator()(argument_type handler)
{
bool result= handler.second->observeEventDo(data);
if (result)
{
/* TRANSLATORS: The leading word "EventObserver" is the name
of the plugin api, and so should not be translated. */
errmsg_printf(ERRMSG_LVL_ERROR,
_("EventIterate event handler '%s' failed for event '%s'"),
handler.second->getName().c_str(), handler.second->eventName(data.event));
}
return result;
}
};
/*==========================================================*/
/* Generic meathods called by drizzle to notify all interested
* plugins of an event,
*/
// Call all event observers interested in the event.
bool EventData::callEventObservers()
{
EventObserverList::ObserverMap *eventObservers;
if (observerList == NULL)
return false; // Nobody was interested in the event. :(
eventObservers = observerList->getObservers(event);
if (eventObservers == NULL)
return false; // Nobody was interested in the event. :(
/* Use find_if instead of foreach so that we can collect return codes */
EventObserverList::ObserverMap::iterator iter=
find_if(eventObservers->begin(), eventObservers->end(),
EventIterate(*this));
/* If iter is == end() here, that means that all of the plugins returned
* false, which in this case means they all succeeded. Since we want to
* return false on success, we return the value of the two being !=.
*/
return iter != eventObservers->end();
}
//--------
bool SessionEventData::callEventObservers()
{
observerList= session.getSessionObservers();
return EventData::callEventObservers();
}
//--------
bool SchemaEventData::callEventObservers()
{
observerList= session.getSchemaObservers(db);
if (!observerList)
{
EventObserver::registerSchemaEvents(session, db);
observerList= session.getSchemaObservers(db);
}
return EventData::callEventObservers();
}
//--------
bool TableEventData::callEventObservers()
{
observerList= table.getMutableShare()->getTableObservers();
return EventData::callEventObservers();
}
/*==========================================================*/
/* Static meathods called by drizzle to notify interested plugins
* of a schema event.
*/
bool EventObserver::beforeDropTable(Session &session, const drizzled::TableIdentifier &table)
{
if (all_event_plugins.empty())
return false;
BeforeDropTableEventData eventData(session, table);
return eventData.callEventObservers();
}
bool EventObserver::afterDropTable(Session &session, const drizzled::TableIdentifier &table, int err)
{
if (all_event_plugins.empty())
return false;
AfterDropTableEventData eventData(session, table, err);
return eventData.callEventObservers();
}
bool EventObserver::beforeRenameTable(Session &session, const drizzled::TableIdentifier &from, const drizzled::TableIdentifier &to)
{
if (all_event_plugins.empty())
return false;
BeforeRenameTableEventData eventData(session, from, to);
return eventData.callEventObservers();
}
bool EventObserver::afterRenameTable(Session &session, const drizzled::TableIdentifier &from, const drizzled::TableIdentifier &to, int err)
{
if (all_event_plugins.empty())
return false;
AfterRenameTableEventData eventData(session, from, to, err);
return eventData.callEventObservers();
}
/*==========================================================*/
/* Static meathods called by drizzle to notify interested plugins
* of a table event.
*
* A quick test is done first to see if there are any interested observers.
*/
bool EventObserver::beforeInsertRecord(Table &table, unsigned char *buf)
{
if (all_event_plugins.empty() || !TableEventData::hasEvents(table))
return false;
BeforeInsertRecordEventData eventData(*(table.in_use), table, buf);
return eventData.callEventObservers();
}
bool EventObserver::afterInsertRecord(Table &table, const unsigned char *buf, int err)
{
if (all_event_plugins.empty() || !TableEventData::hasEvents(table))
return false;
AfterInsertRecordEventData eventData(*(table.in_use), table, buf, err);
return eventData.callEventObservers();
}
bool EventObserver::beforeDeleteRecord(Table &table, const unsigned char *buf)
{
if (all_event_plugins.empty() || !TableEventData::hasEvents(table))
return false;
BeforeDeleteRecordEventData eventData(*(table.in_use), table, buf);
return eventData.callEventObservers();
}
bool EventObserver::afterDeleteRecord(Table &table, const unsigned char *buf, int err)
{
if (all_event_plugins.empty() || !TableEventData::hasEvents(table))
return false;
AfterDeleteRecordEventData eventData(*(table.in_use), table, buf, err);
return eventData.callEventObservers();
}
bool EventObserver::beforeUpdateRecord(Table &table, const unsigned char *old_data, unsigned char *new_data)
{
if (all_event_plugins.empty() || !TableEventData::hasEvents(table))
return false;
BeforeUpdateRecordEventData eventData(*(table.in_use), table, old_data, new_data);
return eventData.callEventObservers();
}
bool EventObserver::afterUpdateRecord(Table &table, const unsigned char *old_data, unsigned char *new_data, int err)
{
if (all_event_plugins.empty() || !TableEventData::hasEvents(table))
return false;
AfterUpdateRecordEventData eventData(*(table.in_use), table, old_data, new_data, err);
return eventData.callEventObservers();
}
/*==========================================================*/
/* Static meathods called by drizzle to notify interested plugins
* of a session event.
*
* A quick test is done first to see if there are any interested observers.
*/
bool EventObserver::beforeCreateDatabase(Session &session, const std::string &db)
{
if (all_event_plugins.empty() || !SessionEventData::hasEvents(session))
return false;
BeforeCreateDatabaseEventData eventData(session, db);
return eventData.callEventObservers();
}
bool EventObserver::afterCreateDatabase(Session &session, const std::string &db, int err)
{
if (all_event_plugins.empty() || !SessionEventData::hasEvents(session))
return false;
AfterCreateDatabaseEventData eventData(session, db, err);
return eventData.callEventObservers();
}
bool EventObserver::beforeDropDatabase(Session &session, const std::string &db)
{
if (all_event_plugins.empty() || !SessionEventData::hasEvents(session))
return false;
BeforeDropDatabaseEventData eventData(session, db);
return eventData.callEventObservers();
}
bool EventObserver::afterDropDatabase(Session &session, const std::string &db, int err)
{
if (all_event_plugins.empty() || !SessionEventData::hasEvents(session))
return false;
AfterDropDatabaseEventData eventData(session, db, err);
return eventData.callEventObservers();
}
bool EventObserver::connectSession(Session &session)
{
if (all_event_plugins.empty() || !SessionEventData::hasEvents(session))
return false;
ConnectSessionEventData eventData(session);
return eventData.callEventObservers();
}
bool EventObserver::disconnectSession(Session &session)
{
if (all_event_plugins.empty() || !SessionEventData::hasEvents(session))
return false;
DisconnectSessionEventData eventData(session);
return eventData.callEventObservers();
}
bool EventObserver::beforeStatement(Session &session)
{
if (all_event_plugins.empty() || !SessionEventData::hasEvents(session))
return false;
BeforeStatementEventData eventData(session);
return eventData.callEventObservers();
}
bool EventObserver::afterStatement(Session &session)
{
if (all_event_plugins.empty() || !SessionEventData::hasEvents(session))
return false;
AfterStatementEventData eventData(session);
return eventData.callEventObservers();
}
} /* namespace plugin */
} /* namespace drizzled */
|