class Mmdb::DB
Constants
- DATA_SEPARATOR_SIZE
Attributes
file_path[R]
Public Class Methods
new(file_path)
click to toggle source
# File lib/mmdb/db.rb, line 7 def initialize(file_path) @file_path = file_path end
Public Instance Methods
query(ip_addr)
click to toggle source
# File lib/mmdb/db.rb, line 11 def query(ip_addr) validate_database! find_node!(ip_addr) end
Private Instance Methods
build_flag(ip_addr, index)
click to toggle source
# File lib/mmdb/db.rb, line 32 def build_flag(ip_addr, index) (ip_addr >> (127 - index)) & 1 end
decode_node(node)
click to toggle source
# File lib/mmdb/db.rb, line 46 def decode_node(node) base = decoder.search_tree_size + DATA_SEPARATOR_SIZE position = (node - decoder.node_count) - DATA_SEPARATOR_SIZE decoder.decode(position: position, base: base) end
decoder()
click to toggle source
# File lib/mmdb/db.rb, line 52 def decoder @decoder ||= Decoder.new(File.binread(file_path)) end
find_node!(ip_addr)
click to toggle source
# File lib/mmdb/db.rb, line 22 def find_node!(ip_addr) (decoder.start_index...128).inject(0) do |node, i| next_node = read_next_node!(node, build_flag(ip_addr, i)) return decode_node(next_node).value if next_node >= decoder.node_count next_node end raise InvalidFileFormat end
read_next_node!(node, flag)
click to toggle source
# File lib/mmdb/db.rb, line 36 def read_next_node!(node, flag) decoder.read(node: node, flag: flag).tap do |next_node| raise InvalidFileFormat if next_node.zero? end end
validate_database!()
click to toggle source
# File lib/mmdb/db.rb, line 42 def validate_database! raise Mmdb::DatabaseNotFound unless File.exist?(file_path) end