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, #transaction

Instance Method Summary collapse

Methods inherited from Record::Bass

#<=>, attr_accessor, attr_reader, attr_writer, #create, create_hooks, destroy_hooks, find, get_instance, get_instance_with_transaction, index_by, #load_hook, load_hooks, reject, #save_hook, save_hooks, select, setup, #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.



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

def initialize
  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.



78
79
80
81
82
83
84
85
# File 'lib/hyperactive/hash.rb', line 78

def [](key)
  element = Hyperactive::Record::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.



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/hyperactive/hash.rb', line 97

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

  if (element = Hyperactive::Record::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::Record::CAPTAIN[my_key_for(key), @transaction] = element
  end
end

#clear!Object

Remove all key/value pairs from this Hash.



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

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.



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

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

  return_value = nil

  element = Hyperactive::Record::CAPTAIN[my_key_for(key), @transaction]
  if element
    Hyperactive::Record::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.



154
155
156
157
# File 'lib/hyperactive/hash.rb', line 154

def destroy!
  self.clear!
  super
end

#each(&block) ⇒ Object

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



131
132
133
134
135
136
137
# File 'lib/hyperactive/hash.rb', line 131

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

#include?(key) ⇒ Boolean

Returns whether key is included in this Hash.

Returns:

  • (Boolean)


90
91
92
# File 'lib/hyperactive/hash.rb', line 90

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

#sizeObject

Return the size of this Hash.



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

def size
  self.list.size
end