Permalink
Browse files

_changes was returning wrong revisions when style=_all_docs

With ?style=all_docs&limit=nnn, the results were clipped to the limit _before_ being sorted by sequence,
so the wrong set of results got returned. This screwed up a remote server trying to pull from this database.
Fixes #131
  • Loading branch information...
snej committed Aug 15, 2012
1 parent c2e01f9 commit 8c44481347294488f7f774e25c2561b7862ef993
Showing with 11 additions and 4 deletions.
  1. +3 −2 Source/TDDatabase.m
  2. +8 −2 Source/TDRouter+Handlers.m
View
@@ -772,9 +772,10 @@ - (TDRevisionList*) changesSinceSequence: (SequenceNumber)lastSequence
}
[r close];
- if (options->sortBySequence)
+ if (options->sortBySequence) {
[changes sortBySequence];
- [changes limit: options->limit];
+ [changes limit: options->limit];
+ }
return changes;
}
View
@@ -404,7 +404,10 @@ - (NSDictionary*) responseBodyForChanges: (NSArray*)changes since: (UInt64)since
}
-- (NSDictionary*) responseBodyForChangesWithConflicts: (NSArray*)changes since: (UInt64)since {
+- (NSDictionary*) responseBodyForChangesWithConflicts: (NSArray*)changes
+ since: (UInt64)since
+ limit: (NSUInteger)limit
+{
// Assumes the changes are grouped by docID so that conflicts will be adjacent.
NSMutableArray* entries = [NSMutableArray arrayWithCapacity: changes.count];
NSString* lastDocID = nil;
@@ -424,6 +427,8 @@ - (NSDictionary*) responseBodyForChangesWithConflicts: (NSArray*)changes since:
return TDSequenceCompare([[e1 objectForKey: @"seq"] longLongValue],
[[e2 objectForKey: @"seq"] longLongValue]);
}];
+ if (entries.count > limit)
+ [entries removeObjectsInRange: NSMakeRange(limit, entries.count - limit)];
id lastSeq = [entries.lastObject objectForKey: @"seq"] ?: $object(since);
return $dict({@"results", entries}, {@"last_seq", lastSeq});
}
@@ -513,7 +518,8 @@ - (TDStatus) do_GET_changes: (TDDatabase*)db {
// Return a response immediately and close the connection:
if (options.includeConflicts)
_response.bodyObject = [self responseBodyForChangesWithConflicts: changes.allRevisions
- since: since];
+ since: since
+ limit: options.limit];
else
_response.bodyObject = [self responseBodyForChanges: changes.allRevisions since: since];
return kTDStatusOK;

0 comments on commit 8c44481

Please sign in to comment.