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
30
#include "CSStrUtil.h"
31
#include "CSHTTPStream.h"
34
#include "Repository_ms.h"
35
#include "OpenTable_ms.h"
39
//-----------------------------------------------------------------------------------------------
40
void PBMSGetError(void *v_bs_thread, PBMSResultPtr result)
42
CSThread *ms_thread = (CSThread*)v_bs_thread;
45
memset(result, 0, sizeof(PBMSResultRec));
47
result->mr_code = ms_thread->myException.getErrorCode();
48
cs_strcpy(MS_RESULT_MESSAGE_SIZE, result->mr_message, ms_thread->myException.getMessage());
51
//-----------------------------------------------------------------------------------------------
52
void *PBMSInitBlobStreamingThread(char *thread_name, PBMSResultPtr result)
54
CSThread *ms_thread = new CSThread( NULL);
57
memset(result, 0, sizeof(PBMSResultRec));
58
result->mr_code = ENOMEM;
59
cs_strcpy(MS_RESULT_MESSAGE_SIZE, result->mr_message, "CSThread::newThread() failed.");
63
ms_thread->pbms_api_owner = true;
64
if (!CSThread::attach(ms_thread)) {
65
memset(result, 0, sizeof(PBMSResultRec));
66
result->mr_code = ms_thread->myException.getErrorCode();
67
cs_strcpy(MS_RESULT_MESSAGE_SIZE, result->mr_message, ms_thread->myException.getMessage());
71
ms_thread->threadName = CSString::newString(thread_name);
77
//-----------------------------------------------------------------------------------------------
78
void PBMSDeinitBlobStreamingThread(void *v_bs_thread)
80
CSThread *ms_thread = (CSThread*)v_bs_thread;
84
CSThread::detach(ms_thread);
85
// 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.
88
//-----------------------------------------------------------------------------------------------
89
bool PBMSCreateBlob(PBMSBlobIDPtr blob_id, char *database_name, u_int64_t size)
91
MSOpenTable *otab = NULL;
92
CSString *iTableURI = NULL;
93
CSString *CSContenttype = NULL;
99
otab = MSTableList::getOpenTableForDB(MSDatabase::getDatabaseID(database_name, false));
101
otab->createBlob(blob_id, size, NULL, 0);
111
otab->returnToPool();
114
CSContenttype->release();
117
iTableURI->release();
122
//-----------------------------------------------------------------------------------------------
123
bool PBMSWriteBlob(PBMSBlobIDPtr blob_id, char *data, size_t size, size_t offset)
126
MSRepoFile *repo_file;
132
if (!(otab = MSTableList::getOpenTableForDB(blob_id->bi_db_id))) {
133
char buffer[CS_EXC_MESSAGE_SIZE];
136
snprintf(id_str, 12, "%"PRIu32"", blob_id->bi_db_id);
138
cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Unknown database id # ");
139
cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, id_str);
140
CSException::throwException(CS_CONTEXT, MS_ERR_UNKNOWN_DB, buffer);
143
repo_file = otab->getDB()->getRepoFileFromPool( blob_id->bi_tab_id, false);
144
frompool_(repo_file);
145
// It is assumed that at this point the blob is a repository blob and so the
146
// blob_id->bi_blob_id is actually the repository blob offset.
147
repo_file->writeBlobChunk(blob_id, blob_id->bi_blob_id, offset, size, data);
148
backtopool_(repo_file);
161
//-----------------------------------------------------------------------------------------------
162
bool PBMSReadBlob(PBMSBlobIDPtr blob_id, char *buffer, size_t *size, size_t offset)
165
MSRepoFile *repo_file;
166
bool done_ok = true, is_repository_blob;
170
is_repository_blob = (blob_id->bi_blob_type == MS_URL_TYPE_REPO);
172
if (!(otab = MSTableList::getOpenTableByID(blob_id->bi_db_id, blob_id->bi_tab_id))) {
173
char buffer[CS_EXC_MESSAGE_SIZE];
177
cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Unknown database: ID # ");
178
snprintf(id_str, 12, "%"PRIu32"", blob_id->bi_db_id);
179
cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, id_str);
180
cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, " or table: ID #");
181
snprintf(id_str, 12, "%"PRIu32"", blob_id->bi_tab_id);
182
cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, id_str);
183
CSException::throwException(CS_CONTEXT, MS_ERR_UNKNOWN_DB, buffer);
190
if (is_repository_blob) {
191
repo_id = blob_id->bi_tab_id;
192
rep_offset = blob_id->bi_blob_id;
195
uint16_t header_size;
196
otab->getDBTable()->readBlobHandle(otab, blob_id->bi_blob_id, &(blob_id->bi_auth_code), &repo_id, &rep_offset, &blob_size, &header_size, true);
199
repo_file = otab->getDB()->getRepoFileFromPool( repo_id, false);
200
frompool_(repo_file);
201
*size = repo_file->readBlobChunk(blob_id, rep_offset, offset, *size, buffer);
202
backtopool_(repo_file);
215
//-----------------------------------------------------------------------------------------------
216
bool PBMSIDToURL(PBMSBlobIDPtr blob_id, char *url)
220
ms_blob.bu_db_id = blob_id->bi_db_id;
221
ms_blob.bu_blob_id = blob_id->bi_blob_id;
222
ms_blob.bu_blob_ref_id = blob_id->bi_blob_ref_id;
223
ms_blob.bu_tab_id = blob_id->bi_tab_id;
224
ms_blob.bu_auth_code = blob_id->bi_auth_code;
225
ms_blob.bu_type = blob_id->bi_blob_type;
226
ms_blob.bu_blob_size = blob_id->bi_blob_size;
227
ms_blob.bu_server_id = ms_my_get_server_id();
229
ms_build_blob_url(&ms_blob, url);
233
//-----------------------------------------------------------------------------------------------
234
bool PBMSURLToID(char *url, PBMSBlobIDPtr blob_id)
242
if (!ms_parse_blob_url(&ms_blob, url)){
243
char buffer[CS_EXC_MESSAGE_SIZE];
245
cs_strcpy(CS_EXC_MESSAGE_SIZE, buffer, "Incorrect URL: ");
246
cs_strcat(CS_EXC_MESSAGE_SIZE, buffer, url);
247
CSException::throwException(CS_CONTEXT, MS_ERR_INCORRECT_URL, buffer);
250
blob_id->bi_db_id = ms_blob.bu_db_id;
251
blob_id->bi_blob_id = ms_blob.bu_blob_id;
252
blob_id->bi_blob_ref_id = ms_blob.bu_blob_ref_id;
253
blob_id->bi_tab_id = ms_blob.bu_tab_id;
254
blob_id->bi_auth_code = ms_blob.bu_auth_code;
255
blob_id->bi_blob_type = ms_blob.bu_type;
256
blob_id->bi_blob_size = ms_blob.bu_blob_size;