Permalink
Browse files

Added -[CouchDocument getLeafRevisions] and CouchQuery.includeDeleted

These require the ?include_deleted support I just added to TouchDB.
  • Loading branch information...
1 parent a16fc17 commit cb77050aaadb83cf416e0a1358372eabdb07c4fa @snej snej committed Sep 4, 2012
Showing with 41 additions and 19 deletions.
  1. +5 −0 Couch/CouchDocument.h
  2. +26 −16 Couch/CouchDocument.m
  3. +5 −1 Couch/CouchQuery.h
  4. +5 −2 Couch/CouchQuery.m
View
@@ -63,6 +63,11 @@
The number of historical revisions available may vary; it depends on how recently the database has been compacted. You should not rely on earlier revisions being available, except for those representing unresolved conflicts. */
- (NSArray*) getRevisionHistory;
+/** Returns all the leaf revisions in the document's revision tree.
+ With TouchDB, this includes deleted revisions (i.e. previously-resolved conflicts.)
+ With CouchDB, deleted revisions are NOT included, so this method acts identically to -getConflictingRevisions. */
+- (NSArray*) getLeafRevisions;
+
#pragma mark PROPERTIES:
/** The contents of the current revision of the document.
View
@@ -157,35 +157,45 @@ - (NSArray*) getRevisionHistory {
#pragma mark CONFLICTS:
-- (NSArray*) getConflictingRevisions {
+- (NSArray*) getLeafRevisionsIncludingDeleted: (BOOL)includeDeleted {
// http://wiki.apache.org/couchdb/Replication_and_conflicts
// Apparently open_revs isn't official API, and ?conflicts is preferred, but open_revs
// has the advantage of returning the contents of all conflicting revisions at once.
- RESTOperation* op = [self sendHTTP: @"GET"
- parameters: [NSDictionary dictionaryWithObjectsAndKeys:
- @"all", @"?open_revs",
- @"application/json", @"Accept",
- nil]];
+ NSMutableDictionary* parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:
+ @"all", @"?open_revs",
+ @"application/json", @"Accept",
+ nil];
+ if (includeDeleted)
+ [parameters setObject: @"true" forKey: @"?include_deleted"];
+ RESTOperation* op = [self sendHTTP: @"GET"
+ parameters: parameters];
if (![op wait])
return nil;
NSArray* items = $castIf(NSArray, op.responseBody.fromJSON);
if (!items)
return nil;
return [items rest_map: ^id(id item) {
NSDictionary* contents = $castIf(NSDictionary, [item objectForKey: @"ok"]);
- if (![[contents objectForKey: @"_deleted"] boolValue]) {
- NSString* revisionID = $castIf(NSString, [contents objectForKey: @"_rev"]);
- if (revisionID) {
- CouchRevision* revision = [self revisionWithID: revisionID];
- if (!revision.propertiesAreLoaded)
- revision.properties = contents;
- return revision;
- }
- }
- return nil;
+ if (!includeDeleted && [[contents objectForKey: @"_deleted"] boolValue])
+ return nil; // ignore this rev
+ NSString* revisionID = $castIf(NSString, [contents objectForKey: @"_rev"]);
+ if (!revisionID)
+ return nil; // bogus rev; ignore
+ CouchRevision* revision = [self revisionWithID: revisionID];
+ if (!revision.propertiesAreLoaded)
+ revision.properties = contents;
+ return revision;
}];
}
+- (NSArray*) getConflictingRevisions {
+ return [self getLeafRevisionsIncludingDeleted: NO];
+}
+
+- (NSArray*) getLeafRevisions {
+ return [self getLeafRevisionsIncludingDeleted: YES];
+}
+
- (RESTOperation*) resolveConflictingRevisions: (NSArray*)conflicts
withProperties: (NSDictionary*)properties
View
@@ -35,7 +35,7 @@ typedef enum {
NSString* _startKeyDocID;
NSString* _endKeyDocID;
CouchStaleness _stale;
- BOOL _descending, _prefetch, _sequences;
+ BOOL _descending, _prefetch, _sequences, _includeDeleted;
NSArray *_keys;
NSUInteger _groupLevel;
NSError* _error;
@@ -85,6 +85,10 @@ typedef enum {
@property BOOL sequences;
+/** If set to YES, deleted documents are included in a getAllDocuments query.
+ This has NO EFFECT in view queries, and it's only supported on TouchDB, not CouchDB. */
+@property BOOL includeDeleted;
+
/** If non-nil, the error of the last execution of the query.
If nil, the last exexution of the query was successful */
@property (readonly,retain) NSError* error;
View
@@ -53,8 +53,8 @@ - (id) initWithQuery: (CouchQuery*)query {
_groupLevel = query.groupLevel;
self.startKeyDocID = query.startKeyDocID;
self.endKeyDocID = query.endKeyDocID;
+ _includeDeleted = query.includeDeleted;
_stale = query.stale;
-
}
return self;
}
@@ -74,7 +74,8 @@ - (void) dealloc
@synthesize limit=_limit, skip=_skip, descending=_descending, startKey=_startKey, endKey=_endKey,
prefetch=_prefetch, keys=_keys, groupLevel=_groupLevel, startKeyDocID=_startKeyDocID,
- endKeyDocID=_endKeyDocID, stale=_stale, sequences=_sequences, error=_error;
+ endKeyDocID=_endKeyDocID, stale=_stale, sequences=_sequences,
+ includeDeleted=_includeDeleted, error=_error;
- (CouchDesignDocument*) designDocument {
@@ -120,6 +121,8 @@ - (NSMutableDictionary*) requestParams {
[params setObject: @"true" forKey: @"?local_seq"];
if (_groupLevel > 0)
[params setObject: [NSNumber numberWithUnsignedLong: _groupLevel] forKey: @"?group_level"];
+ if (_includeDeleted)
+ [params setObject: @"true" forKey: @"?include_deleted"];
[params setObject: @"true" forKey: @"?update_seq"];
return params;
}

0 comments on commit cb77050

Please sign in to comment.