Class: Dynomite::Item::Indexes::Finder
- Inherits:
-
Object
- Object
- Dynomite::Item::Indexes::Finder
- Extended by:
- Memoist
- Includes:
- Client
- Defined in:
- lib/dynomite/item/indexes/finder.rb
Instance Method Summary collapse
- #find(index_name = nil) ⇒ Object
- #find_primary_key_index ⇒ Object
-
#find_secondary_index ⇒ Object
It’s possible to have multiple indexes with the same partition and sort key.
-
#initialize(source, query) ⇒ Finder
constructor
A new instance of Finder.
- #primary_key_found? ⇒ Boolean
- #query_fields ⇒ Object
Constructor Details
#initialize(source, query) ⇒ Finder
Returns a new instance of Finder.
6 7 8 |
# File 'lib/dynomite/item/indexes/finder.rb', line 6 def initialize(source, query) @source, @query = source, query end |
Instance Method Details
#find(index_name = nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/dynomite/item/indexes/finder.rb', line 10 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 |
#find_primary_key_index ⇒ Object
27 28 29 |
# File 'lib/dynomite/item/indexes/finder.rb', line 27 def find_primary_key_index PrimaryIndex.new(@source.primary_key_fields) if primary_key_found? end |
#find_secondary_index ⇒ Object
It’s possible to have multiple indexes with the same partition and sort key. Will use the first one we find.
41 42 43 44 45 46 47 48 49 |
# File 'lib/dynomite/item/indexes/finder.rb', line 41 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 |
#primary_key_found? ⇒ Boolean
31 32 33 34 35 36 37 |
# File 'lib/dynomite/item/indexes/finder.rb', line 31 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 |
#query_fields ⇒ Object
51 52 53 54 55 |
# File 'lib/dynomite/item/indexes/finder.rb', line 51 def query_fields @query[:where].inject([]) do |result, where_group| result += where_group.fields end.uniq.sort.map(&:to_s) end |