Class: Hyperactive::Tree

Inherits:
Record
  • Object
show all
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

Record::HOST

Instance Attribute Summary collapse

Attributes inherited from Record

#record_id

Instance Method Summary collapse

Methods inherited from Record

create_hooks, destroy_hooks, find, get_instance, index_by, reject, #save_hook, save_hooks, select, setup, transaction

Constructor Details

#initialize(options = {}) ⇒ Tree

Dont call this! Call Tree.get_instance(options) instead!



27
28
29
30
31
# File 'lib/hyperactive/tree.rb', line 27

def initialize(options = {})
  @width = options[:width] || WIDTH
  @elements = {}
  @subtrees = nil
end

Instance Attribute Details

#elementsObject

Returns the value of attribute elements.



20
21
22
# File 'lib/hyperactive/tree.rb', line 20

def elements
  @elements
end

#subtreesObject

Returns the value of attribute subtrees.



20
21
22
# File 'lib/hyperactive/tree.rb', line 20

def subtrees
  @subtrees
end

Instance Method Details

#[](key) ⇒ Object

Returns the value for key in this Tree.



113
114
115
116
117
118
119
# File 'lib/hyperactive/tree.rb', line 113

def [](key)
  if @elements
    return @elements[key]
  else
    return subtree_for(key)[key]
  end
end

#[]=(key, value) ⇒ Object

Puts value under key in this Tree.



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

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

#clearObject

Clear everything from this Tree.



168
169
170
171
172
173
174
175
176
# File 'lib/hyperactive/tree.rb', line 168

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 Tree.



124
125
126
127
128
129
130
# File 'lib/hyperactive/tree.rb', line 124

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 Tree.



36
37
38
39
40
41
42
# File 'lib/hyperactive/tree.rb', line 36

def delete(key)
  if @elements
    @elements.delete(key)
  else
    subtree_for(key).delete(key)
  end
end

#destroyObject

Clear everything from this Tree and destroy it.



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/hyperactive/tree.rb', line 151

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 Tree.



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/hyperactive/tree.rb', line 135

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 Tree.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hyperactive/tree.rb', line 79

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 Tree.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hyperactive/tree.rb', line 62

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

#sizeObject

Returns the size of this Tree.



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hyperactive/tree.rb', line 47

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