lib/ddbcli/ddb-driver.rb in ddbcli-0.1.4 vs lib/ddbcli/ddb-driver.rb in ddbcli-0.1.5
- old
+ new
@@ -60,10 +60,12 @@
do_alter_table(parsed)
when :USE
do_use(parsed)
when :CREATE
do_create(parsed)
+ when :CREATE_LIKE
+ do_create_like(parsed)
when :DROP
do_drop(parsed)
when :DESCRIBE
do_describe(parsed)
when :SELECT
@@ -92,30 +94,63 @@
nil
else
raise 'must not happen'
end
- begin
- case script_type
- when :ruby
- retval = retval.data if retval.kind_of?(DynamoDB::Iteratorable)
- retval.instance_eval(script)
- when :shell
- retval = retval.data if retval.kind_of?(DynamoDB::Iteratorable)
- IO.popen(script, "r+") do |f|
- f.puts(retval.kind_of?(Array) ? retval.map {|i| i.to_s }.join("\n") : retval.to_s)
- f.close_write
- f.read
- end
- else
- retval
- end
- rescue Exception => e
- raise DynamoDB::Error, e.message, e.backtrace
- end
- end
+ begin
+ case script_type
+ when :ruby
+ retval = retval.data if retval.kind_of?(DynamoDB::Iteratorable)
+ retval.instance_eval(script)
+ when :shell
+ retval = retval.data if retval.kind_of?(DynamoDB::Iteratorable)
+ IO.popen(script, "r+") do |f|
+ f.puts(retval.kind_of?(Array) ? retval.map {|i| i.to_s }.join("\n") : retval.to_s)
+ f.close_write
+ f.read
+ end
+ else
+ retval
+ end
+ rescue Exception => e
+ raise DynamoDB::Error, e.message, e.backtrace
+ end
+ end
+ def import(table, items)
+ n = 0
+
+ until (chunk = items.slice!(0, MAX_NUMBER_BATCH_PROCESS_ITEMS)).empty?
+ operations = []
+
+ req_hash = {
+ 'RequestItems' => {
+ table => operations,
+ },
+ }
+
+ chunk.each do |item|
+ h = {}
+
+ operations << {
+ 'PutRequest' => {
+ 'Item' => h,
+ },
+ }
+
+ item.each do |name, val|
+ h[name] = convert_to_attribute_value(val)
+ end
+ end
+
+ batch_write_item(req_hash)
+ n += chunk.length
+ end
+
+ return n
+ end
+
private
def do_show_tables(parsed)
req_hash = {}
table_names = []
@@ -296,9 +331,45 @@
end
req_hash['LocalSecondaryIndexes'] << local_secondary_index
end
end # local secondary index
+
+ @client.query('CreateTable', req_hash)
+ nil
+ end
+
+ def do_create_like(parsed)
+ table_info = @client.query('DescribeTable', 'TableName' => parsed.like)['Table']
+
+ req_hash = {
+ 'TableName' => parsed.table,
+ 'AttributeDefinitions' => table_info['AttributeDefinitions'],
+ 'KeySchema' => table_info['KeySchema'],
+ }
+
+ local_secondary_indexes = (table_info['LocalSecondaryIndexes'] || [])
+
+ unless local_secondary_indexes.empty?
+ req_hash['LocalSecondaryIndexes'] = local_secondary_indexes.map do |lsi|
+ h = {}
+
+ %w(IndexName KeySchema Projection).each do |i|
+ h[i] = lsi[i]
+ end
+
+ h
+ end
+ end
+
+ if parsed.capacity
+ req_hash['ProvisionedThroughput'] = {
+ 'ReadCapacityUnits' => parsed.capacity[:read],
+ 'WriteCapacityUnits' => parsed.capacity[:write],
+ }
+ else
+ req_hash['ProvisionedThroughput'] = table_info['ProvisionedThroughput']
+ end
@client.query('CreateTable', req_hash)
nil
end