Class: Cdb::Reader

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

Overview

Provides read-only access to a cdb.

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Reader

Returns a new instance of Reader.



4
5
6
# File 'lib/cdb/reader.rb', line 4

def initialize(file)
  @file = file.binmode
end

Instance Method Details

#[](key) ⇒ Object

Fetches the value associated with the given key.

Returns nil if the key doesn’t exist in the cdb.



11
12
13
14
15
16
# File 'lib/cdb/reader.rb', line 11

def [](key)
  hash = Cdb.hash(key)
  table = tables[hash % Cdb::NUM_HASHTABLES]
  return nil if table.empty?
  key_from_table(table, key, hash)
end