diff options
author | Ben Becker <ben.becker@10gen.com> | 2013-04-25 12:43:15 -0700 |
---|---|---|
committer | Dan Pasette <dan@10gen.com> | 2013-05-13 01:20:24 -0400 |
commit | 928e0a6aca7d1f364ac22df3cae5432b56293c1f (patch) | |
tree | 668546410784e58453907fa73dd5d93988a47478 | |
parent | b5c4e7ac53d57767ce5e88c18d1b76d89fb67808 (diff) | |
download | mongo-928e0a6aca7d1f364ac22df3cae5432b56293c1f.tar.gz |
SERVER-9448: make map and reduce argument and receiver objects mutable
-rw-r--r-- | jstests/mr_mutable_properties.js | 62 | ||||
-rw-r--r-- | src/mongo/db/commands/mr.cpp | 6 |
2 files changed, 65 insertions, 3 deletions
diff --git a/jstests/mr_mutable_properties.js b/jstests/mr_mutable_properties.js new file mode 100644 index 00000000000..7c4442aab9e --- /dev/null +++ b/jstests/mr_mutable_properties.js @@ -0,0 +1,62 @@ +// See SERVER-9448 +// Test argument and receiver (aka 'this') objects and their children can be mutated +// in Map, Reduce and Finalize functions + +var collection = db.mrMutableReceiver; +collection.drop(); +collection.insert({a:1}); + +var map = function() { + // set property on receiver + this.feed = {beef:1}; + + // modify property on receiever + this.a = {cake:1}; + emit(this._id, this.feed); + emit(this._id, this.a); +} + +var reduce = function(key, values) { + // set property on receiver + this.feed = {beat:1}; + + // set property on key arg + key.fed = {mochi:1}; + + // push properties onto values array arg + values.push(this.feed); + values.push(key.fed); + + // modify each value in the (modified) array arg + values.forEach(function(val) { val.mod = 1; }); + return {food:values}; +} + +var finalize = function(key, values) { + // set property on receiver + this.feed = {ice:1}; + + // set property on key arg + key.fed = {cream:1}; + + // push properties onto values array arg + printjson(values); + values.food.push(this.feed); + values.food.push(key.fed); + + // modify each value in the (modified) array arg + values.food.forEach(function(val) { val.mod = 1; }); + return values; +} + +var mr = collection.mapReduce(map, reduce, {finalize: finalize, out: {inline: 1}}); +printjson(mr); + +// verify mutated properties exist (order dictated by emit sequence and properties added) +assert.eq(mr.results[0].value.food[0].beef, 1); +assert.eq(mr.results[0].value.food[1].cake, 1); +assert.eq(mr.results[0].value.food[2].beat, 1); +assert.eq(mr.results[0].value.food[3].mochi, 1); +assert.eq(mr.results[0].value.food[4].ice, 1); +assert.eq(mr.results[0].value.food[5].cream, 1); +mr.results[0].value.food.forEach(function(val) { assert.eq(val.mod, 1); }); diff --git a/src/mongo/db/commands/mr.cpp b/src/mongo/db/commands/mr.cpp index 742392f04ee..8224783f8b6 100644 --- a/src/mongo/db/commands/mr.cpp +++ b/src/mongo/db/commands/mr.cpp @@ -73,8 +73,8 @@ namespace mongo { void JSMapper::map( const BSONObj& o ) { Scope * s = _func.scope(); verify( s ); - if ( s->invoke( _func.func() , &_params, &o , 0 , true, false, true ) ) - throw UserException( 9014, str::stream() << "map invoke failed: " + s->getError() ); + if (s->invoke(_func.func(), &_params, &o, 0, true)) + uasserted(9014, str::stream() << "map invoke failed: " << s->getError()); } /** @@ -193,7 +193,7 @@ namespace mongo { Scope * s = _func.scope(); - s->invokeSafe( _func.func() , &args, 0, 0, false, false, true ); + s->invokeSafe(_func.func(), &args, 0); ++numReduces; if ( s->type( "__returnValue" ) == Array ) { |