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.
30
#ifndef __OPENTABLE_MS_H__
31
#define __OPENTABLE_MS_H__
33
#include "cslib/CSDefs.h"
34
#include "cslib/CSFile.h"
36
#include "engine_ms.h"
38
#include "database_ms.h"
40
class MSOpenTablePool;
42
// Keep the value of MS_OT_BUFFER_SIZE less than 0XFFFF to avoid compiler warnings.
44
// From looking at transmition speed using tcpdump it looks like 16K is a goor buffer size.
45
// It is very important that it be larger than the TCP frame size of about 1.5K on the
46
// machine I was testing on. There did not eam to be much to gain by having a buffer size
49
// I wonder if performance could be gained by having 2 buffers and 2 threads per send
50
// so that one buffer could be being sent while the other was being filled.
52
//#define MS_OT_BUFFER_SIZE ((uint16_t)(512))
53
#define MS_OT_BUFFER_SIZE ((uint16_t)(16384))
55
//#define MS_OT_BUFFER_SIZE ((uint16_t)(0XFFF0))
56
#define MS_OT_BUFFER_SIZE ((uint16_t)(16384))
59
#define MS_UB_SAME_TAB 0
60
#define MS_UB_NEW_HANDLE 1
61
#define MS_UB_NEW_BLOB 2
62
#define MS_UB_RETAINED 3
63
#define MS_UB_NEW_RETAINED 4
66
typedef struct MSUsedBlob {
68
uint16_t ub_col_index;
70
uint64_t ub_repo_offset;
72
uint16_t ub_head_size;
73
uint64_t ub_blob_size;
74
uint64_t ub_blob_ref_id;
76
uint32_t ub_auth_code;
77
char ub_blob_url[PBMS_BLOB_URL_SIZE];
78
} MSUsedBlobRec, *MSUsedBlobPtr;
81
class MSOpenTable : public CSRefObject, public CSPooled {
85
MSOpenTable *nextTable;
86
MSOpenTablePool *myPool;
90
MSRepository *myWriteRepo;
91
MSRepoFile *myWriteRepoFile;
93
MSTempLogFile *myTempLogFile;
95
char myOTBuffer[MS_OT_BUFFER_SIZE];
98
virtual ~MSOpenTable();
100
virtual void returnToPool();
104
void createBlob(PBMSBlobURLPtr bh, uint64_t size, char *metadata, uint16_t metadata_size, CSInputStream *stream, CloudKeyPtr cloud_key = NULL, Md5Digest *checksum = NULL);
105
void createBlob(PBMSBlobIDPtr blob_id, uint64_t blob_size, char *metadata, uint16_t metadata_size);
106
void sendRepoBlob(uint64_t blob_id, uint64_t req_offset, uint64_t req_size, uint32_t auth_code, bool info_only, CSHTTPOutputStream *stream);
107
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);
109
void releaseReference(uint64_t blob_id, uint64_t blob_ref_id);
110
void freeReference(uint64_t blob_id, uint64_t blob_ref_id);
111
void commitReference(uint64_t blob_id, uint64_t blob_ref_id);
112
void checkBlob(CSStringBuffer *buffer, uint64_t blob_id, uint32_t auth_code, uint32_t temp_log_id, uint32_t temp_log_offset);
113
bool deleteReferences(uint32_t temp_log_id, uint32_t temp_log_offset, bool *must_quit);
115
void openForReading();
116
void closeForWriting();
118
uint32_t getTableID();
119
MSTable *getDBTable();
122
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);
123
void formatBlobURL(PBMSBlobURLPtr blob_url, uint64_t blob_id, uint32_t auth_code, uint64_t blob_size, uint64_t blob_ref_id);
124
void formatRepoURL(PBMSBlobURLPtr blob_url, uint32_t log_id, uint64_t log_offset, uint32_t auth_code, uint64_t blob_size);
126
/* Make this object linkable: */
127
virtual CSObject *getNextLink() { return iNextLink; }
128
virtual CSObject *getPrevLink() { return iPrevLink; }
129
virtual void setNextLink(CSObject *link) { iNextLink = link; }
130
virtual void setPrevLink(CSObject *link) { iPrevLink = link; }
136
// uint32_t iUseSize;
137
// uint32_t iUseCount;
138
// MSUsedBlobPtr iUsedBlobs;
141
void openForWriting();
144
static MSOpenTable *newOpenTable(MSOpenTablePool *pool);
147
class MSOpenTablePool : public CSRefObject, public CSPooled {
149
uint32_t myPoolTableID; /* Non-zero if in the ID list. */
150
bool isRemovingTP; /* Set to true if the table pool is being removed. */
151
MSTable *myPoolTable;
152
MSDatabase *myPoolDB;
155
virtual ~MSOpenTablePool();
157
MSOpenTable *getPoolTable(); /* Returns NULL if there is no table in the pool. */
158
void returnOpenTable(MSOpenTable *otab);
160
void addOpenTable(MSOpenTable *otab);
161
void removeOpenTable(MSOpenTable *otab);
163
void removeOpenTablesNotInUse();
165
virtual void returnToPool();
171
uint32_t getSize() { return iPoolTables.getSize(); }
174
MSOpenTable *iTablePool; /* A list of tables currently not in use. */
175
CSLinkedList iPoolTables; /* A list of all tables in this pool */
178
static MSOpenTablePool *newPool(uint32_t db_id, uint32_t tab_id);
181
class MSTableList : public CSObject {
186
static void startUp();
187
static void shutDown();
189
static void debug(MSOpenTable *otab);
191
static MSOpenTable *getOpenTableByID(uint32_t db_id, uint32_t tab_id);
192
static MSOpenTable *getOpenTableForDB(uint32_t db_id);
193
static void releaseTable(MSOpenTable *otab);
195
static bool removeTablePoolIfEmpty(MSOpenTablePool *pool);
196
static void removeTablePool(MSOpenTablePool *pool);
197
static void removeTablePool(MSOpenTable *otab);
198
static void removeDatabaseTables(MSDatabase *database);
200
static MSOpenTablePool *lockTablePoolForDeletion(uint32_t db_id, uint32_t tab_id, CSString *db_name, CSString *tab_name);
201
static MSOpenTablePool *lockTablePoolForDeletion(MSTable *table);
202
static MSOpenTablePool *lockTablePoolForDeletion(CSString *table_url);
203
static MSOpenTablePool *lockTablePoolForDeletion(MSOpenTable *otab);
204
static void unlockTablePool(MSOpenTablePool *pool);
207
static CSSyncOrderedList *gPoolListByID;