1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
require 'thread'
module MaxMindDB
class LowMemoryReader
METADATA_MAX_SIZE = 128 * 1024
def initialize(path)
@mutex = Mutex.new
@file = File.open(path, 'rb')
end
def [](pos, length=1)
atomic_read(length, pos)
end
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
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
end
end
|