-
Notifications
You must be signed in to change notification settings - Fork 298
/
CBLDatabase.m
546 lines (417 loc) · 16.1 KB
/
CBLDatabase.m
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
//
// CBLDatabase.m
// CouchbaseLite
//
// Created by Jens Alfke on 6/17/12.
// Copyright (c) 2012-2013 Couchbase, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
#import "CouchbaseLitePrivate.h"
#import "CBLDatabase.h"
#import "CBLDatabase+Internal.h"
#import "CBLDatabase+Insertion.h"
#import "CBLDatabaseChange.h"
#import "CBL_Shared.h"
#import "CBLInternal.h"
#import "CBLModel_Internal.h"
#import "CBLModelFactory.h"
#import "CBLCache.h"
#import "CBLManager+Internal.h"
#import "CBLMisc.h"
#import "CBLSymmetricKey.h"
#import "MYAction.h"
#import "MYBlockUtils.h"
#import "ExceptionUtils.h"
#if TARGET_OS_IPHONE
#import <UIKit/UIApplication.h>
#endif
// NOTE: This file contains mostly just public-API method implementations.
// The lower-level stuff is in CBLDatabase+Internal.m, etc.
// Size of document cache: max # of otherwise-unreferenced docs that will be kept in memory.
#define kDocRetainLimit 50
NSString* const kCBLDatabaseChangeNotification = @"CBLDatabaseChange";
static id<CBLFilterCompiler> sFilterCompiler;
@implementation CBLDatabase
{
CBLCache* _docCache;
NSMutableSet* _allReplications;
}
@synthesize manager=_manager, unsavedModelsMutable=_unsavedModelsMutable;
@synthesize dir=_dir, name=_name, isOpen=_isOpen;
- (instancetype) initWithDir: (NSString*)dir
name: (NSString*)name
manager: (CBLManager*)manager
readOnly: (BOOL)readOnly
{
self = [self _initWithDir: dir name: name manager: manager readOnly: readOnly];
if (self) {
_unsavedModelsMutable = [NSMutableSet set];
_allReplications = [[NSMutableSet alloc] init];
#if TARGET_OS_IPHONE
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(appBackgrounding:)
name: UIApplicationWillTerminateNotification
object: nil];
// Also clean up when app is backgrounded, on iOS:
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(appBackgrounding:)
name: UIApplicationDidEnterBackgroundNotification
object: nil];
#endif
}
return self;
}
- (void)dealloc {
if (_isOpen) {
Assert(!_manager);
[self _close];
}
[[NSNotificationCenter defaultCenter] removeObserver: self];
}
- (NSUInteger) documentCount {
return _storage.documentCount;
}
- (SequenceNumber) lastSequenceNumber {
return _storage.lastSequence;
}
- (void) postPublicChangeNotification: (NSArray*)changes {
BOOL external = NO;
for (CBLDatabaseChange* change in changes) {
// Notify the corresponding instantiated CBLDocument object (if any):
[[self _cachedDocumentWithID: change.documentID] revisionAdded: change notify: YES];
if (change.source != nil)
external = YES;
}
// Post the public kCBLDatabaseChangeNotification:
NSDictionary* userInfo = @{@"changes": changes,
@"external": @(external)};
NSNotification* n = [NSNotification notificationWithName: kCBLDatabaseChangeNotification
object: self
userInfo: userInfo];
[self postNotification:n];
}
#if TARGET_OS_IPHONE
- (void) appBackgrounding: (NSNotification*)n {
[self doAsync: ^{
[self autosaveAllModels: nil];
}];
}
#endif
- (NSURL*) internalURL {
return [_manager.internalURL URLByAppendingPathComponent: self.name isDirectory: YES];
}
static void catchInBlock(void (^block)()) {
@try {
block();
}catchAndReport(@"-[CBLDatabase doAsync:]");
}
- (void) doAsync: (void (^)())block {
block = ^{
if (_isOpen)
catchInBlock(block);
};
if (_dispatchQueue)
dispatch_async(_dispatchQueue, block);
else
MYOnThreadInModes(_thread, CBL_RunloopModes, NO, block);
}
- (void) doSync: (void (^)())block {
if (_dispatchQueue)
dispatch_sync(_dispatchQueue, ^{catchInBlock(block);});
else
MYOnThreadInModes(_thread, CBL_RunloopModes, YES, ^{catchInBlock(block);});
}
- (void) doAsyncAfterDelay: (NSTimeInterval)delay block: (void (^)())block {
block = ^{
if (_isOpen)
catchInBlock(block);
};
if (_dispatchQueue) {
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC));
dispatch_after(popTime, _dispatchQueue, block);
} else {
//FIX: This schedules on the _current_ thread, not _thread!
MYAfterDelay(delay, block);
}
}
- (BOOL) waitFor: (BOOL (^)())block {
if (_dispatchQueue) {
Warn(@"-[CBLDatabase waitFor:] cannot be used with dispatch queues, only runloops");
return NO;
}
return MYWaitFor(CBL_PrivateRunloopMode, block);
}
- (BOOL) inTransaction: (BOOL(^)(void))block {
return 200 == [_storage inTransaction: ^CBLStatus {
return block() ? 200 : 999;
}];
}
- (BOOL) close: (NSError**)outError {
if (![self saveAllModels: outError])
return NO;
for (CBLReplication* repl in self.allReplications)
[repl stop];
[self _close];
return YES;
}
- (BOOL) deleteDatabase: (NSError**)outError {
if (_readOnly)
return CBLStatusToOutNSError(kCBLStatusForbidden, outError);
LogTo(Database, @"Deleting %@", _dir);
[[NSNotificationCenter defaultCenter] postNotificationName: CBL_DatabaseWillBeDeletedNotification
object: self];
[self _close];
// Wait for all threads to close this database file:
[_manager.shared forgetDatabaseNamed: _name];
if (!self.exists) {
return YES;
}
return [[self class] deleteDatabaseFilesAtPath: _dir error: outError];
}
- (BOOL) compact: (NSError**)outError {
if (_readOnly)
return CBLStatusToOutNSError(kCBLStatusForbidden, outError);
//FIX:
// CBLStatus status = [_storage inTransaction: ^CBLStatus {
// Do this in a transaction because garbageCollectAttachments expects the database to be
// freshly compacted (i.e. only current revisions have bodies), and it could delete new
// attachments added while it's working. So lock out other writers for the duration.
return [_storage compact: outError] && [self garbageCollectAttachments: outError];
// }];
// return !CBLStatusIsError(status);
}
- (NSUInteger) maxRevTreeDepth {
return _storage.maxRevTreeDepth;
}
- (void) setMaxRevTreeDepth: (NSUInteger)maxRevs {
if (maxRevs == 0)
maxRevs = _manager.defaultMaxRevTreeDepth;
if (maxRevs != _storage.maxRevTreeDepth) {
_storage.maxRevTreeDepth = (unsigned)maxRevs;
[_storage setInfo: $sprintf(@"%lu", (unsigned long)maxRevs) forKey: @"max_revs"];
}
}
- (BOOL) replaceUUIDs: (NSError**)outError {
CBLStatus status = [_storage setInfo: CBLCreateUUID() forKey: @"publicUUID"];
if (status == kCBLStatusOK)
status = [_storage setInfo: CBLCreateUUID() forKey: @"privateUUID"];
if (status == kCBLStatusOK)
return YES;
CBLStatusToOutNSError(status, outError);
return NO;
}
- (BOOL) changeEncryptionKey: (id)newKeyOrPassword error: (NSError**)outError {
if (_readOnly)
return CBLStatusToOutNSError(kCBLStatusForbidden, outError);
if (![_storage respondsToSelector: @selector(actionToChangeEncryptionKey:)])
return CBLStatusToOutNSError(kCBLStatusNotImplemented, outError);
CBLSymmetricKey* newKey = nil;
if (newKeyOrPassword) {
newKey = [[CBLSymmetricKey alloc] initWithKeyOrPassword: newKeyOrPassword];
if (!newKey)
return CBLStatusToOutNSError(kCBLStatusBadRequest, outError);
}
if ([_manager.shared countForOpenedDatabase: _name] > 1) {
// FIX: If other database handles are open on the same db file, they will be confused by
// the db suddenly changing its encryption key. This will cause at least errors, and
// possibly file corruption. It would be best to close these other handles and then
// reopen them afterwards, but that's difficult to do, so the workaround is to give up.
if (outError)
*outError = CBLStatusToNSErrorWithInfo(kCBLStatusServiceUnavailable,
@"This database is opened by another CBLManager",
nil, nil);
return NO;
}
MYAction* action = [_storage actionToChangeEncryptionKey: newKey];
if (!action)
return CBLStatusToOutNSError(kCBLStatusNotImplemented, outError);
[action addAction: [_attachments actionToChangeEncryptionKey: newKey]];
[action addPerform:^BOOL(NSError** error) {
[_manager registerEncryptionKey: newKeyOrPassword forDatabaseNamed: _name];
return YES;
} backOut: nil cleanUp: nil];
return [action run: outError];
}
#pragma mark - DOCUMENTS:
- (CBLDocument*) documentWithID: (NSString*)docID mustExist: (BOOL)mustExist isNew: (BOOL)isNew {
CBLDocument* doc = (CBLDocument*) [_docCache resourceWithCacheKey: docID];
if (doc) {
if (mustExist && doc.currentRevision == nil) // loads current revision from db
return nil;
return doc;
}
if (docID.length == 0)
return nil;
doc = [[CBLDocument alloc] initWithDatabase: self
documentID: docID
exists: !isNew];
if (!doc)
return nil;
if (mustExist && doc.currentRevision == nil) // loads current revision from db
return nil;
if (!_docCache)
_docCache = [[CBLCache alloc] initWithRetainLimit: kDocRetainLimit];
[_docCache addResource: doc];
return doc;
}
- (CBLDocument*) documentWithID: (NSString*)docID {
return [self documentWithID: docID mustExist: NO isNew: NO];
}
- (CBLDocument*) existingDocumentWithID: (NSString*)docID {
return [self documentWithID: docID mustExist: YES isNew: NO];
}
- (CBLDocument*) objectForKeyedSubscript: (NSString*)key {
return [self documentWithID: key mustExist: NO isNew: NO];
}
- (CBLDocument*) createDocument {
return [self documentWithID: [[self class] generateDocumentID] mustExist: NO isNew: YES];
}
- (CBLDocument*) _cachedDocumentWithID: (NSString*)docID {
return (CBLDocument*) [_docCache resourceWithCacheKeyDontRecache: docID];
}
- (void) _clearDocumentCache {
[_docCache forgetAllResources];
}
- (void) _pruneDocumentCache {
[_docCache unretainResources];
}
- (void) removeDocumentFromCache: (CBLDocument*)document {
[_docCache forgetResource: document];
}
- (CBLQuery*) createAllDocumentsQuery {
return [[CBLQuery alloc] initWithDatabase: self view: nil];
}
static NSString* makeLocalDocID(NSString* docID) {
return [@"_local/" stringByAppendingString: docID];
}
- (NSDictionary*) existingLocalDocumentWithID: (NSString*)localDocID {
return [_storage getLocalDocumentWithID: makeLocalDocID(localDocID) revisionID: nil].properties;
}
- (BOOL) putLocalDocument: (NSDictionary*)properties
withID: (NSString*)localDocID
error: (NSError**)outError
{
localDocID = makeLocalDocID(localDocID);
CBL_MutableRevision* rev = [[CBL_MutableRevision alloc] initWithDocID: localDocID
revID: nil
deleted: (properties == nil)];
if (properties)
rev.properties = properties;
// Now update the doc (or delete it, if properties is nil):
CBLStatus status;
BOOL ok = [_storage putLocalRevision: rev
prevRevisionID: nil
obeyMVCC: NO
status: &status] != nil;
if (!ok)
CBLStatusToOutNSError(status, outError);
return ok;
}
- (BOOL) deleteLocalDocumentWithID: (NSString*)localDocID error: (NSError**)outError {
return [self putLocalDocument: nil withID: localDocID error: outError];
}
#pragma mark - VIEWS:
- (CBLView*) registerView: (CBLView*)view {
if (!view)
return nil;
if (!_views)
_views = [[NSMutableDictionary alloc] init];
_views[view.name] = view;
return view;
}
- (CBLView*) existingViewNamed: (NSString*)name {
CBLView* view = _views[name];
if (view)
return view;
return [self registerView: [[CBLView alloc] initWithDatabase: self name: name create: NO]];
}
- (CBLView*) viewNamed: (NSString*)name {
CBLView* view = _views[name];
if (view)
return view;
return [self registerView: [[CBLView alloc] initWithDatabase: self name: name create: YES]];
}
- (CBLQuery*) slowQueryWithMap: (CBLMapBlock)mapBlock {
return [[CBLQuery alloc] initWithDatabase: self mapBlock: mapBlock];
}
#pragma mark - VALIDATION & FILTERS:
- (void) setValidationNamed: (NSString*)validationName asBlock: (CBLValidationBlock)validationBlock {
[self.shared setValue: [validationBlock copy]
forType: @"validation" name: validationName inDatabaseNamed: _name];
}
- (CBLValidationBlock) validationNamed: (NSString*)validationName {
return [self.shared valueForType: @"validation" name: validationName inDatabaseNamed: _name];
}
- (void) setFilterNamed: (NSString*)filterName asBlock: (CBLFilterBlock)filterBlock {
[self.shared setValue: [filterBlock copy]
forType: @"filter" name: filterName inDatabaseNamed: _name];
}
- (CBLFilterBlock) filterNamed: (NSString*)filterName {
return [self.shared valueForType: @"filter" name: filterName inDatabaseNamed: _name];
}
+ (void) setFilterCompiler: (id<CBLFilterCompiler>)compiler {
sFilterCompiler = compiler;
}
+ (id<CBLFilterCompiler>) filterCompiler {
return sFilterCompiler;
}
#pragma mark - REPLICATION:
- (NSArray*) allReplications {
return [_allReplications allObjects];
}
- (void) addReplication: (CBLReplication*)repl {
[_allReplications addObject: repl];
}
- (void) forgetReplication: (CBLReplication*)repl {
[_allReplications removeObject: repl];
}
- (CBLReplication*) createPushReplication: (NSURL*)url {
return [[CBLReplication alloc] initWithDatabase: self remote: url pull: NO];
}
- (CBLReplication*) createPullReplication: (NSURL*)url {
return [[CBLReplication alloc] initWithDatabase: self remote: url pull: YES];
}
- (CBLReplication*) existingReplicationWithURL: (NSURL*)url pull: (BOOL)pull {
for (CBLReplication* repl in _allReplications)
if (repl.pull == pull && $equal(repl.remoteURL, url))
return repl;
return nil;
}
@end
@implementation CBLDatabase (CBLModel)
- (NSArray*) unsavedModels {
if (_unsavedModelsMutable.count == 0)
return nil;
return [_unsavedModelsMutable allObjects];
}
- (BOOL) saveAllModels: (NSError**)outError {
NSArray* unsaved = self.unsavedModels;
if (unsaved.count == 0)
return YES;
return [CBLModel saveModels: unsaved error: outError];
}
- (BOOL) autosaveAllModels: (NSError**)outError {
NSArray* unsaved = [self.unsavedModels my_filter: ^int(CBLModel* model) {
return model.autosaves;
}];
if (unsaved.count == 0)
return YES;
return [CBLModel saveModels: unsaved error: outError];
}
@end
@implementation CBLDatabase (CBLModelFactory)
- (CBLModelFactory*) modelFactory {
if (!_modelFactory)
_modelFactory = [[CBLModelFactory alloc] init];
return _modelFactory;
}
- (void) setModelFactory:(CBLModelFactory *)modelFactory {
_modelFactory = modelFactory;
}
@end