Class: Factbase::IndexedEq
- Inherits:
-
Object
- Object
- Factbase::IndexedEq
- Defined in:
- lib/factbase/indexed/indexed_eq.rb
Overview
Indexed term ‘eq’.
Instance Method Summary collapse
-
#initialize(term, idx) ⇒ IndexedEq
constructor
A new instance of IndexedEq.
- #predict(maps, _fb, params) ⇒ Object
Constructor Details
#initialize(term, idx) ⇒ IndexedEq
Returns a new instance of IndexedEq.
8 9 10 11 |
# File 'lib/factbase/indexed/indexed_eq.rb', line 8 def initialize(term, idx) @term = term @idx = idx end |
Instance Method Details
#predict(maps, _fb, params) ⇒ Object
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 |
# File 'lib/factbase/indexed/indexed_eq.rb', line 13 def predict(maps, _fb, params) return nil if @idx.nil? key = [maps.object_id, @term.operands.first, @term.op] return unless @term.operands.first.is_a?(Symbol) && _scalar?(@term.operands[1]) entry = @idx[key] maps_array = maps.to_a if entry.nil? entry = { index: {}, indexed_count: 0 } @idx[key] = entry end if entry[:indexed_count] < maps_array.size prop = @term.operands.first.to_s maps_array[entry[:indexed_count]..].each do |m| m[prop]&.each do |v| entry[:index][v] ||= [] entry[:index][v] << m end end entry[:indexed_count] = maps_array.size end vv = if @term.operands[1].is_a?(Symbol) params[@term.operands[1].to_s] || [] else [@term.operands[1]] end j = vv.flat_map { |v| entry[:index][v] || [] }.uniq(&:object_id) if maps.respond_to?(:inserted) Factbase::Taped.new(j, inserted: maps.inserted, deleted: maps.deleted, added: maps.added) else j end end |