~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/ha_pbms.cc

  • Committer: Monty Taylor
  • Date: 2010-01-12 21:34:24 UTC
  • mto: This revision was merged to the branch mainline in revision 1268.
  • Revision ID: mordred@inaugust.com-20100112213424-6mslywtlca49mvnk
Updated to pandora-buld v0.94

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
2
 
 *
3
 
 * PrimeBase Media Stream for MySQL
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation; either version 2 of the License, or
8
 
 * (at your option) any later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program; if not, write to the Free Software
17
 
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
 
 *
19
 
 * Original author: Paul McCullagh
20
 
 * Continued development: Barry Leslie
21
 
 *
22
 
 * 2007-05-20
23
 
 *
24
 
 * H&G2JCtL
25
 
 *
26
 
 * Table handler.
27
 
 *
28
 
 */
29
 
 
30
 
#ifdef USE_PRAGMA_IMPLEMENTATION
31
 
#pragma implementation                          // gcc: Class implementation
32
 
#endif
33
 
 
34
 
#ifdef DRIZZLED
35
 
#include <config.h>
36
 
 
37
 
#include <drizzled/current_session.h>
38
 
#include <drizzled/common.h>
39
 
#include <drizzled/plugin.h>
40
 
#include <drizzled/field.h>
41
 
#include <drizzled/session.h>
42
 
#include <drizzled/data_home.h>
43
 
#include <drizzled/error.h>
44
 
#include <drizzled/table.h>
45
 
#include <drizzled/plugin/transactional_storage_engine.h>
46
 
 
47
 
#define my_strdup(a,b) strdup(a)
48
 
using namespace drizzled;
49
 
using namespace drizzled::plugin;
50
 
 
51
 
 
52
 
 
53
 
#include "cslib/CSConfig.h"
54
 
#else
55
 
#include "cslib/CSConfig.h"
56
 
#include "mysql_priv.h"
57
 
#include <mysql/plugin.h>
58
 
#include <my_dir.h>
59
 
#endif 
60
 
 
61
 
#include <stdlib.h>
62
 
#include <time.h>
63
 
#include <inttypes.h>
64
 
 
65
 
 
66
 
#include "defs_ms.h"
67
 
 
68
 
#include "cslib/CSDefs.h"
69
 
#include "cslib/CSObject.h"
70
 
#include "cslib/CSGlobal.h"
71
 
#include "cslib/CSThread.h"
72
 
#include "cslib/CSStrUtil.h"
73
 
#include "cslib/CSLog.h"
74
 
 
75
 
#include "engine_ms.h"  
76
 
#include "ha_pbms.h"
77
 
#include "network_ms.h"
78
 
#include "connection_handler_ms.h"
79
 
#include "open_table_ms.h"
80
 
#include "database_ms.h"
81
 
#include "temp_log_ms.h"
82
 
#include "system_table_ms.h"
83
 
#include "mysql_ms.h"
84
 
#include "discover_ms.h"
85
 
#include "metadata_ms.h"
86
 
#include "transaction_ms.h"
87
 
#include "systab_httpheader_ms.h"
88
 
#include "system_table_ms.h"
89
 
#include "parameters_ms.h"
90
 
#include "pbmsdaemon_ms.h"
91
 
#include "version_ms.h"
92
 
 
93
 
/* Note: 'new' used here is NOT CSObject::new which is a DEBUG define*/
94
 
#ifdef new
95
 
#undef new
96
 
#endif
97
 
 
98
 
 
99
 
#ifdef DRIZZLED
100
 
 
101
 
static int pbms_done_func(void *);
102
 
 
103
 
class PBMSStorageEngine : public drizzled::plugin::TransactionalStorageEngine {
104
 
public:
105
 
        PBMSStorageEngine()
106
 
        : TransactionalStorageEngine(std::string("PBMS"), HTON_NO_FLAGS | HTON_HIDDEN) {}
107
 
 
108
 
        ~PBMSStorageEngine()
109
 
        {
110
 
                pbms_done_func(NULL);
111
 
        }
112
 
        
113
 
        int close_connection(Session *);
114
 
        
115
 
        int doStartTransaction(Session *session, start_transaction_option_t options);
116
 
        int doCommit(Session *, bool);
117
 
        int doRollback(Session *, bool);
118
 
        Cursor *create(Table& table);
119
 
        bool doDropSchema(const drizzled::identifier::Schema&);
120
 
        
121
 
        /*
122
 
        * Indicates to a storage engine the start of a
123
 
        * new SQL statement.
124
 
        */
125
 
        void doStartStatement(Session *session)
126
 
        {
127
 
                (void) session;
128
 
        }
129
 
 
130
 
        /*
131
 
        * Indicates to a storage engine the end of
132
 
        * the current SQL statement in the supplied
133
 
        * Session.
134
 
        */
135
 
        void doEndStatement(Session *session)
136
 
        {
137
 
                (void) session;
138
 
        }
139
 
        
140
 
        int doCreateTable(Session&, Table&, const identifier::Table& ident, drizzled::message::Table& );        
141
 
        int doDropTable(Session &, const identifier::Table& );
142
 
        
143
 
        int doRenameTable(Session&, const identifier::Table &from, const identifier::Table &to);
144
 
        
145
 
        void doGetTableIdentifiers(drizzled::CachedDirectory &dir,
146
 
                                   const drizzled::identifier::Schema &schema,
147
 
                                   drizzled::identifier::Table::vector &set_of_identifiers) 
148
 
        {
149
 
                std::set<std::string> set_of_names;
150
 
                
151
 
                doGetTableNames(dir, schema, set_of_names);
152
 
                for (std::set<std::string>::iterator set_iter = set_of_names.begin(); set_iter != set_of_names.end(); ++set_iter)
153
 
                {
154
 
                        set_of_identifiers.push_back(identifier::Table(schema, *set_iter));
155
 
                }
156
 
        }
157
 
        
158
 
        void doGetTableNames(CachedDirectory&, 
159
 
                                        const identifier::Schema &schema, 
160
 
                                        std::set<std::string> &set_of_names) 
161
 
        {
162
 
                bool isPBMS = schema.compare("PBMS");
163
 
                
164
 
                if (isPBMS || PBMSParameters::isBLOBDatabase(schema.getSchemaName().c_str()))
165
 
                        PBMSSystemTables::getSystemTableNames(isPBMS, set_of_names);
166
 
        }
167
 
 
168
 
        int doSetSavepoint(Session *thd, NamedSavepoint &savepoint);
169
 
        int doRollbackToSavepoint(Session *session, NamedSavepoint &savepoint);
170
 
        int doReleaseSavepoint(Session *session, NamedSavepoint &savepoint);
171
 
        const char **bas_ext() const;
172
 
 
173
 
  int doGetTableDefinition(Session&, const identifier::Table &identifier,
174
 
                                          drizzled::message::Table &table_proto)
175
 
  {
176
 
                int err;
177
 
                const char *tab_name = identifier.getTableName().c_str();
178
 
 
179
 
                // Set some required table proto info:
180
 
                table_proto.set_schema(identifier.getSchemaName().c_str());
181
 
                table_proto.set_creation_timestamp(0);
182
 
                table_proto.set_update_timestamp(0);
183
 
                
184
 
                err = PBMSSystemTables::getSystemTableInfo(tab_name, table_proto);
185
 
                if (err)
186
 
                        return err;
187
 
                        
188
 
                return EEXIST;
189
 
  }
190
 
 
191
 
        bool doDoesTableExist(Session&, const identifier::Table &identifier)
192
 
        {
193
 
                const char *tab_name = identifier.getTableName().c_str();
194
 
                const char *db_name = identifier.getSchemaName().c_str();
195
 
                bool isPBMS = identifier.getSchemaName().compare("PBMS");
196
 
                
197
 
                if (isPBMS || PBMSParameters::isBLOBDatabase(db_name)) {
198
 
                        return PBMSSystemTables::isSystemTable(isPBMS, tab_name);                                                                        
199
 
                }
200
 
                
201
 
                return false;           
202
 
        }
203
 
 
204
 
 
205
 
};
206
 
 
207
 
PBMSStorageEngine       *pbms_hton;
208
 
#else
209
 
handlerton              *pbms_hton;
210
 
#endif
211
 
 
212
 
static const char *ha_pbms_exts[] = {
213
 
        NullS
214
 
};
215
 
 
216
 
/*
217
 
 * ---------------------------------------------------------------
218
 
 * UTILITIES
219
 
 */
220
 
 
221
 
#ifndef DRIZZLED
222
 
void pbms_take_part_in_transaction(void *thread)
223
 
{
224
 
        THD                     *thd;
225
 
        if ((thd = (THD *) thread)) {
226
 
                trans_register_ha(thd, true, pbms_hton); 
227
 
        }
228
 
}
229
 
#endif
230
 
 
231
 
#ifdef DRIZZLED
232
 
const char **PBMSStorageEngine::bas_ext() const
233
 
#else
234
 
const char **ha_pbms::bas_ext() const
235
 
#endif
236
 
{
237
 
        return ha_pbms_exts;
238
 
}
239
 
 
240
 
#ifdef DRIZZLED
241
 
int PBMSStorageEngine::close_connection(Session *thd)
242
 
{
243
 
#else
244
 
static int pbms_close_connection(handlerton *hton, THD* thd)
245
 
{
246
 
        (void)hton;
247
 
#endif
248
 
        MSEngine::closeConnection(thd);
249
 
        return 0;
250
 
}
251
 
 
252
 
 
253
 
 
254
 
/*
255
 
 * ---------------------------------------------------------------
256
 
 * HANDLER INTERFACE
257
 
 */
258
 
 
259
 
 
260
 
#ifdef DRIZZLED
261
 
Cursor *PBMSStorageEngine::create(Table& table)
262
 
{
263
 
        PBMSStorageEngine * const hton = this;
264
 
        return new ha_pbms(hton, table);
265
 
}
266
 
#else
267
 
static handler *pbms_create_handler(handlerton *hton, TABLE_SHARE *table, MEM_ROOT *mem_root)
268
 
{
269
 
        return new (mem_root) ha_pbms(hton, table);
270
 
}
271
 
#endif
272
 
 
273
 
#ifdef DRIZZLED
274
 
int PBMSStorageEngine::doStartTransaction(Session *thd, start_transaction_option_t options)
275
 
{
276
 
        (void)thd;
277
 
        (void)options;
278
 
        return 0;
279
 
}
280
 
 
281
 
int PBMSStorageEngine::doCommit(Session *thd, bool all)
282
 
{
283
 
#else
284
 
static int pbms_commit(handlerton *, THD *thd, bool all)
285
 
{
286
 
#endif
287
 
        int                     err = 0;
288
 
        CSThread        *self;
289
 
        PBMSResultRec result;
290
 
 
291
 
        // I am not interesed in single statement transactions.
292
 
        if (all == false)
293
 
                return 0;
294
 
 
295
 
        if (MSEngine::enterConnection(thd, &self, &result, false))
296
 
                return 0;
297
 
        inner_();
298
 
        try_(a) {
299
 
                MSTransactionManager::commit();
300
 
        }
301
 
        catch_(a) {
302
 
                err = MSEngine::exceptionToResult(&self->myException, &result);
303
 
        }
304
 
        cont_(a);
305
 
        self->myIsAutoCommit = true;
306
 
        return_(err);
307
 
}
308
 
 
309
 
#ifdef DRIZZLED
310
 
int PBMSStorageEngine::doRollback(THD *thd, bool all)
311
 
{
312
 
#else
313
 
static int pbms_rollback(handlerton *, THD *thd, bool all)
314
 
{
315
 
#endif
316
 
        int                     err = 0;
317
 
        CSThread        *self;
318
 
        PBMSResultRec result;
319
 
        
320
 
        UNUSED(all);
321
 
        
322
 
        if (MSEngine::enterConnection(thd, &self, &result, false))
323
 
                return 0;
324
 
        inner_();
325
 
        try_(a) {
326
 
                MSTransactionManager::rollback();
327
 
        }
328
 
        catch_(a) {
329
 
                err = MSEngine::exceptionToResult(&self->myException, &result);
330
 
        }
331
 
        cont_(a);
332
 
        self->myIsAutoCommit = true;
333
 
        return_(err);
334
 
}
335
 
 
336
 
#ifdef DRIZZLED
337
 
int PBMSStorageEngine::doSetSavepoint(Session *thd, NamedSavepoint &savepoint)
338
 
{
339
 
        int                     err = 0;
340
 
        CSThread        *self;
341
 
        PBMSResultRec result;
342
 
 
343
 
        if (MSEngine::enterConnection(thd, &self, &result, false))
344
 
                return 0;
345
 
        
346
 
        inner_();
347
 
        try_(a) {
348
 
                MSTransactionManager::setSavepoint(savepoint.getName().c_str());
349
 
        }
350
 
        catch_(a) {
351
 
                err = MSEngine::exceptionToResult(&self->myException, &result);
352
 
        }
353
 
        cont_(a);
354
 
        return_(err);
355
 
        
356
 
}
357
 
 
358
 
int PBMSStorageEngine::doRollbackToSavepoint(Session *session, NamedSavepoint &savepoint)
359
 
{
360
 
        int                     err = 0;
361
 
        CSThread        *self;
362
 
        PBMSResultRec result;
363
 
 
364
 
        if (MSEngine::enterConnection(session, &self, &result, false))
365
 
                return 0;
366
 
        inner_();
367
 
        try_(a) {
368
 
                MSTransactionManager::rollbackTo(savepoint.getName().c_str());
369
 
        }
370
 
        catch_(a) {
371
 
                err = MSEngine::exceptionToResult(&self->myException, &result);
372
 
        }
373
 
        cont_(a);
374
 
        return_(err);
375
 
}
376
 
 
377
 
 
378
 
int PBMSStorageEngine::doReleaseSavepoint(Session *session, NamedSavepoint &savepoint)
379
 
{
380
 
        int                     err = 0;
381
 
        CSThread        *self;
382
 
        PBMSResultRec result;
383
 
 
384
 
        if (MSEngine::enterConnection(session, &self, &result, false))
385
 
                return 0;
386
 
                
387
 
        inner_();
388
 
        try_(a) {
389
 
                MSTransactionManager::releaseSavepoint(savepoint.getName().c_str());
390
 
        }
391
 
        catch_(a) {
392
 
                err = MSEngine::exceptionToResult(&self->myException, &result);
393
 
        }
394
 
        cont_(a);
395
 
        return_(err);
396
 
}
397
 
 
398
 
#else
399
 
static int pbms_savepoint_set(handlerton *hton, THD *thd, void *sv)
400
 
{
401
 
        int                     err = 0;
402
 
        CSThread        *self;
403
 
        PBMSResultRec result;
404
 
 
405
 
        if (MSEngine::enterConnection(thd, &self, &result, false))
406
 
                return 0;
407
 
                
408
 
        *((uint32_t*)sv) = self->myStmtCount;
409
 
        return 0;       
410
 
}
411
 
 
412
 
static int pbms_savepoint_rollback(handlerton *hton, THD *thd, void *sv)
413
 
{
414
 
        int                     err = 0;
415
 
        CSThread        *self;
416
 
        PBMSResultRec result;
417
 
 
418
 
        if (MSEngine::enterConnection(thd, &self, &result, false))
419
 
                return 0;
420
 
        inner_();
421
 
        try_(a) {
422
 
                MSTransactionManager::rollbackToPosition(*((uint32_t*)sv));
423
 
        }
424
 
        catch_(a) {
425
 
                err = MSEngine::exceptionToResult(&self->myException, &result);
426
 
        }
427
 
        cont_(a);
428
 
        return_(err);
429
 
}
430
 
 
431
 
static int pbms_savepoint_release(handlerton *hton, THD *thd, void *sv)
432
 
{
433
 
        return 0;
434
 
}
435
 
 
436
 
#endif
437
 
 
438
 
#ifdef DRIZZLED
439
 
bool  PBMSStorageEngine::doDropSchema(const drizzled::identifier::Schema &schema)
440
 
{
441
 
        CSThread *self;
442
 
        PBMSResultRec result;
443
 
        
444
 
        if (MSEngine::enterConnectionNoThd(&self, &result))
445
 
                return false;
446
 
        inner_();
447
 
        
448
 
        try_(a) {
449
 
                MSDatabase::dropDatabase(schema.getSchemaName().c_str());
450
 
        }
451
 
        catch_(a);
452
 
        self->logException();
453
 
        cont_(a);
454
 
        return_(false);
455
 
}
456
 
#else
457
 
static void pbms_drop_database(handlerton *, char *path)
458
 
{
459
 
        CSThread *self;
460
 
        char db_name[PATH_MAX];
461
 
        PBMSResultRec result;
462
 
        
463
 
        if (MSEngine::enterConnectionNoThd(&self, &result))
464
 
                return;
465
 
        inner_();
466
 
        
467
 
        cs_strcpy(PATH_MAX, db_name, cs_last_directory_of_path(path));
468
 
        cs_remove_dir_char(db_name);
469
 
        try_(a) {
470
 
                MSDatabase::dropDatabase(db_name);
471
 
        }
472
 
        catch_(a);
473
 
        self->logException();
474
 
        cont_(a);
475
 
        exit_();
476
 
}
477
 
#endif
478
 
 
479
 
static bool pbms_started = false;
480
 
 
481
 
 
482
 
#ifdef DRIZZLED
483
 
int pbms_init_func(module::Context &registry);
484
 
int pbms_init_func(module::Context &registry)
485
 
#else
486
 
int pbms_init_func(void *p);
487
 
int pbms_discover_system_tables(handlerton *hton, THD* thd, const char *db, const char *name, uchar **frmblob, size_t *frmlen);
488
 
int pbms_init_func(void *p)
489
 
#endif
490
 
{
491
 
        PBMSResultRec           result;
492
 
        int                                     err;
493
 
        int                                     my_res = 0;
494
 
        CSThread                        *thread;
495
 
 
496
 
        ASSERT(!pbms_started);
497
 
        pbms_started = false;
498
 
        PBMSDaemon::setDaemonState(PBMSDaemon::DaemonStartUp);
499
 
        
500
 
        {
501
 
                char info[120];
502
 
                snprintf(info, 120, "PrimeBase Media Stream (PBMS) Daemon %s loaded...", PBMSVersion::getCString());
503
 
                CSL.logLine(NULL, CSLog::Protocol, info);
504
 
        }
505
 
        CSL.logLine(NULL, CSLog::Protocol, "Barry Leslie, PrimeBase Technologies GmbH, http://www.primebase.org");
506
 
        
507
 
        if ((err = MSEngine::startUp(&result))) {
508
 
                CSL.logLine(NULL, CSLog::Error, result.mr_message);
509
 
                PBMSDaemon::setDaemonState(PBMSDaemon::DaemonError);
510
 
                return(1);
511
 
        }
512
 
 
513
 
#ifdef DRIZZLED
514
 
                pbms_hton= new PBMSStorageEngine();
515
 
                registry.add(pbms_hton);
516
 
#else
517
 
        pbms_hton = (handlerton *) p;
518
 
        pbms_hton->state = SHOW_OPTION_YES;
519
 
        pbms_hton->close_connection = pbms_close_connection; /* close_connection, cleanup thread related data. */
520
 
        pbms_hton->create = pbms_create_handler;
521
 
        pbms_hton->flags = HTON_CAN_RECREATE | HTON_HIDDEN;
522
 
        pbms_hton->drop_database = pbms_drop_database; /* Drop a database */
523
 
        pbms_hton->discover = pbms_discover_system_tables;
524
 
 
525
 
        pbms_hton->commit = pbms_commit; /* commit */
526
 
        pbms_hton->rollback = pbms_rollback; /* rollback */
527
 
 
528
 
        pbms_hton->savepoint_offset = 4;
529
 
        pbms_hton->savepoint_set = pbms_savepoint_set;
530
 
        pbms_hton->savepoint_rollback = pbms_savepoint_rollback; 
531
 
        pbms_hton->savepoint_release = pbms_savepoint_release; 
532
 
#endif
533
 
        
534
 
        /* Startup the Media Stream network: */
535
 
        cs_init_memory();
536
 
        CSThread::startUp();
537
 
        if (!(thread = CSThread::newCSThread())) {
538
 
                CSException::logOSError(CS_CONTEXT, ENOMEM);
539
 
                PBMSDaemon::setDaemonState(PBMSDaemon::DaemonError);
540
 
                return(1);
541
 
        }
542
 
        if (!CSThread::attach(thread)) {
543
 
                PBMSDaemon::setDaemonState(PBMSDaemon::DaemonError);
544
 
                thread->myException.log(NULL);
545
 
                CSThread::shutDown();
546
 
                cs_exit_memory();
547
 
                MSEngine::shutDown();
548
 
                return(1);
549
 
        }
550
 
        enter_();
551
 
        try_(a) {
552
 
                thread->threadName = CSString::newString("startup");
553
 
                MSDatabase::startUp(PBMSParameters::getDefaultMetaDataHeaders());
554
 
                MSTableList::startUp();
555
 
                MSSystemTableShare::startUp();
556
 
                MSNetwork::startUp(PBMSParameters::getPortNumber());
557
 
                MSTransactionManager::startUp();
558
 
                MSNetwork::startNetwork();
559
 
        }
560
 
        catch_(a) {
561
 
                self->logException();
562
 
                my_res = 1;
563
 
        }
564
 
        cont_(a);
565
 
        if (my_res) {
566
 
                try_(b) {
567
 
                        MSNetwork::shutDown();
568
 
                        MSTransactionManager::shutDown();
569
 
                        MSSystemTableShare::shutDown();
570
 
                        MSDatabase::stopThreads();
571
 
                        MSTableList::shutDown();
572
 
                        MSDatabase::shutDown();
573
 
                        CSThread::shutDown();
574
 
                }
575
 
                catch_(b) {
576
 
                        self->logException();
577
 
                }
578
 
                cont_(b);
579
 
        }
580
 
        outer_();
581
 
        CSThread::detach(thread);
582
 
 
583
 
        if (my_res) {
584
 
                cs_exit_memory();
585
 
                MSEngine::shutDown();
586
 
        }
587
 
        else {
588
 
                srandom(time(NULL));
589
 
                pbms_started = true;
590
 
                
591
 
        }
592
 
 
593
 
        if (pbms_started)
594
 
                PBMSDaemon::setDaemonState(PBMSDaemon::DaemonRunning);
595
 
        else
596
 
                PBMSDaemon::setDaemonState(PBMSDaemon::DaemonError);
597
 
 
598
 
        return(my_res);
599
 
}
600
 
 
601
 
#ifdef DRIZZLED
602
 
static int pbms_done_func(void *)
603
 
#else
604
 
int pbms_done_func(void *)
605
 
#endif
606
 
{
607
 
        CSThread        *thread;
608
 
 
609
 
        if (!pbms_started)
610
 
                return 0;
611
 
 
612
 
        PBMSDaemon::setDaemonState(PBMSDaemon::DaemonShuttingDown);
613
 
        CSL.logLine(NULL, CSLog::Protocol, "PrimeBase Media Stream (PBMS) Daemon shutdown...");
614
 
        
615
 
        /* Shutdown the Media Stream network. */
616
 
        if (!(thread = CSThread::newCSThread()))
617
 
                CSException::logOSError(CS_CONTEXT, ENOMEM);
618
 
        else if (!CSThread::attach(thread))
619
 
                thread->myException.log(NULL);
620
 
        else {
621
 
                enter_();
622
 
                try_(a) {
623
 
                        thread->threadName = CSString::newString("shutdown");
624
 
                        MSNetwork::shutDown();
625
 
                        MSSystemTableShare::shutDown();
626
 
                        /* Ensure that the database threads are stopped before
627
 
                         * freeing the tables.
628
 
                         */
629
 
                        MSDatabase::stopThreads();
630
 
                        MSTableList::shutDown();
631
 
                        /* Databases must be shutdown after table because tables
632
 
                         * have references to repositories.
633
 
                         */
634
 
                        MSDatabase::shutDown();
635
 
                        
636
 
                        /* Shutdown the transaction manager after the databases
637
 
                         * incase they want to commit or rollback a transaction.
638
 
                         */
639
 
                        MSTransactionManager::shutDown();
640
 
                }
641
 
                catch_(a) {
642
 
                        self->logException();
643
 
                }
644
 
                cont_(a);
645
 
                outer_();
646
 
                CSThread::shutDown();
647
 
                CSThread::detach(thread);
648
 
        }
649
 
 
650
 
        MSEngine::shutDown();
651
 
        cs_exit_memory();
652
 
 
653
 
        CSL.logLine(NULL, CSLog::Protocol, "PrimeBase Media Stream (PBMS) Daemon shutdown completed");
654
 
        pbms_started = false;
655
 
        return(0);
656
 
}
657
 
 
658
 
#ifdef DRIZZLED
659
 
ha_pbms::ha_pbms(handlerton *hton, Table& table_arg) : handler(*hton, table_arg),
660
 
#else
661
 
ha_pbms::ha_pbms(handlerton *hton, TABLE_SHARE *table_arg) : handler(hton, table_arg),
662
 
#endif
663
 
ha_open_tab(NULL),
664
 
ha_error(0)
665
 
{
666
 
        memset(&ha_result, 0, sizeof(PBMSResultRec));
667
 
}
668
 
 
669
 
#ifndef DRIZZLED
670
 
MX_TABLE_TYPES_T ha_pbms::table_flags() const
671
 
{
672
 
        return (
673
 
                /* We need this flag because records are not packed
674
 
                 * into a table which means #ROWID != offset
675
 
                 */
676
 
                HA_REC_NOT_IN_SEQ |
677
 
                HA_CAN_SQL_HANDLER |
678
 
#if MYSQL_VERSION_ID > 50119
679
 
                /* We can do row logging, but not statement, because
680
 
                 * MVCC is not serializable!
681
 
                 */
682
 
                HA_BINLOG_ROW_CAPABLE |
683
 
#endif
684
 
                /*
685
 
                 * Auto-increment is allowed on a partial key.
686
 
                 */
687
 
                0);
688
 
}
689
 
#endif
690
 
 
691
 
int ha_pbms::open(const char *table_path, int , uint )
692
 
{
693
 
        CSThread *self;
694
 
 
695
 
        if ((ha_error = MSEngine::enterConnection(current_thd, &self, &ha_result, true)))
696
 
                return 1;
697
 
 
698
 
        inner_();
699
 
        try_(a) {
700
 
                ha_open_tab = MSSystemTableShare::openSystemTable(table_path, getTable());
701
 
#ifdef DRIZZLED
702
 
                ha_lock.init(&ha_open_tab->myShare->myThrLock);
703
 
#else
704
 
                thr_lock_data_init(&ha_open_tab->myShare->myThrLock, &ha_lock, NULL);
705
 
#endif
706
 
                ref_length = ha_open_tab->getRefLen();
707
 
        }
708
 
        catch_(a) {
709
 
                ha_error = MSEngine::exceptionToResult(&self->myException, &ha_result);
710
 
        }
711
 
        cont_(a);
712
 
        return_(ha_error != MS_OK);
713
 
}
714
 
 
715
 
int ha_pbms::close(void)
716
 
{
717
 
        CSThread *self;
718
 
 
719
 
        if ((ha_error = MSEngine::enterConnection(current_thd, &self, &ha_result, true)))
720
 
                return 1;
721
 
 
722
 
        inner_();
723
 
        if (ha_open_tab) {
724
 
                ha_open_tab->release();
725
 
                ha_open_tab = NULL;
726
 
        }
727
 
        outer_();
728
 
        MSEngine::exitConnection();
729
 
        return 0;
730
 
}
731
 
 
732
 
#ifdef PBMS_HAS_KEYS
733
 
/* Index access functions: */
734
 
int ha_pbms::index_init(uint idx, bool sorted)
735
 
{
736
 
        int err = 0;
737
 
        UNUSED(sorted);
738
 
        
739
 
        enter_();
740
 
        try_(a) {
741
 
                ha_open_tab->index_init(idx);
742
 
        }
743
 
        catch_(a) {
744
 
                ha_error = MSEngine::exceptionToResult(&self->myException, &ha_result);
745
 
                err = 1;
746
 
        }
747
 
        cont_(a);
748
 
        return_(err);
749
 
}
750
 
 
751
 
//-------
752
 
int ha_pbms::index_end()
753
 
{
754
 
        int err = 0;
755
 
        enter_();
756
 
        try_(a) {
757
 
                ha_open_tab->index_end();
758
 
        }
759
 
        catch_(a) {
760
 
                ha_error = MSEngine::exceptionToResult(&self->myException, &ha_result);
761
 
                err = 1;
762
 
        }
763
 
        cont_(a);
764
 
        return_(err);
765
 
}
766
 
 
767
 
//-------
768
 
int ha_pbms::index_read(byte * buf, const byte * key,
769
 
                                                         uint key_len, enum ha_rkey_function find_flag)
770
 
{
771
 
        int err = 0;
772
 
        enter_();
773
 
        try_(a) {
774
 
                if (!ha_open_tab->index_read(buf, key, key_len, find_flag))
775
 
                        err = HA_ERR_KEY_NOT_FOUND;
776
 
 
777
 
        }
778
 
        catch_(a) {
779
 
                ha_error = MSEngine::exceptionToResult(&self->myException, &ha_result);
780
 
                err = 1;
781
 
        }
782
 
        cont_(a);
783
 
        return_(err);
784
 
}
785
 
 
786
 
//-------
787
 
int ha_pbms::index_read_idx(byte * buf, uint idx, const byte * key,
788
 
                                                                         uint key_len, enum ha_rkey_function find_flag)
789
 
{
790
 
        int err = 0;
791
 
        enter_();
792
 
        try_(a) {
793
 
                if (!ha_open_tab->index_read_idx(buf, idx, key, key_len, find_flag))
794
 
                        err = HA_ERR_KEY_NOT_FOUND;
795
 
        }
796
 
        catch_(a) {
797
 
                ha_error = MSEngine::exceptionToResult(&self->myException, &ha_result);
798
 
                err = 1;
799
 
        }
800
 
        cont_(a);
801
 
        return_(err);
802
 
}
803
 
 
804
 
//-------
805
 
int ha_pbms::index_next(byte * buf)
806
 
{
807
 
        int err = 0;
808
 
        enter_();
809
 
        try_(a) {
810
 
                if (!ha_open_tab->index_next(buf))
811
 
                        err = HA_ERR_END_OF_FILE;
812
 
        }
813
 
        catch_(a) {
814
 
                ha_error = MSEngine::exceptionToResult(&self->myException, &ha_result);
815
 
                err = 1;
816
 
        }
817
 
        cont_(a);
818
 
        return_(err);
819
 
}
820
 
 
821
 
//-------
822
 
int ha_pbms::index_prev(byte * buf)
823
 
{
824
 
        int err = 0;
825
 
        enter_();
826
 
        try_(a) {
827
 
                if (!ha_open_tab->index_prev(buf))
828
 
                        err = HA_ERR_END_OF_FILE;
829
 
        }
830
 
        catch_(a) {
831
 
                ha_error = MSEngine::exceptionToResult(&self->myException, &ha_result);
832
 
                err = 1;
833
 
        }
834
 
        cont_(a);
835
 
        return_(err);
836
 
}
837
 
 
838
 
//-------
839
 
int ha_pbms::index_first(byte * buf)
840
 
{
841
 
        int err = 0;
842
 
        enter_();
843
 
        try_(a) {
844
 
                if (!ha_open_tab->index_first(buf))
845
 
                        err = HA_ERR_END_OF_FILE;
846
 
        }
847
 
        catch_(a) {
848
 
                ha_error = MSEngine::exceptionToResult(&self->myException, &ha_result);
849
 
                err = 1;
850
 
        }
851
 
        cont_(a);
852
 
        return_(err);
853
 
}
854
 
 
855
 
//-------
856
 
int ha_pbms::index_last(byte * buf)
857
 
{
858
 
        int err = 0;
859
 
        enter_();
860
 
        try_(a) {
861
 
                if (!ha_open_tab->index_last(buf))
862
 
                        err = HA_ERR_END_OF_FILE;
863
 
        }
864
 
        catch_(a) {
865
 
                ha_error = MSEngine::exceptionToResult(&self->myException, &ha_result);
866
 
                err = 1;
867
 
        }
868
 
        cont_(a);
869
 
        return_(err);
870
 
}
871
 
 
872
 
//-------
873
 
int ha_pbms::index_read_last(byte * buf, const byte * key, uint key_len)
874
 
{
875
 
        int err = 0;
876
 
        enter_();
877
 
        try_(a) {
878
 
                if (!ha_open_tab->index_read_last(buf, key, key_len))
879
 
                        err = HA_ERR_KEY_NOT_FOUND;
880
 
        }
881
 
        catch_(a) {
882
 
                ha_error = MSEngine::exceptionToResult(&self->myException, &ha_result);
883
 
                err = 1;
884
 
        }
885
 
        cont_(a);
886
 
        return_(err);
887
 
}
888
 
 
889
 
//-------
890
 
 
891
 
#endif // PBMS_HAS_KEYS
892
 
 
893
 
/* Sequential scan functions: */
894
 
#ifdef DRIZZLED
895
 
int ha_pbms::doStartTableScan(bool )
896
 
#else
897
 
int ha_pbms::rnd_init(bool )
898
 
#endif
899
 
{
900
 
        int err = 0;
901
 
        enter_();
902
 
        try_(a) {
903
 
                ha_open_tab->seqScanInit();
904
 
        }
905
 
        catch_(a) {
906
 
                ha_error = MSEngine::exceptionToResult(&self->myException, &ha_result);
907
 
                err = 1;
908
 
        }
909
 
        cont_(a);
910
 
        return_(err);
911
 
}
912
 
 
913
 
//-------
914
 
int ha_pbms::rnd_next(unsigned char *buf)
915
 
{
916
 
        int err = 0;
917
 
        enter_();
918
 
        try_(a) {
919
 
                if (!ha_open_tab->seqScanNext((char *) buf))
920
 
                        err = HA_ERR_END_OF_FILE;
921
 
        }
922
 
        catch_(a) {
923
 
                ha_error = MSEngine::exceptionToResult(&self->myException, &ha_result);
924
 
                err = 1;
925
 
        }
926
 
        cont_(a);
927
 
        return_(err);
928
 
}
929
 
 
930
 
//-------
931
 
void ha_pbms::position(const unsigned char *)
932
 
{
933
 
        ha_open_tab->seqScanPos((uint8_t *) ref);
934
 
}
935
 
 
936
 
//-------
937
 
int ha_pbms::rnd_pos(unsigned char * buf, unsigned char *pos)
938
 
{
939
 
        int err = 0;
940
 
        enter_();
941
 
        try_(a) {
942
 
                ha_open_tab->seqScanRead((uint8_t *) pos, (char *) buf);
943
 
        }
944
 
        catch_(a) {
945
 
                ha_error = MSEngine::exceptionToResult(&self->myException, &ha_result);
946
 
                err = 1;
947
 
        }
948
 
        cont_(a);
949
 
        return_(err);
950
 
}
951
 
 
952
 
//////////////////////////////
953
 
#ifdef DRIZZLED
954
 
int     ha_pbms::doInsertRecord(byte * buf)
955
 
#else
956
 
int ha_pbms::write_row(unsigned char * buf)
957
 
#endif
958
 
{
959
 
        int err = 0;
960
 
        enter_();
961
 
        try_(a) {
962
 
                ha_open_tab->insertRow((char *) buf);
963
 
        }
964
 
        catch_(a) {
965
 
                ha_error = MSEngine::exceptionToResult(&self->myException, &ha_result);
966
 
                err = 1;
967
 
        }
968
 
        cont_(a);
969
 
        return_(err);
970
 
}
971
 
 
972
 
#ifdef DRIZZLED
973
 
int     ha_pbms::doDeleteRecord(const byte * buf)
974
 
#else
975
 
int ha_pbms::delete_row(const  unsigned char * buf)
976
 
#endif
977
 
{
978
 
        int err = 0;
979
 
        enter_();
980
 
        try_(a) {
981
 
                ha_open_tab->deleteRow((char *) buf);
982
 
        }
983
 
        catch_(a) {
984
 
                ha_error = MSEngine::exceptionToResult(&self->myException, &ha_result);
985
 
                err = 1;
986
 
        }
987
 
        cont_(a);
988
 
        return_(err);
989
 
}
990
 
 
991
 
#ifdef DRIZZLED
992
 
int     ha_pbms::doUpdateRecord(const byte * old_data, byte * new_data)
993
 
#else
994
 
int ha_pbms::update_row(const unsigned char * old_data, unsigned char * new_data)
995
 
#endif
996
 
{
997
 
        int err = 0;
998
 
        enter_();
999
 
        try_(a) {
1000
 
                ha_open_tab->updateRow((char *) old_data, (char *) new_data);
1001
 
        }
1002
 
        catch_(a) {
1003
 
                ha_error = MSEngine::exceptionToResult(&self->myException, &ha_result);
1004
 
                err = 1;
1005
 
        }
1006
 
        cont_(a);
1007
 
        return_(err);
1008
 
}
1009
 
 
1010
 
int ha_pbms::info(uint )
1011
 
{
1012
 
        return 0;
1013
 
}
1014
 
 
1015
 
int ha_pbms::external_lock(THD *thd, int lock_type)
1016
 
{
1017
 
        CSThread        *self;
1018
 
        int                     err = 0;
1019
 
 
1020
 
        if ((ha_error = MSEngine::enterConnection(thd, &self, &ha_result, true)))
1021
 
                return 1;
1022
 
 
1023
 
        inner_();
1024
 
        try_(a) {
1025
 
                if (lock_type == F_UNLCK)
1026
 
                        ha_open_tab->unuse();
1027
 
                else
1028
 
                        ha_open_tab->use();
1029
 
        }
1030
 
        catch_(a) {
1031
 
                ha_error = MSEngine::exceptionToResult(&self->myException, &ha_result);
1032
 
                err = 1;
1033
 
        }
1034
 
        cont_(a);
1035
 
        return_(err);
1036
 
}
1037
 
 
1038
 
THR_LOCK_DATA **ha_pbms::store_lock(THD *, THR_LOCK_DATA **to, enum thr_lock_type lock_type)
1039
 
{
1040
 
        if (lock_type != TL_IGNORE && ha_lock.type == TL_UNLOCK)
1041
 
                ha_lock.type = lock_type;
1042
 
        *to++ = &ha_lock;
1043
 
        return to;
1044
 
}
1045
 
 
1046
 
 
1047
 
#ifdef DRIZZLED
1048
 
int PBMSStorageEngine::doCreateTable(Session&, Table&, const identifier::Table& , drizzled::message::Table& )
1049
 
{
1050
 
        /* You cannot create PBMS tables. */
1051
 
        return( HA_ERR_WRONG_COMMAND );
1052
 
}
1053
 
 
1054
 
int PBMSStorageEngine::doDropTable(Session &, const identifier::Table& )
1055
 
{
1056
 
        /* You cannot delete PBMS tables. */
1057
 
        return( 0 );
1058
 
}
1059
 
 
1060
 
int PBMSStorageEngine::doRenameTable(Session&, const identifier::Table &, const identifier::Table &)
1061
 
{
1062
 
        /* You cannot rename PBMS tables. */
1063
 
        return( HA_ERR_WRONG_COMMAND );
1064
 
}
1065
 
 
1066
 
#else // DRIZZLED
1067
 
 
1068
 
int ha_pbms::create(const char *table_name, TABLE *table, HA_CREATE_INFO *)
1069
 
{
1070
 
        bool isPBMS = (strcasecmp(table->s->db.str, "PBMS") == 0);
1071
 
        
1072
 
        if (PBMSSystemTables::isSystemTable(isPBMS, cs_last_name_of_path(table_name)))
1073
 
                return(0);
1074
 
                
1075
 
        /* Create only works for system tables. */
1076
 
        return( HA_ERR_WRONG_COMMAND );
1077
 
}
1078
 
#endif // DRIZZLED
1079
 
 
1080
 
bool ha_pbms::get_error_message(int , String *buf)
1081
 
{
1082
 
        if (!ha_result.mr_code)
1083
 
                return false;
1084
 
 
1085
 
        buf->copy(ha_result.mr_message, strlen(ha_result.mr_message), system_charset_info);
1086
 
        return true;
1087
 
}
1088
 
 
1089
 
 
1090
 
CSThread *pbms_getMySelf(THD *thd);
1091
 
void pbms_setMySelf(THD *thd, CSThread *self);
1092
 
#ifdef DRIZZLED
1093
 
CSThread *pbms_getMySelf(THD *thd) { return ((CSThread *) *thd->getEngineData(pbms_hton));}
1094
 
void pbms_setMySelf(THD *thd, CSThread *self) { *thd->getEngineData(pbms_hton) = (void *)self;}
1095
 
#else
1096
 
CSThread *pbms_getMySelf(THD *thd) { return ((CSThread *) *thd_ha_data(thd, pbms_hton));}
1097
 
void pbms_setMySelf(THD *thd, CSThread *self) { *thd_ha_data(thd, pbms_hton) = (void *)self;}
1098
 
#endif
1099
 
 
1100