1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
module AWS
class DynamoDB
# A utility class for configuring a list of tables, attributes and
# items to request information for.
#
# @see DynamoDB#batch_get
# @see Table#batch_get
#
class BatchGet
include Keys
include Enumerable
include Core::Model
def initialize options = {}
super(options)
@request_items = {}
end
# Add a list of items to fetch in this batch.
#
# @param [Table,String] table The name of the table to fetch attributes
# from.
#
# @param [Symbol, String, Array<String>] attributes 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)
#
# @param [Array<Item,Array>] items 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), ...])
#
# @param [Hash] options
#
# @option options [Boolean] :consistent_read (false) When `true`, items
# are read from this table with consistent reads. When `false`, reads
# are eventually consistent.
#
# @return [nil]
#
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
# 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.
#
# @param [Symbol, String, Array<String>] attributes 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)
#
# @param [Item] items One or more {Item} objects to fetch attributes
# for. These items may come from any number of different tables.
#
def items attributes, *items
[items].flatten.each do |item|
self.table(item.table, attributes, [item])
end
end
# @return [nil]
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
# 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.
def each_attributes
each do |table_name, attributes|
yield(attributes)
end
end
protected
def convert_unprocessed_keys response
return nil if response.data['UnprocessedKeys'].empty?
# convert the json response keys into symbolized keys
str2sym = lambda do |key_desc|
type, value = key_desc.to_a.flatten
case type
when "S" then { :s => value }
when "N" then { :n => value }
else
raise "unhandled key type: #{type}"
end
end
request_items = {}
response.data['UnprocessedKeys'].each_pair do |table,keys|
request_items[table] = {}
request_items[table][:attributes_to_get] = keys['AttributesToGet'] if
keys['AttributesToGet']
request_items[table][:keys] = keys['Keys'].collect do |desc|
key = {}
key[:hash_key_element] = str2sym.call(desc['HashKeyElement'])
key[:range_key_element] = str2sym.call(desc['RangeKeyElement']) if
desc['RangeKeyElement']
key
end
end
request_items
end
end
end
end
|