Class: IParty::MaxMind::LazyReader
- Inherits:
-
Object
- Object
- IParty::MaxMind::LazyReader
- Defined in:
- lib/iparty/max_mind/lazy_reader.rb
Overview
A low memory file reader for MaxMindDB (mmdb) files. Avoids reading the database into memory. Has a lower memory footprint but slower lookup times.
Constant Summary collapse
- METADATA_MAX_SIZE =
128 * 1024
Instance Method Summary collapse
- #[](pos, length = 1) ⇒ Object
- #atomic_read(length, pos) ⇒ Object
- #close ⇒ Object
- #closed? ⇒ Boolean
-
#initialize(path) ⇒ LazyReader
constructor
A new instance of LazyReader.
- #rindex(search) ⇒ Object
Constructor Details
#initialize(path) ⇒ LazyReader
Returns a new instance of LazyReader.
10 11 12 13 |
# File 'lib/iparty/max_mind/lazy_reader.rb', line 10 def initialize path @mutex = Mutex.new @file = File.open(path, "rb") end |
Instance Method Details
#[](pos, length = 1) ⇒ Object
15 16 17 |
# File 'lib/iparty/max_mind/lazy_reader.rb', line 15 def [] pos, length = 1 atomic_read(length, pos) end |
#atomic_read(length, pos) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/iparty/max_mind/lazy_reader.rb', line 34 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 |
#close ⇒ Object
19 20 21 |
# File 'lib/iparty/max_mind/lazy_reader.rb', line 19 def close @file.close end |
#closed? ⇒ Boolean
23 24 25 |
# File 'lib/iparty/max_mind/lazy_reader.rb', line 23 def closed? @file.closed? end |
#rindex(search) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/iparty/max_mind/lazy_reader.rb', line 27 def rindex search base = [0, @file.size - METADATA_MAX_SIZE].max tail = atomic_read(METADATA_MAX_SIZE, base) pos = tail.rindex(search) base + pos unless pos.nil? end |