Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Move MapReduce to use generics. Currently some linq tests are failing.

  • Loading branch information...
commit 41d4628052d1aaafa3c7db5cfa3bae00ea71daf1 1 parent af1489d
@lanwin lanwin authored
View
1  source/MongoDB.Tests/MongoDB.Tests.csproj
@@ -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" />
View
7 source/MongoDB/Exceptions/MongoMapReduceException.cs
@@ -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.
@@ -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);
}
View
2  source/MongoDB/IMongoCollection_1.cs
@@ -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.
View
24 source/MongoDB/Linq/MongoQueryProvider.cs
@@ -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;
@@ -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)
View
39 source/MongoDB/MapReduce.cs
@@ -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>
@@ -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
{
@@ -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)
@@ -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));
}
@@ -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;
@@ -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));
}
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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);
}
}
View
4 source/MongoDB/MongoCollection_1.cs
@@ -224,8 +224,8 @@ public MongoCollection(MongoConfiguration configuration, Connection connection,
return null;
}
}
-
- /// <summary>
+
+ /// <summary>
/// Entrypoint into executing a map/reduce query against the collection.
/// </summary>
/// <returns>A <see cref="MapReduce"/></returns>
View
2  source/MongoDB/MongoDB.csproj
@@ -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" />
View
2  source/MongoDB/Obsolete/IMongoCollection.cs
@@ -129,7 +129,7 @@ public interface IMongoCollection
/// Maps the reduce.
/// </summary>
/// <returns></returns>
- MapReduce MapReduce();
+ MapReduce<Document> MapReduce();
/// <summary>
/// Counts this instance.
View
2  source/MongoDB/Obsolete/MongoCollection.cs
@@ -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();
}
View
74 source/MongoDB/Results/MapReduceResult.cs
@@ -3,89 +3,102 @@
namespace MongoDB.Results
{
/// <summary>
- ///
/// </summary>
public class MapReduceResult
{
private readonly Document _counts;
private readonly Document _result;
- private TimeSpan _span = TimeSpan.MinValue;
+ private TimeSpan _timeSpan = TimeSpan.Zero;
/// <summary>
- /// Initializes a new instance of the <see cref="MapReduceResult"/> class.
+ /// Initializes a new instance of the <see cref = "MapReduceResult" /> class.
/// </summary>
- /// <param name="result">The result.</param>
- public MapReduceResult(Document result){
- this._result = result;
+ /// <param name = "result">The result.</param>
+ public MapReduceResult(Document result)
+ {
+ if(result == null)
+ throw new ArgumentNullException("result");
+
+ _result = result;
_counts = (Document)result["counts"];
}
/// <summary>
- /// Gets the name of the collection.
+ /// Gets the name of the collection.
/// </summary>
/// <value>The name of the collection.</value>
- public string CollectionName{
+ public string CollectionName
+ {
get { return (string)_result["result"]; }
}
/// <summary>
- /// Gets the input count.
+ /// Gets the input count.
/// </summary>
/// <value>The input count.</value>
- public long InputCount{
+ public long InputCount
+ {
get { return Convert.ToInt64(_counts["input"]); }
}
/// <summary>
- /// Gets the emit count.
+ /// Gets the emit count.
/// </summary>
/// <value>The emit count.</value>
- public long EmitCount{
+ public long EmitCount
+ {
get { return Convert.ToInt64(_counts["emit"]); }
}
/// <summary>
- /// Gets the output count.
+ /// Gets the output count.
/// </summary>
/// <value>The output count.</value>
- public long OutputCount{
+ public long OutputCount
+ {
get { return Convert.ToInt64(_counts["output"]); }
}
/// <summary>
- /// Gets the time.
+ /// Gets the time.
/// </summary>
/// <value>The time.</value>
- public long Time{
+ public long Time
+ {
get { return Convert.ToInt64(_result["timeMillis"]); }
}
/// <summary>
- /// Gets the time span.
+ /// Gets the time span.
/// </summary>
/// <value>The time span.</value>
- public TimeSpan TimeSpan{
- get{
- if(_span == TimeSpan.MinValue)
- _span = TimeSpan.FromMilliseconds(Time);
- return _span;
+ public TimeSpan TimeSpan
+ {
+ get
+ {
+ if(_timeSpan == TimeSpan.Zero)
+ _timeSpan = TimeSpan.FromMilliseconds(Time);
+ return _timeSpan;
}
}
/// <summary>
- /// Gets a value indicating whether this <see cref="MapReduceResult"/> is ok.
+ /// Gets a value indicating whether this <see cref = "MapReduceResult" /> is ok.
/// </summary>
/// <value><c>true</c> if ok; otherwise, <c>false</c>.</value>
- public Boolean Ok{
+ public Boolean Ok
+ {
get { return (1.0 == Convert.ToDouble(_result["ok"])); }
}
/// <summary>
- /// Gets the error message.
+ /// Gets the error message.
/// </summary>
/// <value>The error message.</value>
- public String ErrorMessage{
- get{
+ public String ErrorMessage
+ {
+ get
+ {
if(_result.Contains("msg"))
return (String)_result["msg"];
return String.Empty;
@@ -93,12 +106,13 @@ public class MapReduceResult
}
/// <summary>
- /// Returns a <see cref="System.String"/> that represents this instance.
+ /// Returns a <see cref = "System.String" /> that represents this instance.
/// </summary>
/// <returns>
- /// A <see cref="System.String"/> that represents this instance.
+ /// A <see cref = "System.String" /> that represents this instance.
/// </returns>
- public override string ToString(){
+ public override string ToString()
+ {
return _result.ToString();
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.