Class: CuckooFilter::Fingerprint
- Inherits:
-
Object
- Object
- CuckooFilter::Fingerprint
- 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
-
#value ⇒ Numeric
readonly
The actual calculated FNV-1a fingerprint.
Class Method Summary collapse
Instance Method Summary collapse
-
#clear! ⇒ Object
Clear the value and return the old value encapsulated in a new Fingerprint.
- #empty? ⇒ Boolean
-
#initialize(item = nil) ⇒ Fingerprint
constructor
Creates a new fingerprint with the provided item, or without any actual fingerprint value if no item is provided.
-
#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.
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
#value ⇒ Numeric (readonly)
Returns 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
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
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 |