forked from couchbaselabs/CouchCocoa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCouchServer.m
282 lines (216 loc) · 8.81 KB
/
CouchServer.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
//
// CouchServer.m
// CouchCocoa
//
// Created by Jens Alfke on 5/26/11.
// Copyright 2011 Couchbase, Inc.
//
// 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 "CouchServer.h"
#import "CouchInternal.h"
#import "RESTCache.h"
static NSString* const kLocalServerURL = @"http://127.0.0.1:5984/";
int gCouchLogLevel = 0;
@interface CouchServer ()
@property (nonatomic, readwrite, retain) NSArray* activeTasks;
@end
@implementation CouchServer
- (id) initWithURL: (NSURL*)url {
return [super initWithURL: url];
}
/** Without URL, connects to localhost on default port */
- (id) init {
return [self initWithURL:[NSURL URLWithString: kLocalServerURL]];
}
- (id) copyWithZone: (NSZone*)zone {
return [[[self class] alloc] initWithURL: self.URL];
}
- (void)dealloc {
[_activeTasks release];
[_activityRsrc release];
[_replicationsQuery release];
[_dbCache release];
[super dealloc];
}
- (void) close {
[_replicationsQuery release];
_replicationsQuery = nil;
for (CouchDatabase* db in _dbCache.allCachedResources)
[db unretainDocumentCache];
}
- (CouchDatabase*) database { // Overridden from CouchResource.
return nil;
}
- (BOOL) isEmbeddedServer {
return NO; // CouchEmbeddedServer overrides this
}
- (NSString*) getVersion: (NSError**)outError {
RESTOperation* op = [self GET];
[op wait];
if (outError)
*outError = op.error;
return [[op.responseBody.fromJSON objectForKey: @"version"] description]; // Blocks!
}
- (NSArray*) generateUUIDs: (NSUInteger)count {
NSDictionary* params = [NSDictionary dictionaryWithObject:
[NSNumber numberWithUnsignedLong: count]
forKey: @"?count"];
RESTOperation* op = [[self childWithPath: @"_uuids"] sendHTTP: @"GET" parameters: params];
return [op.responseBody.fromJSON objectForKey: @"uuids"];
}
- (NSString*) generateDocumentID {
if (_newDocumentIDs.count == 0) {
// As an optimization, request UUIDs from the server in packages of 10:
NSArray* newIDs = [self generateUUIDs: 10];
if (!newIDs) {
// In an emergency, if we can't get IDs from the server, generate a UUID locally:
CFUUIDRef uuid = CFUUIDCreate(NULL);
CFStringRef cfStr = CFUUIDCreateString(NULL, uuid);
CFRelease(uuid);
return [(id)cfStr autorelease];
}
if (!_newDocumentIDs)
_newDocumentIDs = [[NSMutableArray alloc] init];
[_newDocumentIDs addObjectsFromArray: newIDs];
}
NSString* result = [[[_newDocumentIDs objectAtIndex: 0] retain] autorelease];
[_newDocumentIDs removeObjectAtIndex: 0];
return result;
}
- (NSArray*) getDatabases {
RESTOperation* op = [[self childWithPath: @"_all_dbs"] GET];
NSArray* names = $castIf(NSArray, op.responseBody.fromJSON); // Blocks!
return [names rest_map: ^(id name) {
return [name isKindOfClass:[NSString class]] ? [self databaseNamed: name] : nil;
}];
}
- (Class) databaseClass {
return [CouchDatabase class];
}
- (CouchDatabase*) databaseNamed: (NSString*)name {
CouchDatabase* db = (CouchDatabase*) [_dbCache resourceWithRelativePath: name];
if (!db) {
db = [[[self databaseClass] alloc] initWithParent: self relativePath: name];
if (!db)
return nil;
if (!_dbCache)
_dbCache = [[RESTCache alloc] initWithRetainLimit: 0];
[_dbCache addResource: db];
[db release];
}
return db;
}
/** Same as -databaseNamed:. Enables "[]" access in Xcode 4.4+ */
- (id)objectForKeyedSubscript:(NSString*)key {
return [self databaseNamed: key];
}
#pragma mark - REPLICATOR DATABASE:
- (CouchDatabase*) replicatorDatabase {
return [self databaseNamed: @"_replicator"];
}
- (CouchLiveQuery*) replicationsQuery {
if (!_replicationsQuery) {
CouchDatabase* replicatorDB = [self replicatorDatabase];
replicatorDB.tracksChanges = YES;
_replicationsQuery = [[[replicatorDB getAllDocuments] asLiveQuery] retain];
[_replicationsQuery wait];
}
return _replicationsQuery;
}
- (NSArray*) replications {
return [self.replicationsQuery.rows.allObjects rest_map: ^(id row) {
NSString* docID = [row documentID];
if ([docID hasPrefix: @"_design/"] || [docID hasPrefix: @"_local/"])
return (id)nil;
return [CouchPersistentReplication modelForDocument: [row document]];
}];
}
- (CouchPersistentReplication*) replicationWithSource: (NSString*)source
target: (NSString*)target
{
for (CouchPersistentReplication* repl in self.replications) {
if ($equal(repl.sourceURLStr, source) && $equal(repl.targetURLStr, target))
return repl;
}
return [CouchPersistentReplication createWithReplicatorDatabase: self.replicatorDatabase
source: source target: target];
}
#pragma mark - ACTIVITY MONITOR:
@synthesize activeTasks=_activeTasks;
- (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath
options:(NSKeyValueObservingOptions)options context:(void *)context {
if ([keyPath isEqualToString: @"activeTasks"]) {
if (_activeTasksObserverCount++ == 0)
[self setActivityPollInterval: 0.5];
}
[super addObserver: observer forKeyPath: keyPath options: options context: context];
}
- (void)removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath {
if ([keyPath isEqualToString: @"activeTasks"]) {
if (--_activeTasksObserverCount == 0)
[self setActivityPollInterval: 0.0];
}
[super removeObserver: observer forKeyPath: keyPath];
}
- (void) checkActiveTasks {
if (_activeTasksOp)
return; // already checking
if (!_activityRsrc) {
_activityRsrc = [[RESTResource alloc] initWithParent:self relativePath:@"_active_tasks"];
}
RESTOperation* op = [_activityRsrc GET];
_activeTasksOp = op;
[op onCompletion: ^{
_activeTasksOp = nil;
if (op.isSuccessful) {
[_activityRsrc cacheResponse: op];
NSArray* tasks = $castIf(NSArray, op.responseBody.fromJSON);
if (tasks && ![tasks isEqual: _activeTasks]) {
COUCHLOG2(@"CouchServer: activeTasks = %@", tasks);
self.activeTasks = tasks; // Triggers KVO notification
}
} else {
Warn(@"CouchServer: pollActivity failed with %@", op.error);
self.activityPollInterval = 0.0; // turn off polling
}
}];
}
- (void) setActivityPollInterval: (NSTimeInterval)interval {
if (interval != self.activityPollInterval) {
[_activityPollTimer invalidate];
[_activityPollTimer release];
if (interval > 0) {
_activityPollTimer = [[NSTimer scheduledTimerWithTimeInterval: interval
target: self
selector: @selector(checkActiveTasks)
userInfo: NULL
repeats: YES] retain];
[self checkActiveTasks];
} else {
_activityPollTimer = nil;
}
}
}
- (NSTimeInterval) activityPollInterval {
return _activityPollTimer ? _activityPollTimer.timeInterval : 0.0;
}
- (void) registerActiveTask: (NSDictionary*)activeTask {
// Adds an item to .activeTasks. Using this avoids a nasty race condition in classes (like
// CouchReplication) that manage tasks and observe .activeTasks. If a task has a very short
// lifespan, it might be already finished by the next time I poll _active_tasks, so it'll never
// show up in the list. And since .activeTasks doesn't change, the observers won't be notified
// and won't find out that the task is gone. By changing .activeTasks now to account for the
// new task, we know it'll change again if the task is gone on the next poll, so the observer
// will find out.
NSMutableArray* tasks = _activeTasks ? [[_activeTasks mutableCopy] autorelease]
: [NSMutableArray array];
[tasks addObject: activeTask];
self.activeTasks = tasks;
}
@end