Class: Cdb::HashTable
- Inherits:
-
Object
- Object
- Cdb::HashTable
- Defined in:
- lib/cdb/writer.rb
Overview
In-memory hash table structure. Indexes key/value pairs in a Writer.
Instance Method Summary collapse
-
#bytes ⇒ Object
Returns the on-disk representation of a hash table (a serialized array of 32-bit integers representing the offset of each key/value record in the cdb file).
-
#capacity ⇒ Object
Returns the number of slots in the table.
-
#initialize ⇒ HashTable
constructor
Creates an empty hash table.
-
#put(entry) ⇒ Object
Adds a hash table entry to the table.
Constructor Details
#initialize ⇒ HashTable
Creates an empty hash table.
67 68 69 70 |
# File 'lib/cdb/writer.rb', line 67 def initialize @count = 0 @slots = [] end |
Instance Method Details
#bytes ⇒ Object
Returns the on-disk representation of a hash table (a serialized array of 32-bit integers representing the offset of each key/value record in the cdb file).
82 83 84 85 86 |
# File 'lib/cdb/writer.rb', line 82 def bytes @slots.map { |s| s.nil? && [0, 0] || [s.hash, s.offset] } .flatten .pack('V*') end |
#capacity ⇒ Object
Returns the number of slots in the table.
89 90 91 |
# File 'lib/cdb/writer.rb', line 89 def capacity @slots.length end |
#put(entry) ⇒ Object
Adds a hash table entry to the table.
73 74 75 76 77 |
# File 'lib/cdb/writer.rb', line 73 def put(entry) grow if should_grow? @slots[find_slot(entry)] = entry @count += 1 end |