4
/* Copyright (C) 2009 PrimeBase Technologies GmbH, Germany
6
* PrimeBase Media Stream for MySQL
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
* Created by Barry Leslie on 3/20/09.
25
#include "cslib/CSMd5.h"
31
* - TODO: If cl_deleteData() fails then the BLOB deletion must fail and be rescheduled to try again
33
* - TODO: Copying of BLOBs from one database to another needs to be handled. Look for copyBlob() and
34
* resetBlobHead(). There are 3 cases to handle depending on if the databases involved use
38
//===============================
40
class MSCloudInfo : public CSRefObject {
42
static uint32_t gMaxInfoRef;
43
static CSSyncSparseArray *gCloudInfo;
45
friend class MSCloudTable;
57
new_(gCloudInfo, CSSyncSparseArray(5));
61
static void shutDown()
65
gCloudInfo->release();
71
static MSCloudInfo *getCloudInfo(uint32_t in_cloudRefId)
78
info = (MSCloudInfo *) gCloudInfo->get(in_cloudRefId);
81
snprintf(msg, 80, "Cloud info with reference ID %"PRIu32" not found", in_cloudRefId);
82
CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, msg);
89
MSCloudInfo(uint32_t id, const char *server, const char *bucket, const char *publicKey, const char *privateKey );
92
uint32_t getCloudRefId() { return cloudRefId;}
94
const char *getServer();
96
const char *getBucket();
98
const char *getPublicKey();
100
const char *getPrivateKey();
102
CSString *getSignature(const char *key, const char *content_type, uint32_t *s3AuthorizationTime);
104
CSString *getDataURL(const char *key, int keep_alive);
106
void send(CSInputStream *input, const char *key, off64_t size);
108
void receive(CSOutputStream *output, const char *key);
110
void copy(MSCloudInfo *dst_cloud, const char *dst_key, const char *src_key);
112
void cDelete(const char *key);
114
CSVector *list(const char *key_prefix, uint32_t max = 0);
117
typedef struct CloudKey {
118
uint32_t creation_time;
119
uint32_t ref_index; // Just a sequence counter in case 2 blobs have the same creation time.
120
uint32_t cloud_ref; // A reference into the pbms.pbms_cloud table.
121
} CloudKeyRec, *CloudKeyPtr;
124
//===============================
125
class CloudObjectKey : public CSStringBuffer
127
uint32_t default_db_id;
130
CloudObjectKey(uint32_t id): CSStringBuffer(), default_db_id(id){ }
133
static const uint32_t base_key_size = 64; // enough space for <db_id>/<backup_id>/<creation_time>/<ref_index>
135
void setObjectKey(const char *object_key)
137
setLength(base_key_size + strlen(object_key) +1);
139
snprintf(getBuffer(0), length(), "%"PRIu32"/0/%s",default_db_id, object_key);
142
void setObjectKey(CloudKeyPtr key = NULL, uint32_t backup_id = 0, uint32_t db_id = 0)
144
if (!db_id) db_id = default_db_id;
145
setLength(base_key_size);
148
snprintf(getBuffer(0), length(), "%"PRIu32"/%"PRIu32"/%"PRIu32".%"PRIu32".%"PRIu32"", db_id, backup_id, key->cloud_ref, key->creation_time, key->ref_index);
150
snprintf(getBuffer(0), length(), "%"PRIu32"/%"PRIu32"s/", db_id, backup_id);
154
static void parseObjectKey(const char *object_key, CloudKeyPtr key, uint32_t *backup_id = NULL, uint32_t *db_id = NULL)
158
if (!backup_id) backup_id = &v1;
159
if (!db_id) db_id = &v1;
161
sscanf(object_key, "%"PRIu32"/%"PRIu32"/%"PRIu32".%"PRIu32".%"PRIu32"", db_id, backup_id, &(key->cloud_ref), &(key->creation_time), &(key->ref_index));
165
//===============================
167
class CloudDB: public CSRefObject {
170
static uint32_t gKeyIndex;
171
static CSMutex gCloudKeyLock;
173
uint32_t dfltCloudRefId;
175
uint32_t keep_alive; // The length of time a redirect URL will remain valid. In seconds.
176
uint32_t blob_recovery_no; // This is the backup number from which the recovery should be done.
180
MSBackupInfo *backupInfo;
181
MSCloudInfo *backupCloud;
183
static const uint32_t base_key_size = 64; // enough space for <db_id>/<backup_id>/<creation_time>/<ref_index>
186
CSStringBuffer *clObjectKey;
188
CloudDB(uint32_t db_id);
191
void cl_setDefaultCloudRef(uint32_t dflt) { dfltCloudRefId = dflt;}
192
uint32_t cl_getDefaultCloudRef() { return dfltCloudRefId;}
194
MSCloudInfo *cl_getCloudInfo(uint32_t cloudRefId = 0)
196
return MSCloudInfo::getCloudInfo((cloudRefId)?cloudRefId:dfltCloudRefId);
199
void cl_getNewKey(CloudKeyPtr key)
202
lock_(&gCloudKeyLock);
204
key->creation_time = time(NULL);
205
key->ref_index = gKeyIndex++;
206
key->cloud_ref = dfltCloudRefId;
208
unlock_(&gCloudKeyLock);
212
bool cl_mustRecoverBlobs() { return (blob_recovery_no != 0);}
214
void cl_setRecoveryNumber(const char *number)
216
blob_recovery_no = atol(number);
219
const char *cl_getRecoveryNumber()
221
static char number[20];
223
snprintf(number, 20, "%"PRIu32"", blob_recovery_no);
227
CSString *cl_getObjectKey(CloudKeyPtr key)
229
CloudObjectKey *objectKey;
232
new_(objectKey, CloudObjectKey(blob_db_id));
235
objectKey->setObjectKey(key);
237
CSString *str = CSString::newString(objectKey->getCString());
243
void cl_setKeepAlive(uint32_t keep_alive_arg) {keep_alive = keep_alive_arg;}
248
uint32_t cl_getNextBackupNumber(uint32_t cloud_ref = 0);
251
// setting backup_blob_no to -1 ensures that if the database is dropped no BLOBs will be deleted.
252
void cl_setCloudIsBackup(){ isBackup = true;}
253
void cl_setBackupInfo(MSBackupInfo *info){ backupInfo = info;}
254
MSBackupInfo *cl_getBackupInfo();
255
void cl_clearBackupInfo();
257
void cl_backupBLOB(CloudKeyPtr key);
258
void cl_restoreBLOB(CloudKeyPtr key, uint32_t backup_db_id);
260
void cl_putData( CloudKeyPtr key, CSInputStream *stream, off64_t size);
261
off64_t cl_getData(CloudKeyPtr key, char *data, off64_t size);
262
CSString *cl_getDataURL(CloudKeyPtr key);
263
void cl_deleteData(CloudKeyPtr key);
264
CSString *cl_getSignature(CloudKeyPtr key, CSString *content_type, uint32_t *s3AuthorizationTime);
268
#endif // __CLOUD_H__