Method: PureCDB::Reader#initialize
- Defined in:
- lib/purecdb/reader.rb
#initialize(target, *options) ⇒ Reader
Open a CDB file for reading.
:call-seq:
r = PureCDB::Reader.new(file)
r = PureCDB::Reader.new(file, options)
PureCDB::Reader.new(file) {|r| ... }
PureCDB::Reader.new(file, options) {|r| ... }
file can be a String or any object that meets the minimum requirements, which means having #sysseek, #sysopen to and #sysclose which does not arbitrarily sttop access ot it.
If Mmap is available, the code will attempt to use it if target is a filename
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/purecdb/reader.rb', line 29 def initialize target, * if target.is_a?(String) @name = target @io = File.new(target,"rb") raise "Unable to open file #{target}" if !@io else set_stream(target) end @io.sysseek(-8,IO::SEEK_END) tail = @io.sysread(8) raise "Unable to read trailing 8 bytes for magic cookie" if tail.size != 8 mode = tail == CDB64_MAGIC ? 64 : 32 super * if @mode == :detect set_mode(mode) elsif @mode != mode raise "#{mode}bit mode detected in file; options request #{@mode}bit mode" end @m = FFI::Mmap.new(target,"r", FFI::Mmap::MAP_SHARED) rescue nil read_hashes raise "Invalid File (Hashes are all empty)" if @hashes.uniq == [0] if block_given? yield(self) close else end end |