Sha256: d39f8ac6072eda2efe52d37f099540e29acfdea846526c391944178dfd21c935

Contents?: true

Size: 1.64 KB

Versions: 4

Compression:

Stored size: 1.64 KB

Contents

module Dynomite::Item::Indexes
  class Finder
    extend Memoist
    include Dynomite::Client

    def initialize(source, query)
      @source, @query = source, query
    end

    def find(index_name=nil)
      if index_name # explicit index name
        index = @source.indexes.find { |i| i.index_name == index_name }
        if index
          return index
        else
          logger.info <<~EOL
            WARN: Index #{index_name} specified but not found for table #{@source.table_name}
            Falling back to auto-discovery of indexes
          EOL
        end
      end

      # auto-discover
      find_primary_key_index || find_secondary_index
    end

    def find_primary_key_index
      PrimaryIndex.new(@source.primary_key_fields) if primary_key_found?
    end

    def primary_key_found?
      if @source.composite_key?
        query_fields.include?(@source.partition_key_field) && query_fields.include?(@source.sort_key_field)
      else
        query_fields.include?(@source.partition_key_field)
      end
    end

    # It's possible to have multiple indexes with the same partition and sort key.
    # Will use the first one we find.
    def find_secondary_index
      @source.indexes.find do |i|
        # If query field has comparision expression like
        #   Product.where("category.in": ["Electronics"]).count
        # then it wont match, which is correct.
        intersect = query_fields & i.fields
        intersect == i.fields
      end
    end

    def query_fields
      @query[:where].inject([]) do |result, where_group|
        result += where_group.fields
      end.uniq.sort.map(&:to_s)
    end
    memoize :query_fields
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dynomite-2.0.3 lib/dynomite/item/indexes/finder.rb
dynomite-2.0.2 lib/dynomite/item/indexes/finder.rb
dynomite-2.0.1 lib/dynomite/item/indexes/finder.rb
dynomite-2.0.0 lib/dynomite/item/indexes/finder.rb