Class: Cubicle::Aggregation::AggregationManager
- Inherits:
-
Object
- Object
- Cubicle::Aggregation::AggregationManager
- Defined in:
- lib/cubicle/aggregation/aggregation_manager.rb
Instance Attribute Summary collapse
-
#aggregation ⇒ Object
readonly
Returns the value of attribute aggregation.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#profiler ⇒ Object
readonly
Returns the value of attribute profiler.
Instance Method Summary collapse
- #aggregate(query, options = {}) ⇒ Object
- #collection ⇒ Object
- #database ⇒ Object
-
#execute_query(query, options = {}) ⇒ Object
noinspection RubyArgCount.
- #expire! ⇒ Object
- #expire_metadata!(opts = {}) ⇒ Object
-
#initialize(aggregation) ⇒ AggregationManager
constructor
A new instance of AggregationManager.
- #process(options = {}) ⇒ Object
- #target_collection_name ⇒ Object
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
#aggregation ⇒ Object (readonly)
Returns the value of attribute aggregation.
5 6 7 |
# File 'lib/cubicle/aggregation/aggregation_manager.rb', line 5 def aggregation @aggregation end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
5 6 7 |
# File 'lib/cubicle/aggregation/aggregation_manager.rb', line 5 def @metadata end |
#profiler ⇒ Object (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,={}) view = AggregationView.new(aggregation,query) map, reduce = MapReduceHelper.generate_map_function(query), MapReduceHelper.generate_reduce_function [:finalize] = MapReduceHelper.generate_finalize_function(query) ["query"] = (prepare_filter(query,[:where] || {}),view) query.source_collection_name = .delete(:source_collection) || query.source_collection_name || aggregation.source_collection_name target_collection = .delete(:target_collection) target_collection ||= query.target_collection_name if query.respond_to?(:target_collection_name) [: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 = .delete(:reason) || "Unknown" agg_info= .delete(:aggregation_info) if aggregation.filter && (query.transient? || query == aggregation) (['query'] ||= {}).merge!(aggregation.filter) end result = map_reduce(query.source_collection_name,(map, view),reduce,) @profiler.record_map_reduce_result(query,,result,reason,agg_info) @profiler.measure(:create_indexes, :target_collection=>[: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 |
#collection ⇒ Object
17 18 19 |
# File 'lib/cubicle/aggregation/aggregation_manager.rb', line 17 def collection database[aggregation.target_collection_name] end |
#database ⇒ Object
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,={}) count = 0 = { :limit=>query.limit || 0, :skip=>query.offset || 0 } [:sort] = prepare_order_by(query) filter = {} if query == aggregation || query.transient? reduction = aggregate(query,.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,[: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=>) do count = reduction.count results = reduction.find(filter,).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 :force=>true end end |
#expire_metadata!(opts = {}) ⇒ Object
93 94 95 |
# File 'lib/cubicle/aggregation/aggregation_manager.rb', line 93 def (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(={}) @metadata.update_processing_stats do aggregate(aggregation,.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 end end |
#target_collection_name ⇒ Object
21 22 23 |
# File 'lib/cubicle/aggregation/aggregation_manager.rb', line 21 def target_collection_name aggregation.target_collection_name end |