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.
69 70 71 72 |
# File 'lib/cdb/writer.rb', line 69 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).
84 85 86 87 88 |
# File 'lib/cdb/writer.rb', line 84 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.
91 92 93 |
# File 'lib/cdb/writer.rb', line 91 def capacity @slots.length end |
#put(entry) ⇒ Object
Adds a hash table entry to the table.
75 76 77 78 79 |
# File 'lib/cdb/writer.rb', line 75 def put(entry) grow if should_grow? @slots[find_slot(entry)] = entry @count += 1 end |