diff --git a/lib/simple_record.rb b/lib/simple_record.rb index 449c2be..838ac59 100644 --- a/lib/simple_record.rb +++ b/lib/simple_record.rb @@ -767,11 +767,14 @@ def get_atts_to_delete end # Run pre_save on each object, then runs batch_put_attributes - # Returns + # Returns + # Modified to support using a hash instead of an array, to capture + # sharded domain. Also removes restriction on 25 records (will write multiple) + # batches of 25 if more than 25 batch options are supplied. def self.batch_save(objects, options={}) options[:create_domain] = true if options[:create_domain].nil? results = [] - to_save = [] + to_save = {} if objects && objects.size > 0 objects.each do |o| ok = o.pre_save(options) @@ -779,14 +782,24 @@ def self.batch_save(objects, options={}) results << ok next if !ok # todo: this shouldn't be here should it? raises above o.pre_save2 - to_save << Aws::SdbInterface::Item.new(o.id, o.attributes, true) - if to_save.size == 25 # Max amount SDB will accept - connection.batch_put_attributes(domain, to_save, options) - to_save.clear - end + if to_save[o.sharded_domain] + to_save[o.sharded_domain] << Aws::SdbInterface::Item.new(o.id, o.attributes, true) + else + to_save[o.sharded_domain] = [Aws::SdbInterface::Item.new(o.id, o.attributes, true)] + end + #if to_save[o.sharded_domain].size == 25 # Max amount SDB will accept + # connection.batch_put_attributes(domain, to_save[o.sharded_domai], options) + # to_save.clear + #end + end + end + # connection.batch_put_attributes(domain, to_save, options) if to_save.size > 0 + to_save.each_pair do |k,v| + v.each_slice(25) do |g| + connection.batch_put_attributes(k, g, options) if g.size > 0 end end - connection.batch_put_attributes(domain, to_save, options) if to_save.size > 0 + objects.each do |o| o.save_lobs(nil) end