Class: IParty::MaxMind::EagerReader

Inherits:
Object
  • Object
show all
Defined in:
lib/iparty/max_mind/eager_reader.rb

Overview

A fast read-access reader for MaxMindDB (mmdb) files. Reads the database into memory. This creates a higher memory overhead and slower init phase but faster lookup times.

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ EagerReader

Returns a new instance of EagerReader.



8
9
10
# File 'lib/iparty/max_mind/eager_reader.rb', line 8

def initialize path
  @data = File.binread(path)
end

Instance Method Details

#[](pos, length = 1) ⇒ Object

Raises:

  • (IOError)


12
13
14
15
16
# File 'lib/iparty/max_mind/eager_reader.rb', line 12

def [] pos, length = 1
  raise IOError, "closed stream" unless @data

  @data.slice(pos, length)
end

#closeObject



24
25
26
# File 'lib/iparty/max_mind/eager_reader.rb', line 24

def close
  @data = nil
end

#closed?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/iparty/max_mind/eager_reader.rb', line 28

def closed?
  @data.nil?
end

#rindex(search) ⇒ Object

Raises:

  • (IOError)


18
19
20
21
22
# File 'lib/iparty/max_mind/eager_reader.rb', line 18

def rindex search
  raise IOError, "closed stream" unless @data

  @data.rindex(search)
end