Class: IP2LocationOrderedHash
- Inherits:
-
Object
- Object
- IP2LocationOrderedHash
- Defined in:
- lib/logstash/filters/ip2location.rb
Overview
class LogStash::Filters::IP2Location
Constant Summary collapse
- ONE =
1
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#times_queried ⇒ Object
readonly
ip -> times queried.
Instance Method Summary collapse
- #add(key) ⇒ Object
- #delete_least_used ⇒ Object
- #first_pile_with_someting ⇒ Object
- #increment(key) ⇒ Object
-
#initialize ⇒ IP2LocationOrderedHash
constructor
A new instance of IP2LocationOrderedHash.
- #reorder(key) ⇒ Object
Constructor Details
#initialize ⇒ IP2LocationOrderedHash
Returns a new instance of IP2LocationOrderedHash.
104 105 106 107 |
# File 'lib/logstash/filters/ip2location.rb', line 104 def initialize @times_queried = Hash.new(0) # ip -> times queried @hash = {} # number of hits -> array of ips end |
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
102 103 104 |
# File 'lib/logstash/filters/ip2location.rb', line 102 def hash @hash end |
#times_queried ⇒ Object (readonly)
ip -> times queried
101 102 103 |
# File 'lib/logstash/filters/ip2location.rb', line 101 def times_queried @times_queried end |
Instance Method Details
#add(key) ⇒ Object
109 110 111 112 113 |
# File 'lib/logstash/filters/ip2location.rb', line 109 def add(key) hash[ONE] ||= [] hash[ONE] << key times_queried[key] = ONE end |
#delete_least_used ⇒ Object
131 132 133 |
# File 'lib/logstash/filters/ip2location.rb', line 131 def delete_least_used first_pile_with_someting.shift.tap { |key| times_queried.delete(key) } end |
#first_pile_with_someting ⇒ Object
135 136 137 |
# File 'lib/logstash/filters/ip2location.rb', line 135 def first_pile_with_someting hash[hash.keys.min] end |
#increment(key) ⇒ Object
125 126 127 128 129 |
# File 'lib/logstash/filters/ip2location.rb', line 125 def increment(key) add(key) unless times_queried.has_key?(key) reorder(key) times_queried[key] += 1 end |
#reorder(key) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/logstash/filters/ip2location.rb', line 115 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 |