class Derrick::Pattern

Constants

FIRST_SEGMENT_PATTERN
IDENTIFIER_PATTERNS
SEGMENT_PATTERNS
SEGMENT_SEPARATORS

Attributes

count[R]
expirable_count[R]
pattern[R]
persisted_count[R]
types_count[R]

Public Class Methods

extract(key_name) click to toggle source
# File lib/derrick/pattern.rb, line 12
def self.extract(key_name)
  key_pattern = SEGMENT_PATTERNS.inject(key_name.inspect[1..-2]) do |key, pattern|
    key.gsub(pattern, '\1*\3')
  end

  return "#{key_name[FIRST_SEGMENT_PATTERN]}*" if key_pattern == key_name

  key_pattern
end
new() click to toggle source
# File lib/derrick/pattern.rb, line 22
def initialize
  @count = 0
  @expirable_count = 0
  @persisted_count = 0
  @types_count = Hash.new(0)
end

Public Instance Methods

aggregate(key) click to toggle source
# File lib/derrick/pattern.rb, line 50
def aggregate(key)
  @count += 1

  if key.ttl == -1
    @persisted_count += 1
  else
    @expirable_count += 1
  end

  @types_count[key.type] += 1
end
expirable_ratio() click to toggle source
# File lib/derrick/pattern.rb, line 39
def expirable_ratio
  return 1 if count == 0
  expirable_count.to_f / count
end
merge!(other) click to toggle source
# File lib/derrick/pattern.rb, line 29
def merge!(other)
  @count += other.count
  @expirable_count += other.expirable_count
  @persisted_count += other.persisted_count
  other.types_count.each do |type, count|
    @types_count[type] += count
  end
  self
end
types_ratio() click to toggle source
# File lib/derrick/pattern.rb, line 44
def types_ratio
  Hash[@types_count.map do |type, sub_count|
    [type, sub_count.to_f / count]
  end]
end