Skip to content

Commit

Permalink
Refactore MapReduce. MapReduce is renamed to MapReduceCommand which i…
Browse files Browse the repository at this point in the history
…s now not more then a data holder. MapReduceBuilder is renamed to MapReduce and gets the functions of the old MapReduce.
  • Loading branch information
lanwin committed May 11, 2010
1 parent 262f39c commit 117fd65
Show file tree
Hide file tree
Showing 13 changed files with 523 additions and 570 deletions.
175 changes: 122 additions & 53 deletions source/MongoDB.Tests/IntegrationTests/TestMapReduce.cs
@@ -1,57 +1,126 @@
using System;
using NUnit.Framework;

namespace MongoDB.IntegrationTests
{
[TestFixture()]
public class TestMapReduce : MongoTestBase
namespace MongoDB.IntegrationTests
{
[TestFixture]
public class TestMapReduce : MongoTestBase
{
IMongoCollection mrcol;
string mapfunction = "function(){\n" +
" this.tags.forEach(\n" +
" function(z){\n" +
" emit( z , { count : 1 } );\n" +
" });\n" +
"};";
string reducefunction = "function( key , values ){\n" +
" var total = 0;\n" +
" for ( var i=0; i<values.length; i++ )\n" +
" total += values[i].count;\n" +
" return { count : total };\n" +
"};";

public override string TestCollections {
get {
return "mr";
}
}

public override void OnInit (){
mrcol = DB["mr"];
mrcol.Insert(new Document().Add("_id", 1).Add("tags", new String[] { "dog", "cat" }));
mrcol.Insert(new Document().Add("_id", 2).Add("tags", new String[] { "dog" }));
mrcol.Insert(new Document().Add("_id", 3).Add("tags", new String[] { "mouse", "cat", "dog" }));
mrcol.Insert(new Document().Add("_id", 4).Add("tags", new String[] { }));

}


[Test()]
public void TestGetMapReduceObject(){
MapReduce mr = mrcol.MapReduce();
Assert.IsNotNull(mr);
Assert.AreEqual(mrcol.Name, mr.Name);
}

[Test()]
public void TestExecuteSimple(){
MapReduce mr = mrcol.MapReduce();
mr.Map = new Code(mapfunction);
mr.Reduce = new Code(reducefunction);
mr.Execute();
Assert.IsNotNull(mr.Result);
}

}

}
private IMongoCollection _collection;

private const string MapFunc = "function(){\n" +
" this.tags.forEach(\n" +
" function(z){\n" +
" emit( z , { count : 1 } );\n" +
" });\n" +
"};";

private const string ReduceFunc = "function( key , values ){\n" +
" var total = 0;\n" +
" for ( var i=0; i<values.length; i++ )\n" +
" total += values[i].count;\n" +
" return { count : total };\n" +
"};";

public override string TestCollections
{
get { return "mr"; }
}

public override void OnInit()
{
_collection = DB["mr"];
_collection.Insert(new Document().Add("_id", 1).Add("tags", new[] {"dog", "cat"}));
_collection.Insert(new Document().Add("_id", 2).Add("tags", new[] {"dog"}));
_collection.Insert(new Document().Add("_id", 3).Add("tags", new[] {"mouse", "cat", "dog"}));
_collection.Insert(new Document().Add("_id", 4).Add("tags", new String[] {}));
}

[Test]
public void TestBuilderSetsAllProperties()
{
var query = new Document().Add("x", 1);
var scope = new Document().Add("y", 2);
var sort = new Document().Add("z", 3);
var mrb = _collection.MapReduce();
mrb.Map(MapFunc)
.Reduce(ReduceFunc)
.KeepTemp(true)
.Limit(5)
.Out("outtest")
.Query(query)
.Scope(scope)
.Sort(sort)
.Verbose(false);

var mr = mrb.Command;
Assert.AreEqual(query.ToString(), mr.Query.ToString());
Assert.AreEqual(scope.ToString(), mr.Scope.ToString());
Assert.AreEqual(sort.ToString(), mr.Sort.ToString());
Assert.AreEqual(true, mr.KeepTemp);
Assert.AreEqual(5, mr.Limit);
Assert.AreEqual("outtest", mr.Out);
Assert.AreEqual(false, mr.Verbose);
}

[Test]
public void TestCreateMapReduceWithStringFunctions()
{
var mr = _collection.MapReduce();
mr.Map(MapFunc).Reduce(ReduceFunc);

Assert.IsNotNull(mr.Command.Map);
Assert.IsNotNull(mr.Command.Reduce);
}

[Test]
public void TestExecuteSimple()
{
var mrb = _collection.MapReduce();
var mr = mrb.Map(MapFunc).Reduce(ReduceFunc);

mr.RetrieveData();

Assert.IsNotNull(mr.Result);
Assert.IsTrue(mr.Result.Ok);
Assert.AreEqual(4, mr.Result.InputCount);
Assert.AreEqual(6, mr.Result.EmitCount);
Assert.AreEqual(3, mr.Result.OutputCount);
}

[Test]
public void TestExecuteSimple2()
{
var mr = _collection.MapReduce();
mr.Command.Map = new Code(MapFunc);
mr.Command.Reduce = new Code(ReduceFunc);

mr.RetrieveData();

Assert.IsNotNull(mr.Result);
}

[Test]
public void TestExecuteUsing()
{
String tempcollname;
using(var mrb = _collection.MapReduce().Map(MapFunc).Reduce(ReduceFunc))
{
mrb.RetrieveData();
Assert.IsNotNull(mrb.Result);
Assert.IsTrue(mrb.Result.Ok);
tempcollname = DB.Name + "." + mrb.Result.CollectionName;
Assert.IsTrue(DB.GetCollectionNames().Contains(tempcollname));
}
Assert.IsFalse(DB.GetCollectionNames().Contains(tempcollname));
}

[Test]
public void TestGetMapReduceObject()
{
var mr = _collection.MapReduce();
Assert.IsNotNull(mr);
Assert.AreEqual(_collection.Name, mr.Command.Name);
}
}
}
96 changes: 0 additions & 96 deletions source/MongoDB.Tests/IntegrationTests/TestMapReduceBuilder.cs

This file was deleted.

6 changes: 4 additions & 2 deletions source/MongoDB/Exceptions/MongoMapReduceException.cs
@@ -1,3 +1,5 @@
using MongoDB.Results;

namespace MongoDB
{
/// <summary>
Expand All @@ -9,7 +11,7 @@ public class MongoMapReduceException : MongoCommandException
/// Gets or sets the map reduce result.
/// </summary>
/// <value>The map reduce result.</value>
public MapReduce.MapReduceResult MapReduceResult { get; private set; }
public MapReduceResult MapReduceResult { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="MongoMapReduceException"/> class.
Expand All @@ -18,7 +20,7 @@ public class MongoMapReduceException : MongoCommandException
/// <param name="mapReduce">The map reduce.</param>
public MongoMapReduceException(MongoCommandException exception, MapReduce mapReduce)
:base(exception.Message,exception.Error, exception.Command) {
MapReduceResult = new MapReduce.MapReduceResult(exception.Error);
MapReduceResult = new MapReduceResult(exception.Error);
}
}
}
6 changes: 0 additions & 6 deletions source/MongoDB/IMongoCollection_1.cs
Expand Up @@ -148,12 +148,6 @@ public interface IMongoCollection<T>
/// <returns></returns>
MapReduce MapReduce();

/// <summary>
/// Provides a fluent interface into building a map reduce command against the database.
/// </summary>
/// <returns></returns>
MapReduceBuilder MapReduceBuilder();

///<summary>
/// Count all items in the collection.
///</summary>
Expand Down
13 changes: 7 additions & 6 deletions source/MongoDB/Linq/MongoQueryProvider.cs
Expand Up @@ -257,15 +257,16 @@ private object ExecuteMapReduce(MongoQueryObject queryObject)
var collection = miGetCollection.Invoke(queryObject.Database, new[] { queryObject.CollectionName });

var mapReduce = (MapReduce)collection.GetType().GetMethod("MapReduce").Invoke(collection, null);
mapReduce.Map = new Code(queryObject.MapFunction);
mapReduce.Reduce = new Code(queryObject.ReduceFunction);
mapReduce.Finalize = new Code(queryObject.FinalizerFunction);
mapReduce.Query = queryObject.Query;
mapReduce.Map(new Code(queryObject.MapFunction));
mapReduce.Reduce(new Code(queryObject.ReduceFunction));
mapReduce.Finalize(new Code(queryObject.FinalizerFunction));
mapReduce.Query(queryObject.Query);

if(queryObject.Sort != null)
mapReduce.Sort = queryObject.Sort;
mapReduce.Sort(queryObject.Sort);

mapReduce.Limit(queryObject.NumberToLimit);

mapReduce.Limit = queryObject.NumberToLimit;
if (queryObject.NumberToSkip != 0)
throw new InvalidQueryException("MapReduce queries do no support Skips.");

Expand Down

0 comments on commit 117fd65

Please sign in to comment.