class Dynamoid::Criteria::KeyFieldsDetector

@private

Public Class Methods

new(query, source, forced_index_name: nil) click to toggle source
# File lib/dynamoid/criteria/key_fields_detector.rb, line 27
def initialize(query, source, forced_index_name: nil)
  @query = query
  @source = source
  @query = Query.new(query)
  @forced_index_name = forced_index_name
  @result = find_keys_in_query
end

Public Instance Methods

hash_key() click to toggle source
# File lib/dynamoid/criteria/key_fields_detector.rb, line 43
def hash_key
  @result && @result[:hash_key]
end
index_name() click to toggle source
# File lib/dynamoid/criteria/key_fields_detector.rb, line 51
def index_name
  @result && @result[:index_name]
end
key_present?() click to toggle source
# File lib/dynamoid/criteria/key_fields_detector.rb, line 39
def key_present?
  @result.present? && @query.contain_with_eq_operator?(hash_key)
end
non_key_present?() click to toggle source
# File lib/dynamoid/criteria/key_fields_detector.rb, line 35
def non_key_present?
  !@query.contain_only?([hash_key, range_key].compact)
end
range_key() click to toggle source
# File lib/dynamoid/criteria/key_fields_detector.rb, line 47
def range_key
  @result && @result[:range_key]
end

Private Instance Methods

find_keys_in_query() click to toggle source
# File lib/dynamoid/criteria/key_fields_detector.rb, line 57
def find_keys_in_query
  return match_forced_index if @forced_index_name

  match_table_and_sort_key ||
    match_local_secondary_index ||
    match_global_secondary_index_and_sort_key ||
    match_table ||
    match_global_secondary_index
end
match_forced_index() click to toggle source
# File lib/dynamoid/criteria/key_fields_detector.rb, line 140
def match_forced_index
  idx = @source.find_index_by_name(@forced_index_name)

  {
    hash_key: idx.hash_key,
    range_key: idx.range_key,
    index_name: idx.name,
  }
end
match_global_secondary_index() click to toggle source
# File lib/dynamoid/criteria/key_fields_detector.rb, line 126
def match_global_secondary_index
  gsi = @source.global_secondary_indexes.values.find do |i|
    @query.contain_with_eq_operator?(i.hash_key) && i.projected_attributes == :all
  end

  if gsi.present?
    {
      hash_key: gsi.hash_key,
      range_key: gsi.range_key,
      index_name: gsi.name,
    }
  end
end
match_global_secondary_index_and_sort_key() click to toggle source

See if can use any global secondary index Chooses the first GSI found that can be utilized for the query GSI with range key involved into query conditions has higher priority But only do so if projects ALL attributes otherwise we won’t get back full data

# File lib/dynamoid/criteria/key_fields_detector.rb, line 103
def match_global_secondary_index_and_sort_key
  gsi = @source.global_secondary_indexes.values.find do |i|
    @query.contain_with_eq_operator?(i.hash_key) && i.projected_attributes == :all &&
      @query.contain?(i.range_key)
  end

  if gsi.present?
    {
      hash_key: gsi.hash_key,
      range_key: gsi.range_key,
      index_name: gsi.name,
    }
  end
end
match_local_secondary_index() click to toggle source

See if can use any local secondary index range key Chooses the first LSI found that can be utilized for the query

# File lib/dynamoid/criteria/key_fields_detector.rb, line 82
def match_local_secondary_index
  return unless @query.contain_with_eq_operator?(@source.hash_key)

  lsi = @source.local_secondary_indexes.values.find do |i|
    @query.contain?(i.range_key)
  end

  if lsi.present?
    {
      hash_key: @source.hash_key,
      range_key: lsi.range_key,
      index_name: lsi.name,
    }
  end
end
match_table() click to toggle source
# File lib/dynamoid/criteria/key_fields_detector.rb, line 118
def match_table
  return unless @query.contain_with_eq_operator?(@source.hash_key)

  {
    hash_key: @source.hash_key,
  }
end
match_table_and_sort_key() click to toggle source

Use table’s default range key

# File lib/dynamoid/criteria/key_fields_detector.rb, line 68
def match_table_and_sort_key
  return unless @query.contain_with_eq_operator?(@source.hash_key)
  return unless @source.range_key

  if @query.contain?(@source.range_key)
    {
      hash_key: @source.hash_key,
      range_key: @source.range_key
    }
  end
end