You are viewing documentation for version 1 of the AWS SDK for Ruby. Version 2 documentation can be found here.

Class: AWS::DynamoDB::BatchGet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/aws/dynamo_db/batch_get.rb

Overview

A utility class for configuring a list of tables, attributes and items to request information for.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BatchGet

Returns a new instance of BatchGet



29
30
31
32
# File 'lib/aws/dynamo_db/batch_get.rb', line 29

def initialize options = {}
  super(options)
  @request_items = {}
end

Instance Method Details

#each(&block) ⇒ nil

Returns:

  • (nil)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/aws/dynamo_db/batch_get.rb', line 142

def each &block

  options = { :request_items => @request_items }

  begin

    response = client.batch_get_item(options)

    response.data['Responses'].each_pair do |table_name,details|
      details['Items'].each do |hash_data|
        attributes = values_from_response_hash(hash_data)
        yield(table_name, attributes)
      end
    end

    options[:request_items] = convert_unprocessed_keys(response)

  end while options[:request_items]

  nil

end

#each_attributesObject

Yields only attribute hashes. This removes the outer hash that normally provides the :table_name and :attributes keys. This is useful when your batch get requested items from a single table.



168
169
170
171
172
# File 'lib/aws/dynamo_db/batch_get.rb', line 168

def each_attributes
  each do |table_name, attributes|
    yield(attributes)
  end
end

#items(attributes, *items) ⇒ Object

Specify a list of Item objects to batch fetch attributes for. The table name is retrieved from the items objects, this means the items do not need to belong to the same table.

Parameters:

  • attributes (Symbol, String, Array<String>)

    The list of attributes to fetch. If you want to load ALL attributes for the named items, then pass the symbol :all.

    # get all attributes
    batch_get.table('mytable', :all, items)
    
    # get one attribute for each item
    batch_get.table('mytable', ['name'], items)
    
    # get a list of attributes for each item
    batch_get.table('mytable', ['name', 'size'], items)
    
  • items (Item)

    One or more Item objects to fetch attributes for. These items may come from any number of different tables.



135
136
137
138
139
# File 'lib/aws/dynamo_db/batch_get.rb', line 135

def items attributes, *items
  [items].flatten.each do |item|
    self.table(item.table, attributes, [item])
  end
end

#table(table, attributes, items, options = {}) ⇒ nil

Add a list of items to fetch in this batch.

Parameters:

  • table (Table, String)

    The name of the table to fetch attributes from.

  • attributes (Symbol, String, Array<String>)

    The list of attributes to fetch. If you want to load ALL attributes for the named items, then pass the symbol :all.

    # get all attributes
    batch_get.table('mytable', :all, items)
    
    # get one attribute for each item
    batch_get.table('mytable', ['name'], items)
    
    # get a list of attributes for each item
    batch_get.table('mytable', ['name', 'size'], items)
    
  • items (Array<Item,Array>)

    One or more items to fetch attributes for. Each attribute should be one of the following:

    • an Item object
    • a hash key value
    • a hash key value and a range key value

    You must provide both the hash key and range key values if the table schema has both.

    batch_get.table('mytable', :all, [%w(hk1 rk1), %w(hk1 rk2), ...])
    
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :consistent_read (Boolean) — default: false

    When true, items are read from this table with consistent reads. When false, reads are eventually consistent.

Returns:

  • (nil)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/aws/dynamo_db/batch_get.rb', line 72

def table table, attributes, items, options = {}

  table = table.is_a?(Table) ? table.name : table.to_s

  attributes = attributes == :all ? nil : [attributes].flatten

  keys = items.collect do |item|
    case item
    when Item then item_key_hash(item)
    when Array
      {
        :hash_key_element => format_attribute_value(item[0]),
        :range_key_element => format_attribute_value(item[1]),
      }
    else
      { :hash_key_element => format_attribute_value(item) }
    end
  end

  # ensure we don't receive 2 different lists of attributes for
  # the same table

  if
    @request_items.has_key?(table) and
    @request_items[table][:attributes_to_get] != attributes
  then
    msg = "When batch getting attributes, you may only provide " +
      "1 list of attributes per table, but the `#{table}` table " +
      "has received reqeusts for 2 different sets of attributes"
    raise ArgumentError, msg
  end

  # merge attributes and items with the request items

  @request_items[table] ||= { :keys => [] }
  @request_items[table][:attributes_to_get] = attributes if attributes
  @request_items[table][:keys] += keys
  @request_items[table].merge!(options)

  nil

end