Class: Factbase::IndexedGt

Inherits:
Object
  • Object
show all
Defined in:
lib/factbase/indexed/indexed_gt.rb

Overview

Indexed term ‘gt’.

Instance Method Summary collapse

Constructor Details

#initialize(term, idx) ⇒ IndexedGt

Returns a new instance of IndexedGt.



8
9
10
11
# File 'lib/factbase/indexed/indexed_gt.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
# File 'lib/factbase/indexed/indexed_gt.rb', line 13

def predict(maps, _fb, params)
  return nil if @idx.nil?
  return unless @term.operands.first.is_a?(Symbol) && _scalar?(@term.operands[1])
  prop = @term.operands.first.to_s
  cache_key = [maps.object_id, @term.operands.first, :sorted]
  if @idx[cache_key].nil?
    @idx[cache_key] = []
    maps.to_a.each do |m|
      values = m[prop]
      next if values.nil?
      values.each do |v|
        @idx[cache_key] << [v, m]
      end
    end
    @idx[cache_key].sort_by! { |pair| pair[0] }
  end
  threshold = @term.operands[1].is_a?(Symbol) ? params[@term.operands[1].to_s]&.first : @term.operands[1]
  return nil if threshold.nil?
  i = @idx[cache_key].bsearch_index { |pair| pair[0] > threshold } || @idx[cache_key].size
  result = @idx[cache_key][i..].map { |pair| pair[1] }.uniq
  (maps & []) | result
end