Browse files

Merge pull request #80 from matthiasjakel/master

Add support for atomic bulk save requests
  • Loading branch information...
2 parents 2ee7f1f + 21e2bb9 commit 239e6c95cfec7cb027bc613f9bb24f82c5751d64 @samlown samlown committed Dec 9, 2011
Showing with 13 additions and 2 deletions.
  1. +6 −2 lib/couchrest/database.rb
  2. +7 −0 spec/couchrest/database_spec.rb
View
8 lib/couchrest/database.rb
@@ -163,7 +163,7 @@ def batch_save_doc(doc)
# missing ids, supply one from the uuid cache.
#
# If called with no arguments, bulk saves the cache of documents to be bulk saved.
- def bulk_save(docs = nil, use_uuids = true)
+ def bulk_save(docs = nil, use_uuids = true, all_or_nothing = false)
if docs.nil?
docs = @bulk_save_cache
@bulk_save_cache = []
@@ -176,7 +176,11 @@ def bulk_save(docs = nil, use_uuids = true)
doc['_id'] = nextid if nextid
end
end
- CouchRest.post "#{@root}/_bulk_docs", {:docs => docs}
+ request_body = {:docs => docs}
+ if all_or_nothing
+ request_body[:all_or_nothing] = true
+ end
+ CouchRest.post "#{@root}/_bulk_docs", request_body
end
alias :bulk_delete :bulk_save
View
7 spec/couchrest/database_spec.rb
@@ -246,6 +246,13 @@
@db.bulk_save
@db.get("bulk_cache_1")["val"].should == "test"
end
+
+ it "should make an atomic write when all_or_nothing is set" do
+ docs = [{"_id" => "oneB", "wild" => "and random"}, {"_id" => "twoB", "mild" => "yet local"}]
+ CouchRest.should_receive(:post).with("#{COUCHHOST}/couchrest-test/_bulk_docs", {:all_or_nothing => true, :docs => docs})
+
+ @db.bulk_save(docs, false, true)
+ end
it "should raise an error that is useful for recovery" do
@r = @db.save_doc({"_id" => "taken", "field" => "stuff"})

0 comments on commit 239e6c9

Please sign in to comment.