Class: AWS::DynamoDB::BatchWrite
- Inherits:
-
Object
- Object
- AWS::DynamoDB::BatchWrite
- Includes:
- Core::Model, Types
- Defined in:
- lib/aws/dynamo_db/batch_write.rb
Instance Attribute Summary
Attributes included from Core::Model
Instance Method Summary collapse
-
#delete(table, items) ⇒ nil
Adds one or more items to the batch to delete.
-
#initialize(options = {}) ⇒ BatchWrite
constructor
A new instance of BatchWrite.
-
#process! ⇒ nil
Proccesses pending request items.
-
#put(table, items) ⇒ nil
Adds one or more items to the batch write operation.
-
#write(table, options = {}) ⇒ Object
Add items to the batch.
Methods included from Core::Model
#client, #config_prefix, #inspect
Methods included from Types
#format_attribute_value, #value_from_response, #values_from_response_hash
Constructor Details
#initialize(options = {}) ⇒ BatchWrite
Returns a new instance of BatchWrite.
21 22 23 24 |
# File 'lib/aws/dynamo_db/batch_write.rb', line 21 def initialize = {} super() @request_items = {} end |
Instance Method Details
#delete(table, items) ⇒ nil
Adds one or more items to the batch to delete.
# for a table w/out a range key
batch = AWS::DynamoDB::BatchWrite.new
batch.delete('table-name', %w(hk1 hk2))
batch.process!
# for a table with a range key
batch = AWS::DynamoDB::BatchWrite.new
batch.delete('table-name', [['hk1', 'rk2'], ['hk1', 'rk2']]])
batch.process!
83 84 85 86 |
# File 'lib/aws/dynamo_db/batch_write.rb', line 83 def delete table, items write(table, :delete => items) nil end |
#process! ⇒ nil
Proccesses pending request items.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/aws/dynamo_db/batch_write.rb', line 136 def process! return if @request_items.empty? opts = { :request_items => @request_items } begin response = client.batch_write_item(opts) unprocessed = response.data['UnprocessedItems'] opts[:request_items] = convert_unprocessed_items(unprocessed) end while opts[:request_items] @request_items = {} nil end |
#put(table, items) ⇒ nil
Adds one or more items to the batch write operation.
# adding one item at a time to the batch
batch = AWS::DynamoDB::BatchWrite.new
batch.put('table-name', :id => 'id1', :color => 'red')
batch.put('table-name', :id => 'id2', :color => 'blue')
batch.process!
# adding multiple items to a batch
batch = AWS::DynamoDB::BatchWrite.new
batch.put('table-name', [
{ :id => 'id1', :color => 'red' },
{ :id => 'id2', :color => 'blue' },
{ :id => 'id3', :color => 'green' },
])
batch.process!
51 52 53 54 |
# File 'lib/aws/dynamo_db/batch_write.rb', line 51 def put table, items write(table, :put => items.flatten) nil end |
#write(table, options = {}) ⇒ Object
Add items to the batch. Accepts both item to put and and items to delete.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/aws/dynamo_db/batch_write.rb', line 116 def write table, = {} items = table_items(table) if put = [:put] put.each do |attributes| items << { :put_request => { :item => format_put(attributes) }} end end if del = [:delete] del.each do |keys| items << { :delete_request => { :key => format_delete(keys) }} end end end |