bulk_save has an option to have couch add the uuids

This commit is contained in:
Chris Anderson 2009-01-23 23:25:24 -08:00
parent 9906df3332
commit 8f24d7d5a3
3 changed files with 10 additions and 8 deletions

View file

@ -1,6 +1,6 @@
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = %q{couchrest} s.name = %q{couchrest}
s.version = "0.12.4" s.version = "0.12.5"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["J. Chris Anderson"] s.authors = ["J. Chris Anderson"]

View file

@ -25,7 +25,7 @@ require 'couchrest/monkeypatches'
# = CouchDB, close to the metal # = CouchDB, close to the metal
module CouchRest module CouchRest
VERSION = '0.12.4' VERSION = '0.12.5'
autoload :Server, 'couchrest/core/server' autoload :Server, 'couchrest/core/server'
autoload :Database, 'couchrest/core/database' autoload :Database, 'couchrest/core/database'

View file

@ -149,16 +149,18 @@ module CouchRest
# missing ids, supply one from the uuid cache. # missing ids, supply one from the uuid cache.
# #
# If called with no arguments, bulk saves the cache of documents to be bulk saved. # If called with no arguments, bulk saves the cache of documents to be bulk saved.
def bulk_save (docs = nil) def bulk_save(docs = nil, use_uuids = true)
if docs.nil? if docs.nil?
docs = @bulk_save_cache docs = @bulk_save_cache
@bulk_save_cache = [] @bulk_save_cache = []
end end
ids, noids = docs.partition{|d|d['_id']} if (use_uuids)
uuid_count = [noids.length, @server.uuid_batch_count].max ids, noids = docs.partition{|d|d['_id']}
noids.each do |doc| uuid_count = [noids.length, @server.uuid_batch_count].max
nextid = @server.next_uuid(uuid_count) rescue nil noids.each do |doc|
doc['_id'] = nextid if nextid nextid = @server.next_uuid(uuid_count) rescue nil
doc['_id'] = nextid if nextid
end
end end
CouchRest.post "#{@root}/_bulk_docs", {:docs => docs} CouchRest.post "#{@root}/_bulk_docs", {:docs => docs}
end end