Skip to content
This repository
Browse code

REST _changes now supports style=all_docs, to show conflicting revisi…

…on IDs.
  • Loading branch information...
commit 4e0d3944da541f710198f51dc094b20b5b73094a 1 parent 4c86648
Jens Alfke snej authored

Showing 1 changed file with 30 additions and 3 deletions. Show diff stats Hide diff stats

  1. +30 3 Source/TDRouter.m
33 Source/TDRouter.m
@@ -508,12 +508,11 @@ - (TDStatus) do_POST_ensure_full_commit: (TDDatabase*)db {
508 508 - (NSDictionary*) changeDictForRev: (TDRevision*)rev {
509 509 return $dict({@"seq", $object(rev.sequence)},
510 510 {@"id", rev.docID},
511   - {@"changes", $array($dict({@"rev", rev.revID}))},
  511 + {@"changes", $marray($dict({@"rev", rev.revID}))},
512 512 {@"deleted", rev.deleted ? $true : nil},
513 513 {@"doc", (_changesIncludeDocs ? rev.properties : nil)});
514 514 }
515 515
516   -
517 516 - (NSDictionary*) responseBodyForChanges: (NSArray*)changes since: (UInt64)since {
518 517 NSArray* results = [changes my_map: ^(id rev) {return [self changeDictForRev: rev];}];
519 518 if (changes.count > 0)
@@ -522,6 +521,29 @@ - (NSDictionary*) responseBodyForChanges: (NSArray*)changes since: (UInt64)since
522 521 }
523 522
524 523
  524 +- (NSDictionary*) responseBodyForChangesWithConflicts: (NSArray*)changes since: (UInt64)since {
  525 + // Assumes the changes are grouped by docID so that conflicts will be adjacent.
  526 + NSMutableArray* entries = [NSMutableArray arrayWithCapacity: changes.count];
  527 + NSString* lastDocID = nil;
  528 + NSDictionary* lastEntry = nil;
  529 + for (TDRevision* rev in changes) {
  530 + NSString* docID = rev.docID;
  531 + if ($equal(docID, lastDocID)) {
  532 + [[lastEntry objectForKey: @"changes"] addObject: $dict({@"rev", rev.revID})];
  533 + } else {
  534 + lastEntry = [self changeDictForRev: rev];
  535 + [entries addObject: lastEntry];
  536 + lastDocID = docID;
  537 + }
  538 + }
  539 + // After collecting revisions, sort by sequence:
  540 + [entries sortUsingComparator: ^NSComparisonResult(id e1, id e2) {
  541 + return [[e1 objectForKey: @"seq"] longLongValue] - [[e2 objectForKey: @"seq"] longLongValue];
  542 + }];
  543 + return $dict({@"results", entries}, {@"last_seq", $object(since)});
  544 +}
  545 +
  546 +
525 547 - (void) sendContinuousChange: (TDRevision*)rev {
526 548 NSDictionary* changeDict = [self changeDictForRev: rev];
527 549 NSMutableData* json = [[NSJSONSerialization dataWithJSONObject: changeDict
@@ -559,6 +581,7 @@ - (TDStatus) do_GET_changes: (TDDatabase*)db {
559 581 _changesIncludeDocs = [self boolQuery: @"include_docs"];
560 582 options.includeDocs = _changesIncludeDocs;
561 583 options.includeConflicts = $equal([self query: @"style"], @"all_docs");
  584 + options.sortBySequence = !options.includeConflicts;
562 585 options.limit = [self intQuery: @"limit" defaultValue: options.limit];
563 586 int since = [[self query: @"since"] intValue];
564 587
@@ -593,7 +616,11 @@ - (TDStatus) do_GET_changes: (TDDatabase*)db {
593 616 _waiting = YES;
594 617 return 0;
595 618 } else {
596   - _response.bodyObject = [self responseBodyForChanges: changes.allRevisions since: since];
  619 + if (options.includeConflicts)
  620 + _response.bodyObject = [self responseBodyForChangesWithConflicts: changes.allRevisions
  621 + since: since];
  622 + else
  623 + _response.bodyObject = [self responseBodyForChanges: changes.allRevisions since: since];
597 624 return 200;
598 625 }
599 626 }

0 comments on commit 4e0d394

Please sign in to comment.
Something went wrong with that request. Please try again.