~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

Fixed sql_builtin.cc.in... stupid generated files.

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