Class: Seasy::InMemoryStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/seasy/index.rb

Overview

a store got search queries as keys and an array of target-weight tuples as values

Instance Method Summary collapse

Constructor Details

#initializeInMemoryStorage

Returns a new instance of InMemoryStorage.



75
76
77
78
# File 'lib/seasy/index.rb', line 75

def initialize
  @store = {}
  @sources = {}
end

Instance Method Details

#add(weight, key, target) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/seasy/index.rb', line 93

def add weight, key, target
  if @store[key].nil?
    @store[key] = {target => weight}
  elsif @store[key][target].nil?
    @store[key][target] = weight
  else
    @store[key][target] += weight
  end
end

#clearObject



108
109
110
111
# File 'lib/seasy/index.rb', line 108

def clear
  @store = {}
  @sources = {}
end

#remove(source) ⇒ Object



113
114
115
116
# File 'lib/seasy/index.rb', line 113

def remove source
  targets = @sources[source]
  @store.delete_if {|key,value| !value[targets.first].nil?}
end

#save(target, weights, options = {}) ⇒ Object

target is a simple value - we care not what weights are all fragments (indices) and their weight eg. { “aba” => 1, “ab” => 1, “ba” => 1, “b” => 1, “a” => 2 } for the string “aba”



83
84
85
86
87
88
89
90
91
# File 'lib/seasy/index.rb', line 83

def save target, weights, options = {}
  raise ":source need to be set" if options[:source].nil?
  source = options[:source]
  @sources[source] ||= []
  @sources[source] << target
  weights.keys.each do |key|
    add weights[key], key, target
  end
end

#search(query) ⇒ Object

return { target1 => weight, target2 => weight }



104
105
106
# File 'lib/seasy/index.rb', line 104

def search query
  @store[query] || {}
end