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
32
DROP TABLE IF EXISTS pbms_repository;
33
CREATE TABLE pbms_repository (
34
Repository_id INT COMMENT 'The reppository file number',
35
Repo_blob_offset BIGINT COMMENT 'The offset of the BLOB in the repository file',
36
Blob_size BIGINT COMMENT 'The size of the BLOB in bytes',
37
Head_size SMALLINT UNSIGNED COMMENT 'The size of the BLOB header - preceeds the BLOB data',
38
Access_code INT COMMENT 'The 4-byte authorisation code required to access the BLOB - part of the BLOB URL',
39
Creation_time TIMESTAMP COMMENT 'The time the BLOB was created',
40
Last_ref_time TIMESTAMP COMMENT 'The last time the BLOB was referenced',
41
Last_access_time TIMESTAMP COMMENT 'The last time the BLOB was accessed (read)',
42
Content_type CHAR(128) COMMENT 'The content type of the BLOB - returned by HTTP GET calls',
43
Blob_data LONGBLOB COMMENT 'The data of this BLOB'
46
PRIMARY KEY (Repository_id, Repo_blob_offset)
48
DROP TABLE IF EXISTS pbms_reference;
49
CREATE TABLE pbms_reference (
50
Table_name CHAR(64) COMMENT 'The name of the referencing table',
51
Blob_id BIGINT COMMENT 'The BLOB reference number - part of the BLOB URL',
52
Column_name CHAR(64) COMMENT 'The column name of the referencing field',
53
Row_condition VARCHAR(255) COMMENT 'This condition identifies the row in the table',
54
Blob_url VARCHAR(200) COMMENT 'The BLOB URL for HTTP GET access',
55
Repository_id INT COMMENT 'The repository file number of the BLOB',
56
Repo_blob_offset BIGINT COMMENT 'The offset in the repository file',
57
Blob_size BIGINT COMMENT 'The size of the BLOB in bytes',
58
Deletion_time TIMESTAMP COMMENT 'The time the BLOB was deleted',
59
Remove_in INT COMMENT 'The number of seconds before the reference/BLOB is removed perminently',
60
Temp_log_id INT COMMENT 'Temporary log number of the referencing deletion entry',
61
Temp_log_offset BIGINT COMMENT 'Temporary log offset of the referencing deletion entry'
64
PRIMARY KEY (Table_name, Blob_id, Column_name, Condition)
68
#ifndef __SYSTEMTABLE_MS_H__
69
#define __SYSTEMTABLE_MS_H__
71
#include "cslib/CSMutex.h"
74
#include "repository_ms.h"
78
using namespace drizzled;
80
int pbms_discover_system_tables(handlerton *hton, THD* thd, const char *db, const char *name, uchar **frmblob, size_t *frmlen);
83
class PBMSSystemTables
88
static int getSystemTableInfo(const char *name, drizzled::message::Table &table);
89
static void getSystemTableNames(bool isPBMS, std::set<std::string> &set_of_names);
92
static bool isSystemTable(bool isPBMS, const char *name);
94
static void transferSystemTables(MSDatabase *dst_db, MSDatabase *src_db);
95
static void loadSystemTables(MSDatabase *db);
96
static void removeSystemTables(CSString *path);
98
static CSStringBuffer *dumpSystemTables(MSDatabase *db);
99
static void restoreSystemTables(MSDatabase *db, const char *data, size_t size);
101
static void systemTablesStartUp();
102
static void systemTableShutDown();
105
static bool try_loadSystemTables(CSThread *self, int i, MSDatabase *db);
108
class MSSystemTableShare;
114
#define GET_FIELD(table, column_index) table->getField(column_index)
115
#define GET_TABLE_FIELDS(table) table->getFields()
117
#define GET_FIELD(table, column_index) table->field[column_index]
118
#define GET_TABLE_FIELDS(table) table->field
122
class MSOpenSystemTable : public CSRefObject {
124
MSSystemTableShare *myShare;
127
MSOpenSystemTable(MSSystemTableShare *share, TABLE *table);
128
virtual ~MSOpenSystemTable();
131
* The getFieldValue() methods access fields in a server record.
132
* They assumes the caller knows what it is doing and does no error checking.
134
inline void getFieldValue(const char *row, uint16_t column_index, String *value)
136
Field *assumed_str_field = GET_FIELD(mySQLTable, column_index);
137
unsigned char *old_ptr = assumed_str_field->ptr;
141
assumed_str_field->ptr = (unsigned char *)row + assumed_str_field->offset(mySQLTable->getInsertRecord());
142
assumed_str_field->setReadSet();
144
assumed_str_field->ptr = (unsigned char *)row + assumed_str_field->offset(mySQLTable->record[0]);
145
assumed_str_field->table->use_all_columns();
147
value = assumed_str_field->val_str_internal(value);
149
assumed_str_field->ptr = old_ptr;
152
inline void getFieldValue(const char *row, uint16_t column_index, uint64_t *value)
154
Field *assumed_int_field = GET_FIELD(mySQLTable, column_index);
155
unsigned char *old_ptr = assumed_int_field->ptr;
159
assumed_int_field->ptr = (unsigned char *)row + assumed_int_field->offset(mySQLTable->getInsertRecord());
160
assumed_int_field->setReadSet();
162
assumed_int_field->ptr = (unsigned char *)row + assumed_int_field->offset(mySQLTable->record[0]);
163
assumed_int_field->table->use_all_columns();
165
*value = assumed_int_field->val_int();
167
assumed_int_field->ptr = old_ptr;
170
inline void getFieldValue(const char *row, uint16_t column_index, uint32_t *value)
173
getFieldValue(row, column_index, &v64);
174
*value = (uint32_t) v64;
177
virtual void use() { }
178
virtual void unuse() { }
179
virtual void backupSeqScanInit() { }
180
virtual void seqScanInit() { }
181
virtual bool seqScanNext(char *buf) { UNUSED(buf); return false; }
182
virtual int getRefLen() { return 0; }
183
virtual void seqScanPos(unsigned char *pos) { UNUSED(pos);}
184
virtual void seqScanRead(unsigned char *pos , char *buf) {UNUSED(pos);UNUSED(buf); }
185
virtual void insertRow(char *buf) {UNUSED(buf); }
186
virtual void deleteRow(char *buf) {UNUSED(buf); }
187
virtual void updateRow(char *old_data, char *new_data) {UNUSED(old_data);UNUSED(new_data); }
189
virtual void index_init(uint8_t idx) { }
190
virtual void index_end() { }
191
virtual bool index_read(char *buf, const char *key,
192
uint key_len, enum ha_rkey_function find_flag){ return false;}
193
virtual bool index_read_idx(char *buf, uint idx, const char *key,
194
uint key_len, enum ha_rkey_function find_flag){ return false;}
195
virtual bool index_next(char *buf){ return false;}
196
virtual bool index_prev(char *buf){ return false;}
197
virtual bool index_first(char *buf){ return false;}
198
virtual bool index_last(char *buf){ return false;}
199
virtual bool index_read_last(char *buf, const char *key, uint key_len){ return false;}
202
static void setNotNullInRecord(Field *field, char *record);
203
static void setNullInRecord(Field *field, char *record);
208
typedef struct MSRefData {
209
uint32_t rd_ref_count;
212
uint64_t rd_blob_ref_id;
213
uint32_t rd_temp_log_id;
214
uint32_t rd_temp_log_offset;
215
uint16_t rd_col_index;
216
} MSRefDataRec, *MSRefDataPtr;
218
class MSRepositoryTable : public MSOpenSystemTable {
220
MSRepositoryTable(MSSystemTableShare *share, TABLE *table);
221
virtual ~MSRepositoryTable();
224
virtual void unuse();
225
virtual void seqScanInit();
226
virtual bool seqScanNext(char *buf);
227
virtual int getRefLen();
228
virtual void seqScanPos(unsigned char *pos);
229
virtual void seqScanRead(uint32_t repo, uint64_t offset, char *buf);
230
virtual void seqScanRead(unsigned char *pos, char *buf);
232
friend class MSReferenceTable;
233
friend class MSBlobDataTable;
234
friend class MSMetaDataTable;
235
friend class MSBlobAliasTable;
236
friend class MSDumpTable;
240
uint64_t iRepoCurrentOffset;
241
uint64_t iRepoOffset;
242
uint64_t iRepoFileSize;
243
CSDaemon *iCompactor;
244
MSRepoFile *iRepoFile;
245
CSStringBuffer *iBlobBuffer;
247
virtual bool returnRecord(char *buf);
248
virtual bool returnSubRecord(char *buf);
249
virtual bool returnRow(MSBlobHeadPtr blob, char *buf);
250
virtual bool resetScan(bool positioned, uint32_t iRepoIndex = 0);
253
class MSBlobDataTable : public MSRepositoryTable {
255
MSBlobDataTable(MSSystemTableShare *share, TABLE *table);
259
virtual bool returnRow(MSBlobHeadPtr blob, char *buf);
262
// The main purpose of this class if for internal use when
263
// building the alias index.
264
class MSBlobAliasTable : public MSRepositoryTable {
266
MSBlobAliasTable(MSSystemTableShare *share, TABLE *table):MSRepositoryTable(share, table){}
269
virtual bool returnRow(MSBlobHeadPtr blob, char *buf);
273
class MSReferenceTable : public MSRepositoryTable {
275
MSReferenceTable(MSSystemTableShare *share, TABLE *table);
276
virtual ~MSReferenceTable();
281
void seqScanPos(unsigned char *pos);
282
virtual void seqScanRead(uint32_t repo, uint64_t offset, char *buf) { return MSRepositoryTable::seqScanRead(repo, offset, buf);}
283
void seqScanRead(unsigned char *pos, char *buf);
284
bool seqScanNext(char *buf);
287
MSRefDataPtr iRefDataList;
289
* I need my own copy of the current repository index and offset
290
* to ensure it referess to the same blob as the reference data position.
292
uint32_t iRefCurrentIndex;
293
uint64_t iRefCurrentOffset;
294
uint32_t iRefCurrentDataUsed;
295
uint32_t iRefCurrentDataPos;
296
uint32_t iRefDataSize;
297
uint32_t iRefDataUsed;
298
uint32_t iRefDataPos;
299
uint32_t iRefAuthCode;
300
uint64_t iRefBlobSize;
301
uint32_t iRefBlobRepo;
302
uint64_t iRefBlobOffset;
303
MSOpenTable *iRefOpenTable;
304
MSTempLogFile *iRefTempLog;
306
virtual bool returnRecord(char *buf);
307
virtual bool returnSubRecord(char *buf);
308
virtual bool returnRow(MSBlobHeadPtr blob, char *buf) { return MSRepositoryTable::returnRow(blob, buf);}
309
virtual void returnRow(MSRefDataPtr ref_data, char *buf);
310
virtual bool resetScan(bool positioned, uint32_t iRepoIndex = 0);
313
class MSMetaDataTable : public MSRepositoryTable {
315
MSMetaDataTable(MSSystemTableShare *share, TABLE *table);
316
virtual ~MSMetaDataTable();
322
void seqScanPos(unsigned char *pos);
323
virtual void seqScanRead(uint32_t repo, uint64_t offset, char *buf) { return MSRepositoryTable::seqScanRead(repo, offset, buf);}
324
void seqScanRead(unsigned char *pos, char *buf);
325
bool seqScanNext(char *buf);
326
void insertRow(char *buf);
327
void deleteRow(char *buf);
328
void updateRow(char *old_data, char *new_data);
330
#ifdef HAVE_ALIAS_SUPPORT
331
bool matchAlias(uint32_t repo_id, uint64_t offset, const char *alias);
334
friend class MSSysMeta;
337
CSStringBuffer *iMetData;
338
uint32_t iMetCurrentBlobRepo;
339
uint64_t iMetCurrentBlobOffset;
340
uint32_t iMetCurrentDataPos;
341
uint32_t iMetCurrentDataSize;
342
uint32_t iMetDataPos;
343
uint32_t iMetDataSize;
344
uint32_t iMetBlobRepo;
345
uint64_t iMetBlobOffset;
346
uint8_t iMetState[20];
349
bool nextRecord(char **name, char **value);
352
virtual bool returnRecord(char *buf);
353
virtual bool returnSubRecord(char *buf);
354
virtual bool returnRow(MSBlobHeadPtr blob, char *buf) { return MSRepositoryTable::returnRow(blob, buf);}
355
virtual void returnRow(char *name, char *value, char *buf);
356
virtual bool resetScan(bool positioned, uint32_t index = 0) {bool have_data= false; return resetScan(positioned, &have_data, index);}
357
virtual bool resetScan(bool positioned, bool *have_data, uint32_t iRepoIndex = 0);
359
static MSMetaDataTable *newMSMetaDataTable(MSDatabase *db);
362
class MSSystemTableShare : public CSRefObject {
364
CSString *myTablePath;
366
MSDatabase *mySysDatabase;
368
MSSystemTableShare();
369
~MSSystemTableShare();
371
/* Make this object sortable: */
372
virtual CSObject *getKey();
373
virtual int compareKey(CSObject *);
379
static CSSyncSortedList *gSystemTableList;
381
// Close all open system tables for the database being dropped.
382
static void removeDatabaseSystemTables(MSDatabase *doomed_db);
383
static void startUp();
384
static void shutDown();
386
static MSOpenSystemTable *openSystemTable(const char *table_path, TABLE *table);
387
static void releaseSystemTable(MSOpenSystemTable *tab);
389
static MSSystemTableShare *newTableShare(CSString *table_path);