Class: Aws::Record::Batch

Inherits:
Object
  • Object
show all
Extended by:
ClientConfiguration
Defined in:
lib/aws-record/record/batch.rb

Class Method Summary collapse

Methods included from ClientConfiguration

configure_client, dynamodb_client

Class Method Details

.read(opts = {}) {|batch| ... } ⇒ Aws::Record::BatchRead

Provides support for the Aws::DynamoDB::Client#batch_get_item for aws-record models.

Aws::Record::Batch is Enumerable and using Enumerable methods will handle paging through all requested keys automatically. Alternatively, a lower level interface is available. You can determine if there are any unprocessed keys by calling .complete? and any unprocessed keys can be processed by calling .execute!. You can access all processed items through .items.

The batch_get_item supports up to 100 operations in a single call and a single operation can retrieve up to 16 MB of data.

Aws::Record::BatchRead can take more than 100 item keys. The first 100 requests will be processed and the remaining requests will be stored. When using Enumerable methods, any pending item keys will be automatically processed and the new items will be added to items. Alternately, use .execute! to process any pending item keys.

All processed operations can be accessed by items - which is an array of modeled items from the response. The items will be unordered since DynamoDB does not return items in any particular order.

If a requested item does not exist in the database, it is not returned in the response.

If there is a returned item from the call and there’s no reference model class to be found, the item will not show up under items.

Examples:

Usage Example

class Lunch
  include Aws::Record
  integer_attr :id,   hash_key: true
  string_attr  :name, range_key: true
end

class Dessert
  include Aws::Record
  integer_attr :id,   hash_key: true
  string_attr  :name, range_key: true
end

# batch operations
operation = Aws::Record::Batch.read do |db|
  db.find(Lunch, id: 1, name: 'Papaya Salad')
  db.find(Lunch, id: 2, name: 'BLT Sandwich')
  db.find(Dessert, id: 1, name: 'Apple Pie')
end

# BatchRead is enumerable and handles pagination
operation.each { |item| item.id }

# Alternatively, BatchRead provides a lower level
# interface through: execute!, complete? and items.
# Unprocessed items can be processed by calling:
operation.execute! until operation.complete?

Parameters:

  • opts (Hash) (defaults to: {})

    the options you wish to use to create the client. Note that if you include the option :client, all other options will be ignored. See the documentation for other options in the AWS SDK for Ruby.

Options Hash (opts):

  • :client (Aws::DynamoDB::Client)

    allows you to pass in your own pre-configured client.

Yields:

  • (batch)

Returns:

  • (Aws::Record::BatchRead)

    An instance that contains modeled items from the BatchGetItem result and stores unprocessed keys to be manually processed later.



138
139
140
141
142
143
# File 'lib/aws-record/record/batch.rb', line 138

def read(opts = {})
  batch = BatchRead.new(client: _build_client(opts))
  yield(batch)
  batch.execute!
  batch
end

.write(opts = {}) {|batch| ... } ⇒ Aws::Record::BatchWrite

Provides a thin wrapper to the Aws::DynamoDB::Client#batch_write_item method. Up to 25 PutItem or DeleteItem operations are supported. A single request may write up to 16 MB of data, with each item having a write limit of 400 KB.

Note: this operation does not support dirty attribute handling, nor does it enforce safe write operations (i.e. update vs new record checks).

This call may partially execute write operations. Failed operations are returned as unprocessed_items (i.e. the table fails to meet requested write capacity). Any unprocessed items may be retried by calling .execute! again. You can determine if the request needs to be retried by calling the .complete? method - which returns true when all operations have been completed.

Please see Batch Operations and Error Handling in the DynamoDB Developer Guide for more details.

Examples:

Usage Example

class Breakfast
  include Aws::Record
  integer_attr :id,   hash_key: true
  string_attr  :name, range_key: true
  string_attr  :body
end

# setup
eggs = Breakfast.new(id: 1, name: "eggs").save!
waffles = Breakfast.new(id: 2, name: "waffles")
pancakes = Breakfast.new(id: 3, name: "pancakes")

# batch operations
operation = Aws::Record::Batch.write(client: Breakfast.dynamodb_client) do |db|
  db.put(waffles)
  db.delete(eggs)
  db.put(pancakes)
end

# unprocessed items can be retried by calling Aws::Record::BatchWrite#execute!
operation.execute! until operation.complete?

Parameters:

  • opts (Hash) (defaults to: {})

    the options you wish to use to create the client. Note that if you include the option :client, all other options will be ignored. See the documentation for other options in the AWS SDK for Ruby.

Options Hash (opts):

  • :client (Aws::DynamoDB::Client)

    allows you to pass in your own pre-configured client.

Yields:

  • (batch)

Returns:



65
66
67
68
69
# File 'lib/aws-record/record/batch.rb', line 65

def write(opts = {})
  batch = BatchWrite.new(client: _build_client(opts))
  yield(batch)
  batch.execute!
end