Class: FastExists::Statistics::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_exists/statistics/tracker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTracker

Returns a new instance of Tracker.



10
11
12
13
# File 'lib/fast_exists/statistics/tracker.rb', line 10

def initialize
  @mutex = Mutex.new
  reset!
end

Instance Attribute Details

#bloom_hitsObject (readonly)

Returns the value of attribute bloom_hits.



8
9
10
# File 'lib/fast_exists/statistics/tracker.rb', line 8

def bloom_hits
  @bloom_hits
end

#bloom_missesObject (readonly)

Returns the value of attribute bloom_misses.



8
9
10
# File 'lib/fast_exists/statistics/tracker.rb', line 8

def bloom_misses
  @bloom_misses
end

#database_lookupsObject (readonly)

Returns the value of attribute database_lookups.



8
9
10
# File 'lib/fast_exists/statistics/tracker.rb', line 8

def database_lookups
  @database_lookups
end

#false_positivesObject (readonly)

Returns the value of attribute false_positives.



8
9
10
# File 'lib/fast_exists/statistics/tracker.rb', line 8

def false_positives
  @false_positives
end

#queries_avoidedObject (readonly)

Returns the value of attribute queries_avoided.



8
9
10
# File 'lib/fast_exists/statistics/tracker.rb', line 8

def queries_avoided
  @queries_avoided
end

Instance Method Details

#false_positive_rateObject



52
53
54
55
# File 'lib/fast_exists/statistics/tracker.rb', line 52

def false_positive_rate
  return 0.0 if @bloom_hits.zero?
  (@false_positives.to_f / @bloom_hits).round(4)
end

#hit_ratioObject



40
41
42
43
44
# File 'lib/fast_exists/statistics/tracker.rb', line 40

def hit_ratio
  total = @bloom_hits + @bloom_misses
  return 0.0 if total.zero?
  (@bloom_hits.to_f / total).round(4)
end

#miss_ratioObject



46
47
48
49
50
# File 'lib/fast_exists/statistics/tracker.rb', line 46

def miss_ratio
  total = @bloom_hits + @bloom_misses
  return 0.0 if total.zero?
  (@bloom_misses.to_f / total).round(4)
end

#record_avoidedObject



15
16
17
18
19
20
# File 'lib/fast_exists/statistics/tracker.rb', line 15

def record_avoided
  @mutex.synchronize do
    @queries_avoided += 1
    @bloom_misses += 1
  end
end

#record_db_lookupObject



28
29
30
31
32
# File 'lib/fast_exists/statistics/tracker.rb', line 28

def record_db_lookup
  @mutex.synchronize do
    @database_lookups += 1
  end
end

#record_false_positiveObject



34
35
36
37
38
# File 'lib/fast_exists/statistics/tracker.rb', line 34

def record_false_positive
  @mutex.synchronize do
    @false_positives += 1
  end
end

#record_hitObject



22
23
24
25
26
# File 'lib/fast_exists/statistics/tracker.rb', line 22

def record_hit
  @mutex.synchronize do
    @bloom_hits += 1
  end
end

#reset!Object



72
73
74
75
76
77
78
79
80
# File 'lib/fast_exists/statistics/tracker.rb', line 72

def reset!
  @mutex.synchronize do
    @queries_avoided = 0
    @database_lookups = 0
    @bloom_hits = 0
    @bloom_misses = 0
    @false_positives = 0
  end
end

#snapshotObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fast_exists/statistics/tracker.rb', line 57

def snapshot
  @mutex.synchronize do
    {
      queries_avoided: @queries_avoided,
      database_lookups: @database_lookups,
      bloom_hits: @bloom_hits,
      bloom_misses: @bloom_misses,
      false_positives: @false_positives,
      hit_ratio: hit_ratio,
      miss_ratio: miss_ratio,
      false_positive_rate: false_positive_rate
    }
  end
end