Drizzled Public API Documentation

api_ms.cc
00001 #ifdef NOT_USED_IN_ANY_THING
00002 
00003 /* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
00004  *
00005  * PrimeBase Media Stream for MySQL
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00020  *
00021  * Barry Leslie
00022  *
00023  * 2007-11-25
00024  *
00025  * H&G2JCtL
00026  *
00027  */
00028 
00029 #include "cslib/CSConfig.h"
00030 #include "cslib/CSGlobal.h"
00031 #include "cslib/CSLog.h"
00032 #include "cslib/CSStrUtil.h"
00033 #include "cslib/CSHTTPStream.h"
00034 #include "cslib/CSStream.h"
00035 
00036 #include "repository_ms.h"
00037 #include "open_table_ms.h"
00038 #include "mysql_ms.h"
00039 
00040 //-----------------------------------------------------------------------------------------------
00041 void PBMSGetError(void *v_bs_thread, PBMSResultPtr result)
00042 {
00043   CSThread *ms_thread = (CSThread*)v_bs_thread;
00044   
00045   ASSERT(ms_thread);
00046   memset(result, 0, sizeof(PBMSResultRec));
00047   
00048   result->mr_code =  ms_thread->myException.getErrorCode();
00049   cs_strcpy(MS_RESULT_MESSAGE_SIZE, result->mr_message,  ms_thread->myException.getMessage());
00050 }
00051 
00052 //-----------------------------------------------------------------------------------------------
00053 void *PBMSInitBlobStreamingThread(char *thread_name, PBMSResultPtr result)
00054 {
00055   CSThread *ms_thread =  new CSThread( NULL);
00056   
00057   if (!ms_thread) {
00058     memset(result, 0, sizeof(PBMSResultRec));
00059     result->mr_code = ENOMEM;
00060     cs_strcpy(MS_RESULT_MESSAGE_SIZE, result->mr_message, "CSThread::newThread() failed.");
00061     return NULL; 
00062   }
00063   
00064   ms_thread->pbms_api_owner = true;
00065   if (!CSThread::attach(ms_thread)) {
00066     memset(result, 0, sizeof(PBMSResultRec));
00067     result->mr_code =  ms_thread->myException.getErrorCode();
00068     cs_strcpy(MS_RESULT_MESSAGE_SIZE, result->mr_message,  ms_thread->myException.getMessage());
00069     ms_thread->release();
00070     ms_thread = NULL;
00071   } else
00072     ms_thread->threadName = CSString::newString(thread_name);
00073   
00074   return ms_thread;
00075 }
00076 
00077 
00078 //-----------------------------------------------------------------------------------------------
00079 void PBMSDeinitBlobStreamingThread(void *v_bs_thread)
00080 {
00081   CSThread *ms_thread = (CSThread*)v_bs_thread;
00082   
00083   ASSERT(ms_thread);
00084 
00085   CSThread::detach(ms_thread);
00086   // ms_thread->release(); Don't do this. Ownership of the thread is passed to the attach call so the thread is released when it is detached.
00087 }
00088 
00089 //-----------------------------------------------------------------------------------------------
00090 bool PBMSCreateBlob(PBMSBlobIDPtr blob_id, char *database_name, uint64_t size)
00091 {
00092   MSOpenTable *otab = NULL;
00093   CSString *iTableURI =  NULL;
00094   CSString *CSContenttype = NULL;
00095   bool done_ok = true;
00096 
00097   enter_();
00098 
00099   try_(a) {
00100     otab = MSTableList::getOpenTableForDB(MSDatabase::getDatabaseID(database_name, false));
00101     
00102     otab->createBlob(blob_id, size, NULL, 0);
00103   }
00104   
00105   catch_(a) {
00106     done_ok = false;
00107   }
00108   cont_(a);
00109 
00110   exit:
00111   if (otab)
00112     otab->returnToPool();
00113   
00114   if (CSContenttype)
00115     CSContenttype->release();
00116     
00117   if (iTableURI)
00118     iTableURI->release();
00119     
00120   return_(done_ok);
00121 }
00122 
00123 //-----------------------------------------------------------------------------------------------
00124 bool PBMSWriteBlob(PBMSBlobIDPtr blob_id, char *data, size_t size, size_t offset)
00125 {
00126   MSOpenTable *otab;
00127   MSRepoFile  *repo_file;
00128   bool done_ok = true;
00129 
00130   enter_();
00131 
00132   try_(a) {
00133     if (!(otab = MSTableList::getOpenTableForDB(blob_id->bi_db_id))) {
00134       char buffer[CS_EXC_MESSAGE_SIZE];
00135       char id_str[12];
00136       
00137       snprintf(id_str, 12, "%"PRIu32"", blob_id->bi_db_id);
00138 
00139       cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Unknown database id #  ");
00140       cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, id_str);
00141       CSException::throwException(CS_CONTEXT, MS_ERR_UNKNOWN_DB, buffer);
00142     }
00143     frompool_(otab);
00144     repo_file = otab->getDB()->getRepoFileFromPool( blob_id->bi_tab_id, false);
00145     frompool_(repo_file);
00146     // It is assumed that at this point the blob is a repository blob and so the 
00147     // blob_id->bi_blob_id is actually the repository blob offset. 
00148     repo_file->writeBlobChunk(blob_id, blob_id->bi_blob_id, offset, size, data);
00149     backtopool_(repo_file);
00150     backtopool_(otab);
00151 
00152   }
00153   catch_(a) {
00154     done_ok = false;
00155   }
00156   
00157   cont_(a);
00158     
00159   return_(done_ok);
00160 }
00161 
00162 //-----------------------------------------------------------------------------------------------
00163 bool PBMSReadBlob(PBMSBlobIDPtr blob_id, char *buffer, size_t *size, size_t offset)
00164 {
00165   MSOpenTable *otab;
00166   MSRepoFile  *repo_file;
00167   bool done_ok = true, is_repository_blob;
00168 
00169   enter_();
00170 
00171   is_repository_blob = (blob_id->bi_blob_type == MS_URL_TYPE_REPO);
00172   try_(a) {
00173     if (!(otab = MSTableList::getOpenTableByID(blob_id->bi_db_id, blob_id->bi_tab_id))) {
00174       char buffer[CS_EXC_MESSAGE_SIZE];
00175       char id_str[12];
00176       
00177   
00178       cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Unknown database: ID # ");
00179       snprintf(id_str, 12, "%"PRIu32"", blob_id->bi_db_id);
00180       cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, id_str);
00181       cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, " or table: ID #");
00182       snprintf(id_str, 12, "%"PRIu32"", blob_id->bi_tab_id);
00183       cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, id_str);
00184       CSException::throwException(CS_CONTEXT, MS_ERR_UNKNOWN_DB, buffer);
00185     }
00186     uint32_t repo_id;
00187     uint64_t rep_offset;
00188 
00189     
00190     frompool_(otab);
00191     if (is_repository_blob) {
00192       repo_id = blob_id->bi_tab_id;
00193       rep_offset = blob_id->bi_blob_id;
00194     } else {
00195       uint64_t blob_size;
00196       uint16_t header_size; 
00197       otab->getDBTable()->readBlobHandle(otab, blob_id->bi_blob_id, &(blob_id->bi_auth_code), &repo_id, &rep_offset, &blob_size, &header_size, true);
00198     }
00199     
00200     repo_file = otab->getDB()->getRepoFileFromPool( repo_id, false);
00201     frompool_(repo_file);
00202     *size = repo_file->readBlobChunk(blob_id, rep_offset, offset, *size, buffer);
00203     backtopool_(repo_file);
00204     backtopool_(otab);
00205 
00206   }
00207   catch_(a) {
00208     done_ok = false;
00209   }
00210   
00211   cont_(a);
00212     
00213   return_(done_ok);
00214 }
00215 
00216 //-----------------------------------------------------------------------------------------------
00217 bool PBMSIDToURL(PBMSBlobIDPtr blob_id, PBMSBlobURLPtr url)
00218 { 
00219   MSBlobURL ms_blob;
00220 
00221   ms_blob.bu_db_id = blob_id->bi_db_id;
00222   ms_blob.bu_blob_id = blob_id->bi_blob_id;
00223   ms_blob.bu_blob_ref_id = blob_id->bi_blob_ref_id;
00224   ms_blob.bu_tab_id = blob_id->bi_tab_id;
00225   ms_blob.bu_auth_code = blob_id->bi_auth_code;
00226   ms_blob.bu_type = blob_id->bi_blob_type;
00227   ms_blob.bu_blob_size = blob_id->bi_blob_size; 
00228   ms_blob.bu_server_id = ms_my_get_server_id();
00229   
00230   PBMSBlobURLTools::buildBlobURL(&ms_blob, url);
00231   return true;
00232 }
00233 
00234 //-----------------------------------------------------------------------------------------------
00235 bool PBMSURLToID(char *url, PBMSBlobIDPtr blob_id)
00236 { 
00237   MSBlobURL ms_blob;
00238   bool done_ok = true;
00239   enter_();
00240 
00241   try_(a) {
00242   
00243     if (!PBMSBlobURLTools::couldBeURL(url, &ms_blob)){
00244       char buffer[CS_EXC_MESSAGE_SIZE];
00245 
00246       cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Incorrect URL: ");
00247       cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, url);
00248       CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
00249     }
00250     
00251     blob_id->bi_db_id = ms_blob.bu_db_id;
00252     blob_id->bi_blob_id = ms_blob.bu_blob_id;
00253     blob_id->bi_blob_ref_id = ms_blob.bu_blob_ref_id;
00254     blob_id->bi_tab_id = ms_blob.bu_tab_id;
00255     blob_id->bi_auth_code = ms_blob.bu_auth_code;
00256     blob_id->bi_blob_type = ms_blob.bu_type;
00257     blob_id->bi_blob_size = ms_blob.bu_blob_size; 
00258     
00259   }
00260   catch_(a) {
00261     done_ok = false;
00262   }
00263   
00264   cont_(a);
00265   
00266   return_(done_ok);
00267 }
00268 
00269 
00270 #endif // NOT_USED_IN_ANY_THING