module Cdb

Cdb is a lightweight, pure-ruby reader/writer for DJ Bernstein's cdb format (cr.yp.to/cdb.html).

Author

Olly Smith

License

Apache 2.0 (www.apache.org/licenses/LICENSE-2.0)

Cdbs are fast, immutable, on-disk hashtables. They're great for storing modest (up to 4GB) amounts of arbitrary key-value pairs. They allow random lookup, but no enumeration or traversal.

file = File.new('table.cdb')
Cdb.writer(file) do |cdb|
  cdb['key1'] = 'value1'
  cdb['key2'] = 'value2'
  # ...
end
reader = Cdb.reader(file)
reader['key1']
# => "value1"

Constants

HASHTABLE_MAX_FULLNESS
INITIAL_HASH
NUM_HASHTABLES

Public Class Methods

create(file) { |writer| ... } click to toggle source

Write data to a cdb in a file-like object.

# File lib/cdb.rb, line 26
def self.create(file)
  writer = Cdb::Writer.create(file)
  yield(writer)
  writer.close
end
hash(key) click to toggle source

Calculate a cdb hash value.

The cdb hash function is “h = ((h << 5) + h) ^ c'', with a starting hash of 5381.

# File lib/cdb.rb, line 41
def self.hash(key)
  key.bytes.inject(Cdb::INITIAL_HASH) do |h, c|
    0xffffffff & ((h << 5) + h) ^ c
  end
end
open(file) click to toggle source

Open a cdb for reading.

# File lib/cdb.rb, line 33
def self.open(file)
  Cdb::Reader.new(file)
end