Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Use DBObject as type for jsScope in MapReduceCommand #44

Closed
wants to merge 1 commit into from

4 participants

@troger

Hi,

I don't know if it's ok for backward compatibility but I find it more convenient to use DBObject for jsScope type as it should be an Object with each field added to the global scope.

Moreover when using String, it just does not work when setting the jsScope with:

...
jsScope = Some("{ testVar: true }")
...

When looking at the mongo logs, it produces the following:

scope: "{ testVar: true }"

instead of

scope: { testVar: true }

I've added a test to the MapReduceSpec.

Maybe it's better to try fixing the jsScope as String instead of using DBObject.

WDYT?

@troger

Thinking about it and looking at the Java driver, it may be better to use Map[String, Any]:

jsScope: Option[Map[String, Any]]
@bwmcadams

For backwards compatibility, this needs to be a Type Class to take both Strings and DBObjects.

@mumoshu

Hi,

Thank you for the great Scala driver.
I'm using this for my production service :)

For backwards compatibility, this needs to be a Type Class to take both Strings and DBObjects.

Excuse me, but do you really want backward compatibility here?
Can't we just make it use DBObject as troger originally suggested?

First of all, the current implementation jsScope: Option[String] is not working at all: Basically, it must be an Option[DBObject] or an Option[Map[_, _]] to properly serialized to a JavaScript object, as the original mapReduce comand's scope parameter expects.
In other words, all the users passing Option[String]s would be encountering the bug that the scope variables are totally missing from scope - as troger reported.
Now, shouldn't the users be notified with compile errors for passing jsScope an Option[String]?

@mumoshu

Also, how do you implement the type class serializes a String like """{ testVar: true }""" to a DBObject or a Map?
Parsing JSON manually?

@rozza rozza referenced this pull request from a commit in rozza/casbah
@rozza rozza Fixed support for jsScope (SCALA-43) (#44) d955eb1
@rozza rozza referenced this pull request from a commit in rozza/casbah
@rozza rozza Added troger to authors #44 a77fe59
@rozza
Collaborator

This has been fixed in 2.6.1 - apologies for the delay

@rozza rozza closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 3, 2012
  1. @troger

    Use DBObject as type for jsScope in MapReduceCommand as the String ty…

    troger authored
    …pe does not work; add test
This page is out of date. Refresh to see the latest.
View
2  casbah-core/src/main/scala/MongoCollection.scala
@@ -506,7 +506,7 @@ abstract class MongoCollection extends Logging with Iterable[DBObject] {
sort: Option[DBObject] = None,
limit: Option[Int] = None,
finalizeFunction: Option[JSFunction] = None,
- jsScope: Option[String] = None,
+ jsScope: Option[DBObject] = None,
verbose: Boolean = false): map_reduce.MapReduceResult =
map_reduce.MapReduceResult(getDB.command(MapReduceCommand(name, mapFunction, reduceFunction,
output, query, sort, limit, finalizeFunction,
View
4 casbah-core/src/main/scala/map_reduce/MapReduceCommand.scala
@@ -49,7 +49,7 @@ object MapReduceCommand {
sort: Option[DBObject] = None,
limit: Option[Int] = None,
finalizeFunction: Option[JSFunction] = None,
- jsScope: Option[String] = None,
+ jsScope: Option[DBObject] = None,
verbose: Boolean = false) = {
val mrc = new MapReduceCommand()
mrc.input = input
@@ -89,7 +89,7 @@ class MapReduceCommand protected[mongodb] () {
var sort: Option[DBObject] = None
var limit: Option[Int] = None
var finalizeFunction: Option[JSFunction] = None
- var jsScope: Option[String] = None
+ var jsScope: Option[DBObject] = None
def toDBObject = {
val dataObj = MongoDBObject.newBuilder
View
30 casbah-core/src/test/scala/MapReduceSpec.scala
@@ -120,6 +120,36 @@ class MapReduceSpec extends CasbahSpecification {
item must beEqualTo(MongoDBObject("_id" -> 90.0, "value" -> 8.552400000000002))
}
+ "Produce results with variable from jsScope" in {
+ val f = """
+ function f( year, value ){
+ value.scopeVar = scopeVar;
+ return scopeVar;
+ }
+ """
+
+ val coll = mongoDB("yield_historical.in")
+ val result = coll.mapReduce(
+ mapJS,
+ reduceJS,
+ MapReduceInlineOutput,
+ finalizeFunction = Some(f),
+ jsScope = Some(MongoDBObject("scopeVar" -> "testScopeVar")),
+ verbose = true)
+
+ /*log.warn("M/R Result: %s", result)*/
+
+
+ result.isError must beFalse
+ result.raw.getAs[String]("result") must beNone
+ result.size must beGreaterThan(0)
+ result.size must beEqualTo(result.raw.expand[Int]("counts.output").getOrElse(-1))
+
+ val item = result.next
+ item must beDBObject
+ item must beEqualTo(MongoDBObject("_id" -> 90.0, "value" -> "testScopeVar"))
+ }
+
"Produce results for merged output" in {
verifyAndInitTreasuryData
Something went wrong with that request. Please try again.