Method: PureCDB::Writer#initialize

Defined in:
lib/purecdb/writer.rb

#initialize(target, *options) ⇒ Writer

Open a CDB file for writing, or preparing an IO like object for writing.

:call-seq:

w = PureCDB::Writer.new(target)
w = PureCDB::Writer.new(target, *options)
PureCDB::Writer.new(target)  {|w| ... }
PureCDB::Writer.new(target, *options) {|w| ... }

If :mode is passed in options, it must be the integers 32 or 64, indicating whether you wish to write a standard (32 bit) CDB file, or a 64 bit CDB-like file. The default is 32.

If target is a String it is treated as a filename of a file to be opened to write to. Otherwise target is assumed to be an IO-like object that ideally responds to #sysseek and #syswrite. If it doesn’t, it will be wrapped with an object delegating #sysseek and #syswrite to #seek and #write respectively, and these must be present.

(IO and StringIO both satisfy these requirements)

If passed a block, the writer is yielded to the block and PureCDB::Writer#close is called afterwards.

WARNING: To complete writing the hash tables, you must ensure #close is called when you are done.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/purecdb/writer.rb', line 51

def initialize target, *options
  super *options

  @hash_fill_factor = 0.7

  set_mode(32) if @mode == :detect

  if target.is_a?(String)
    @io = File.new(target,"wb")
  else
    set_stream(target)
  end

  @hashes = [nil] * num_hashes

  @hashptrs = [0] * num_hashes * 2
  write_hashptrs

  @pos = hash_size

  if block_given?
    yield(self)
    close
    nil
  else
    self
  end
end