Class: OrderedHash

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/filters/ip2location.rb

Overview

class LogStash::Filters::IP2Location

Constant Summary collapse

ONE =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOrderedHash

Returns a new instance of OrderedHash.



69
70
71
72
# File 'lib/logstash/filters/ip2location.rb', line 69

def initialize
  @times_queried = Hash.new(0) # ip -> times queried

  @hash = {} # number of hits -> array of ips

end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



67
68
69
# File 'lib/logstash/filters/ip2location.rb', line 67

def hash
  @hash
end

#times_queriedObject (readonly)

ip -> times queried



66
67
68
# File 'lib/logstash/filters/ip2location.rb', line 66

def times_queried
  @times_queried
end

Instance Method Details

#add(key) ⇒ Object



74
75
76
77
78
# File 'lib/logstash/filters/ip2location.rb', line 74

def add(key)
  hash[ONE] ||= []
  hash[ONE] << key
  times_queried[key] = ONE
end

#delete_least_usedObject



96
97
98
# File 'lib/logstash/filters/ip2location.rb', line 96

def delete_least_used
  first_pile_with_someting.shift.tap { |key| times_queried.delete(key) }
end

#first_pile_with_sometingObject



100
101
102
# File 'lib/logstash/filters/ip2location.rb', line 100

def first_pile_with_someting
  hash[hash.keys.min]
end

#increment(key) ⇒ Object



90
91
92
93
94
# File 'lib/logstash/filters/ip2location.rb', line 90

def increment(key)
  add(key) unless times_queried.has_key?(key)
  reorder(key)
  times_queried[key] += 1
end

#reorder(key) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/logstash/filters/ip2location.rb', line 80

def reorder(key)
  number_of_queries = times_queried[key]

  hash[number_of_queries].delete(key)
  hash.delete(number_of_queries) if hash[number_of_queries].empty?

  hash[number_of_queries + 1] ||= []
  hash[number_of_queries + 1] << key
end