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, #_transfer, 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.



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

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



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

def _delete_reference_to_id(id)
  @data.delete_if do |k, v|
    v && v.respond_to?(:is_poxreference?) && v.id == id
  end
  @store.cache.cache_write(self)
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



115
116
117
118
119
120
# File 'lib/perobs/Hash.rb', line 115

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



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

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

#inspectString

Textual dump for debugging purposes

Returns:

  • (String)


124
125
126
127
128
129
130
131
# File 'lib/perobs/Hash.rb', line 124

def inspect
  "<#{self.class}:#{@_id}>\n{\n" +
  @data.map do |k, v|
    "  #{k.inspect} => " + (v.respond_to?(:is_poxreference?) ?
            "<PEROBS::ObjectBase:#{v._id}>" : v.inspect)
  end.join(",\n") +
  "\n}\n"
end