Class: PEROBS::Hash

Inherits:
ObjectBase show all
Defined in:
lib/perobs/Hash.rb

Overview

A Hash that is transparently persisted in the back-end storage. It is very similar to the Ruby built-in Hash class but has some additional limitations. The hash key must always be a String.

The implementation is largely a proxy around the standard Hash class. But all mutating methods must be re-implemented to convert PEROBS::Objects to POXReference objects and to register the object as modified with the cache.

We explicitely don’t support Hash::store() as it conflicts with ObjectBase::store() method to access the store.

Instance Attribute Summary

Attributes inherited from ObjectBase

#_id, #myself, #store

Instance Method Summary collapse

Methods inherited from ObjectBase

#==, _finalize, #_initialize, #_restore, #_stash, #_sync, read, #restore

Constructor Details

#initialize(p, default = nil) ⇒ Hash

New PEROBS objects must always be created by calling # Store.new(). PEROBS users should never call this method or equivalents of derived methods directly.

Parameters:

  • p (PEROBS::Handle)

    PEROBS handle

  • default (Any) (defaults to: nil)

    The default value that is returned when no value is stored for a specific key.



83
84
85
86
87
88
89
90
# File 'lib/perobs/Hash.rb', line 83

def initialize(p, default = nil)
  super(p)
  @default = nil
  @data = {}

  # Ensure that the newly created object will be pushed into the database.
  @store.cache.cache_write(self)
end

Instance Method Details

#_delete_reference_to_id(id) ⇒ Object

This method should only be used during store repair operations. It will delete all referenced to the given object ID.

Parameters:

  • id (Fixnum/Bignum)

    targeted object ID



103
104
105
106
107
# File 'lib/perobs/Hash.rb', line 103

def _delete_reference_to_id(id)
  @data.delete_if do |k, v|
    v && v.respond_to?(:is_poxreference?) && v.id == id
  end
end

#_deserialize(data) ⇒ Object

Restore the persistent data from a single data structure. This is a library internal method. Do not use outside of this library.

Parameters:

  • data (Hash)

    the actual Hash object



113
114
115
116
117
118
# File 'lib/perobs/Hash.rb', line 113

def _deserialize(data)
  @data = {}
  data.each { |k, v| @data[k] = v.is_a?(POReference) ?
                                POXReference.new(@store, v.id) : v }
  @data
end

#_referenced_object_idsArray of Fixnum or Bignum

Return a list of all object IDs of all persistend objects that this Hash is referencing.

Returns:

  • (Array of Fixnum or Bignum)

    IDs of referenced objects



95
96
97
98
# File 'lib/perobs/Hash.rb', line 95

def _referenced_object_ids
  @data.each_value.select { |v| v && v.respond_to?(:is_poxreference?) }.
    map { |o| o.id }
end