Class: FastExists::Bloom::Scalable
- Inherits:
-
Object
- Object
- FastExists::Bloom::Scalable
- Defined in:
- lib/fast_exists/bloom/scalable.rb
Instance Attribute Summary collapse
-
#false_positive_rate ⇒ Object
readonly
Returns the value of attribute false_positive_rate.
-
#growth_factor ⇒ Object
readonly
Returns the value of attribute growth_factor.
-
#initial_capacity ⇒ Object
readonly
Returns the value of attribute initial_capacity.
-
#tightening_ratio ⇒ Object
readonly
Returns the value of attribute tightening_ratio.
Instance Method Summary collapse
- #add(element) ⇒ Object
- #capacity ⇒ Object
- #clear ⇒ Object
- #contains?(element) ⇒ Boolean
- #count ⇒ Object
-
#initialize(initial_capacity: 10_000, false_positive_rate: 0.001, growth_factor: 2, tightening_ratio: 0.8) ⇒ Scalable
constructor
A new instance of Scalable.
- #stats ⇒ Object
Constructor Details
#initialize(initial_capacity: 10_000, false_positive_rate: 0.001, growth_factor: 2, tightening_ratio: 0.8) ⇒ Scalable
Returns a new instance of Scalable.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/fast_exists/bloom/scalable.rb', line 8 def initialize(initial_capacity: 10_000, false_positive_rate: 0.001, growth_factor: 2, tightening_ratio: 0.8) @initial_capacity = initial_capacity @false_positive_rate = false_positive_rate @growth_factor = growth_factor @tightening_ratio = tightening_ratio @mutex = Mutex.new @filters = [] add_filter end |
Instance Attribute Details
#false_positive_rate ⇒ Object (readonly)
Returns the value of attribute false_positive_rate.
6 7 8 |
# File 'lib/fast_exists/bloom/scalable.rb', line 6 def false_positive_rate @false_positive_rate end |
#growth_factor ⇒ Object (readonly)
Returns the value of attribute growth_factor.
6 7 8 |
# File 'lib/fast_exists/bloom/scalable.rb', line 6 def growth_factor @growth_factor end |
#initial_capacity ⇒ Object (readonly)
Returns the value of attribute initial_capacity.
6 7 8 |
# File 'lib/fast_exists/bloom/scalable.rb', line 6 def initial_capacity @initial_capacity end |
#tightening_ratio ⇒ Object (readonly)
Returns the value of attribute tightening_ratio.
6 7 8 |
# File 'lib/fast_exists/bloom/scalable.rb', line 6 def tightening_ratio @tightening_ratio end |
Instance Method Details
#add(element) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/fast_exists/bloom/scalable.rb', line 19 def add(element) @mutex.synchronize do current_filter = @filters.last if current_filter.count >= current_filter.capacity add_filter current_filter = @filters.last end current_filter.add(element) end true end |
#capacity ⇒ Object
51 52 53 54 55 |
# File 'lib/fast_exists/bloom/scalable.rb', line 51 def capacity @mutex.synchronize do @filters.sum(&:capacity) end end |
#clear ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/fast_exists/bloom/scalable.rb', line 37 def clear @mutex.synchronize do @filters.clear add_filter end true end |
#contains?(element) ⇒ Boolean
31 32 33 34 35 |
# File 'lib/fast_exists/bloom/scalable.rb', line 31 def contains?(element) @mutex.synchronize do @filters.any? { |filter| filter.contains?(element) } end end |
#count ⇒ Object
45 46 47 48 49 |
# File 'lib/fast_exists/bloom/scalable.rb', line 45 def count @mutex.synchronize do @filters.sum(&:count) end end |
#stats ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/fast_exists/bloom/scalable.rb', line 57 def stats @mutex.synchronize do { type: :scalable_bloom, filter_count: @filters.size, inserted_items: count, capacity: capacity, memory_usage_bytes: @filters.sum(&:memory_usage_bytes) } end end |