lib/sdbport/domain/import.rb in sdbport-0.1.1 vs lib/sdbport/domain/import.rb in sdbport-0.2.0
- old
+ new
@@ -8,10 +8,11 @@
@name = args[:name]
@logger = args[:logger]
@access_key = args[:access_key]
@secret_key = args[:secret_key]
@region = args[:region]
+ @buffer = {}
end
def import(input)
create_domain
@@ -19,37 +20,53 @@
@logger.info "Importing #{@name} in #{@region} from #{input}."
file = File.open(input, 'r')
while (line = file.gets)
- add_line line
+ add_line_to_buffer line
+ write_buffer if buffer_full?
end
+ write_buffer unless buffer_empty?
true
end
private
- def create_domain
- sdb.create_domain_unless_present @name
- end
-
- def add_line(line)
+ def add_line_to_buffer(line)
line.chomp!
data = JSON.parse line
id = data.first
attributes = data.last
@logger.debug "Adding #{id} with attributes #{attributes.to_s}."
- sdb.put_attributes @name, id, attributes
+ @buffer.merge!({ id => attributes })
end
+ def buffer_full?
+ @buffer.count > 24
+ end
+
+ def buffer_empty?
+ @buffer.count.zero?
+ end
+
+ def write_buffer
+ @logger.debug "Writing #{@buffer.count} entries to SimpleDB."
+ sdb.batch_put_attributes @name, @buffer
+ @buffer.clear
+ end
+
def ensure_domain_empty
if sdb.domain_empty? @name
true
else
@logger.error "Domain #{@name} in #{@region} not empty."
false
end
+ end
+
+ def create_domain
+ sdb.create_domain_unless_present @name
end
def sdb
@sdb ||= AWS::SimpleDB.new :access_key => @access_key,
:secret_key => @secret_key,