diff options
author | Eliot Horowitz <eliot@10gen.com> | 2009-12-03 11:50:28 -0500 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2009-12-03 11:50:28 -0500 |
commit | ec49006e35e929b83459f4668463d4bb3f6e2189 (patch) | |
tree | a5e694f80c9850fe209d18bec02043d43fcf96a4 | |
parent | c67c2f7dd681152f1784c8e1c2119b979e65881d (diff) | |
download | mongo-ec49006e35e929b83459f4668463d4bb3f6e2189.tar.gz |
allow adding variable to global scope for M/R SERVER-449
-rw-r--r-- | db/mr.cpp | 12 | ||||
-rw-r--r-- | jstests/mr4.js | 45 |
2 files changed, 54 insertions, 3 deletions
diff --git a/db/mr.cpp b/db/mr.cpp index 754a2342e89..58d28164d12 100644 --- a/db/mr.cpp +++ b/db/mr.cpp @@ -128,9 +128,11 @@ namespace mongo { if ( cmdObj["mapparams"].type() == Array ){ mapparams = cmdObj["mapparams"].embeddedObjectUserCheck(); } - else { - mapparams = BSONObj(); + + if ( cmdObj["scope"].type() == Object ){ + scopeSetup = cmdObj["scope"].embeddedObjectUserCheck(); } + } { // query options @@ -183,7 +185,8 @@ namespace mongo { string finalizeCode; BSONObj mapparams; - + BSONObj scopeSetup; + // output tables string incLong; @@ -208,6 +211,9 @@ namespace mongo { else finalize = 0; + if ( ! setup.scopeSetup.isEmpty() ) + scope->init( &setup.scopeSetup ); + db.dropCollection( setup.tempLong ); db.dropCollection( setup.incLong ); diff --git a/jstests/mr4.js b/jstests/mr4.js new file mode 100644 index 00000000000..c0d19cfdd42 --- /dev/null +++ b/jstests/mr4.js @@ -0,0 +1,45 @@ + +t = db.mr4; +t.drop(); + +t.save( { x : 1 , tags : [ "a" , "b" ] } ); +t.save( { x : 2 , tags : [ "b" , "c" ] } ); +t.save( { x : 3 , tags : [ "c" , "a" ] } ); +t.save( { x : 4 , tags : [ "b" , "c" ] } ); + +m = function(){ + this.tags.forEach( + function(z){ + emit( z , { count : xx } ); + } + ); +}; + +r = function( key , values ){ + var total = 0; + for ( var i=0; i<values.length; i++ ){ + total += values[i].count; + } + return { count : total }; +}; + +res = t.mapReduce( m , r , { scope : { xx : 1 } } ); +z = res.convertToSingleObject() + +assert.eq( 3 , z.keySet().length , "A1" ); +assert.eq( 2 , z.a.count , "A2" ); +assert.eq( 3 , z.b.count , "A3" ); +assert.eq( 3 , z.c.count , "A4" ); + +res.drop(); + + +res = t.mapReduce( m , r , { scope : { xx : 2 } } ); +z = res.convertToSingleObject() + +assert.eq( 3 , z.keySet().length , "A1" ); +assert.eq( 4 , z.a.count , "A2" ); +assert.eq( 6 , z.b.count , "A3" ); +assert.eq( 6 , z.c.count , "A4" ); + +res.drop(); |