Skip to content

Commit

Permalink
allow adding variable to global scope for M/R SERVER-449
Browse files Browse the repository at this point in the history
  • Loading branch information
erh committed Dec 3, 2009
1 parent c67c2f7 commit ec49006
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
12 changes: 9 additions & 3 deletions db/mr.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -183,7 +185,8 @@ namespace mongo {
string finalizeCode;

BSONObj mapparams;

BSONObj scopeSetup;

// output tables
string incLong;

Expand All @@ -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 );

Expand Down
45 changes: 45 additions & 0 deletions 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();

0 comments on commit ec49006

Please sign in to comment.