Skip to content

Commit

Permalink
Add support for atomic bulk save requests
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasjakel committed Dec 9, 2011
1 parent 2ee7f1f commit 21e2bb9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/couchrest/database.rb
Expand Up @@ -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 = []
Expand All @@ -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

Expand Down
7 changes: 7 additions & 0 deletions spec/couchrest/database_spec.rb
Expand Up @@ -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"})
Expand Down

0 comments on commit 21e2bb9

Please sign in to comment.