Class: CuckooFilter::Fingerprint

Inherits:
Object
  • Object
show all
Defined in:
lib/cuckoo_filter/fingerprint.rb

Overview

A fingerprint is a fixed size “hash” of an element which is stored in one of the buckets of the Cuckoo Filter.

Typically modeled as just a collection of bits (generally 6), since Ruby doesn’t allow that low level an access to the memory, we just go ahead with a full blown class.

value stores the actual value of the fingerprint

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item = nil) ⇒ Fingerprint

Creates a new fingerprint with the provided item, or without any actual fingerprint value if no item is provided



18
19
20
# File 'lib/cuckoo_filter/fingerprint.rb', line 18

def initialize(item = nil)
  @value = self.class.make(item).freeze unless item.nil?
end

Instance Attribute Details

#valueNumeric (readonly)

Returns the actual calculated FNV-1a fingerprint.

Returns:

  • (Numeric)

    the actual calculated FNV-1a fingerprint



14
15
16
# File 'lib/cuckoo_filter/fingerprint.rb', line 14

def value
  @value
end

Class Method Details

.make(item) ⇒ Object



45
46
47
# File 'lib/cuckoo_filter/fingerprint.rb', line 45

def self.make(item)
  Fnv::Hash.fnv_1a(item, size: 32)
end

Instance Method Details

#clear!Object

Clear the value and return the old value encapsulated in a new CuckooFilter::Fingerprint



35
36
37
38
39
# File 'lib/cuckoo_filter/fingerprint.rb', line 35

def clear!
  old_value = self.dup
  @value = nil
  old_value
end

#empty?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/cuckoo_filter/fingerprint.rb', line 41

def empty?
  @value.nil?
end

#set(fingerprint) ⇒ Object

Update the value of a fingerprint if it was created without an item (i.e. the value is nil), else return error

Parameters:

  • fingerprint (Fingerprint)

    the fingerprint with which to update the value



26
27
28
29
30
31
32
# File 'lib/cuckoo_filter/fingerprint.rb', line 26

def set(fingerprint)
  if @value.nil?
    @value = fingerprint.value.freeze
  else
    fail CuckooFilterError, "Fingerprint value cannot be changed once set"
  end
end