Class: Seasy::InMemoryStorage
- Inherits:
-
Object
- Object
- Seasy::InMemoryStorage
- 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
- #add(weight, key, target) ⇒ Object
- #clear ⇒ Object
-
#initialize ⇒ InMemoryStorage
constructor
A new instance of InMemoryStorage.
- #remove(source) ⇒ Object
-
#save(target, weights, options = {}) ⇒ Object
target is a simple value - we care not what weights are all fragments (indices) and their weight eg.
-
#search(query) ⇒ Object
return { target1 => weight, target2 => weight }.
Constructor Details
#initialize ⇒ InMemoryStorage
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 |
#clear ⇒ Object
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, = {} raise ":source need to be set" if [:source].nil? source = [: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 |