Class: IP2LocationOrderedHash

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

#initializeIP2LocationOrderedHash

Returns a new instance of IP2LocationOrderedHash.



84
85
86
87
# File 'lib/logstash/filters/ip2location.rb', line 84

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.



82
83
84
# File 'lib/logstash/filters/ip2location.rb', line 82

def hash
  @hash
end

#times_queriedObject (readonly)

ip -> times queried



81
82
83
# File 'lib/logstash/filters/ip2location.rb', line 81

def times_queried
  @times_queried
end

Instance Method Details

#add(key) ⇒ Object



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

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

#delete_least_usedObject



111
112
113
# File 'lib/logstash/filters/ip2location.rb', line 111

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

#first_pile_with_sometingObject



115
116
117
# File 'lib/logstash/filters/ip2location.rb', line 115

def first_pile_with_someting
  hash[hash.keys.min]
end

#increment(key) ⇒ Object



105
106
107
108
109
# File 'lib/logstash/filters/ip2location.rb', line 105

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

#reorder(key) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/logstash/filters/ip2location.rb', line 95

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