Class: SimpleHashEviction

Inherits:
Object
  • Object
show all
Defined in:
lib/repositories/simple_hash_eviction.rb

Overview

Creates a limited hash with a FIFO.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n = nil) ⇒ SimpleHashEviction

Returns a new instance of SimpleHashEviction.



6
7
8
9
10
# File 'lib/repositories/simple_hash_eviction.rb', line 6

def initialize(n=nil)
  @key_queue = []
  @last_hash = {}
  @n = n || 10
end

Instance Attribute Details

#key_queueObject

Returns the value of attribute key_queue.



4
5
6
# File 'lib/repositories/simple_hash_eviction.rb', line 4

def key_queue
  @key_queue
end

#last_hashObject

Returns the value of attribute last_hash.



4
5
6
# File 'lib/repositories/simple_hash_eviction.rb', line 4

def last_hash
  @last_hash
end

#nObject

Returns the value of attribute n.



4
5
6
# File 'lib/repositories/simple_hash_eviction.rb', line 4

def n
  @n
end

Instance Method Details

#update(hsh) ⇒ Object

Complicated little diddy:

1) Add the hash keys to the key queue, keeping the values unique.

2) Find which values have been changed in the hash by comparing it to either what we saw last time, or an empty hash.

3) Shift any of the older keys all the way right, if they were updated. This isn’t always going to be the right answer, but if there is more than one key that has been changed, we can’t know the order of their change here or anywhere. If it’s important to keep this straight, do operations on one key at a time. I.e.:

h[1] = 2 instead of h.merge!({1 => 2, 3 => 4})

4) Store the hash as we know it today 5) Trim off any extra values with a nontainting delete, so that update isn’t called for something update is doing.



29
30
31
32
33
34
35
36
37
# File 'lib/repositories/simple_hash_eviction.rb', line 29

def update(hsh)
  self.key_queue = self.key_queue | hsh.keys
  self.key_queue.shift_mask_right(updated_keys(hsh))
  @last_hash = hsh.dup
  
  while self.key_queue.size > self.n
    hsh.nontainting_delete(self.key_queue.shift)
  end
end