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
26
* Media Stream Tables.
31
#ifndef __OPENTABLE_MS_H__
32
#define __OPENTABLE_MS_H__
34
#include "cslib/CSDefs.h"
35
#include "cslib/CSFile.h"
37
#include "engine_ms.h"
39
#include "database_ms.h"
41
class MSOpenTablePool;
43
// Keep the value of MS_OT_BUFFER_SIZE less than 0XFFFF to avoid compiler warnings.
45
// From looking at transmition speed using tcpdump it looks like 16K is a goor buffer size.
46
// It is very important that it be larger than the TCP frame size of about 1.5K on the
47
// machine I was testing on. There did not eam to be much to gain by having a buffer size
50
// I wonder if performance could be gained by having 2 buffers and 2 threads per send
51
// so that one buffer could be being sent while the other was being filled.
53
//#define MS_OT_BUFFER_SIZE ((uint16_t)(512))
54
#define MS_OT_BUFFER_SIZE ((uint16_t)(16384))
56
//#define MS_OT_BUFFER_SIZE ((uint16_t)(0XFFF0))
57
#define MS_OT_BUFFER_SIZE ((uint16_t)(16384))
60
#define MS_UB_SAME_TAB 0
61
#define MS_UB_NEW_HANDLE 1
62
#define MS_UB_NEW_BLOB 2
63
#define MS_UB_RETAINED 3
64
#define MS_UB_NEW_RETAINED 4
67
typedef struct MSUsedBlob {
69
uint16_t ub_col_index;
71
uint64_t ub_repo_offset;
73
uint16_t ub_head_size;
74
uint64_t ub_blob_size;
75
uint64_t ub_blob_ref_id;
77
uint32_t ub_auth_code;
78
char ub_blob_url[PBMS_BLOB_URL_SIZE];
79
} MSUsedBlobRec, *MSUsedBlobPtr;
82
class MSOpenTable : public CSRefObject, public CSPooled {
86
MSOpenTable *nextTable;
87
MSOpenTablePool *myPool;
91
MSRepository *myWriteRepo;
92
MSRepoFile *myWriteRepoFile;
94
MSTempLogFile *myTempLogFile;
96
char myOTBuffer[MS_OT_BUFFER_SIZE];
99
virtual ~MSOpenTable();
101
virtual void returnToPool();
105
void createBlob(PBMSBlobURLPtr bh, uint64_t size, char *metadata, uint16_t metadata_size, CSInputStream *stream, CloudKeyPtr cloud_key = NULL, Md5Digest *checksum = NULL);
106
void createBlob(PBMSBlobIDPtr blob_id, uint64_t blob_size, char *metadata, uint16_t metadata_size);
107
void sendRepoBlob(uint64_t blob_id, uint64_t req_offset, uint64_t req_size, uint32_t auth_code, bool info_only, CSHTTPOutputStream *stream);
108
void useBlob(int type, uint32_t db_id, uint32_t tab_id, uint64_t blob_id, uint32_t auth_code, uint16_t col_index, uint64_t blob_size, uint64_t blob_ref_id, PBMSBlobURLPtr ret_blob_url);
110
void releaseReference(uint64_t blob_id, uint64_t blob_ref_id);
111
void freeReference(uint64_t blob_id, uint64_t blob_ref_id);
112
void commitReference(uint64_t blob_id, uint64_t blob_ref_id);
113
void checkBlob(CSStringBuffer *buffer, uint64_t blob_id, uint32_t auth_code, uint32_t temp_log_id, uint32_t temp_log_offset);
114
bool deleteReferences(uint32_t temp_log_id, uint32_t temp_log_offset, bool *must_quit);
116
void openForReading();
117
void closeForWriting();
119
bool haveTable() { return (myPool != NULL); }
120
uint32_t getTableID();
121
MSTable *getDBTable();
124
void formatBlobURL(PBMSBlobURLPtr blob_url, uint64_t blob_id, uint32_t auth_code, uint64_t blob_size, uint32_t tab_id, uint64_t blob_ref_id);
125
void formatBlobURL(PBMSBlobURLPtr blob_url, uint64_t blob_id, uint32_t auth_code, uint64_t blob_size, uint64_t blob_ref_id);
126
void formatRepoURL(PBMSBlobURLPtr blob_url, uint32_t log_id, uint64_t log_offset, uint32_t auth_code, uint64_t blob_size);
128
/* Make this object linkable: */
129
virtual CSObject *getNextLink() { return iNextLink; }
130
virtual CSObject *getPrevLink() { return iPrevLink; }
131
virtual void setNextLink(CSObject *link) { iNextLink = link; }
132
virtual void setPrevLink(CSObject *link) { iPrevLink = link; }
138
// uint32_t iUseSize;
139
// uint32_t iUseCount;
140
// MSUsedBlobPtr iUsedBlobs;
143
void openForWriting();
146
static MSOpenTable *newOpenTable(MSOpenTablePool *pool);
149
class MSOpenTablePool : public CSRefObject, public CSPooled {
151
uint32_t myPoolTableID; /* Non-zero if in the ID list. */
152
bool isRemovingTP; /* Set to true if the table pool is being removed. */
153
MSTable *myPoolTable;
154
MSDatabase *myPoolDB;
157
virtual ~MSOpenTablePool();
159
MSOpenTable *getPoolTable(); /* Returns NULL if there is no table in the pool. */
160
void returnOpenTable(MSOpenTable *otab);
162
void addOpenTable(MSOpenTable *otab);
163
void removeOpenTable(MSOpenTable *otab);
165
void removeOpenTablesNotInUse();
167
virtual void returnToPool();
173
uint32_t getSize() { return iPoolTables.getSize(); }
176
MSOpenTable *iTablePool; /* A list of tables currently not in use. */
177
CSLinkedList iPoolTables; /* A list of all tables in this pool */
180
static MSOpenTablePool *newPool(uint32_t db_id, uint32_t tab_id);
183
class MSTableList : public CSObject {
188
static void startUp();
189
static void shutDown();
191
static void debug(MSOpenTable *otab);
193
static MSOpenTable *getOpenTableByID(uint32_t db_id, uint32_t tab_id);
194
static MSOpenTable *getOpenTableForDB(uint32_t db_id);
195
static void releaseTable(MSOpenTable *otab);
197
static bool removeTablePoolIfEmpty(MSOpenTablePool *pool);
198
static void removeTablePool(MSOpenTablePool *pool);
199
static void removeTablePool(MSOpenTable *otab);
200
static void removeDatabaseTables(MSDatabase *database);
202
static MSOpenTablePool *lockTablePoolForDeletion(uint32_t db_id, uint32_t tab_id, CSString *db_name, CSString *tab_name);
203
static MSOpenTablePool *lockTablePoolForDeletion(MSTable *table);
204
static MSOpenTablePool *lockTablePoolForDeletion(CSString *table_url);
205
static MSOpenTablePool *lockTablePoolForDeletion(MSOpenTable *otab);
206
static void unlockTablePool(MSOpenTablePool *pool);
209
static CSSyncOrderedList *gPoolListByID;