Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Breaking Aggregation into separated files

  • Loading branch information...
commit ed15fc281cfa2f29dfefa8e6f8ab924c4a06d377 1 parent 2b121f1
Brendan W. McAdams authored
120  ...h-query/src/main/scala/AggregationFramework.scala → casbah-query/src/main/scala/aggregation/Group.scala
... ...
@@ -1,5 +1,5 @@
1 1
 /**
2  
- * Copyright (c) 2010 - 2012 10gen, Inc. <http://10gen.com>
  2
+ * Copyright (c) 2010 - 2013 10gen, Inc. <http://10gen.com>
3 3
  *
4 4
  * Licensed under the Apache License, Version 2.0 (the "License");
5 5
  * you may not use this file except in compliance with the License.
@@ -18,126 +18,11 @@
18 18
  *     http://github.com/mongodb/casbah
19 19
  *
20 20
  */
21  
-
22 21
 package com.mongodb.casbah.query.dsl
23  
-
24  
-import com.mongodb.casbah.commons.Logging
25  
-
26  
-import scalaj.collection.Imports._
  22
+package aggregation
27 23
 
28 24
 import com.mongodb.casbah.query.Imports._
29 25
 
30  
-import scala.util.matching._
31  
-import scala.collection.Iterable
32  
-import scala.collection.mutable.{Seq => MutableSeq}
33  
-
34  
-import org.bson._
35  
-import org.bson.types.BasicBSONList
36  
-
37  
-object AggregationFramework {}
38  
-
39  
-/**
40  
- * Base trait for a Pipeline Operator for 
41  
- * the Aggregation Framework.
42  
- * These operators are the "core" of Aggregation,
43  
- * representing the primary pipeline.
44  
- */
45  
-trait PipelineOperator {
46  
-  def list: MongoDBList
47  
-
48  
-  protected def op(oper: String, target: Any) = 
49  
-    PipelineOperator(oper, target)(list)
50  
-  
51  
-  override def toString = list.toString
52  
-  
53  
-}
54  
-
55  
-object PipelineOperator {
56  
-  
57  
-  def apply[A <: String, B <: Any](kv: (A, B))(pipeline: MongoDBList): DBObject with PipelineOperations with PipelineOperator = {
58  
-    val obj = new BasicDBObject with PipelineOperations with PipelineOperator { val list = pipeline }
59  
-    obj.put(kv._1, kv._2)
60  
-    pipeline += obj
61  
-    obj
62  
-  }
63  
-}
64  
-
65  
-
66  
-// TODO - Validations of things like "ran group after sort" for certain opers
67  
-trait PipelineOperations extends GroupOperator
68  
-   with LimitOperator
69  
-   with SkipOperator 
70  
-   with MatchOperator
71  
-   with ProjectOperator
72  
-   with SortOperator
73  
-   with UnwindOperator
74  
-   
75  
-trait BasePipelineOperations extends PipelineOperations { val list = MongoDBList.empty }
76  
- 
77  
-trait GroupSubOperators extends GroupSumOperator
78  
-  with GroupPushOperator
79  
-  with GroupAvgOperator
80  
-  with GroupMinOperator
81  
-  with GroupMaxOperator
82  
-  with GroupFirstOperator
83  
-  with GroupLastOperator
84  
-  with GroupAddToSetOperator
85  
-  
86  
-
87  
-
88  
-trait LimitOperator extends PipelineOperator {
89  
-  private val operator = "$limit"
90  
-
91  
-  // TODO - Accept Numeric? As long as we can downconvert for mongo type?
92  
-  def $limit(target: Int) = op(operator, target)
93  
-
94  
-  def $limit(target: BigInt) = op(operator, target)
95  
-
96  
-}
97  
-
98  
-trait SkipOperator extends PipelineOperator {
99  
-  private val operator = "$skip"
100  
-
101  
-  // TODO - Accept Numeric? As long as we can downconvert for mongo type?
102  
-  def $skip(target: Int) = op(operator, target)
103  
-    
104  
-  def $skip(target: BigInt) = op(operator, target)
105  
-
106  
-}
107  
-
108  
-trait MatchOperator extends PipelineOperator {
109  
-  private val operator = "$match"
110  
-
111  
-  // TODO - Better type filtering to prevent say, group 
112  
-  def $match(query: DBObject) = op(operator, query)
113  
-}
114  
-
115  
-trait SortOperator extends PipelineOperator {
116  
-  private val operator = "$sort"
117  
-
118  
-  def $sort(fields: (String, Int)*) = {
119  
-     val bldr = MongoDBObject.newBuilder
120  
-     for ((k, v) <- fields) bldr += k -> v
121  
-     op(operator, bldr.result)
122  
-  }
123  
-}
124  
-
125  
-trait UnwindOperator extends PipelineOperator {
126  
-  private val operator = "$unwind"
127  
-
128  
-  def $unwind(target: String) = {
129  
-    require(target.startsWith("$"), "The $unwind operator only accepts a $<fieldName> argument; bare field names " +
130  
-    		"will not function. See http://docs.mongodb.org/manual/reference/aggregation/#_S_unwind")
131  
-    op(operator, target)
132  
-  }
133  
-}
134  
-
135  
-// TODO - Implement me
136  
-trait ProjectOperator extends PipelineOperator {
137  
-  private val operator = "$project"
138  
-}
139  
-
140  
-
141 26
 trait GroupOperator extends PipelineOperator {
142 27
   private val operator = "$group"
143 28
 
@@ -314,3 +199,4 @@ trait GroupSumOperator extends GroupSubOperator {
314 199
   }
315 200
 }
316 201
 
  202
+}
38  casbah-query/src/main/scala/aggregation/Limit.scala
... ...
@@ -0,0 +1,38 @@
  1
+/**
  2
+ * Copyright (c) 2010 - 2013 10gen, Inc. <http://10gen.com>
  3
+ *
  4
+ * Licensed under the Apache License, Version 2.0 (the "License");
  5
+ * you may not use this file except in compliance with the License.
  6
+ * You may obtain a copy of the License at
  7
+ *
  8
+ *   http://www.apache.org/licenses/LICENSE-2.0
  9
+ *
  10
+ * Unless required by applicable law or agreed to in writing, software
  11
+ * distributed under the License is distributed on an "AS IS" BASIS,
  12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13
+ * See the License for the specific language governing permissions and
  14
+ * limitations under the License.
  15
+ *
  16
+ * For questions and comments about this product, please see the project page at:
  17
+ *
  18
+ *     http://github.com/mongodb/casbah
  19
+ *
  20
+ */
  21
+
  22
+package com.mongodb.casbah.query.dsl
  23
+package aggregation
  24
+
  25
+
  26
+import com.mongodb.casbah.query.Imports._
  27
+
  28
+/**
  29
+ * Trait to implement $limit
  30
+ * 
  31
+ * @author brendan
  32
+ */
  33
+trait LimitOperator extends PipelineOperator {
  34
+  private val operator = "$limit"
  35
+
  36
+  // TODO - Accept Numeric? As long as we can downconvert for mongo type?
  37
+  def $limit(target: Int) = op(operator, target)
  38
+}
38  casbah-query/src/main/scala/aggregation/Match.scala
... ...
@@ -0,0 +1,38 @@
  1
+/**
  2
+ * Copyright (c) 2010 - 2013 10gen, Inc. <http://10gen.com>
  3
+ *
  4
+ * Licensed under the Apache License, Version 2.0 (the "License");
  5
+ * you may not use this file except in compliance with the License.
  6
+ * You may obtain a copy of the License at
  7
+ *
  8
+ *   http://www.apache.org/licenses/LICENSE-2.0
  9
+ *
  10
+ * Unless required by applicable law or agreed to in writing, software
  11
+ * distributed under the License is distributed on an "AS IS" BASIS,
  12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13
+ * See the License for the specific language governing permissions and
  14
+ * limitations under the License.
  15
+ *
  16
+ * For questions and comments about this product, please see the project page at:
  17
+ *
  18
+ *     http://github.com/mongodb/casbah
  19
+ *
  20
+ */
  21
+
  22
+package com.mongodb.casbah.query.dsl
  23
+package aggregation
  24
+
  25
+import com.mongodb.casbah.query.Imports._
  26
+
  27
+/**
  28
+ * Base trait to implement $match,
  29
+ * uses mostly the core Query DSL API to operate.
  30
+ * 
  31
+ * @author brendan
  32
+ */
  33
+trait MatchOperator extends PipelineOperator {
  34
+  private val operator = "$match"
  35
+
  36
+  // TODO - Better type filtering to prevent say, group 
  37
+  def $match(query: DBObject) = op(operator, query)
  38
+}
34  casbah-query/src/main/scala/aggregation/Project.scala
... ...
@@ -0,0 +1,34 @@
  1
+/**
  2
+ * Copyright (c) 2010 - 2013 10gen, Inc. <http://10gen.com>
  3
+ *
  4
+ * Licensed under the Apache License, Version 2.0 (the "License");
  5
+ * you may not use this file except in compliance with the License.
  6
+ * You may obtain a copy of the License at
  7
+ *
  8
+ *   http://www.apache.org/licenses/LICENSE-2.0
  9
+ *
  10
+ * Unless required by applicable law or agreed to in writing, software
  11
+ * distributed under the License is distributed on an "AS IS" BASIS,
  12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13
+ * See the License for the specific language governing permissions and
  14
+ * limitations under the License.
  15
+ *
  16
+ * For questions and comments about this product, please see the project page at:
  17
+ *
  18
+ *     http://github.com/mongodb/casbah
  19
+ *
  20
+ */
  21
+
  22
+package com.mongodb.casbah.query.dsl
  23
+package aggregation
  24
+
  25
+import com.mongodb.casbah.query.Imports._
  26
+
  27
+/**
  28
+ * Base trait to implement $project
  29
+ * 
  30
+ * @author brendan
  31
+ */
  32
+trait ProjectOperator extends PipelineOperator {
  33
+  private val operator = "$project"
  34
+}
38  casbah-query/src/main/scala/aggregation/Skip.scala
... ...
@@ -0,0 +1,38 @@
  1
+/**
  2
+ * Copyright (c) 2010 - 2013 10gen, Inc. <http://10gen.com>
  3
+ *
  4
+ * Licensed under the Apache License, Version 2.0 (the "License");
  5
+ * you may not use this file except in compliance with the License.
  6
+ * You may obtain a copy of the License at
  7
+ *
  8
+ *   http://www.apache.org/licenses/LICENSE-2.0
  9
+ *
  10
+ * Unless required by applicable law or agreed to in writing, software
  11
+ * distributed under the License is distributed on an "AS IS" BASIS,
  12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13
+ * See the License for the specific language governing permissions and
  14
+ * limitations under the License.
  15
+ *
  16
+ * For questions and comments about this product, please see the project page at:
  17
+ *
  18
+ *     http://github.com/mongodb/casbah
  19
+ *
  20
+ */
  21
+
  22
+package com.mongodb.casbah.query.dsl
  23
+package aggregation
  24
+
  25
+import com.mongodb.casbah.query.Imports._
  26
+
  27
+/**
  28
+ * Trait to implement $skip
  29
+ * 
  30
+ * @author brendan
  31
+ */
  32
+trait SkipOperator extends PipelineOperator {
  33
+  private val operator = "$skip"
  34
+
  35
+  // TODO - Accept Numeric? As long as we can downconvert for mongo type?
  36
+  def $skip(target: Int) = op(operator, target)
  37
+}
  38
+
41  casbah-query/src/main/scala/aggregation/Sort.scala
... ...
@@ -0,0 +1,41 @@
  1
+/**
  2
+ * Copyright (c) 2010 - 2013 10gen, Inc. <http://10gen.com>
  3
+ *
  4
+ * Licensed under the Apache License, Version 2.0 (the "License");
  5
+ * you may not use this file except in compliance with the License.
  6
+ * You may obtain a copy of the License at
  7
+ *
  8
+ *   http://www.apache.org/licenses/LICENSE-2.0
  9
+ *
  10
+ * Unless required by applicable law or agreed to in writing, software
  11
+ * distributed under the License is distributed on an "AS IS" BASIS,
  12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13
+ * See the License for the specific language governing permissions and
  14
+ * limitations under the License.
  15
+ *
  16
+ * For questions and comments about this product, please see the project page at:
  17
+ *
  18
+ *     http://github.com/mongodb/casbah
  19
+ *
  20
+ */
  21
+
  22
+package com.mongodb.casbah.query.dsl
  23
+package aggregation
  24
+
  25
+import com.mongodb.casbah.query.Imports._
  26
+
  27
+/**
  28
+ * Base trait to implement $sort
  29
+ * 
  30
+ * @author brendan
  31
+ */
  32
+trait SortOperator extends PipelineOperator {
  33
+  private val operator = "$sort"
  34
+
  35
+  def $sort(fields: (String, Int)*) = {
  36
+     val bldr = MongoDBObject.newBuilder
  37
+     for ((k, v) <- fields) bldr += k -> v
  38
+     op(operator, bldr.result)
  39
+  }
  40
+}
  41
+
39  casbah-query/src/main/scala/aggregation/Unwind.scala
... ...
@@ -0,0 +1,39 @@
  1
+/**
  2
+ * Copyright (c) 2010 - 2013 10gen, Inc. <http://10gen.com>
  3
+ *
  4
+ * Licensed under the Apache License, Version 2.0 (the "License");
  5
+ * you may not use this file except in compliance with the License.
  6
+ * You may obtain a copy of the License at
  7
+ *
  8
+ *   http://www.apache.org/licenses/LICENSE-2.0
  9
+ *
  10
+ * Unless required by applicable law or agreed to in writing, software
  11
+ * distributed under the License is distributed on an "AS IS" BASIS,
  12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13
+ * See the License for the specific language governing permissions and
  14
+ * limitations under the License.
  15
+ *
  16
+ * For questions and comments about this product, please see the project page at:
  17
+ *
  18
+ *     http://github.com/mongodb/casbah
  19
+ *
  20
+ */
  21
+package com.mongodb.casbah.query.dsl
  22
+package aggregation
  23
+
  24
+import com.mongodb.casbah.query.Imports._
  25
+
  26
+
  27
+
  28
+/**
  29
+ * Base trait to implement $unwind
  30
+ */
  31
+trait UnwindOperator extends PipelineOperator {
  32
+  private val operator = "$unwind"
  33
+
  34
+  def $unwind(target: String) = {
  35
+    require(target.startsWith("$"), "The $unwind operator only accepts a $<fieldName> argument; bare field names " +
  36
+    		"will not function. See http://docs.mongodb.org/manual/reference/aggregation/#_S_unwind")
  37
+    op(operator, target)
  38
+  }
  39
+}
92  casbah-query/src/main/scala/aggregation/package.scala
... ...
@@ -0,0 +1,92 @@
  1
+/**
  2
+ * Copyright (c) 2010 - 2013 10gen, Inc. <http://10gen.com>
  3
+ *
  4
+ * Licensed under the Apache License, Version 2.0 (the "License");
  5
+ * you may not use this file except in compliance with the License.
  6
+ * You may obtain a copy of the License at
  7
+ *
  8
+ *   http://www.apache.org/licenses/LICENSE-2.0
  9
+ *
  10
+ * Unless required by applicable law or agreed to in writing, software
  11
+ * distributed under the License is distributed on an "AS IS" BASIS,
  12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13
+ * See the License for the specific language governing permissions and
  14
+ * limitations under the License.
  15
+ *
  16
+ * For questions and comments about this product, please see the project page at:
  17
+ *
  18
+ *     http://github.com/mongodb/casbah
  19
+ *
  20
+ */
  21
+
  22
+package com.mongodb.casbah.query.dsl
  23
+
  24
+import com.mongodb.casbah.commons.Logging
  25
+
  26
+import scalaj.collection.Imports._
  27
+
  28
+import com.mongodb.casbah.query.Imports._
  29
+
  30
+import scala.util.matching._
  31
+import scala.collection.Iterable
  32
+import scala.collection.mutable.{ Seq => MutableSeq }
  33
+
  34
+import org.bson._
  35
+import org.bson.types.BasicBSONList
  36
+
  37
+/**
  38
+ * Base traits and configuration for aggregation framework.
  39
+ *
  40
+ * @author brendan
  41
+ *
  42
+ */
  43
+package object aggregation {
  44
+
  45
+  /**
  46
+   * Base trait for a Pipeline Operator for
  47
+   * the Aggregation Framework.
  48
+   * These operators are the "core" of Aggregation,
  49
+   * representing the primary pipeline.
  50
+   */
  51
+  trait PipelineOperator {
  52
+    def list: MongoDBList
  53
+
  54
+    protected def op(oper: String, target: Any) =
  55
+      PipelineOperator(oper, target)(list)
  56
+
  57
+    override def toString = list.toString
  58
+
  59
+  }
  60
+
  61
+  object PipelineOperator {
  62
+
  63
+    // TODO - this should be a LIST, not a DBObject.
  64
+    def apply[A <: String, B <: Any](kv: (A, B))(pipeline: MongoDBList): DBObject with PipelineOperations with PipelineOperator = {
  65
+      val obj = new BasicDBObject with PipelineOperations with PipelineOperator { val list = pipeline }
  66
+      obj.put(kv._1, kv._2)
  67
+      pipeline += obj
  68
+      obj
  69
+    }
  70
+  }
  71
+
  72
+  // TODO - Validations of things like "ran group after sort" for certain opers
  73
+  trait PipelineOperations extends GroupOperator
  74
+    with LimitOperator
  75
+    with SkipOperator
  76
+    with MatchOperator
  77
+    with ProjectOperator
  78
+    with SortOperator
  79
+    with UnwindOperator
  80
+
  81
+  trait BasePipelineOperations extends PipelineOperations { val list = MongoDBList.empty }
  82
+
  83
+  trait GroupSubOperators extends GroupSumOperator
  84
+    with GroupPushOperator
  85
+    with GroupAvgOperator
  86
+    with GroupMinOperator
  87
+    with GroupMaxOperator
  88
+    with GroupFirstOperator
  89
+    with GroupLastOperator
  90
+    with GroupAddToSetOperator
  91
+
  92
+}

0 notes on commit ed15fc2

Please sign in to comment.
Something went wrong with that request. Please try again.