class MaxMindDB::LowMemoryReader

Constants

METADATA_MAX_SIZE

Public Class Methods

new(path) click to toggle source
# File lib/maxminddb/reader.rb, line 8
def initialize(path)
  @mutex = Mutex.new
  @file = File.open(path, 'rb')
end

Public Instance Methods

[](pos, length=1) click to toggle source
# File lib/maxminddb/reader.rb, line 13
def [](pos, length=1)
  atomic_read(length, pos)
end
atomic_read(length, pos) click to toggle source
# File lib/maxminddb/reader.rb, line 25
def atomic_read(length, pos)
  # Prefer `pread` in environments where it is available. `pread` provides
  # atomic file access across processes.
  if @file.respond_to?(:pread)
    @file.pread(length, pos)
  else
    @mutex.synchronize do
      @file.seek(pos)
      @file.read(length)
    end
  end
end
rindex(search) click to toggle source
# File lib/maxminddb/reader.rb, line 17
def rindex(search)
  base = [0, @file.size - METADATA_MAX_SIZE].max
  tail = atomic_read(METADATA_MAX_SIZE, base)
  pos = tail.rindex(search)
  return nil if pos.nil?
  base + pos
end