Class: Factbase::IndexedUnique

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

Overview

Indexed term ‘unique’. The @idx structure: {

count: Integer (number of facts already processed),
buckets: {
  key => {
    facts: Array (unique facts found),
    seen: Set (composite values already indexed to skip duplicates)
  }
}

} Example 1: (unique “fruit”)

- Apple, Apple, Banana
- count: 3, facts: [Apple, Banana], seen: { [Apple], [Banana] }

Example 2: (unique “fruit” “color”)

- [Apple, Red], [Apple, Green], [Apple, Red]
- count: 3, facts: [[Apple, Red], [Apple, Green]], seen: { [Apple, Red], [Apple, Green] }

Instance Method Summary collapse

Constructor Details

#initialize(term, idx) ⇒ IndexedUnique



25
26
27
28
# File 'lib/factbase/indexed/indexed_unique.rb', line 25

def initialize(term, idx)
  @term = term
  @idx = idx
end

Instance Method Details

#predict(maps, _fb, _params) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/factbase/indexed/indexed_unique.rb', line 30

def predict(maps, _fb, _params)
  operands = @term.operands.map(&:to_s)
  bucket_key = operands.join('|')
  idx_key = [maps.object_id, @term.op.to_s, bucket_key]
  entry = (@idx[idx_key] ||= { buckets: {}, count: 0 })
  feed(maps.to_a, entry, operands, bucket_key)
  matches = entry[:buckets][bucket_key][:facts]
  maps.respond_to?(:repack) ? maps.repack(matches) : matches
end