Class: Cdb::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/cdb/writer.rb

Overview

Provides write-only access to a cdb.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(file) ⇒ Object

Initializes an empty cdb for writing to the given file-like object.



7
8
9
10
11
# File 'lib/cdb/writer.rb', line 7

def self.create(file)
  file.truncate(0)
  file.write(empty_header)
  new(file)
end

.empty_headerObject

Returns an empty header – NUM_HASHTABLES pairs of 32-bit integers, all containing zero.



32
33
34
# File 'lib/cdb/writer.rb', line 32

def self.empty_header
  "\0" * (Cdb::NUM_HASHTABLES * 8)
end

Instance Method Details

#[]=(key, value) ⇒ Object

Writes a key/value pair to the cdb.

Attempting to write the same key twice will cause an error.



16
17
18
19
# File 'lib/cdb/writer.rb', line 16

def []=(key, value)
  offset = append(key, value)
  index(key, offset)
end

#closeObject

Finish writing the cdb.

This flushes the hash table structure to disk.



24
25
26
27
28
# File 'lib/cdb/writer.rb', line 24

def close
  lookups = @tables.map { |t| write_table(t) }
  @file.rewind
  @file.write(lookups.flatten.pack('V*'))
end