class Dynamoid::AdapterPlugin::AwsSdkV3::BatchGetItem

Documentation docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#batch_get_item-instance_method

Attributes

client[R]
options[R]
tables_with_ids[R]

Public Class Methods

new(client, tables_with_ids, options = {}) click to toggle source
# File lib/dynamoid/adapter_plugin/aws_sdk_v3/batch_get_item.rb, line 12
def initialize(client, tables_with_ids, options = {})
  @client = client
  @tables_with_ids = tables_with_ids
  @options = options
end

Public Instance Methods

call() { |batch_results, successful_partially?| ... } click to toggle source
# File lib/dynamoid/adapter_plugin/aws_sdk_v3/batch_get_item.rb, line 18
def call
  results = {}

  tables_with_ids.each do |table, ids|
    if ids.blank?
      results[table.name] = []
      next
    end

    ids = Array(ids).dup

    while ids.present?
      batch = ids.shift(Dynamoid::Config.batch_size)
      request = build_request(table, batch)
      api_response = client.batch_get_item(request)
      response = Response.new(api_response)

      if block_given?
        # return batch items as a result
        batch_results = Hash.new([].freeze)
        batch_results.update(response.items_grouped_by_table)

        yield(batch_results, response.successful_partially?)
      else
        # collect all the batches to return at the end
        results.update(response.items_grouped_by_table) { |_, its1, its2| its1 + its2 }
      end

      if response.successful_partially?
        ids += response.unprocessed_ids(table)
      end
    end
  end

  results unless block_given?
end

Private Instance Methods

build_request(table, ids) click to toggle source
# File lib/dynamoid/adapter_plugin/aws_sdk_v3/batch_get_item.rb, line 57
def build_request(table, ids)
  ids = Array(ids)

  keys = if table.range_key.nil?
           ids.map { |hk| { table.hash_key => hk } }
         else
           ids.map { |hk, rk| { table.hash_key => hk, table.range_key => rk } }
         end

  {
    request_items: {
      table.name => {
        keys: keys,
        consistent_read: options[:consistent_read]
      }
    }
  }
end