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.



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

def initialize
  super
  self.list = nil
end

Instance Attribute Details

#listObject

Returns the value of attribute list.



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

def list
  @list
end

Instance Method Details

#[](key) ⇒ Object

Return the value for key.



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

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.



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

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

  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.



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

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.



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

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

  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.



169
170
171
172
# File 'lib/hyperactive/hash.rb', line 169

def destroy!
  self.clear!
  super
end

#each(&block) ⇒ Object

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



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

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)


86
87
88
# File 'lib/hyperactive/hash.rb', line 86

def empty?
  self.list.empty?
end

#include?(key) ⇒ Boolean

Returns whether key is included in this Hash.

Returns:

  • (Boolean)


105
106
107
# File 'lib/hyperactive/hash.rb', line 105

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

#sizeObject

Return the size of this Hash.



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

def size
  self.list.size
end