This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
TDDatabase+Replication.m
145 lines (122 loc) · 5.03 KB
/
TDDatabase+Replication.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
//
// TDDatabase+Replication.m
// TouchDB
//
// Created by Jens Alfke on 12/27/11.
// Copyright (c) 2011 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 "TDDatabase+Replication.h"
#import "TDInternal.h"
#import "TDPuller.h"
#import "MYBlockUtils.h"
#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"
#define kActiveReplicatorCleanupDelay 10.0
@implementation TDDatabase (Replication)
- (NSArray*) activeReplicators {
return _activeReplicators;
}
- (TDReplicator*) activeReplicatorWithRemoteURL: (NSURL*)remote
push: (BOOL)push {
TDReplicator* repl;
for (repl in _activeReplicators) {
if ($equal(repl.remote, remote) && repl.isPush == push && repl.running)
return repl;
}
return nil;
}
- (TDReplicator*) replicatorWithRemoteURL: (NSURL*)remote
push: (BOOL)push
continuous: (BOOL)continuous {
TDReplicator* repl = [self activeReplicatorWithRemoteURL: remote push: push];
if (repl)
return repl;
repl = [[TDReplicator alloc] initWithDB: self
remote: remote
push: push
continuous: continuous];
if (!repl)
return nil;
if (!_activeReplicators) {
_activeReplicators = [[NSMutableArray alloc] init];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(replicatorDidStop:)
name: TDReplicatorStoppedNotification
object: nil];
}
[_activeReplicators addObject: repl];
return repl;
}
- (void) stopAndForgetReplicator: (TDReplicator*)repl {
[repl databaseClosing];
[_activeReplicators removeObjectIdenticalTo: repl];
}
- (void) replicatorDidStop: (NSNotification*)n {
TDReplicator* repl = n.object;
if (repl.error) // Leave it around a while so clients can see the error
MYAfterDelay(kActiveReplicatorCleanupDelay,
^{[_activeReplicators removeObjectIdenticalTo: repl];});
else
[_activeReplicators removeObjectIdenticalTo: repl];
}
- (NSString*) lastSequenceWithCheckpointID: (NSString*)checkpointID {
// This table schema is out of date but I'm keeping it the way it is for compatibility.
// The 'remote' column now stores the opaque checkpoint IDs, and 'push' is ignored.
return [_fmdb stringForQuery:@"SELECT last_sequence FROM replicators WHERE remote=?",
checkpointID];
}
- (BOOL) setLastSequence: (NSString*)lastSequence withCheckpointID: (NSString*)checkpointID {
return [_fmdb executeUpdate:
@"INSERT OR REPLACE INTO replicators (remote, push, last_sequence) VALUES (?, -1, ?)",
checkpointID, lastSequence];
}
+ (NSString*) joinQuotedStrings: (NSArray*)strings {
if (strings.count == 0)
return @"";
NSMutableString* result = [NSMutableString stringWithString: @"'"];
BOOL first = YES;
for (NSString* str in strings) {
if (first)
first = NO;
else
[result appendString: @"','"];
NSRange range = NSMakeRange(result.length, str.length);
[result appendString: str];
[result replaceOccurrencesOfString: @"'" withString: @"''"
options: NSLiteralSearch range: range];
}
[result appendString: @"'"];
return result;
}
- (BOOL) findMissingRevisions: (TDRevisionList*)revs {
if (revs.count == 0)
return YES;
NSString* sql = $sprintf(@"SELECT docid, revid FROM revs, docs "
"WHERE revid in (%@) AND docid IN (%@) "
"AND revs.doc_id == docs.doc_id",
[TDDatabase joinQuotedStrings: revs.allRevIDs],
[TDDatabase joinQuotedStrings: revs.allDocIDs]);
// ?? Not sure sqlite will optimize this fully. May need a first query that looks up all
// the numeric doc_ids from the docids.
FMResultSet* r = [_fmdb executeQuery: sql];
if (!r)
return NO;
while ([r next]) {
@autoreleasepool {
TDRevision* rev = [revs revWithDocID: [r stringForColumnIndex: 0]
revID: [r stringForColumnIndex: 1]];
if (rev)
[revs removeRev: rev];
}
}
[r close];
return YES;
}
@end