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.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/perobs/BigHash.rb', line 132 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 else return entry.value if entry.key == key 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.
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 122 123 124 125 126 |
# File 'lib/perobs/BigHash.rb', line 89 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.
233 234 235 |
# File 'lib/perobs/BigHash.rb', line 233 def check return @btree.check end |
#delete(key) ⇒ Object
Delete and return the entry for the given key. Return nil if no matching entry exists.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/perobs/BigHash.rb', line 175 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| if ae.key == key return entry.delete_at(i).value end end else return entry.value if entry.key == key end nil end |
#each(&block) ⇒ Object
Calls the given block for each key/value pair. @yield(key, value)
210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/perobs/BigHash.rb', line 210 def each(&block) @btree.each do |index, 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.
204 205 206 |
# File 'lib/perobs/BigHash.rb', line 204 def empty? @btree.entry_counter == 0 end |
#has_key?(key) ⇒ TrueClass or FalseClass Also known as: include?
Check if the is a value stored for the given key.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/perobs/BigHash.rb', line 152 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 else return true if entry.key == key 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.
225 226 227 228 229 |
# File 'lib/perobs/BigHash.rb', line 225 def keys ks = [] each { |k, v| ks << k } ks end |
#length ⇒ Integer Also known as: size
Return the number of entries stored in the hash.
196 197 198 |
# File 'lib/perobs/BigHash.rb', line 196 def length @btree.entry_counter end |
#restore ⇒ Object
81 82 |
# File 'lib/perobs/BigHash.rb', line 81 def restore end |