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.



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

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.



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

def hash
  @hash
end

#times_queriedObject (readonly)

ip -> times queried



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

def times_queried
  @times_queried
end

Instance Method Details

#add(key) ⇒ Object



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

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

#delete_least_usedObject



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

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

#first_pile_with_sometingObject



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

def first_pile_with_someting
  hash[hash.keys.min]
end

#increment(key) ⇒ Object



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

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

#reorder(key) ⇒ Object



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

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