Class: Aws::Record::BatchRead

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/aws-record/record/batch_read.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ BatchRead

Returns a new instance of BatchRead.

Parameters:

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

Options Hash (opts):

  • client (Aws::DynamoDB::Client)

    the DynamoDB SDK client.



13
14
15
# File 'lib/aws-record/record/batch_read.rb', line 13

def initialize(opts = {})
  @client = opts[:client]
end

Instance Method Details

#complete?Boolean

Indicates if all item keys have been processed.

See Aws::Record::Batch.read for example usage.

Returns:

  • (Boolean)

    true if all item keys has been processed, false otherwise.



78
79
80
# File 'lib/aws-record/record/batch_read.rb', line 78

def complete?
  unprocessed_keys.none?
end

#each {|item| ... } ⇒ Enumerable<BatchRead>

Provides an enumeration of the results from the batch_get_item request and handles pagination.

Any pending item keys will be automatically processed and be added to the #items.

See Aws::Record::Batch.read for example usage.

Yield Parameters:

Returns:

  • (Enumerable<BatchRead>)

    an enumeration over the results of batch_get_item request.



63
64
65
66
67
68
69
70
71
72
# File 'lib/aws-record/record/batch_read.rb', line 63

def each(&block)
  return enum_for(:each) unless block_given?

  @items.each(&block)

  until complete?
    new_items = execute!
    new_items.each(&block)
  end
end

#execute!Array

Perform a batch_get_item request.

This method processes the first 100 item keys and returns an array of new modeled items.

See Aws::Record::Batch.read for example usage.

Returns:

  • (Array)

    an array of unordered new items



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/aws-record/record/batch_read.rb', line 39

def execute!
  operation_keys = unprocessed_keys[0..BATCH_GET_ITEM_LIMIT - 1]
  @unprocessed_keys = unprocessed_keys[BATCH_GET_ITEM_LIMIT..-1] || []

  operations = build_operations(operation_keys)
  result = @client.batch_get_item(request_items: operations)
  new_items = build_items(result.responses)
  items.concat(new_items)

  update_unprocessed_keys(result.unprocessed_keys) unless result.unprocessed_keys.nil?

  new_items
end

#find(klass, key = {}) ⇒ Object

Append the item keys to a batch read request.

See Aws::Record::Batch.read for example usage.

Parameters:

  • klass (Aws::Record)

    a model class that includes Aws::Record

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

    attribute-value pairs for the key you wish to search for.

Raises:

  • (Aws::Record::Errors::KeyMissing)

    if your option parameters do not include all item keys defined in the model.

  • (ArgumentError)

    if the provided item keys is a duplicate request in the same instance.



26
27
28
29
30
# File 'lib/aws-record/record/batch_read.rb', line 26

def find(klass, key = {})
  unprocessed_key = format_unprocessed_key(klass, key)
  store_unprocessed_key(klass, unprocessed_key)
  store_item_class(klass, unprocessed_key)
end

#itemsArray

Returns an array of modeled items. The items are marshaled into classes used in #find method. These items will be unordered since DynamoDB does not return items in any particular order.

See Aws::Record::Batch.read for example usage.

Returns:

  • (Array)

    an array of modeled items from the batch_get_item call.



87
88
89
# File 'lib/aws-record/record/batch_read.rb', line 87

def items
  @items ||= []
end