Class: ActiveWarehouse::Aggregate::DwarfAggregate

Inherits:
Aggregate
  • Object
show all
Includes:
DwarfCommon
Defined in:
lib/active_warehouse/aggregate/dwarf_aggregate.rb

Overview

Implementation of the Dwarf algorithm described in

Defined Under Namespace

Classes: Cell, Node

Instance Attribute Summary collapse

Attributes inherited from Aggregate

#cube_class

Instance Method Summary collapse

Methods included from DwarfCommon

#create_tuple, #dimension_order, #dimension_order=, #sorted_facts

Constructor Details

#initialize(cube_class) ⇒ DwarfAggregate

Initialize the aggregate



8
9
10
# File 'lib/active_warehouse/aggregate/dwarf_aggregate.rb', line 8

def initialize(cube_class)
  super
end

Instance Attribute Details

#number_of_dimensionsObject

Accessor for the number of dimensions in the cube.



179
180
181
# File 'lib/active_warehouse/aggregate/dwarf_aggregate.rb', line 179

def number_of_dimensions
  @number_of_dimensions
end

Instance Method Details

#calculate_aggregate(cells) ⇒ Object

Aggregate the node by summing all of the values in the cells TODO: support aggregations other than sum



76
77
78
79
80
81
82
83
84
# File 'lib/active_warehouse/aggregate/dwarf_aggregate.rb', line 76

def calculate_aggregate(cells)
  value = Array.new(cells.first.value.length, 0)
  cells.each do |c|
    c.value.each_with_index do |v, index|
      value[index] += v
    end
  end
  value
end

#calculate_prefix(current_tuple, last_tuple) ⇒ Object

Calculates a common prefix between the two tuples



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/active_warehouse/aggregate/dwarf_aggregate.rb', line 185

def calculate_prefix(current_tuple, last_tuple)
  return [] if last_tuple.nil?
  prefix = []
  last_matched_index = nil
  0.upto(number_of_dimensions) do |i|
    if current_tuple[i] == last_tuple[i]
      prefix << current_tuple[i]
    else
      break
    end
  end
  prefix
end

#close_nodes(prefix) ⇒ Object

Close all of the last nodes that match the specified prefix and return the list of newly closed nodes



201
202
203
204
205
206
207
208
209
210
# File 'lib/active_warehouse/aggregate/dwarf_aggregate.rb', line 201

def close_nodes(prefix)
  new_closed = []
  if @last_nodes
    @last_nodes[prefix.length + 1, @last_nodes.length].each do |n|
      n.closed = true
      new_closed << n
    end
  end
  new_closed
end

#create_dwarf_cube(sorted_facts) ⇒ Object

Create the dwarf cube with the sorted_facts



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/active_warehouse/aggregate/dwarf_aggregate.rb', line 87

def create_dwarf_cube(sorted_facts)
  last_tuple = nil
  @last_nodes = nil
  sorted_facts.each do |row|
    tuple = row.is_a?(Hash) ? create_tuple(row) : row
    
    prefix = calculate_prefix(tuple, last_tuple)
    
    close_nodes(prefix).each do |n|
      if n.leaf?
        n.all_cell = Cell.new('*', calculate_aggregate(n.cells))
      else
        n.all_cell = Cell.new('*')
        n.all_cell.child = suffix_coalesce(n.children)
      end
      n.processed = true
    end
    
    nodes = create_nodes(tuple, prefix)
    
    write_nodes(nodes)
    last_tuple = tuple
    if @last_nodes.nil? then @root_node = nodes.first end
    @last_nodes = nodes
  end
  
  # Alg 1, Line 13
  last_leaf_node = @last_nodes.last
  last_leaf_node.all_cell = Cell.new('*', calculate_aggregate(last_leaf_node.cells))
  
  # Alg 1, Line 14
  @last_nodes[0..@last_nodes.length - 2].reverse.each do |n|
    n.all_cell = Cell.new('*')
    n.all_cell.child = suffix_coalesce(n.children)
  end
  
  require File.dirname(__FILE__) + '/dwarf_printer'
  puts DwarfPrinter.print_node(@root_node)
end

#create_nodes(current_tuple, prefix) ⇒ Object

Create the nodes for the current tuple



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/active_warehouse/aggregate/dwarf_aggregate.rb', line 213

def create_nodes(current_tuple, prefix)
  nodes = []
  new_nodes_needed_for = []
  if @last_nodes.nil?
    0.upto(number_of_dimensions - 1) do |i|
      k = current_tuple[i]
      parent_cell = (nodes.last.nil?) ? nil : nodes.last.cells.last
      nodes << Node.new(k, parent_cell)
    end
  else
    if prefix.length > 0
      0.upto(prefix.length - 1) do |i|
        nodes << @last_nodes[i]
      end
    end
    k = current_tuple[prefix.length]
    n = @last_nodes[prefix.length]
    n.add_cell(Cell.new(k))
    nodes << n
    
    (prefix.length + 1).upto(number_of_dimensions - 1) do |i|
      k = current_tuple[i]
      parent_cell = (nodes.last.nil?) ? nil : nodes.last.cells.last
      nodes << Node.new(k, parent_cell)
    end
  end
  
  nodes.last.leaf = true
  cell = nodes.last.cells.last
  unless cell.value
    cell.value = current_tuple[number_of_dimensions..current_tuple.length-1]
  end
  
  nodes
end

#filter_nodes(node, dimension_ids, depth, filtered_nodes) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/active_warehouse/aggregate/dwarf_aggregate.rb', line 59

def filter_nodes(node, dimension_ids, depth, filtered_nodes)
  #puts "filtering node #{print_node(node, depth, false)}"
  dimension = dimension_order[depth]
  #puts "dimension at #{depth} is #{dimension}"
  node.cells.each do |c|
    if dimension_ids[dimension].include?(c.key)
      if depth == dimension_order.length - 1
        filtered_nodes << node
      else
        filter_nodes(c.child, dimension_ids, depth+1, filtered_nodes) unless c.child.nil?
      end
    end
  end
end

#populateObject

Populate the aggregate



13
14
15
# File 'lib/active_warehouse/aggregate/dwarf_aggregate.rb', line 13

def populate
  create_dwarf_cube(sorted_facts)
end

#query(*args) ⇒ Object

query



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_warehouse/aggregate/dwarf_aggregate.rb', line 18

def query(*args)
  options = parse_query_args(*args)
  
  column_dimension_name = options[:column_dimension_name]
  column_hierarchy_name = options[:column_hierarchy_name]
  row_dimension_name = options[:row_dimension_name]
  row_hierarchy_name = options[:row_hierarchy_name]
  conditions = options[:conditions]
  cstage = options[:cstage]
  rstage = options[:rstage]
  filters = options[:filters]
  
  column_dimension = Dimension.class_for_name(column_dimension_name)
  row_dimension = Dimension.class_for_name(row_dimension_name)
  column_hierarchy = column_dimension.hierarchy(column_hierarchy_name)
  row_hierarchy = row_dimension.hierarchy(row_hierarchy_name)
  dimension_ids = {}
  
  dimension_order.each do |d|
    where_clause = []
    sql = "SELECT id FROM #{d.table_name}"
    filters.each do |key, value|
      dimension, column = key.split('.')
      if d.table_name == dimension
        where_clause << "#{dimension}.#{column} = '#{value}'" # TODO: protect from SQL injection
      end
    end
    sql += %Q(\nWHERE\n  #{where_clause.join(" AND\n  ")}) if where_clause.length > 0
    dimension_ids[d] = cube_class.connection.select_values(sql)
  end
  #puts "dimension ids: #{dimension_ids.inspect}"
  
  values = Array.new(cube_class.fact_class.aggregate_fields.length, 0)
  
  home_nodes = []
  filter_nodes(@root_node, dimension_ids, 0, home_nodes)
  #puts "filtered nodes: #{home_nodes.collect(&:id)}"
  
  values
end

#sorted_keys(nodes) ⇒ Object

Get a list of sorted keys for the cells in the specified nodes



168
169
170
171
172
173
174
175
176
# File 'lib/active_warehouse/aggregate/dwarf_aggregate.rb', line 168

def sorted_keys(nodes)
  keys = []
  nodes.each do |n|
    n.cells.each do |c|
      keys << c.key
    end
  end
  keys.uniq.sort { |a, b| a <=> b }
end

#suffix_coalesce(nodes) ⇒ Object

Coalesce the nodes and return a single node



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/active_warehouse/aggregate/dwarf_aggregate.rb', line 128

def suffix_coalesce(nodes)
  if nodes.length == 1
    return nodes[0]
  else
    sub_dwarf = Node.new
    sub_dwarf.leaf = nodes.first.leaf
    
    keys = sorted_keys(nodes)
    keys.each do |k|
      to_merge = []
      nodes.each do |n|
        n.cells.each do |c|
          to_merge << c if c.key == k
        end
      end
      
      if sub_dwarf.leaf?
        cur_aggr = calculate_aggregate(to_merge)    # Alg 2, Line 8
        sub_dwarf.add_cell(Cell.new(k, cur_aggr))   # Alg 2, Line 9
      else
        # Alg 2, Line 11
        cell = Cell.new(k)
        cell.child = suffix_coalesce(to_merge.collect{|c| c.child})
        sub_dwarf.add_cell(cell)
      end
    end
    
    if sub_dwarf.leaf?
      sub_dwarf.all_cell = Cell.new("*", calculate_aggregate(sub_dwarf.cells))
    else
      cell = Cell.new("*")
      cell.child = suffix_coalesce(sub_dwarf.children)
      sub_dwarf.all_cell = cell
    end
  end
  
  sub_dwarf
end

#write_nodes(nodes) ⇒ Object

Write nodes to the filesystem.



250
251
252
253
254
# File 'lib/active_warehouse/aggregate/dwarf_aggregate.rb', line 250

def write_nodes(nodes)
  # open(File.new(cube_class.name + '.dat'), 'w') do |f|
#           
#         end
end