Class: Hyperactive::Hash::Head

Inherits:
Record::Bass show all
Includes:
Archipelago::Current::ThreadedCollection
Defined in:
lib/hyperactive/hash.rb

Overview

A class suitable for storing large and often-changing datasets in an Archipelago environment.

Constant Summary

Constants inherited from Record::Bass

Record::Bass::HOST

Instance Attribute Summary collapse

Attributes inherited from Record::Bass

#record_id

Attributes included from Transactions::Participant

#transaction

Instance Method Summary collapse

Methods inherited from Record::Bass

#<=>, #create, find, get_instance

Methods included from Hyperactive::Hooker::Pimp

append_features, #load_hook, #save_hook

Methods included from Transactions::Participant

append_features, #with_transaction

Methods included from Transactions::Accessors

append_features

Methods included from Index::Indexable

append_features

Constructor Details

#initializeHead

Initialize a Hash::Head.

NB: Remember to call create on the new instance or use Head.get_instance to get that done automatically.



64
65
66
67
# File 'lib/hyperactive/hash.rb', line 64

def initialize
  super
  self.list = nil
end

Instance Attribute Details

#listObject

Returns the value of attribute list.



55
56
57
# File 'lib/hyperactive/hash.rb', line 55

def list
  @list
end

Instance Method Details

#[](key) ⇒ Object

Return the value for key.



86
87
88
89
90
91
92
93
# File 'lib/hyperactive/hash.rb', line 86

def [](key)
  element = Hyperactive::CAPTAIN[my_key_for(key), @transaction]
  if element
    return element.value
  else
    return nil
  end
end

#[]=(key, value) ⇒ Object

Insert value under key in this Hash.



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/hyperactive/hash.rb', line 105

def []=(key, value)
  self.list = Hyperactive::List::Head.get_instance_with_transaction(@transaction) unless self.list

  if (element = Hyperactive::CAPTAIN[my_key_for(key), @transaction])
    element.value = value
  else          
    element = Element.get_instance_with_transaction(@transaction, key, value, nil)
    self.list << element
    element.list_element = self.list.last_element
    Hyperactive::CAPTAIN[my_key_for(key), @transaction] = element
  end
end

#clear!Object

Remove all key/value pairs from this Hash.



150
151
152
153
154
155
156
157
# File 'lib/hyperactive/hash.rb', line 150

def clear!
  self.list = Hyperactive::List::Head.get_instance_with_transaction(@transaction) unless self.list

  self.list.t_each do |element|
    element.destroy!
  end
  self.list.clear!
end

#delete(key) ⇒ Object

Delete key from this Hash.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/hyperactive/hash.rb', line 121

def delete(key)
  self.list = Hyperactive::List::Head.get_instance_with_transaction(@transaction) unless self.list

  return_value = nil

  element = Hyperactive::CAPTAIN[my_key_for(key), @transaction]
  if element
    Hyperactive::CAPTAIN.delete(my_key_for(key), @transaction)
    self.list.unlink!(element.list_element)
    return_value = element.value
    element.destroy!
  end
  return return_value
end

#destroy!Object

Clear everything from this Tree and destroy it.



162
163
164
165
# File 'lib/hyperactive/hash.rb', line 162

def destroy!
  self.clear!
  super
end

#each(&block) ⇒ Object

Will yield to block once for each key/value pair in this Hash.



139
140
141
142
143
144
145
# File 'lib/hyperactive/hash.rb', line 139

def each(&block)
  self.list = Hyperactive::List::Head.get_instance_with_transaction(@transaction) unless self.list

  self.list.each do |element|
    yield([element.key, element.value])
  end
end

#empty?Boolean

Return whether this Hash is empty.

Returns:

  • (Boolean)


79
80
81
# File 'lib/hyperactive/hash.rb', line 79

def empty?
  self.list.empty?
end

#include?(key) ⇒ Boolean

Returns whether key is included in this Hash.

Returns:

  • (Boolean)


98
99
100
# File 'lib/hyperactive/hash.rb', line 98

def include?(key)
  Hyperactive::CAPTAIN.include?(my_key_for(key), @transaction)
end

#sizeObject

Return the size of this Hash.



72
73
74
# File 'lib/hyperactive/hash.rb', line 72

def size
  self.list.size
end