Class: Cdb::Writer
- Inherits:
-
Object
- Object
- Cdb::Writer
- Defined in:
- lib/cdb/writer.rb
Overview
Provides write-only access to a cdb.
Class Method Summary collapse
-
.create(file) ⇒ Object
Initializes an empty cdb for writing to the given file-like object.
-
.empty_header ⇒ Object
Returns an empty header – NUM_HASHTABLES pairs of 32-bit integers, all containing zero.
Instance Method Summary collapse
-
#[]=(key, value) ⇒ Object
Writes a key/value pair to the cdb.
-
#close ⇒ Object
Finish writing the cdb.
Class Method Details
.create(file) ⇒ Object
Initializes an empty cdb for writing to the given file-like object.
5 6 7 8 9 |
# File 'lib/cdb/writer.rb', line 5 def self.create(file) file.truncate(0) file.write(empty_header) Writer.new(file) end |
.empty_header ⇒ Object
Returns an empty header – NUM_HASHTABLES pairs of 32-bit integers, all containing zero.
30 31 32 |
# File 'lib/cdb/writer.rb', line 30 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.
14 15 16 17 |
# File 'lib/cdb/writer.rb', line 14 def []=(key, value) offset = append(key, value) index(key, offset) end |
#close ⇒ Object
Finish writing the cdb.
This flushes the hash table structure to disk.
22 23 24 25 26 |
# File 'lib/cdb/writer.rb', line 22 def close lookups = @tables.map { |t| write_table(t) } @file.rewind @file.write(lookups.flatten.pack('V*')) end |