Class: Hyperactive::Tree::Root
- Inherits:
-
Record::Bass
- Object
- Record::Bass
- Hyperactive::Tree::Root
- Defined in:
- lib/hyperactive/tree.rb
Overview
A class suitable for storing large and often-changing datasets in an Archipelago environment.
Is constructed like a set of nested Hashes that automatically create new children on demand, and will thusly only have to check the path from the root node to the leaf for changes when method calls return (see Archipelago::Treasure::Chest) and will only have to actually store into database the leaf itself if it has changed.
Constant Summary collapse
- WIDTH =
1 << 3
Constants inherited from Record::Bass
Instance Attribute Summary collapse
-
#elements ⇒ Object
Returns the value of attribute elements.
-
#subtrees ⇒ Object
Returns the value of attribute subtrees.
Attributes inherited from Record::Bass
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns the value for
key
in this Root. -
#[]=(key, value) ⇒ Object
Puts
value
underkey
in this Root. -
#clear ⇒ Object
Clear everything from this Root.
-
#collect(callable) ⇒ Object
Returns an Array containing
callable
.call(key, value) from all values in this Root. -
#delete(key) ⇒ Object
Deletes
key
in this Root. -
#destroy ⇒ Object
Clear everything from this Tree and destroy it.
-
#each(callable) ⇒ Object
Does
callable
.call(key, value) on all values in this Root. -
#initialize(options = {}) ⇒ Root
constructor
Dont call this! Call Root.get_instance(options) instead!.
-
#reject(callable) ⇒ Object
Returns all keys and values returning false for
callable
.call(key, value) in this Root. -
#select(callable) ⇒ Object
Returns all keys and values returning true for
callable
.call(key, value) in this Root. -
#size ⇒ Object
Returns the size of this Root.
Methods inherited from Record::Bass
create_hooks, destroy_hooks, find, get_instance, index_by, reject, #save_hook, save_hooks, select, setup, transaction
Constructor Details
Instance Attribute Details
#elements ⇒ Object
Returns the value of attribute elements.
43 44 45 |
# File 'lib/hyperactive/tree.rb', line 43 def elements @elements end |
#subtrees ⇒ Object
Returns the value of attribute subtrees.
43 44 45 |
# File 'lib/hyperactive/tree.rb', line 43 def subtrees @subtrees end |
Instance Method Details
#[](key) ⇒ Object
Returns the value for key
in this Root.
136 137 138 139 140 141 142 |
# File 'lib/hyperactive/tree.rb', line 136 def [](key) if @elements return @elements[key] else return subtree_for(key)[key] end end |
#[]=(key, value) ⇒ Object
Puts value
under key
in this Root.
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/hyperactive/tree.rb', line 119 def []=(key, value) if @elements if @elements.size < @width @elements[key] = value else split! subtree_for(key)[key] = value end else subtree_for(key)[key] = value end return value end |
#clear ⇒ Object
Clear everything from this Root.
191 192 193 194 195 196 197 198 199 |
# File 'lib/hyperactive/tree.rb', line 191 def clear unless @elements @subtrees.each do |tree_id, tree| tree.clear end @subtrees = nil end @elements = {} end |
#collect(callable) ⇒ Object
Returns an Array containing callable
.call(key, value) from all values in this Root.
147 148 149 150 151 152 153 |
# File 'lib/hyperactive/tree.rb', line 147 def collect(callable) rval = [] self.each(Proc.new do |k,v| rval << callable.call(k,v) end) return rval end |
#delete(key) ⇒ Object
Deletes key
in this Root.
59 60 61 62 63 64 65 |
# File 'lib/hyperactive/tree.rb', line 59 def delete(key) if @elements @elements.delete(key) else subtree_for(key).delete(key) end end |
#destroy ⇒ Object
Clear everything from this Tree and destroy it.
174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/hyperactive/tree.rb', line 174 def destroy if @elements @elements.each do |key, value| value.destroy if value.respond_to?(:destroy) end else @subtrees.each do |tree_id, tree| tree.destroy end end freeze super end |
#each(callable) ⇒ Object
Does callable
.call(key, value) on all values in this Root.
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/hyperactive/tree.rb', line 158 def each(callable) if @elements @elements.each do |key, value| callable.call(key, value) end else @subtrees.t_each do |tree_id, tree| tree.each(callable) end nil end end |
#reject(callable) ⇒ Object
Returns all keys and values returning false for callable
.call(key, value) in this Root.
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/hyperactive/tree.rb', line 102 def reject(callable) if @elements @elements.reject do |k,v| callable.call(k,v) end else @subtrees.t_collect do |tree_id, tree| tree.reject(callable) end.inject([]) do |sum, match| sum + match end end end |
#select(callable) ⇒ Object
Returns all keys and values returning true for callable
.call(key, value) in this Root.
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/hyperactive/tree.rb', line 85 def select(callable) if @elements @elements.select do |k,v| callable.call(k,v) end else @subtrees.t_collect do |tree_id, tree| tree.select(callable) end.inject([]) do |sum, match| sum + match end end end |
#size ⇒ Object
Returns the size of this Root.
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/hyperactive/tree.rb', line 70 def size if @elements @elements.size else @subtrees.t_collect do |tree_id, tree| tree.size end.inject(0) do |sum, size| sum + size end end end |