Class: Derrick::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/derrick/pattern.rb

Constant Summary collapse

SEGMENT_SEPARATORS =
%w(: _ /).freeze
FIRST_SEGMENT_PATTERN =
/.*(#{SEGMENT_SEPARATORS.map { |s| Regexp.escape(s) }.join('|')})/
IDENTIFIER_PATTERNS =
'\d+|[0-9a-f]{32,40}'
SEGMENT_PATTERNS =
SEGMENT_SEPARATORS.map do |separator|
  /(^|#{Regexp.escape(separator)})(#{IDENTIFIER_PATTERNS})($|#{Regexp.escape(separator)})/
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePattern

Returns a new instance of Pattern.



22
23
24
25
26
27
# File 'lib/derrick/pattern.rb', line 22

def initialize
  @count = 0
  @expirable_count = 0
  @persisted_count = 0
  @types_count = Hash.new(0)
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



10
11
12
# File 'lib/derrick/pattern.rb', line 10

def count
  @count
end

#expirable_countObject (readonly)

Returns the value of attribute expirable_count.



10
11
12
# File 'lib/derrick/pattern.rb', line 10

def expirable_count
  @expirable_count
end

#patternObject (readonly)

Returns the value of attribute pattern.



10
11
12
# File 'lib/derrick/pattern.rb', line 10

def pattern
  @pattern
end

#persisted_countObject (readonly)

Returns the value of attribute persisted_count.



10
11
12
# File 'lib/derrick/pattern.rb', line 10

def persisted_count
  @persisted_count
end

#types_countObject (readonly)

Returns the value of attribute types_count.



10
11
12
# File 'lib/derrick/pattern.rb', line 10

def types_count
  @types_count
end

Class Method Details

.extract(key_name) ⇒ Object



12
13
14
15
16
17
18
19
20
# 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

Instance Method Details

#aggregate(key) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# 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_ratioObject



39
40
41
42
# File 'lib/derrick/pattern.rb', line 39

def expirable_ratio
  return 1 if count == 0
  expirable_count.to_f / count
end

#merge!(other) ⇒ Object



29
30
31
32
33
34
35
36
37
# 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_ratioObject



44
45
46
47
48
# 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