Class: Cubicle::Aggregation::AggregationManager

Inherits:
Object
  • Object
show all
Defined in:
lib/cubicle/aggregation/aggregation_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aggregation) ⇒ AggregationManager

Returns a new instance of AggregationManager.



7
8
9
10
11
# File 'lib/cubicle/aggregation/aggregation_manager.rb', line 7

def initialize(aggregation)
  @aggregation = aggregation
  @metadata = Cubicle::Aggregation::CubicleMetadata.new(aggregation)
  @profiler = Cubicle::Aggregation::Profiler.new(aggregation)
end

Instance Attribute Details

#aggregationObject (readonly)

Returns the value of attribute aggregation.



5
6
7
# File 'lib/cubicle/aggregation/aggregation_manager.rb', line 5

def aggregation
  @aggregation
end

#metadataObject (readonly)

Returns the value of attribute metadata.



5
6
7
# File 'lib/cubicle/aggregation/aggregation_manager.rb', line 5

def 
  @metadata
end

#profilerObject (readonly)

Returns the value of attribute profiler.



5
6
7
# File 'lib/cubicle/aggregation/aggregation_manager.rb', line 5

def profiler
  @profiler
end

Instance Method Details

#aggregate(query, options = {}) ⇒ Object



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
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/cubicle/aggregation/aggregation_manager.rb', line 97

def aggregate(query,options={})
  view = AggregationView.new(aggregation,query)

  map, reduce = MapReduceHelper.generate_map_function(query), MapReduceHelper.generate_reduce_function
  options[:finalize] = MapReduceHelper.generate_finalize_function(query)
  options["query"] = expand_template(prepare_filter(query,options[:where] || {}),view)

  query.source_collection_name = options.delete(:source_collection) || query.source_collection_name || aggregation.source_collection_name

  target_collection = options.delete(:target_collection)
  target_collection ||= query.target_collection_name if query.respond_to?(:target_collection_name)

  options[:out] = target_collection unless target_collection.blank? || query.transient?

  #This is defensive - some tests run without ever initializing any collections
  unless database.collection_names.include?(query.source_collection_name)
    Cubicle.logger.info "No collection was found in the database with a name of #{query.source_collection_name}"
    return []
  end

  reason = options.delete(:reason) || "Unknown"
  agg_info= options.delete(:aggregation_info)

  if aggregation.filter && (query.transient? || query == aggregation)
    (options['query'] ||= {}).merge!(aggregation.filter)
  end
  
  result = map_reduce(query.source_collection_name,expand_template(map, view),reduce,options)

  @profiler.record_map_reduce_result(query,options,result,reason,agg_info)

  @profiler.measure(:create_indexes, :target_collection=>options[:out] || "transient", :reason=>:finalize_aggregation) do
    ensure_indexes(target_collection,query.dimension_names)
  end if target_collection && !query.transient?

  #A bug, possibly in Mongo, does not produce a count on MR collections
  #sometimes, so we'll just add it from the result.
  output = database[result["result"]]
  output.instance_eval "def count; #{result["counts"]["output"]}; end"
  output
end

#collectionObject



17
18
19
# File 'lib/cubicle/aggregation/aggregation_manager.rb', line 17

def collection
  database[aggregation.target_collection_name]
end

#databaseObject



13
14
15
# File 'lib/cubicle/aggregation/aggregation_manager.rb', line 13

def database
  Cubicle.mongo.database
end

#execute_query(query, options = {}) ⇒ Object

noinspection RubyArgCount



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
58
59
60
61
62
63
64
65
66
# File 'lib/cubicle/aggregation/aggregation_manager.rb', line 27

def execute_query(query,options={})
  count = 0

  find_options = {
          :limit=>query.limit || 0,
          :skip=>query.offset || 0
  }

  find_options[:sort] = prepare_order_by(query)
  filter = {}

  if query == aggregation || query.transient?
    reduction = aggregate(query,options.merge(:reason=>"Transient query"))
  else
    process_if_required
    agg_data = aggregation_for(query)
    reduction = agg_data.collection
    #if the query exactly matches the aggregation in terms of requested members, we can issue a simple find
    #otherwise, a second map reduce is required to reduce the data set one last time
    if query.all_dimensions? || (agg_data.member_names - query.member_names - [:all_measures]).blank?
      filter = prepare_filter(query,options[:where] || {})
    else
      reduction = aggregate(query,:source_collection=>agg_data.target_collection_name, :reason=>"Last mile reduction - source aggregation has too many members (#{agg_data.member_names.join(",").inspect})")
    end
  end

  if reduction.blank?
    Cubicle::Data::Table.new(query,[],0)
  else

    @profiler.measure(:find, :source=>reduction.name, :reason=>"Fetch final query results", :query=>find_options) do
      count = reduction.count
      results = reduction.find(filter,find_options).to_a
      #reduction.drop if reduction.name =~ /^tmp.mr.*/
      Cubicle::Data::Table.new(query, results, count)
    end

  end

end

#expire!Object



86
87
88
89
90
91
# File 'lib/cubicle/aggregation/aggregation_manager.rb', line 86

def expire!
  @profiler.measure(:expire_aggregations, :reason=>"Expire aggregations") do
    collection.drop
    expire_metadata! :force=>true
  end
end

#expire_metadata!(opts = {}) ⇒ Object



93
94
95
# File 'lib/cubicle/aggregation/aggregation_manager.rb', line 93

def expire_metadata!(opts={})
  @metadata.expire!(opts)
end

#process(options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cubicle/aggregation/aggregation_manager.rb', line 68

def process(options={})
  @metadata.update_processing_stats do
    aggregate(aggregation,options.merge(:reason=>"Processing fact collection"))

    @metadata.unprotect_all #Mark all aggregations available for deletion.
    #Sort desc by length of array, so that larget
    #aggregations are processed first, hopefully increasing efficiency
    #of the processing step
    aggregation.aggregations.sort!{|a,b|b.length<=>a.length}
    aggregation.aggregations.each do |member_list|
      agg_start = Time.now
      aggregation_for(aggregation.query(:defer=>true){select member_list},true)
      Cubicle.logger.info "#{aggregation.name} aggregation #{member_list.inspect} processed in #{Time.now-agg_start} seconds"
    end
    expire_metadata!
  end
end

#target_collection_nameObject



21
22
23
# File 'lib/cubicle/aggregation/aggregation_manager.rb', line 21

def target_collection_name
  aggregation.target_collection_name
end