Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Simple_Record to support Batch Saves to sharded domains #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions lib/simple_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -767,26 +767,39 @@ 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)
raise "Pre save failed on object [" + o.inspect + "]" if !ok
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
Expand Down