~pbms-core/pbms/5.11-beta

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/* Copyright (c) 2009 PrimeBase Technologies GmbH, Germany
 *
 * PrimeBase Media Stream for MySQL
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *  Created by Barry Leslie on 3/20/09.
 *
 */
 
#include "CSConfig.h"

#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include "CSGlobal.h"
#include "CSThread.h"
#include "CSLog.h"
#include "CSPath.h"
#include "CSFile.h"
#include "CSString.h"
#include "CSStrUtil.h"
#include "CSStorage.h"
#include "CSEncode.h"
#include "CSS3Protocol.h"

#include "backup_ms.h"
#include "cloud.h"

CSSyncSparseArray	*MSCloudInfo::gCloudInfo;
csWord4			MSCloudInfo::gMaxInfoRef;

csWord4			CloudDB::gKeyIndex;
CSMutex			CloudDB::gCloudKeyLock;

//==============================
MSCloudInfo::MSCloudInfo(csWord4 id,
						const char *server,
						const char *bucket_arg,
						const char *publicKey,
						const char *privateKey
						):
	cloudRefId(id),
	bucket(NULL),
	s3Prot(NULL)
{		
	new_(s3Prot, CSS3Protocol());
	s3Prot->s3_setServer(server);
	s3Prot->s3_setPublicKey(publicKey);
	s3Prot->s3_setPrivateKey(privateKey);
	
	bucket = CSString::newString(bucket_arg);		
}
		
//-------------------------------
MSCloudInfo::~MSCloudInfo()
{
	if (bucket)
		bucket->release();
		
	if (s3Prot)
		s3Prot->release();
}
	
//-------------------------------
const char *MSCloudInfo::getServer() 
{ 
	return s3Prot->s3_getServer();
}

//-------------------------------
const char *MSCloudInfo::getBucket() 
{ 
	return bucket->getCString();
}

//-------------------------------
const char *MSCloudInfo::getPublicKey() 
{ 
	return s3Prot->s3_getPublicKey();
}

//-------------------------------
const char *MSCloudInfo::getPrivateKey() 
{ 
	return s3Prot->s3_getPrivateKey();
}

//-------------------------------
CSString *MSCloudInfo::getSignature(const char *key, const char *content_type, time_t *s3AuthorizationTime)
{
	return s3Prot->s3_getAuthorization(bucket->getCString(), key, content_type, s3AuthorizationTime);
}

//-------------------------------
CSString *MSCloudInfo::getDataURL(const char *key, int keep_alive)
{
	return s3Prot->s3_getDataURL(bucket->getCString(), key, keep_alive);
}

//-------------------------------
void MSCloudInfo::send(CSInputStream *input, const char *key, off_t size)
{
	CSVector *headers;
	headers = s3Prot->s3_send(input, bucket->getCString(), key, size);
	headers->release();
}

//-------------------------------
CSVector *MSCloudInfo::list(const char *key_prefix, csWord4 max)
{
	return s3Prot->s3_list(bucket->getCString(), key_prefix, max);
}

//-------------------------------
void MSCloudInfo::receive(CSOutputStream *output, const char *key)
{
	bool found;
	CSVector *headers;
	
	headers = s3Prot->s3_receive(output, bucket->getCString(), key, &found);
	headers->release();
	if (!found) {
		CSStringBuffer *err;
		enter_();
		
		new_(err, CSStringBuffer());
		push_(err);
		err->append("S3 object not found: ");
		err->append(getServer());
		err->append("/");
		err->append(bucket->getCString());
		err->append("/");
		err->append(key);

		CSException::throwException(CS_CONTEXT, CS_ERR_GENERIC_ERROR, err->getCString());
		release_(err);
		outer_();
	}
}

//-------------------------------
void MSCloudInfo::cDelete(const char *key)
{
	s3Prot->s3_delete(bucket->getCString(), key);
}

//-------------------------------
void MSCloudInfo::copy(MSCloudInfo *dst_cloud, const char *dst_key, const char *src_key)
{
	enter_();
	push_(dst_cloud);
	
	s3Prot->s3_copy(dst_cloud->getServer() ,dst_cloud->bucket->getCString(), dst_key, bucket->getCString(), src_key);

	release_(dst_cloud);
	exit_();
}

//==============================
CloudDB::CloudDB(csWord4 db_id):
	isBackup(false),
	blob_recovery_no(0),
	blob_db_id(db_id),
	dfltCloudRefId(0),
	backupCloud(NULL),
	backupInfo(NULL),
	clObjectKey(NULL)
{
	enter_();

	keep_alive = (5 * 60); // default URL keep alive in seconds.
	new_(clObjectKey, CSStringBuffer());
	clObjectKey->setLength(base_key_size);
		
	exit_();
}

//-------------------------------
CloudDB::~CloudDB()
{
	
	if (backupInfo)
		backupInfo->release();
		
	if (backupCloud)
		backupCloud->release();
		
	if (clObjectKey)
		clObjectKey->release();
		
}
//-------------------------------
MSBackupInfo *CloudDB::cl_getBackupInfo()
{ 
	if (backupInfo)
		backupInfo->retain();
		
	return backupInfo;
}

//-------------------------------
void CloudDB::cl_clearBackupInfo(){ backupInfo->release(); backupInfo = NULL;}

//-------------------------------
void CloudDB::cl_createDB()
{
// This is a no-op. 
}

//-------------------------------
// Restore all the 
void CloudDB::cl_restoreDB()
{
	CSVector *list;
	CSString *key;
	CloudObjectKey *src_objectKey, *dst_objectKey;
	CloudKeyRec		cloudKey;
	csWord4			src_cloudRef, dst_cloudRef = 0;
	MSBackupInfo	*backup_info;
	MSCloudInfo		*src_cloud, *dst_cloud;
	enter_();

	if (!blob_recovery_no)
		exit_(); // nothing to do.
 
	backup_info = MSBackupInfo::getBackupInfo(blob_recovery_no);
	push_(backup_info);
	
	src_cloudRef = backup_info->getcloudRef();
	src_cloud = MSCloudInfo::getCloudInfo(src_cloudRef);
	push_(src_cloud);
	
	new_(dst_objectKey, CloudObjectKey(blob_db_id));
	push_(dst_objectKey);
	
	// Get the key for the backup BLOB
	new_(src_objectKey, CloudObjectKey(blob_db_id));
	push_(src_objectKey);
	src_objectKey->setObjectKey(NULL, backup_info->getcloudBackupNo(), backup_info->getDatabaseId()); 

	// Get a list of all the BLOBs that were backed up.
	list = src_cloud->list(src_objectKey->getCString());
	release_(src_objectKey);
	push_(list);
	
	// Go through the list copying the keys.
	try_(a) {
		dst_cloudRef = src_cloudRef;
		dst_cloud = src_cloud;
		dst_cloud->retain();
	
		while ((key = (CSString*)(list->take(0))) ) {
			push_(key);

			// The source key name must be parsed to get its
			// destination cloud reference. The destination for
			// the BLOBs may not all be in the same cloud. 
			CloudObjectKey::parseObjectKey(key->getCString(), &cloudKey);
			
			// Reset the destination cloud if required.
			if (cloudKey.cloud_ref != dst_cloudRef) {
				if (dst_cloud) {
					dst_cloud->release();
					dst_cloud = NULL;
				}
				dst_cloudRef = 	cloudKey.cloud_ref;
				dst_cloud = MSCloudInfo::getCloudInfo(dst_cloudRef);
			}

			// Copy the BLOB to the recovered database.
			dst_objectKey->setObjectKey(&cloudKey);
			src_cloud->copy(RETAIN(dst_cloud), dst_objectKey->getCString(), key->getCString());
			release_(key);
			
		}
	}
	
	finally_(a) {
		if (dst_cloud)
			dst_cloud->release();
	}
	cont_(a);
	
	blob_recovery_no = 0;
	release_(list);
	release_(dst_objectKey);	
	release_(src_cloud);
	release_(backup_info);
	exit_();	
}

//-------------------------------
csWord4 CloudDB::cl_getNextBackupNumber(csWord4 cloud_ref)
{
	CloudObjectKey *objectKey;
	CSVector *list;
	csWord4 backup_no = 0, size = 1;
	MSCloudInfo *s3Cloud;
	enter_();

	s3Cloud = MSCloudInfo::getCloudInfo((cloud_ref)?cloud_ref:dfltCloudRefId);
	push_(s3Cloud);
	
	new_(objectKey, CloudObjectKey(blob_db_id));
	push_(objectKey);

	// Find the next available backup number
	while (size) {
		backup_no++;
		objectKey->setObjectKey(NULL, backup_no); // use the key prefix with the backup number for listing.
		list = s3Cloud->list(objectKey->getCString(), 1);
		size = list->size();
		list->release();
	}
	
	release_(objectKey);
	release_(s3Cloud);

	return_(backup_no);
}

//-------------------------------
void CloudDB::cl_backupBLOB(CloudKeyPtr key)
{
	CloudObjectKey *src_objectKey, *dst_objectKey;
	csWord4 cloudRef, backupNo;
	MSCloudInfo *src_cloud, *dst_cloud;
	enter_();

	ASSERT(backupInfo);
	
	if ((cloudRef = backupInfo->getcloudRef()) == 0) {
		backupInfo->setcloudRef(dfltCloudRefId);
		cloudRef = dfltCloudRefId;
	}
		
	if ((backupNo = backupInfo->getcloudBackupNo()) == 0) {		
		backupNo = cl_getNextBackupNumber(cloudRef);
		backupInfo->setcloudBackupNo(backupNo);
	}
	
	// Set the source object's key
	new_(src_objectKey, CloudObjectKey(blob_db_id));
	push_(src_objectKey);
	src_objectKey->setObjectKey(key);

	// Set the destination object's key
	new_(dst_objectKey, CloudObjectKey(blob_db_id));
	push_(dst_objectKey);
	dst_objectKey->setObjectKey(key, backupNo);

	// Get the source cloud
	src_cloud = MSCloudInfo::getCloudInfo((key->cloud_ref)?key->cloud_ref:dfltCloudRefId);
	push_(src_cloud);
	
	// Copy the object to the destination cloud
	dst_cloud = MSCloudInfo::getCloudInfo(cloudRef);
	src_cloud->copy(dst_cloud, dst_objectKey->getCString(), src_objectKey->getCString());

	release_(src_cloud);
	release_(dst_objectKey);
	release_(src_objectKey);
	exit_();
}

//-------------------------------
void CloudDB::cl_restoreBLOB(CloudKeyPtr key, csWord4 backup_db_id)
{
	CloudObjectKey *src_objectKey, *dst_objectKey;
	csWord4 cloudRef, backupNo;
	MSCloudInfo *src_cloud, *dst_cloud;
	enter_();

	ASSERT(backupInfo);
	
	if ((cloudRef = backupInfo->getcloudRef()) == 0) {
		backupInfo->setcloudRef(dfltCloudRefId);
		cloudRef = dfltCloudRefId;
	}
		
	if ((backupNo = backupInfo->getcloudBackupNo()) == 0) {		
		backupNo = cl_getNextBackupNumber(cloudRef);
		backupInfo->setcloudBackupNo(backupNo);
	}
	
	// Set the source object's key
	new_(src_objectKey, CloudObjectKey(backup_db_id));
	push_(src_objectKey);
	src_objectKey->setObjectKey(key, backupNo);

	// Set the destination object's key
	new_(dst_objectKey, CloudObjectKey(blob_db_id));
	push_(dst_objectKey);
	dst_objectKey->setObjectKey(key);

	// Get the source cloud
	src_cloud = MSCloudInfo::getCloudInfo(cloudRef);
	push_(src_cloud);
	
	// Copy the object to the destination cloud
	dst_cloud = MSCloudInfo::getCloudInfo((key->cloud_ref)?key->cloud_ref:dfltCloudRefId);
	src_cloud->copy(dst_cloud, dst_objectKey->getCString(), src_objectKey->getCString());

	release_(src_cloud);
	release_(dst_objectKey);
	release_(src_objectKey);
	exit_();
}

//-------------------------------
// Drop database deletes all objects with the database key prefix
void CloudDB::cl_dropDB()
{
	CSVector *list;
	CSString *key;
	CloudObjectKey *objectKey;	
	MSCloudInfo *s3Cloud = NULL;
	int i;
	const char *key_str;
	
	enter_();
	new_(objectKey, CloudObjectKey(blob_db_id));
	push_(objectKey);
	
	lock_(MSCloudInfo::gCloudInfo);

	if (isBackup) {
		csWord4 backup_no;
		if (backupInfo && (backup_no = backupInfo->getcloudBackupNo())) {
			objectKey->setObjectKey(NULL, backup_no); // use the key prefix for the backup for listing.
			if ((s3Cloud = MSCloudInfo::getCloudInfo(backupInfo->getcloudRef())))
				push_(s3Cloud);
		}
	} else {
		objectKey->setObjectKey(); // use the key prefix for listing.
		i = 0;
		s3Cloud = (MSCloudInfo*)MSCloudInfo::gCloudInfo->itemAt(i++); // <-- unreferenced object 
	}
		
	key_str = objectKey->getCString();

	// For non backup BLOBs all known clouds must be searched 
	// for possible BLOBs and deleted. The BLOBs belonging to a backup
	// will ever only be in one cloud storage location.
	while (s3Cloud) {
		list = s3Cloud->list(key_str);
		push_(list);
		
		// Go through the list deleting the keys.
		while ((key = (CSString*)(list->take(0))) ) {
			push_(key);
			s3Cloud->cDelete(key->getCString());
			release_(key);
		}
		
		release_(list);
		if (isBackup) {
			release_(s3Cloud); // Only the backup s3Cloud needs to be released.
			s3Cloud = NULL;
		} else
			s3Cloud = (MSCloudInfo*)MSCloudInfo::gCloudInfo->itemAt(i++);// <-- unreferenced object
	}
	
done:
	unlock_(MSCloudInfo::gCloudInfo);
	release_(objectKey);
	exit_();
}

//-------------------------------
void CloudDB::cl_putData(CloudKeyPtr key, CSInputStream *stream, off_t size)
{
	CloudObjectKey *objectKey;
	MSCloudInfo *s3Cloud;
	
	enter_();
	
	push_(stream);
	
	new_(objectKey, CloudObjectKey(blob_db_id));
	push_(objectKey);
	
	objectKey->setObjectKey(key);
	
	s3Cloud = MSCloudInfo::getCloudInfo((key->cloud_ref)?key->cloud_ref:dfltCloudRefId);
	push_(s3Cloud);
	s3Cloud->send(RETAIN(stream), objectKey->getCString(), size);
	release_(s3Cloud);
	
	release_(objectKey);
	release_(stream);
	
	exit_();
}

//-------------------------------
off_t CloudDB::cl_getData(CloudKeyPtr key,  char *buffer, off_t size)
{	
	CloudObjectKey *objectKey;
	CSStaticMemoryOutputStream *output;
	MSCloudInfo *s3Cloud;
	enter_();
	
	new_(objectKey, CloudObjectKey(blob_db_id));
	push_(objectKey);
	
	s3Cloud = MSCloudInfo::getCloudInfo(key->cloud_ref);
	push_(s3Cloud);

	new_(output, CSStaticMemoryOutputStream((u_char *)buffer, size));
	push_(output);
	
	objectKey->setObjectKey(key);
	
	s3Cloud->receive(RETAIN(output), objectKey->getCString());	
	size = output->getSize();
	release_(output);
	
	release_(s3Cloud);
	release_(objectKey);
	return_(size);
}

//-------------------------------
void CloudDB::cl_deleteData(CloudKeyPtr key)
{
	MSCloudInfo *s3Cloud;
	CloudObjectKey *objectKey;
	enter_();
	
	new_(objectKey, CloudObjectKey(blob_db_id));
	push_(objectKey);
	
	s3Cloud = MSCloudInfo::getCloudInfo(key->cloud_ref);
	push_(s3Cloud);

	objectKey->setObjectKey(key);

	s3Cloud->cDelete(objectKey->getCString());	
	
	release_(s3Cloud);
	release_(objectKey);

	exit_();
}

//-------------------------------
CSString *CloudDB::cl_getDataURL(CloudKeyPtr key)
{
	CloudObjectKey *objectKey;
	CSString *url;
	MSCloudInfo *s3Cloud;
	enter_();
	
	new_(objectKey, CloudObjectKey(blob_db_id));
	push_(objectKey);
	
	objectKey->setObjectKey(key);
	
	s3Cloud = MSCloudInfo::getCloudInfo(key->cloud_ref);  
	push_(s3Cloud);
		
	url = s3Cloud->getDataURL(objectKey->getCString(), keep_alive);
	
	release_(s3Cloud);
	release_(objectKey);

	return_(url);
}

//-------------------------------
CSString *CloudDB::cl_getSignature(CloudKeyPtr key, CSString *content_type_arg, time_t *s3AuthorizationTime)
{
	CSString *signature;
	CloudObjectKey *objectKey;
	const char *content_type = NULL;
	MSCloudInfo *s3Cloud;
	enter_();
	
	new_(objectKey, CloudObjectKey(blob_db_id));
	push_(objectKey);
	
	if (content_type_arg) {
		push_(content_type_arg);
		content_type = content_type_arg->getCString();
	}
	
	objectKey->setObjectKey(key);
	s3Cloud = MSCloudInfo::getCloudInfo(key->cloud_ref);  
	push_(s3Cloud);
	
	signature = s3Cloud->getSignature(objectKey->getCString(), content_type, s3AuthorizationTime);
	
	if (content_type_arg) 
		release_(content_type_arg);

	release_(s3Cloud);
	release_(objectKey);
	
	return_(signature);
}

//==============================