Class: PEROBS::BigHash
- Inherits:
-
Object
- Object
- ObjectBase
- Object
- PEROBS::BigHash
- Defined in:
- lib/perobs/BigHash.rb
Overview
The BigHash is similar to the Hash object in that it provides a simple hash functionality. The difference is that this class scales to much larger data sets essentially limited to the amount of space available on your backing store. The data is persisted immediately and uses transactions to ensure the data consistent. It only provides a small subset of the methods provided by the native Hash class that make sense for giant data sets.
Defined Under Namespace
Classes: Collisions, Entry
Constant Summary
Constants inherited from ObjectBase
Instance Attribute Summary
Attributes inherited from Object
Attributes inherited from ObjectBase
Instance Method Summary collapse
-
#[](key) ⇒ Any PEROBS storable object
Retrieve the value for the given key.
-
#[]=(key, value) ⇒ Object
Insert a value that is associated with the given key.
-
#check ⇒ Boolean
Check if the data structure contains any errors.
-
#delete(key) ⇒ Object
Delete and return the entry for the given key.
-
#each(&block) ⇒ Object
Calls the given block for each key/value pair.
-
#empty? ⇒ TrueClass, FalseClass
Return true if hash is empty.
-
#has_key?(key) ⇒ TrueClass or FalseClass
(also: #include?)
Check if the is a value stored for the given key.
-
#initialize(p) ⇒ BigHash
constructor
Create a new BigHash object.
-
#keys ⇒ Array
This is mostly intended for debugging as the result can be very big.
-
#length ⇒ Integer
(also: #size)
Return the number of entries stored in the hash.
- #restore ⇒ Object
Methods inherited from Object
#_delete_reference_to_id, #_deserialize, #_referenced_object_ids, #attr_init, attr_persist, #init_attr, #inspect, #mark_as_modified
Methods inherited from ObjectBase
#==, #_check_assignment_value, _finalize, #_initialize, #_restore, #_stash, #_sync, #_transfer, read
Constructor Details
Instance Method Details
#[](key) ⇒ Any PEROBS storable object
Retrieve the value for the given key. If no value for the key is found nil is returned.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/perobs/BigHash.rb', line 127 def [](key) hashed_key = hash_key(key) unless (entry = @btree.get(hashed_key)) return nil end if entry.is_a?(PEROBS::Array) entry.each do |ae| return ae.value if ae.key == key end elsif entry.key == key return entry.value end nil end |
#[]=(key, value) ⇒ Object
Insert a value that is associated with the given key. If a value for this key already exists, the value will be overwritten with the newly provided value.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/perobs/BigHash.rb', line 84 def []=(key, value) hashed_key = hash_key(key) @store.transaction do entry = @store.new(Entry, key, value) if (existing_entry = @btree.get(hashed_key)) # There is already an existing entry for this hashed key. if existing_entry.is_a?(Collisions) # Find the right index to insert the new entry. If there is # already an entry with the same key overwrite that entry. index_to_insert = 0 overwrite = false existing_entry.each do |ae| if ae.key == key overwrite = true break end index_to_insert += 1 end existing_entry[index_to_insert] = entry elsif existing_entry.key == key # The existing value is for the identical key. We can safely # overwrite @btree.insert(hashed_key, entry) else # There is a single existing entry, but for a different key. Create # a new PEROBS::Array and store both entries. array_entry = @store.new(Collisions) array_entry << existing_entry array_entry << entry @btree.insert(hashed_key, array_entry) end else # No existing entry. Insert the new entry. @btree.insert(hashed_key, entry) end end end |
#check ⇒ Boolean
Check if the data structure contains any errors.
226 227 228 |
# File 'lib/perobs/BigHash.rb', line 226 def check @btree.check end |
#delete(key) ⇒ Object
Delete and return the entry for the given key. Return nil if no matching entry exists.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/perobs/BigHash.rb', line 170 def delete(key) hashed_key = hash_key(key) unless (entry = @btree.get(hashed_key)) return nil end if entry.is_a?(PEROBS::Array) entry.each_with_index do |ae, i| return entry.delete_at(i).value if ae.key == key end elsif entry.key == key return entry.value end nil end |
#each(&block) ⇒ Object
Calls the given block for each key/value pair. @yield(key, value)
203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/perobs/BigHash.rb', line 203 def each(&block) @btree.each do |_, entry| if entry.is_a?(Collisions) break if entry.each do |c_entry| yield(c_entry.key, c_entry.value) end.nil? else yield(entry.key, entry.value) end end end |
#empty? ⇒ TrueClass, FalseClass
Return true if hash is empty. False otherweise.
197 198 199 |
# File 'lib/perobs/BigHash.rb', line 197 def empty? @btree.entry_counter.zero? end |
#has_key?(key) ⇒ TrueClass or FalseClass Also known as: include?
Check if the is a value stored for the given key.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/perobs/BigHash.rb', line 147 def has_key?(key) hashed_key = hash_key(key) unless (entry = @btree.get(hashed_key)) return false end if entry.is_a?(PEROBS::Array) entry.each do |ae| return true if ae.key == key end elsif entry.key == key return true end false end |
#keys ⇒ Array
This is mostly intended for debugging as the result can be very big. It returns an Array of keys stored in the hash.
218 219 220 221 222 |
# File 'lib/perobs/BigHash.rb', line 218 def keys ks = [] each { |k, _| ks << k } ks end |
#length ⇒ Integer Also known as: size
Return the number of entries stored in the hash.
189 190 191 |
# File 'lib/perobs/BigHash.rb', line 189 def length @btree.entry_counter end |
#restore ⇒ Object
77 |
# File 'lib/perobs/BigHash.rb', line 77 def restore; end |