Class: PEROBS::BigHash

Inherits:
Object show all
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

ObjectBase::NATIVE_CLASSES

Instance Attribute Summary

Attributes inherited from Object

#attributes

Attributes inherited from ObjectBase

#_id, #myself, #store

Instance Method Summary collapse

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

#initialize(p) ⇒ BigHash

Create a new BigHash object.

Parameters:



75
76
77
78
79
# File 'lib/perobs/BigHash.rb', line 75

def initialize(p)
  super(p)
  restore
  self.btree = @store.new(PEROBS::BigTree)
end

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.

Parameters:

  • key (Integer or String)

Returns:

  • (Any PEROBS storable object)


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.

Parameters:

  • key (Integer or String)
  • value (Any PEROBS storable object)


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

#checkBoolean

Check if the data structure contains any errors.

Returns:

  • (Boolean)

    true if no erros were found, false otherwise



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.

Parameters:

  • key (Integer or String)

Returns:



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.

Returns:

  • (TrueClass, FalseClass)


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.

Parameters:

  • key (Integer or String)

Returns:

  • (TrueClass or FalseClass)


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

#keysArray

This is mostly intended for debugging as the result can be very big. It returns an Array of keys stored in the hash.

Returns:

  • (Array)

    A list of all keys



225
226
227
228
229
# File 'lib/perobs/BigHash.rb', line 225

def keys
  ks = []
  each { |k, v| ks << k }
  ks
end

#lengthInteger Also known as: size

Return the number of entries stored in the hash.

Returns:

  • (Integer)


196
197
198
# File 'lib/perobs/BigHash.rb', line 196

def length
  @btree.entry_counter
end

#restoreObject



81
82
# File 'lib/perobs/BigHash.rb', line 81

def restore
end