Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

_bulk_docs now supports new_edits=false

TouchDB now passes the conflicts.js unit test.
  • Loading branch information...
commit 10ceb2eae58de6915465e9fd488570de56948688 1 parent 666dbca
@snej snej authored
Showing with 47 additions and 32 deletions.
  1. +13 −8 Source/TDDatabase+Insertion.m
  2. +34 −24 Source/TDRouter+Handlers.m
View
21 Source/TDDatabase+Insertion.m
@@ -110,11 +110,11 @@ + (NSArray*) parseCouchDBRevisionHistory: (NSDictionary*)docProperties {
if (!revisions)
return nil;
// Extract the history, expanding the numeric prefixes:
- __block int start = [$castIf(NSNumber, [revisions objectForKey: @"start"]) intValue];
NSArray* revIDs = $castIf(NSArray, [revisions objectForKey: @"ids"]);
- return [revIDs my_map: ^(id revID) {
- return (start ? $sprintf(@"%d-%@", start--, revID) : revID);
- }];
+ __block int start = [$castIf(NSNumber, [revisions objectForKey: @"start"]) intValue];
+ if (start)
+ revIDs = [revIDs my_map: ^(id revID) {return $sprintf(@"%d-%@", start--, revID);}];
+ return revIDs;
}
@@ -352,11 +352,16 @@ - (TDStatus) forceInsert: (TDRevision*)rev
revisionHistory: (NSArray*)history // in *reverse* order, starting with rev's revID
source: (NSURL*)source
{
- NSUInteger historyCount = history.count;
- if (historyCount < 1 || !$equal([history objectAtIndex: 0], rev.revID))
- return 400;
NSString* docID = rev.docID;
- if (![TDDatabase isValidDocumentID: docID])
+ NSString* revID = rev.revID;
+ if (![TDDatabase isValidDocumentID: docID] || !revID)
+ return 400;
+
+ NSUInteger historyCount = history.count;
+ if (historyCount == 0) {
+ history = $array(revID);
+ historyCount = 1;
+ } else if (!$equal([history objectAtIndex: 0], revID))
return 400;
BOOL success = NO;
View
58 Source/TDRouter+Handlers.m
@@ -229,35 +229,47 @@ - (TDStatus) do_POST_bulk_docs: (TDDatabase*)db {
return 400;
id allObj = [body objectForKey: @"all_or_nothing"];
BOOL allOrNothing = (allObj && allObj != $false);
- //BOOL noNewEdits = ([body objectForKey: @"new_edits"] == $false);
+ BOOL noNewEdits = ([body objectForKey: @"new_edits"] == $false);
BOOL ok = NO;
NSMutableArray* results = [NSMutableArray arrayWithCapacity: docs.count];
[_db beginTransaction];
@try{
for (NSDictionary* doc in docs) {
- NSString* docID = [doc objectForKey: @"_id"];
- TDRevision* rev;
- TDStatus status = [self update: db
- docID: docID
- body: [TDBody bodyWithProperties: doc]
- deleting: NO
- allowConflict: allOrNothing
- createdRev: &rev];
- NSDictionary* result;
- if (status < 300) {
- Assert(rev.revID);
- result = $dict({@"id", rev.docID}, {@"rev", rev.revID}, {@"ok", $true});
- } else if (allOrNothing) {
- return status; // all_or_nothing backs out if there's any error
- } else if (status == 403) {
- result = $dict({@"id", docID}, {@"error", @"validation failed"});
- } else if (status == 409) {
- result = $dict({@"id", docID}, {@"error", @"conflict"});
- } else {
- return status; // abort the whole thing if something goes badly wrong
+ @autoreleasepool {
+ NSString* docID = [doc objectForKey: @"_id"];
+ TDRevision* rev;
+ TDStatus status;
+ TDBody* docBody = [TDBody bodyWithProperties: doc];
+ if (noNewEdits) {
+ rev = [[[TDRevision alloc] initWithBody: docBody] autorelease];
+ NSArray* history = [TDDatabase parseCouchDBRevisionHistory: doc];
+ status = rev ? [db forceInsert: rev revisionHistory: history source: nil] : 400;
+ } else {
+ status = [self update: db
+ docID: docID
+ body: docBody
+ deleting: NO
+ allowConflict: allOrNothing
+ createdRev: &rev];
+ }
+ NSDictionary* result = nil;
+ if (status < 300) {
+ Assert(rev.revID);
+ if (!noNewEdits)
+ result = $dict({@"id", rev.docID}, {@"rev", rev.revID}, {@"ok", $true});
+ } else if (allOrNothing) {
+ return status; // all_or_nothing backs out if there's any error
+ } else if (status == 403) {
+ result = $dict({@"id", docID}, {@"error", @"validation failed"});
+ } else if (status == 409) {
+ result = $dict({@"id", docID}, {@"error", @"conflict"});
+ } else {
+ return status; // abort the whole thing if something goes badly wrong
+ }
+ if (result)
+ [results addObject: result];
}
- [results addObject: result];
}
ok = YES;
} @finally {
@@ -552,8 +564,6 @@ - (TDStatus) do_PUT: (TDDatabase*)db docID: (NSString*)docID {
if (!rev || !$equal(rev.docID, docID) || !rev.revID)
return 400;
NSArray* history = [TDDatabase parseCouchDBRevisionHistory: body.properties];
- if (!history)
- history = $array(rev.revID);
return [_db forceInsert: rev revisionHistory: history source: nil];
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.