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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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"
41
#include "engine_ms.h"
42
#include "connection_handler_ms.h"
43
#include "open_table_ms.h"
44
#include "network_ms.h"
45
#include "transaction_ms.h"
54
extern CSThread *pbms_getMySelf(THD *thd);
55
extern void pbms_setMySelf(THD *thd, CSThread *self);
60
* ---------------------------------------------------------------
61
* ENGINE CALL-IN INTERFACE
64
static PBMS_API *StreamingEngines;
65
// If PBMS support is built directly into the mysql/drizzle handler code
66
// then calls from all other handlers are ignored.
67
static bool have_handler_support = false;
70
* ---------------------------------------------------------------
71
* ENGINE CALLBACK INTERFACE
74
static void ms_register_engine(PBMSEnginePtr engine)
76
if (engine->ms_internal)
77
have_handler_support = true;
80
static void ms_deregister_engine(PBMSEnginePtr engine)
85
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)
87
if (have_handler_support && !internal) {
88
errorResult(CS_CONTEXT, MS_ERR_INVALID_OPERATION, "Invalid ms_create_blob() call", result);
92
return MSEngine::createBlob(db_name, tab_name, blob, blob_len, blob_url, result);
96
* ms_use_blob() may or may not alter the blob url depending on the type of URL and if the BLOB is in a
97
* different database or not. It may also add a BLOB reference to the BLOB table log if the BLOB was from
98
* a different table or no table was specified when the BLOB was uploaded.
100
* There is no need to undo this function because it will be undone automaticly if the BLOB is not retained.
102
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)
104
if (have_handler_support && !internal) {
105
cs_strcpy(PBMS_BLOB_URL_SIZE, ret_blob_url->bu_data, blob_url); // This should have already been converted.
109
return MSEngine::referenceBlob(db_name, tab_name, ret_blob_url, blob_url, col_index, result);
112
static int ms_release_blob(bool internal, const char *db_name, const char *tab_name, char *blob_url, PBMSResultPtr result)
115
if (have_handler_support && !internal)
118
return MSEngine::dereferenceBlob(db_name, tab_name, blob_url, result);
121
static int ms_drop_table(bool internal, const char *db_name, const char *tab_name, PBMSResultPtr result)
123
if (have_handler_support && !internal)
126
return MSEngine::dropTable(db_name, tab_name, result);
129
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)
131
if (have_handler_support && !internal)
134
return MSEngine::renameTable(db_name, from_table, to_db, to_table, result);
137
static void ms_completed(bool internal, bool ok)
139
if (have_handler_support && !internal)
142
MSEngine::callCompleted(ok);
145
PBMSCallbacksRec engine_callbacks = {
148
ms_deregister_engine,
157
// =============================
158
int MSEngine::startUp(PBMSResultPtr result)
162
StreamingEngines = new PBMS_API();
163
err = StreamingEngines->PBMSStartup(&engine_callbacks, result);
165
delete StreamingEngines;
166
else { // Register the PBMS enabled engines the startup before PBMS
167
PBMSSharedMemoryPtr sh_mem = StreamingEngines->sharedMemory;
168
PBMSEnginePtr engine;
170
for (int i=0; i<sh_mem->sm_list_len; i++) {
171
if ((engine = sh_mem->sm_engine_list[i]))
172
ms_register_engine(engine);
178
void MSEngine::shutDown()
180
StreamingEngines->PBMSShutdown();
182
delete StreamingEngines;
185
const PBMSEnginePtr MSEngine::getEngineInfoAt(int indx)
187
PBMSSharedMemoryPtr sh_mem = StreamingEngines->sharedMemory;
188
PBMSEnginePtr engine = NULL;
191
for (int i=0; i<sh_mem->sm_list_len; i++) {
192
if ((engine = sh_mem->sm_engine_list[i])) {
205
int32_t MSEngine::createBlob(const char *db_name, const char *tab_name, char *blob, size_t blob_len, PBMSBlobURLPtr blob_url, PBMSResultPtr result)
211
CSInputStream *i_stream = NULL;
213
CLOBBER_PROTECT(err);
215
if ((err = enterConnectionNoThd(&self, result)))
220
otab = openTable(db_name, tab_name, true);
223
if (!otab->getDB()->isRecovering()) {
224
i_stream = CSMemoryInputStream::newStream((unsigned char *)blob, blob_len);
225
otab->createBlob(blob_url, blob_len, NULL, 0, i_stream);
227
CSException::throwException(CS_CONTEXT, MS_ERR_RECOVERY_IN_PROGRESS, "Cannot create BLOBs during repository recovery.");
232
err = exceptionToResult(&self->myException, result);
239
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)
247
CLOBBER_PROTECT(err);
249
if ((err = enterConnectionNoThd(&self, result)))
255
if (! PBMSBlobURLTools::couldBeURL(blob_url, &blob)){
256
char buffer[CS_EXC_MESSAGE_SIZE];
258
cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Incorrect URL: ");
259
cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, blob_url);
260
CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
263
otab = openTable(db_name, tab_name, true);
266
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);
271
err = exceptionToResult(&self->myException, result);
278
int32_t MSEngine::dereferenceBlob(const char *db_name, const char *tab_name, char *blob_url, PBMSResultPtr result)
285
CLOBBER_PROTECT(err);
287
if ((err = enterConnectionNoThd(&self, result)))
292
if (! PBMSBlobURLTools::couldBeURL(blob_url, &blob)){
293
char buffer[CS_EXC_MESSAGE_SIZE];
295
cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Incorrect URL: ");
296
cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, blob_url);
297
CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
300
otab = openTable(db_name, tab_name, true);
302
if (!otab->getDB()->isRecovering()) {
303
if (otab->getTableID() == blob.bu_tab_id)
304
otab->releaseReference(blob.bu_blob_id, blob.bu_blob_ref_id);
306
char buffer[CS_EXC_MESSAGE_SIZE];
308
cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Incorrect table ID: ");
309
cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, blob_url);
310
CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
314
char buffer[CS_EXC_MESSAGE_SIZE];
316
cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Incorrect URL: ");
317
cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, blob_url);
318
CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
324
err = exceptionToResult(&self->myException, result);
330
int32_t MSEngine::dropDatabase(const char *db_name, PBMSResultPtr result)
335
CLOBBER_PROTECT(err);
337
if ((err = enterConnectionNoThd(&self, result)))
342
MSDatabase::dropDatabase(db_name);
345
err = exceptionToResult(&self->myException, result);
351
typedef struct UnDoInfo {
353
CSString *udo_toDatabaseName;
354
CSString *udo_fromDatabaseName;
355
CSString *udo_OldName;
356
CSString *udo_NewName;
357
} UnDoInfoRec, *UnDoInfoPtr;
359
int32_t MSEngine::dropTable(const char *db_name, const char *tab_name, PBMSResultPtr result)
364
CLOBBER_PROTECT(err);
366
if ((err = enterConnectionNoThd(&self, result)))
375
MSOpenTablePool *tab_pool;
377
UnDoInfoPtr undo_info = NULL;
379
otab = openTable(db_name, tab_name, false);
383
// If we are recovering do not delete the table.
384
// It is normal for MySQL recovery scripts to delete any table they aare about to
385
// recover and then recreate it. If this is done after the repository has been recovered
386
// then this would delete all the recovered BLOBs in the table.
387
if (otab->getDB()->isRecovering()) {
388
otab->returnToPool();
394
// Before dropping the table the table ref file is renamed so that
395
// it is out of the way incase a new table is created before the
396
// old one is cleaned up.
398
old_path = otab->getDBTable()->getTableFile();
401
new_path = otab->getDBTable()->getTableFile(tab_name, true);
403
// Rearrage the object stack to pop the otab object
411
tab = otab->getDBTable();
415
tab_pool = MSTableList::lockTablePoolForDeletion(otab);
418
if (old_path->exists())
419
old_path->move(new_path);
420
tab->myDatabase->dropTable(RETAIN(tab));
422
/* Add the table to the temp delete list if we are not recovering... */
423
tab->prepareToDelete();
425
backtopool_(tab_pool); // The will unlock and close the table pool freeing all tables in it.
426
pop_(tab); // Returning the pool will have released this. (YUK!)
431
undo_info = (UnDoInfoPtr) cs_malloc(sizeof(UnDoInfoRec));
433
undo_info->udo_WasRename = false;
434
self->myInfo = undo_info;
441
err = exceptionToResult(&self->myException, result);
450
static void completeDeleteTable(UnDoInfoPtr info, bool ok)
452
// TO DO: figure out a way to undo the delete.
455
CSException::throwException(CS_CONTEXT, MS_ERR_NOT_IMPLEMENTED, "Cannot undo delete table.");
459
bool MSEngine::renameTable(const char *from_db_name, const char *from_table, const char *to_db_name, const char *to_table)
464
MSOpenTablePool *tab_pool;
469
if (strcmp(to_db_name, from_db_name) != 0) {
470
CSException::throwException(CS_CONTEXT, MS_ERR_NOT_IMPLEMENTED, "Cannot rename tables containing BLOBs across databases (yet). Sorry!");
473
otab = openTable(from_db_name, from_table, false);
479
if (otab->getDB()->isRecovering())
480
CSException::throwException(CS_CONTEXT, MS_ERR_RECOVERY_IN_PROGRESS, "Cannot rename tables during repository recovery.");
482
from_path = otab->getDBTable()->getTableFile();
485
to_path = otab->getDBTable()->getTableFile(to_table, false);
487
// Rearrage the object stack to pop the otab object
495
otab->openForReading();
496
tab = otab->getDBTable();
501
tab_pool = MSTableList::lockTablePoolForDeletion(otab);
504
from_path->move(to_path);
505
tab->myDatabase->renameTable(tab, to_table);
507
backtopool_(tab_pool); // The will unlock and close the table pool freeing all tables in it.
508
pop_(tab); // Returning the pool will have released this. (YUK!)
516
int32_t MSEngine::renameTable(const char *from_db_name, const char *from_table, const char *to_db_name, const char *to_table, PBMSResultPtr result)
521
CLOBBER_PROTECT(err);
523
if ((err = enterConnectionNoThd(&self, result)))
528
UnDoInfoPtr undo_info = (UnDoInfoPtr) cs_malloc(sizeof(UnDoInfoRec));
529
push_ptr_(undo_info);
531
undo_info->udo_WasRename = true;
532
if (renameTable(from_db_name, from_table, to_db_name, to_table)) {
533
undo_info->udo_fromDatabaseName = CSString::newString(from_db_name);
534
push_(undo_info->udo_fromDatabaseName);
536
undo_info->udo_toDatabaseName = CSString::newString(to_db_name);
537
push_(undo_info->udo_toDatabaseName);
539
undo_info->udo_OldName = CSString::newString(from_table);
540
push_(undo_info->udo_OldName);
542
undo_info->udo_NewName = CSString::newString(to_table);
544
pop_(undo_info->udo_OldName);
545
pop_(undo_info->udo_toDatabaseName);
546
pop_(undo_info->udo_fromDatabaseName);
548
undo_info->udo_fromDatabaseName = undo_info->udo_toDatabaseName = undo_info->udo_OldName = undo_info->udo_NewName = NULL;
550
self->myInfo = undo_info;
554
err = exceptionToResult(&self->myException, result);
563
void MSEngine::completeRenameTable(UnDoInfoPtr info, bool ok)
565
// Swap the paths around here to revers the rename.
566
CSString *from_db_name= info->udo_toDatabaseName;
567
CSString *to_db_name= info->udo_fromDatabaseName;
568
CSString *from_table= info->udo_NewName;
569
CSString *to_table= info->udo_OldName;
580
renameTable(from_db_name->getCString(), from_table->getCString(), to_db_name->getCString(), to_table->getCString());
583
release_(to_db_name);
584
release_(from_table);
585
release_(from_db_name);
590
void MSEngine::callCompleted(bool ok)
593
PBMSResultRec result;
595
if (enterConnectionNoThd(&self, &result))
599
UnDoInfoPtr info = (UnDoInfoPtr) self->myInfo;
600
if (info->udo_WasRename)
601
completeRenameTable(info, ok);
603
completeDeleteTable(info, ok);
607
} else if (self->myTID && (self->myIsAutoCommit || !ok)) {
611
MSTransactionManager::commit();
612
else if (self->myIsAutoCommit)
613
MSTransactionManager::rollback();
615
MSTransactionManager::rollbackToPosition(self->myStartStmt); // Rollback the last logical statement.
618
self->logException();
624
self->myStartStmt = self->myStmtCount;
628
MSOpenTable *MSEngine::openTable(const char *db_name, const char *tab_name, bool create)
630
MSOpenTable *otab = NULL;
631
uint32_t db_id, tab_id;
634
if ( MSDatabase::convertTableAndDatabaseToIDs(db_name, tab_name, &db_id, &tab_id, create))
635
otab = MSTableList::getOpenTableByID(db_id, tab_id);
641
bool MSEngine::couldBeURL(const char *blob_url, size_t length)
644
return PBMSBlobURLTools::couldBeURL(blob_url, length, &blob);
648
int MSEngine::exceptionToResult(CSException *e, PBMSResultPtr result)
650
const char *context, *trace;
652
result->mr_code = e->getErrorCode();
653
cs_strcpy(MS_RESULT_MESSAGE_SIZE, result->mr_message, e->getMessage());
654
context = e->getContext();
655
trace = e->getStackTrace();
656
if (context && *context) {
657
cs_strcpy(MS_RESULT_STACK_SIZE, result->mr_stack, context);
659
cs_strcat(MS_RESULT_STACK_SIZE, result->mr_stack, "\n");
662
*result->mr_stack = 0;
664
cs_strcat(MS_RESULT_STACK_SIZE, result->mr_stack, trace);
665
return MS_ERR_ENGINE;
669
int MSEngine::errorResult(const char *func, const char *file, int line, int err, const char *message, PBMSResultPtr result)
673
e.initException(func, file, line, err, message);
674
return exceptionToResult(&e, result);
678
int MSEngine::osErrorResult(const char *func, const char *file, int line, int err, PBMSResultPtr result)
682
e.initOSError(func, file, line, err);
683
return MSEngine::exceptionToResult(&e, result);
687
int MSEngine::enterConnection(THD *thd, CSThread **r_self, PBMSResultPtr result, bool doCreate)
689
CSThread *self = NULL;
692
// In drizzle there is no 1:1 relationship between pthreads and sessions
693
// so we must always get it from the session handle NOT the current pthread.
694
self = CSThread::getSelf();
698
if (!(self = pbms_getMySelf(thd))) {
700
return MS_ERR_NOT_FOUND;
702
if (!(self = CSThread::newCSThread()))
703
return osErrorResult(CS_CONTEXT, ENOMEM, result);
704
if (!CSThread::attach(self))
705
return MSEngine::exceptionToResult(&self->myException, result);
706
pbms_setMySelf(thd, self);
708
if (!CSThread::setSelf(self))
709
return MSEngine::exceptionToResult(&self->myException, result);
713
return MS_ERR_NOT_FOUND;
715
if (!(self = CSThread::newCSThread()))
716
return osErrorResult(CS_CONTEXT, ENOMEM, result);
717
if (!CSThread::attach(self))
718
return MSEngine::exceptionToResult(&self->myException, result);
727
int MSEngine::enterConnectionNoThd(CSThread **r_self, PBMSResultPtr result)
729
return enterConnection(current_thd, r_self, result, true);
733
void MSEngine::exitConnection()
735
THD *thd = (THD *) current_thd;
738
self = CSThread::getSelf();
739
if (self && self->pbms_api_owner)
744
CSThread::setSelf(NULL);
746
self = CSThread::getSelf();
747
CSThread::detach(self);
752
void MSEngine::closeConnection(THD* thd)
756
self = CSThread::getSelf();
757
if (self && self->pbms_api_owner)
761
if ((self = pbms_getMySelf(thd))) {
762
pbms_setMySelf(thd, NULL);
763
CSThread::setSelf(self);
764
CSThread::detach(self);
768
self = CSThread::getSelf();
769
CSThread::detach(self);