Class: Aws::Record::BatchRead

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

Constant Summary collapse

BATCH_GET_ITEM_LIMIT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

100

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ BatchRead

Returns a new instance of BatchRead.

Parameters:

  • client (Aws::DynamoDB::Client)

    the DynamoDB SDK client.



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

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.



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

def each
  return enum_for(:each) unless block_given?

  @items.each { |item| yield item }
  until complete?
    new_items = execute!
    new_items.each { |new_item| yield new_item }
  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



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

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)

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

  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.



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

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