Class: Hyperactive::Hash::Head

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Cleaner::Accessors

append_features, #dirty?, #is_clean!, #is_dirty!

Methods included from Transactions::Accessors

append_features

Methods included from Index::Indexable

append_features

Methods included from Record::Persistent

#<=>, append_features, #create, #with_transaction

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.



69
70
71
72
# File 'lib/hyperactive/hash.rb', line 69

def initialize
  super
  self.list = nil
end

Instance Attribute Details

#listObject

Returns the value of attribute list.



62
63
64
# File 'lib/hyperactive/hash.rb', line 62

def list
  @list
end

Instance Method Details

#[](key) ⇒ Object

Return the value for key.



91
92
93
94
95
96
97
98
# File 'lib/hyperactive/hash.rb', line 91

def [](key)
  element = Archipelago::Pirate::BLACKBEARD[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.



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/hyperactive/hash.rb', line 110

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

  if (element = Archipelago::Pirate::BLACKBEARD[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
    Archipelago::Pirate::BLACKBEARD[my_key_for(key), @transaction] = element
  end
end

#clear!Object

Remove all key/value pairs from this Hash.



155
156
157
158
159
160
161
162
# File 'lib/hyperactive/hash.rb', line 155

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.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/hyperactive/hash.rb', line 126

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

  return_value = nil

  element = Archipelago::Pirate::BLACKBEARD[my_key_for(key), @transaction]
  if element
    Archipelago::Pirate::BLACKBEARD.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.



167
168
169
170
# File 'lib/hyperactive/hash.rb', line 167

def destroy!
  self.clear!
  super
end

#each(&block) ⇒ Object

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



144
145
146
147
148
149
150
# File 'lib/hyperactive/hash.rb', line 144

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)


84
85
86
# File 'lib/hyperactive/hash.rb', line 84

def empty?
  self.list.empty?
end

#include?(key) ⇒ Boolean

Returns whether key is included in this Hash.

Returns:

  • (Boolean)


103
104
105
# File 'lib/hyperactive/hash.rb', line 103

def include?(key)
  Archipelago::Pirate::BLACKBEARD.include?(my_key_for(key), @transaction)
end

#sizeObject

Return the size of this Hash.



77
78
79
# File 'lib/hyperactive/hash.rb', line 77

def size
  self.list.size
end