Skip to content

Commit

Permalink
Move MapReduce to use generics. Currently some linq tests are failing.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanwin committed May 11, 2010
1 parent af1489d commit 41d4628
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 69 deletions.
1 change: 0 additions & 1 deletion source/MongoDB.Tests/MongoDB.Tests.csproj
Expand Up @@ -146,7 +146,6 @@
<Compile Include="UnitTests\Bson\TestBsonWriter.cs" />
<Compile Include="IntegrationTests\TestDatabaseJavascript.cs" />
<Compile Include="IntegrationTests\TestMapReduce.cs" />
<Compile Include="IntegrationTests\TestMapReduceBuilder.cs" />
<Compile Include="IntegrationTests\TestConcurrency.cs" />
<Compile Include="IntegrationTests\TestCollectionSafeMode.cs" />
<Compile Include="MongoTestBase.cs" />
Expand Down
7 changes: 3 additions & 4 deletions source/MongoDB/Exceptions/MongoMapReduceException.cs
Expand Up @@ -5,7 +5,7 @@ namespace MongoDB
/// <summary>
/// Raised when a map reduce call fails.
/// </summary>
public class MongoMapReduceException : MongoCommandException
public class MongoMapReduceException<T> : MongoCommandException
{
/// <summary>
/// Gets or sets the map reduce result.
Expand All @@ -14,11 +14,10 @@ public class MongoMapReduceException : MongoCommandException
public MapReduceResult MapReduceResult { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="MongoMapReduceException"/> class.
/// Initializes a new instance of the <see cref="MongoMapReduceException&lt;T&gt;"/> class.
/// </summary>
/// <param name="exception">The exception.</param>
/// <param name="mapReduce">The map reduce.</param>
public MongoMapReduceException(MongoCommandException exception, MapReduce mapReduce)
public MongoMapReduceException(MongoCommandException exception)
:base(exception.Message,exception.Error, exception.Command) {
MapReduceResult = new MapReduceResult(exception.Error);
}
Expand Down
2 changes: 1 addition & 1 deletion source/MongoDB/IMongoCollection_1.cs
Expand Up @@ -146,7 +146,7 @@ public interface IMongoCollection<T>
/// Entrypoint into executing a map/reduce query against the collection.
/// </summary>
/// <returns></returns>
MapReduce MapReduce();
MapReduce<T> MapReduce();

///<summary>
/// Count all items in the collection.
Expand Down
24 changes: 15 additions & 9 deletions source/MongoDB/Linq/MongoQueryProvider.cs
Expand Up @@ -4,7 +4,7 @@
using System.Linq.Expressions;
using System.Reflection;
using System.Text;

using MongoDB.Commands;
using MongoDB.Connections;
using MongoDB.Linq.Expressions;
using MongoDB.Linq.Translators;
Expand Down Expand Up @@ -255,23 +255,29 @@ private object ExecuteMapReduce(MongoQueryObject queryObject)
{
var miGetCollection = typeof(IMongoDatabase).GetMethods().Where(m => m.Name == "GetCollection" && m.GetGenericArguments().Length == 1 && m.GetParameters().Length == 1).Single().MakeGenericMethod(queryObject.DocumentType);
var collection = miGetCollection.Invoke(queryObject.Database, new[] { queryObject.CollectionName });

var mapReduce = collection.GetType().GetMethod("MapReduce").Invoke(collection, null);

var mapReduceCommand = (MapReduceCommand)mapReduce.GetType().GetProperty("Command").GetValue(mapReduce, null);

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);
mapReduceCommand.Map = new Code(queryObject.MapFunction);
mapReduceCommand.Reduce = new Code(queryObject.ReduceFunction);
mapReduceCommand.Finalize = new Code(queryObject.FinalizerFunction);
mapReduceCommand.Query = queryObject.Query;

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

mapReduce.Limit(queryObject.NumberToLimit);
mapReduceCommand.Limit = queryObject.NumberToLimit;

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

//mapReduce.GetType().GetProperty()

var executor = GetExecutor(typeof(Document), queryObject.Projector, queryObject.Aggregator, true);
return executor.Compile().DynamicInvoke(mapReduce.Documents);
return null;
//executor.Compile().DynamicInvoke(mapReduce.Documents);
}

private static LambdaExpression GetExecutor(Type documentType, LambdaExpression projector, LambdaExpression aggregator, bool boxReturn)
Expand Down
39 changes: 20 additions & 19 deletions source/MongoDB/MapReduce.cs
Expand Up @@ -8,14 +8,15 @@ namespace MongoDB
/// <summary>
/// Provides a Fluent interface to build and execute Map/Reduce calls.
/// </summary>
public class MapReduce : IDisposable
public class MapReduce<T> : IDisposable
where T : class
{
private readonly IMongoDatabase _database;
private bool _canModify = true;
private bool _disposing;

/// <summary>
/// Initializes a new instance of the <see cref = "MapReduce" /> class.
/// Initializes a new instance of the <see cref = "MapReduce&lt;T&gt;" /> class.
/// </summary>
/// <param name = "database">The database.</param>
/// <param name = "name">The name.</param>
Expand Down Expand Up @@ -46,7 +47,7 @@ public MapReduce(IMongoDatabase database, string name)
/// Gets the documents.
/// </summary>
/// <value>The documents.</value>
public IEnumerable<Document> Documents
public IEnumerable<T> Documents
{
get
{
Expand All @@ -55,7 +56,7 @@ public IEnumerable<Document> Documents
if(Result == null || Result.Ok == false)
throw new InvalidOperationException("Documents cannot be iterated when an error was returned from execute.");

var docs = _database[Result.CollectionName].FindAll().Documents;
var docs = _database.GetCollection<T>().FindAll().Documents;
using((IDisposable)docs)
{
foreach(var doc in docs)
Expand Down Expand Up @@ -86,7 +87,7 @@ public void Dispose()
/// A map function must call emit(key,value) at least once, but may be invoked any number of times,
/// as may be appropriate.
/// </summary>
public MapReduce Map(string function)
public MapReduce<T> Map(string function)
{
return Map(new Code(function));
}
Expand All @@ -96,7 +97,7 @@ public MapReduce Map(string function)
/// A map function must call emit(key,value) at least once, but may be invoked any number of times,
/// as may be appropriate.
/// </summary>
public MapReduce Map(Code function)
public MapReduce<T> Map(Code function)
{
TryModify();
Command.Map = function;
Expand All @@ -111,7 +112,7 @@ public MapReduce Map(Code function)
/// The MapReduce engine may invoke reduce functions iteratively; thus, these functions
/// must be idempotent. If you need to perform an operation only once, use a finalize function.
/// </remarks>
public MapReduce Reduce(string function)
public MapReduce<T> Reduce(string function)
{
return Reduce(new Code(function));
}
Expand All @@ -124,7 +125,7 @@ public MapReduce Reduce(string function)
/// The MapReduce engine may invoke reduce functions iteratively; thus, these functions
/// must be idempotent. If you need to perform an operation only once, use a finalize function.
/// </remarks>
public MapReduce Reduce(Code function)
public MapReduce<T> Reduce(Code function)
{
TryModify();
Command.Reduce = function;
Expand All @@ -134,7 +135,7 @@ public MapReduce Reduce(Code function)
/// <summary>
/// Query filter object
/// </summary>
public MapReduce Query(Document query)
public MapReduce<T> Query(Document query)
{
TryModify();
Command.Query = query;
Expand All @@ -144,7 +145,7 @@ public MapReduce Query(Document query)
/// <summary>
/// Sort the query. Useful for optimization
/// </summary>
public MapReduce Sort(Document sort)
public MapReduce<T> Sort(Document sort)
{
TryModify();
Command.Sort = sort;
Expand All @@ -154,7 +155,7 @@ public MapReduce Sort(Document sort)
/// <summary>
/// Number of objects to return from collection
/// </summary>
public MapReduce Limit(long limit)
public MapReduce<T> Limit(long limit)
{
TryModify();
Command.Limit = limit;
Expand All @@ -167,7 +168,7 @@ public MapReduce Limit(long limit)
/// <remarks>
/// A temporary collection is still used and then renamed to the target name atomically.
/// </remarks>
public MapReduce Out(String name)
public MapReduce<T> Out(String name)
{
TryModify();
Command.Out = name;
Expand All @@ -178,7 +179,7 @@ public MapReduce Out(String name)
/// When true the generated collection is not treated as temporary. Specifying out automatically makes
/// the collection permanent
/// </summary>
public MapReduce KeepTemp(bool keep)
public MapReduce<T> KeepTemp(bool keep)
{
TryModify();
Command.KeepTemp = keep;
Expand All @@ -188,7 +189,7 @@ public MapReduce KeepTemp(bool keep)
/// <summary>
/// Provides statistics on job execution time.
/// </summary>
public MapReduce Verbose(bool val)
public MapReduce<T> Verbose(bool val)
{
TryModify();
Command.Verbose = val;
Expand All @@ -198,7 +199,7 @@ public MapReduce Verbose(bool val)
/// <summary>
/// Function to apply to all the results when finished.
/// </summary>
public MapReduce Finalize(Code function)
public MapReduce<T> Finalize(Code function)
{
TryModify();
Command.Finalize = function;
Expand All @@ -208,7 +209,7 @@ public MapReduce Finalize(Code function)
/// <summary>
/// Document where fields go into javascript global scope
/// </summary>
public MapReduce Scope(Document scope)
public MapReduce<T> Scope(Document scope)
{
TryModify();
Command.Scope = scope;
Expand All @@ -229,10 +230,10 @@ internal void RetrieveData()
{
Result = new MapReduceResult(_database.SendCommand(Command.Command));
}
catch(MongoCommandException mce)
catch(MongoCommandException exception)
{
Result = new MapReduceResult(mce.Error);
throw new MongoMapReduceException(mce, this);
Result = new MapReduceResult(exception.Error);
throw new MongoMapReduceException<T>(exception);
}
}

Expand Down
4 changes: 2 additions & 2 deletions source/MongoDB/MongoCollection_1.cs
Expand Up @@ -224,8 +224,8 @@ public MongoCollection(MongoConfiguration configuration, Connection connection,
/// Entrypoint into executing a map/reduce query against the collection.
/// </summary>
/// <returns>A <see cref="MapReduce"/></returns>
public MapReduce MapReduce(){
return new MapReduce(Database, Name);
public MapReduce<T> MapReduce(){
return new MapReduce<T>(Database, Name);
}

///<summary>
Expand Down
2 changes: 1 addition & 1 deletion source/MongoDB/MongoDB.csproj
Expand Up @@ -130,7 +130,7 @@
<Compile Include="Linq\Expressions\MongoExpression.cs" />
<Compile Include="Linq\Translators\ExpressionReplacer.cs" />
<Compile Include="MapReduce.cs" />
<Compile Include="MapReduceCommand.cs" />
<Compile Include="Commands\MapReduceCommand.cs" />
<Compile Include="Results\MapReduceResult.cs" />
<Compile Include="Util\ScopedDictionary.cs" />
<Compile Include="Linq\Translators\AggregateChecker.cs" />
Expand Down
2 changes: 1 addition & 1 deletion source/MongoDB/Obsolete/IMongoCollection.cs
Expand Up @@ -129,7 +129,7 @@ public interface IMongoCollection
/// Maps the reduce.
/// </summary>
/// <returns></returns>
MapReduce MapReduce();
MapReduce<Document> MapReduce();

/// <summary>
/// Counts this instance.
Expand Down
2 changes: 1 addition & 1 deletion source/MongoDB/Obsolete/MongoCollection.cs
Expand Up @@ -180,7 +180,7 @@ public Document FindAndModify (Document document, Document spec, Document sort,
/// Maps the reduce.
/// </summary>
/// <returns></returns>
public MapReduce MapReduce(){
public MapReduce<Document> MapReduce(){
return _collection.MapReduce();
}

Expand Down

0 comments on commit 41d4628

Please sign in to comment.