1
/* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
3
* PrimeBase Media Stream for MySQL
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.
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.
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
19
* Original author: Paul McCullagh
20
* Continued development: Barry Leslie
31
#include <drizzled/common.h>
32
#include <drizzled/session.h>
36
#include "cslib/CSConfig.h"
37
#include "cslib/CSGlobal.h"
38
#include "cslib/CSStrUtil.h"
39
#include "cslib/CSThread.h"
42
#define PBMS_API pbms_internal
46
#include "engine_ms.h"
47
#include "connection_handler_ms.h"
48
#include "open_table_ms.h"
49
#include "network_ms.h"
50
#include "transaction_ms.h"
59
extern CSThread *pbms_getMySelf(THD *thd);
60
extern void pbms_setMySelf(THD *thd, CSThread *self);
65
* ---------------------------------------------------------------
66
* ENGINE CALL-IN INTERFACE
69
static PBMS_API *StreamingEngines;
70
// If PBMS support is built directly into the mysql/drizzle handler code
71
// then calls from all other handlers are ignored.
72
static bool have_handler_support = false;
75
* ---------------------------------------------------------------
76
* ENGINE CALLBACK INTERFACE
79
static void ms_register_engine(PBMSEnginePtr engine)
81
if (engine->ms_internal)
82
have_handler_support = true;
85
static void ms_deregister_engine(PBMSEnginePtr engine)
90
static int ms_create_blob(bool internal, const char *db_name, const char *tab_name, char *blob, size_t blob_len, PBMSBlobURLPtr blob_url, PBMSResultPtr result)
92
if (have_handler_support && !internal) {
93
MSEngine::errorResult(CS_CONTEXT, MS_ERR_INVALID_OPERATION, "Invalid ms_create_blob() call", result);
97
return MSEngine::createBlob(db_name, tab_name, blob, blob_len, blob_url, result);
101
* ms_use_blob() may or may not alter the blob url depending on the type of URL and if the BLOB is in a
102
* different database or not. It may also add a BLOB reference to the BLOB table log if the BLOB was from
103
* a different table or no table was specified when the BLOB was uploaded.
105
* There is no need to undo this function because it will be undone automaticly if the BLOB is not retained.
107
static int ms_retain_blob(bool internal, const char *db_name, const char *tab_name, PBMSBlobURLPtr ret_blob_url, char *blob_url, unsigned short col_index, PBMSResultPtr result)
109
if (have_handler_support && !internal) {
110
cs_strcpy(PBMS_BLOB_URL_SIZE, ret_blob_url->bu_data, blob_url); // This should have already been converted.
114
return MSEngine::referenceBlob(db_name, tab_name, ret_blob_url, blob_url, col_index, result);
117
static int ms_release_blob(bool internal, const char *db_name, const char *tab_name, char *blob_url, PBMSResultPtr result)
120
if (have_handler_support && !internal)
123
return MSEngine::dereferenceBlob(db_name, tab_name, blob_url, result);
126
static int ms_drop_table(bool internal, const char *db_name, const char *tab_name, PBMSResultPtr result)
128
if (have_handler_support && !internal)
131
return MSEngine::dropTable(db_name, tab_name, result);
134
static int ms_rename_table(bool internal, const char * db_name, const char *from_table, const char *to_db, const char *to_table, PBMSResultPtr result)
136
if (have_handler_support && !internal)
139
return MSEngine::renameTable(db_name, from_table, to_db, to_table, result);
142
static void ms_completed(bool internal, bool ok)
144
if (have_handler_support && !internal)
147
MSEngine::callCompleted(ok);
150
PBMSCallbacksRec engine_callbacks = {
153
ms_deregister_engine,
162
// =============================
163
int MSEngine::startUp(PBMSResultPtr result)
167
StreamingEngines = new PBMS_API();
168
err = StreamingEngines->PBMSStartup(&engine_callbacks, result);
170
delete StreamingEngines;
171
else { // Register the PBMS enabled engines the startup before PBMS
172
PBMSSharedMemoryPtr sh_mem = StreamingEngines->sharedMemory;
173
PBMSEnginePtr engine;
175
for (int i=0; i<sh_mem->sm_list_len; i++) {
176
if ((engine = sh_mem->sm_engine_list[i]))
177
ms_register_engine(engine);
183
void MSEngine::shutDown()
185
StreamingEngines->PBMSShutdown();
187
delete StreamingEngines;
190
const PBMSEnginePtr MSEngine::getEngineInfoAt(int indx)
192
PBMSSharedMemoryPtr sh_mem = StreamingEngines->sharedMemory;
193
PBMSEnginePtr engine = NULL;
196
for (int i=0; i<sh_mem->sm_list_len; i++) {
197
if ((engine = sh_mem->sm_engine_list[i])) {
205
return (const PBMSEnginePtr)NULL;
210
bool MSEngine::try_createBlob(CSThread *self, const char *db_name, const char *tab_name, char *blob, size_t blob_len, PBMSBlobURLPtr blob_url)
212
volatile bool rtc = true;
216
CSInputStream *i_stream = NULL;
218
otab = openTable(db_name, tab_name, true);
221
if (!otab->getDB()->isRecovering()) {
222
i_stream = CSMemoryInputStream::newStream((unsigned char *)blob, blob_len);
223
otab->createBlob(blob_url, blob_len, NULL, 0, i_stream);
225
CSException::throwException(CS_CONTEXT, MS_ERR_RECOVERY_IN_PROGRESS, "Cannot create BLOBs during repository recovery.");
236
int32_t MSEngine::createBlob(const char *db_name, const char *tab_name, char *blob, size_t blob_len, PBMSBlobURLPtr blob_url, PBMSResultPtr result)
242
if ((err = enterConnectionNoThd(&self, result)))
246
if (try_createBlob(self, db_name, tab_name, blob, blob_len, blob_url))
247
err = exceptionToResult(&self->myException, result);
253
bool MSEngine::try_referenceBlob(CSThread *self, const char *db_name, const char *tab_name, PBMSBlobURLPtr ret_blob_url, char *blob_url, uint16_t col_index)
255
volatile bool rtc = true;
260
if (! PBMSBlobURLTools::couldBeURL(blob_url, &blob)){
261
char buffer[CS_EXC_MESSAGE_SIZE];
263
cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Incorrect URL: ");
264
cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, blob_url);
265
CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
268
otab = openTable(db_name, tab_name, true);
271
otab->useBlob(blob.bu_type, blob.bu_db_id, blob.bu_tab_id, blob.bu_blob_id, blob.bu_auth_code, col_index, blob.bu_blob_size, blob.bu_blob_ref_id, ret_blob_url);
282
int32_t MSEngine::referenceBlob(const char *db_name, const char *tab_name, PBMSBlobURLPtr ret_blob_url, char *blob_url, uint16_t col_index, PBMSResultPtr result)
288
if ((err = enterConnectionNoThd(&self, result)))
292
if (try_referenceBlob(self, db_name, tab_name, ret_blob_url, blob_url, col_index))
293
err = exceptionToResult(&self->myException, result);
300
bool MSEngine::try_dereferenceBlob(CSThread *self, const char *db_name, const char *tab_name, char *blob_url)
302
volatile bool rtc = true;
307
if (! PBMSBlobURLTools::couldBeURL(blob_url, &blob)){
308
char buffer[CS_EXC_MESSAGE_SIZE];
310
cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Incorrect URL: ");
311
cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, blob_url);
312
CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
315
otab = openTable(db_name, tab_name, true);
317
if (!otab->getDB()->isRecovering()) {
318
if (otab->getTableID() == blob.bu_tab_id)
319
otab->releaseReference(blob.bu_blob_id, blob.bu_blob_ref_id);
321
char buffer[CS_EXC_MESSAGE_SIZE];
323
cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Incorrect table ID: ");
324
cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, blob_url);
325
CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
329
char buffer[CS_EXC_MESSAGE_SIZE];
331
cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Incorrect URL: ");
332
cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, blob_url);
333
CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
344
int32_t MSEngine::dereferenceBlob(const char *db_name, const char *tab_name, char *blob_url, PBMSResultPtr result)
349
if ((err = enterConnectionNoThd(&self, result)))
353
if (try_dereferenceBlob(self, db_name, tab_name, blob_url))
354
err = exceptionToResult(&self->myException, result);
359
bool MSEngine::try_dropDatabase(CSThread *self, const char *db_name)
361
volatile bool rtc = true;
363
MSDatabase::dropDatabase(db_name);
372
int32_t MSEngine::dropDatabase(const char *db_name, PBMSResultPtr result)
377
if ((err = enterConnectionNoThd(&self, result)))
382
if (try_dropDatabase(self, db_name))
383
err = exceptionToResult(&self->myException, result);
389
typedef struct UnDoInfo {
391
CSString *udo_toDatabaseName;
392
CSString *udo_fromDatabaseName;
393
CSString *udo_OldName;
394
CSString *udo_NewName;
395
} UnDoInfoRec, *UnDoInfoPtr;
398
bool MSEngine::try_dropTable(CSThread *self, const char *db_name, const char *tab_name)
400
volatile bool rtc = true;
406
MSOpenTablePool *tab_pool;
408
UnDoInfoPtr undo_info = NULL;
410
otab = openTable(db_name, tab_name, false);
415
// If we are recovering do not delete the table.
416
// It is normal for MySQL recovery scripts to delete any table they aare about to
417
// recover and then recreate it. If this is done after the repository has been recovered
418
// then this would delete all the recovered BLOBs in the table.
419
if (otab->getDB()->isRecovering()) {
420
otab->returnToPool();
426
// Before dropping the table the table ref file is renamed so that
427
// it is out of the way incase a new table is created before the
428
// old one is cleaned up.
430
old_path = otab->getDBTable()->getTableFile();
433
new_path = otab->getDBTable()->getTableFile(tab_name, true);
435
// Rearrage the object stack to pop the otab object
443
tab = otab->getDBTable();
447
tab_pool = MSTableList::lockTablePoolForDeletion(otab);
450
if (old_path->exists())
451
old_path->move(RETAIN(new_path));
452
tab->myDatabase->dropTable(RETAIN(tab));
454
/* Add the table to the temp delete list if we are not recovering... */
455
tab->prepareToDelete();
457
backtopool_(tab_pool); // The will unlock and close the table pool freeing all tables in it.
458
pop_(tab); // Returning the pool will have released this. (YUK!)
463
undo_info = (UnDoInfoPtr) cs_malloc(sizeof(UnDoInfoRec));
465
undo_info->udo_WasRename = false;
466
self->myInfo = undo_info;
477
int32_t MSEngine::dropTable(const char *db_name, const char *tab_name, PBMSResultPtr result)
482
if ((err = enterConnectionNoThd(&self, result)))
486
if (try_dropTable(self, db_name, tab_name))
487
err = exceptionToResult(&self->myException, result);
495
static void completeDeleteTable(UnDoInfoPtr info, bool ok)
497
// TO DO: figure out a way to undo the delete.
500
CSException::throwException(CS_CONTEXT, MS_ERR_NOT_IMPLEMENTED, "Cannot undo delete table.");
504
bool MSEngine::renameTable(const char *from_db_name, const char *from_table, const char *to_db_name, const char *to_table)
509
MSOpenTablePool *tab_pool;
514
if (strcmp(to_db_name, from_db_name) != 0) {
515
CSException::throwException(CS_CONTEXT, MS_ERR_NOT_IMPLEMENTED, "Cannot rename tables containing BLOBs across databases (yet). Sorry!");
518
otab = openTable(from_db_name, from_table, false);
524
if (otab->getDB()->isRecovering())
525
CSException::throwException(CS_CONTEXT, MS_ERR_RECOVERY_IN_PROGRESS, "Cannot rename tables during repository recovery.");
527
from_path = otab->getDBTable()->getTableFile();
530
to_path = otab->getDBTable()->getTableFile(to_table, false);
532
// Rearrage the object stack to pop the otab object
540
otab->openForReading();
541
tab = otab->getDBTable();
546
tab_pool = MSTableList::lockTablePoolForDeletion(otab);
549
from_path->move(RETAIN(to_path));
550
tab->myDatabase->renameTable(tab, to_table);
552
backtopool_(tab_pool); // The will unlock and close the table pool freeing all tables in it.
553
pop_(tab); // Returning the pool will have released this. (YUK!)
561
bool MSEngine::try_renameTable(CSThread *self, const char *from_db_name, const char *from_table, const char *to_db_name, const char *to_table)
563
volatile bool rtc = true;
565
UnDoInfoPtr undo_info = (UnDoInfoPtr) cs_malloc(sizeof(UnDoInfoRec));
566
push_ptr_(undo_info);
568
undo_info->udo_WasRename = true;
569
if (renameTable(from_db_name, from_table, to_db_name, to_table)) {
570
undo_info->udo_fromDatabaseName = CSString::newString(from_db_name);
571
push_(undo_info->udo_fromDatabaseName);
573
undo_info->udo_toDatabaseName = CSString::newString(to_db_name);
574
push_(undo_info->udo_toDatabaseName);
576
undo_info->udo_OldName = CSString::newString(from_table);
577
push_(undo_info->udo_OldName);
579
undo_info->udo_NewName = CSString::newString(to_table);
581
pop_(undo_info->udo_OldName);
582
pop_(undo_info->udo_toDatabaseName);
583
pop_(undo_info->udo_fromDatabaseName);
585
undo_info->udo_fromDatabaseName = undo_info->udo_toDatabaseName = undo_info->udo_OldName = undo_info->udo_NewName = NULL;
587
self->myInfo = undo_info;
597
int32_t MSEngine::renameTable(const char *from_db_name, const char *from_table, const char *to_db_name, const char *to_table, PBMSResultPtr result)
602
if ((err = enterConnectionNoThd(&self, result)))
606
if (try_renameTable(self, from_db_name, from_table, to_db_name, to_table))
607
err = exceptionToResult(&self->myException, result);
615
void MSEngine::completeRenameTable(UnDoInfoPtr info, bool ok)
617
// Swap the paths around here to revers the rename.
618
CSString *from_db_name= info->udo_toDatabaseName;
619
CSString *to_db_name= info->udo_fromDatabaseName;
620
CSString *from_table= info->udo_NewName;
621
CSString *to_table= info->udo_OldName;
632
renameTable(from_db_name->getCString(), from_table->getCString(), to_db_name->getCString(), to_table->getCString());
635
release_(to_db_name);
636
release_(from_table);
637
release_(from_db_name);
643
static bool try_CompleteTransaction(CSThread *self, bool ok)
645
volatile bool rtc = true;
648
MSTransactionManager::commit();
649
else if (self->myIsAutoCommit)
650
MSTransactionManager::rollback();
652
MSTransactionManager::rollbackToPosition(self->myStartStmt); // Rollback the last logical statement.
662
void MSEngine::callCompleted(bool ok)
665
PBMSResultRec result;
667
if (enterConnectionNoThd(&self, &result))
671
UnDoInfoPtr info = (UnDoInfoPtr) self->myInfo;
672
if (info->udo_WasRename)
673
completeRenameTable(info, ok);
675
completeDeleteTable(info, ok);
679
} else if (self->myTID && (self->myIsAutoCommit || !ok)) {
681
if (try_CompleteTransaction(self, ok)) {
682
self->logException();
687
self->myStartStmt = self->myStmtCount;
691
MSOpenTable *MSEngine::openTable(const char *db_name, const char *tab_name, bool create)
693
MSOpenTable *otab = NULL;
694
uint32_t db_id, tab_id;
697
if ( MSDatabase::convertTableAndDatabaseToIDs(db_name, tab_name, &db_id, &tab_id, create))
698
otab = MSTableList::getOpenTableByID(db_id, tab_id);
704
bool MSEngine::couldBeURL(const char *blob_url, size_t length)
707
return PBMSBlobURLTools::couldBeURL(blob_url, length, &blob);
711
int MSEngine::exceptionToResult(CSException *e, PBMSResultPtr result)
713
const char *context, *trace;
715
result->mr_code = e->getErrorCode();
716
cs_strcpy(MS_RESULT_MESSAGE_SIZE, result->mr_message, e->getMessage());
717
context = e->getContext();
718
trace = e->getStackTrace();
719
if (context && *context) {
720
cs_strcpy(MS_RESULT_STACK_SIZE, result->mr_stack, context);
722
cs_strcat(MS_RESULT_STACK_SIZE, result->mr_stack, "\n");
725
*result->mr_stack = 0;
727
cs_strcat(MS_RESULT_STACK_SIZE, result->mr_stack, trace);
728
return MS_ERR_ENGINE;
732
int MSEngine::errorResult(const char *func, const char *file, int line, int err, const char *message, PBMSResultPtr result)
736
e.initException(func, file, line, err, message);
737
return exceptionToResult(&e, result);
741
int MSEngine::osErrorResult(const char *func, const char *file, int line, int err, PBMSResultPtr result)
745
e.initOSError(func, file, line, err);
746
return MSEngine::exceptionToResult(&e, result);
750
int MSEngine::enterConnection(THD *thd, CSThread **r_self, PBMSResultPtr result, bool doCreate)
752
CSThread *self = NULL;
755
// In drizzle there is no 1:1 relationship between pthreads and sessions
756
// so we must always get it from the session handle NOT the current pthread.
757
self = CSThread::getSelf();
761
if (!(self = pbms_getMySelf(thd))) {
763
return MS_ERR_NOT_FOUND;
765
if (!(self = CSThread::newCSThread()))
766
return osErrorResult(CS_CONTEXT, ENOMEM, result);
767
if (!CSThread::attach(self))
768
return MSEngine::exceptionToResult(&self->myException, result);
769
pbms_setMySelf(thd, self);
771
if (!CSThread::setSelf(self))
772
return MSEngine::exceptionToResult(&self->myException, result);
776
return MS_ERR_NOT_FOUND;
778
if (!(self = CSThread::newCSThread()))
779
return osErrorResult(CS_CONTEXT, ENOMEM, result);
780
if (!CSThread::attach(self))
781
return MSEngine::exceptionToResult(&self->myException, result);
790
int MSEngine::enterConnectionNoThd(CSThread **r_self, PBMSResultPtr result)
792
return enterConnection(current_thd, r_self, result, true);
796
void MSEngine::exitConnection()
798
THD *thd = (THD *) current_thd;
801
self = CSThread::getSelf();
802
if (self && self->pbms_api_owner)
807
CSThread::setSelf(NULL);
809
self = CSThread::getSelf();
810
CSThread::detach(self);
815
void MSEngine::closeConnection(THD* thd)
819
self = CSThread::getSelf();
820
if (self && self->pbms_api_owner)
824
if ((self = pbms_getMySelf(thd))) {
825
pbms_setMySelf(thd, NULL);
826
CSThread::setSelf(self);
827
CSThread::detach(self);
831
self = CSThread::getSelf();
832
CSThread::detach(self);