Module: Julewire::Core::Processing::Sampling

Defined in:
lib/julewire/core/processing/sampling.rb

Defined Under Namespace

Classes: Head

Class Method Summary collapse

Class Method Details

.head(rate:, key: nil) ⇒ Object



17
18
19
# File 'lib/julewire/core/processing/sampling.rb', line 17

def head(rate:, key: nil)
  Head.new(rate: rate, key: key)
end

.keep?(rate:, key:) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
# File 'lib/julewire/core/processing/sampling.rb', line 21

def keep?(rate:, key:)
  threshold = threshold_for(rate)
  return false if key.nil?

  stable_hash(key) < threshold
end

.stable_hash(value) ⇒ Object



34
35
36
37
38
39
# File 'lib/julewire/core/processing/sampling.rb', line 34

def stable_hash(value)
  hash = key_string(value).each_byte.reduce(FNV_OFFSET) do |hash, byte|
    ((hash ^ byte) * FNV_PRIME) & HASH_MASK
  end
  mix_hash(hash)
end

.threshold_for(rate) ⇒ Object

Raises:

  • (ArgumentError)


28
29
30
31
32
# File 'lib/julewire/core/processing/sampling.rb', line 28

def threshold_for(rate)
  raise ArgumentError, "rate must be a finite Numeric between 0 and 1" unless valid_rate?(rate)

  (rate * HASH_SPACE).floor
end