Class: Cubicle::Aggregation::AggregationMetadata

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cubicle_metadata, member_names_or_attribute_hash, protect = false) ⇒ AggregationMetadata

Returns a new instance of AggregationMetadata.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cubicle/aggregation/aggregation_metadata.rb', line 53

def initialize(,member_names_or_attribute_hash,protect=false)
  @cubicle_metadata = 
  @attributes = nil
  if (member_names_or_attribute_hash.kind_of?(Hash))
    @attributes = member_names_or_attribute_hash
  else
    member_names = member_names_or_attribute_hash

    unless protect
      @candidate_aggregation = self.class.collection.find(
              :aggregation=>@cubicle_metadata.aggregation.name,
              :member_names=>{"$all"=>member_names}, :document_count=>{"$gte"=>0}).sort([:document_count, :asc]).limit(1).next_document


      #since the operator used in the query was $all, having equal lengths in the original and returned
      #member array means that they are identical, which means that regardless of the number of documents
      #in the aggregation, it is the candidate we want. Otherwise, we'll check to see if we
      #boil down the data further, or just make our soup with what we've got.
      @attributes = @candidate_aggregation if @candidate_aggregation &&
              (@candidate_aggregation["member_names"].length == member_names.length ||
                      @candidate_aggregation["document_count"] < self.class.min_records_to_reduce)

    end

    unless @attributes
      self.class.collection.remove(:aggregation=>@cubicle_metadata.aggregation.name,:member_names=>member_names)
      @attributes = HashWithIndifferentAccess.new({:aggregation=>@cubicle_metadata.aggregation.name,
                                                   :member_names=>member_names,
                                                   :document_count=>-1,
                                                   :created_at=>Time.now.utc,
                                                   :protect=>protect})

      #materialize the aggregation, and, if the operation was successful,
      #register it as available for use by future queries
      @attributes[:_id] = self.class.collection.insert(@attributes)
      materialize!
    end

  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/cubicle/aggregation/aggregation_metadata.rb', line 126

def method_missing(sym,*args)
  if (@attributes.keys.include?(sym.to_s))
    @attributes[sym.to_s]
  else
    super(sym,*args)
  end
end

Class Method Details

.aggregation_collection_name(cubicle_name, aggregation_id) ⇒ Object



15
16
17
# File 'lib/cubicle/aggregation/aggregation_metadata.rb', line 15

def aggregation_collection_name(cubicle_name,aggregation_id)
  "cubicle.aggregation.#{cubicle_name}._#{aggregation_id}"
end

.collectionObject



6
7
8
9
# File 'lib/cubicle/aggregation/aggregation_metadata.rb', line 6

def collection
  @@aggregations_collection_name ||= "#{Cubicle::Aggregation::CubicleMetadata.collection.name}.aggregations"
  Cubicle.mongo.database[@@aggregations_collection_name]
end

.collection=(collection_name) ⇒ Object



11
12
13
# File 'lib/cubicle/aggregation/aggregation_metadata.rb', line 11

def collection=(collection_name)
  @@aggregations_collection_name = collection_name
end

.expire(aggregation, opts = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cubicle/aggregation/aggregation_metadata.rb', line 37

def expire(aggregation,opts={})
  aggregation_name = case aggregation
                       when String then aggregation
                       when Symbol then aggregation.to_s
                       when Cubicle::Aggregation::CubicleMetadata then aggregation.aggregation.name
                       else aggregation.name
                     end
  collection.find(:aggregation=>aggregation_name).each do |agg|
    next if agg["protect"] && !opts[:force]
    col_name = aggregation_collection_name(aggregation_name,agg["_id"].to_s)
    Cubicle.mongo.database[col_name].drop if Cubicle.mongo.database[col_name]
    collection.remove(:_id=>agg["_id"])
  end
end

.min_records_to_reduceObject



19
20
21
# File 'lib/cubicle/aggregation/aggregation_metadata.rb', line 19

def min_records_to_reduce
  @min_records_to_reduce ||= 100
end

.min_records_to_reduce=(min) ⇒ Object



23
24
25
# File 'lib/cubicle/aggregation/aggregation_metadata.rb', line 23

def min_records_to_reduce=(min)
  @min_records_to_reduce = min
end

.unprotect_all(aggregation) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/cubicle/aggregation/aggregation_metadata.rb', line 27

def unprotect_all(aggregation)
  aggregation_name = case aggregation
                       when String then aggregation
                       when Symbol then aggregation.to_s
                       when Cubicle::Aggregation::CubicleMetadata then aggregation.aggregation.name
                       else aggregation.name
                     end
  collection.update({:aggregation=>aggregation_name},{"$set"=>{:protect=>false}})
end

Instance Method Details

#collectionObject



114
115
116
# File 'lib/cubicle/aggregation/aggregation_metadata.rb', line 114

def collection
  @collection ||= Cubicle.mongo.database[target_collection_name] if materialized?
end

#collection=(collection) ⇒ Object



118
119
120
# File 'lib/cubicle/aggregation/aggregation_metadata.rb', line 118

def collection=(collection)
  @collection = collection
end

#document_countObject



122
123
124
# File 'lib/cubicle/aggregation/aggregation_metadata.rb', line 122

def document_count
  @attributes["document_count"]
end

#materialized?Boolean

Returns:

  • (Boolean)


108
109
110
111
112
# File 'lib/cubicle/aggregation/aggregation_metadata.rb', line 108

def materialized?
  document_count >= 0 &&
          (!@collection.blank? ||
                  Cubicle.mongo.database.collection_names.include?(target_collection_name))
end

#member_namesObject



106
# File 'lib/cubicle/aggregation/aggregation_metadata.rb', line 106

def member_names; @attributes["member_names"] || []; end

#source_collection_nameObject



98
99
100
101
102
103
104
# File 'lib/cubicle/aggregation/aggregation_metadata.rb', line 98

def source_collection_name
  if @candidate_aggregation
    candidate = Cubicle::Aggregation::AggregationMetadata.new(@cubicle_metadata,@candidate_aggregation)
    return candidate.target_collection_name
  end
  @cubicle_metadata.aggregation.target_collection_name
end

#target_collection_nameObject



94
95
96
# File 'lib/cubicle/aggregation/aggregation_metadata.rb', line 94

def target_collection_name
  AggregationMetadata.aggregation_collection_name(@cubicle_metadata.aggregation.name,@attributes["_id"].to_s)
end