/casbah/casbah-core/src/main/scala/map_reduce/MapReduceCommand.scala

http://github.com/ymasory/scala-corpus · Scala · 154 lines · 92 code · 21 blank · 41 comment · 0 complexity · 4e5e441804699cc2b12ff0376e5f85aa MD5 · raw file

  1. /**
  2. * Copyright (c) 2010 10gen, Inc. <http://10gen.com>
  3. * Copyright (c) 2009, 2010 Novus Partners, Inc. <http://novus.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. * For questions and comments about this product, please see the project page at:
  18. *
  19. * http://github.com/mongodb/casbah
  20. *
  21. */
  22. package com.mongodb.casbah
  23. package map_reduce
  24. import com.mongodb.casbah.Imports._
  25. import com.mongodb.casbah.commons.Logging
  26. import scalaj.collection.Imports._
  27. class MapReduceError(msg: String) extends Error("MongoDB Map/Reduce Error: " + msg)
  28. /**
  29. * Wrapper Object to provide apply methods for the MapReduceCommand class.
  30. *
  31. * @see <a href="http://www.mongodb.org/display/DOCS/MapReduce">The MongoDB Map/Reduce Documentation</a>
  32. *
  33. * @author Brendan W. McAdams <brendan@10gen.com>
  34. */
  35. object MapReduceCommand {
  36. def apply(collection: String,
  37. mapFunction: JSFunction,
  38. reduceFunction: JSFunction,
  39. outputCollection: Option[String] = None,
  40. query: Option[DBObject] = None,
  41. sort: Option[DBObject] = None,
  42. finalizeFunction: Option[JSFunction] = None,
  43. jsScope: Option[String] = None) = {
  44. val mrc = new MapReduceCommand()
  45. mrc.collection = collection
  46. mrc.mapFunction = mapFunction
  47. mrc.reduceFunction = reduceFunction
  48. mrc.outputCollection = outputCollection
  49. mrc.query = query
  50. mrc.sort = sort
  51. mrc.finalizeFunction = finalizeFunction
  52. mrc.jsScope = jsScope
  53. mrc
  54. }
  55. def apply(collection: String,
  56. mapFunction: JSFunction,
  57. reduceFunction: JSFunction,
  58. outputCollection: String) = {
  59. val mrc = new MapReduceCommand()
  60. mrc.collection = collection
  61. mrc.mapFunction = mapFunction
  62. mrc.reduceFunction = reduceFunction
  63. mrc.outputCollection = Some(outputCollection)
  64. mrc
  65. }
  66. }
  67. /**
  68. * Wrapper class for invoking MongoDB mapReduces.
  69. *
  70. * The Java driver doesn't provide support for many of the possible options in the latest
  71. * versions of MongoDB, so this wrapper class is used in it's place, and passed directly to
  72. * a db.runCommand call.
  73. *
  74. * @todo Integrate support for Lift's JavaScript DSL
  75. *
  76. * @see http://www.mongodb.org/display/DOCS/MapReduce
  77. *
  78. * @author Brendan W. McAdams <brendan@10gen.com>
  79. */
  80. class MapReduceCommand {
  81. var collection: String = ""
  82. var mapFunction: JSFunction = ""
  83. var reduceFunction: JSFunction = ""
  84. var keepTemp = false // moot if outputCollection is specific
  85. var verbose = true // provides statistics on job execution
  86. var outputCollection: Option[String] = None
  87. var query: Option[DBObject] = None
  88. var sort: Option[DBObject] = None
  89. var finalizeFunction: Option[JSFunction] = None
  90. var jsScope: Option[String] = None
  91. def asDBObject = toDBObj
  92. def toDBObj = {
  93. val dataObj = MongoDBObject.newBuilder
  94. collection match {
  95. case "" => throw new MapReduceError("collection must be defined.")
  96. case null => throw new MapReduceError("collection must be defined.")
  97. case other => dataObj += "mapreduce" -> collection
  98. }
  99. mapFunction match {
  100. case "" => throw new MapReduceError("mapFunction must be defined.")
  101. case null => throw new MapReduceError("mapFunction must be defined.")
  102. case other => dataObj += "map" -> mapFunction.toString
  103. }
  104. reduceFunction match {
  105. case "" => throw new MapReduceError("reduceFunction must be defined.")
  106. case null => throw new MapReduceError("reduceFunction must be defined.")
  107. case other => dataObj += "reduce" -> reduceFunction.toString
  108. }
  109. dataObj += "keepTemp" -> keepTemp
  110. dataObj += "verbose" -> verbose
  111. outputCollection match {
  112. case Some(out) => dataObj += "out" -> out
  113. case None => {}
  114. }
  115. query match {
  116. case Some(q) => dataObj += "query" -> q
  117. case None => {}
  118. }
  119. sort match {
  120. case Some(s) => dataObj += "sort" -> s
  121. case None => {}
  122. }
  123. finalizeFunction match {
  124. case Some(fF) => dataObj += "finalize" -> fF.toString
  125. case None => {}
  126. }
  127. jsScope match {
  128. case Some(s) => dataObj += "scope" -> s
  129. case None => {}
  130. }
  131. dataObj.result
  132. }
  133. }