Skip to content

Commit 13966b7

Browse files
committed
CSHARP-667, CSHARP-668: added Explain and WithIndex extension methods.
1 parent 38ed158 commit 13966b7

File tree

4 files changed

+35
-31
lines changed

4 files changed

+35
-31
lines changed

MongoDB.Driver/Linq/LinqToMongo.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2012 10gen Inc.
1+
/* Copyright 2010-2013 10gen Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -55,31 +55,37 @@ public static bool ContainsAny<TSource>(this IEnumerable<TSource> source, IEnume
5555
/// <summary>
5656
/// Returns an explanation of how the query was executed (instead of the results).
5757
/// </summary>
58-
/// <param name="query">The LINQ query to explain</param>
58+
/// <param name="source">The LINQ query to explain.</param>
5959
/// <returns>An explanation of thow the query was executed.</returns>
60-
public static BsonDocument Explain<T>(this IQueryable<T> query)
60+
public static BsonDocument Explain<T>(this IQueryable<T> source)
6161
{
62-
return Explain(query, false);
62+
return Explain(source, false);
6363
}
6464

6565
/// <summary>
6666
/// Returns an explanation of how the query was executed (instead of the results).
6767
/// </summary>
68-
/// <param name="query">The LINQ query to explain</param>
68+
/// <param name="source">The LINQ query to explain</param>
6969
/// <param name="verbose">Whether the explanation should contain more details.</param>
7070
/// <returns>An explanation of thow the query was executed.</returns>
71-
public static BsonDocument Explain<T>(this IQueryable<T> query, bool verbose)
71+
public static BsonDocument Explain<T>(this IQueryable<T> source, bool verbose)
7272
{
73-
var selectQuery = (SelectQuery)MongoQueryTranslator.Translate(query);
73+
var queryProvider = source.Provider as MongoQueryProvider;
74+
if (queryProvider == null)
75+
{
76+
throw new NotSupportedException("Explain can only be called on a Linq to Mongo queryable.");
77+
}
78+
79+
var selectQuery = (SelectQuery)MongoQueryTranslator.Translate(queryProvider, source.Expression);
7480
if (selectQuery.Take.HasValue && selectQuery.Take.Value == 0)
7581
{
7682
throw new NotSupportedException("A query that has a .Take(0) expression will not be sent to the server and can't be explained");
7783
}
7884
var projector = selectQuery.Execute() as IProjector;
7985
if (projector == null)
8086
{
81-
//This is mainly for .Distinct() queries. First, Last, FirstOrDefault, LastOrDefault don't return
82-
//IQueryable<T>, so .Explain() can't be called on them anyway.
87+
// this is mainly for .Distinct() queries. First, Last, FirstOrDefault, LastOrDefault don't return
88+
// IQueryable<T>, so .Explain() can't be called on them anyway.
8389
throw new NotSupportedException("Explain can only be called on Linq queries that return an IProjector");
8490
}
8591
return projector.Cursor.Explain(verbose);
@@ -111,31 +117,31 @@ public static bool Inject(this IMongoQuery query)
111117
/// Sets an index hint on the query that's being built.
112118
/// </summary>
113119
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
114-
/// <param name="query">The query being built.</param>
120+
/// <param name="source">The query being built.</param>
115121
/// <param name="indexName">The name of the index to use.</param>
116122
/// <returns>New query where the expression includes a WithIndex method call.</returns>
117-
public static IQueryable<TSource> WithIndex<TSource>(this IQueryable<TSource> query, string indexName)
123+
public static IQueryable<TSource> WithIndex<TSource>(this IQueryable<TSource> source, string indexName)
118124
{
119-
return WithIndex(query, (BsonValue)indexName);
125+
return WithIndex(source, (BsonValue)indexName);
120126
}
121127

122128
/// <summary>
123129
/// Sets an index hint on the query that's being built.
124130
/// </summary>
125131
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
126-
/// <param name="query">The query being built.</param>
132+
/// <param name="source">The query being built.</param>
127133
/// <param name="indexHint">Hint for what index to use.</param>
128134
/// <returns>New query where the expression includes a WithIndex method call.</returns>
129-
public static IQueryable<TSource> WithIndex<TSource>(this IQueryable<TSource> query, BsonDocument indexHint)
135+
public static IQueryable<TSource> WithIndex<TSource>(this IQueryable<TSource> source, BsonDocument indexHint)
130136
{
131-
return WithIndex(query, (BsonValue)indexHint);
137+
return WithIndex(source, (BsonValue)indexHint);
132138
}
133139

134140
// private static methods
135-
private static IQueryable<TSource> WithIndex<TSource>(IQueryable<TSource> query, BsonValue index)
141+
private static IQueryable<TSource> WithIndex<TSource>(IQueryable<TSource> query, BsonValue indexHint)
136142
{
137143
var method = ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod(typeof(TSource));
138-
var args = new[] { query.Expression, Expression.Constant(index) };
144+
var args = new[] { query.Expression, Expression.Constant(indexHint) };
139145
var expression = Expression.Call(null, method, args);
140146
return query.Provider.CreateQuery<TSource>(expression);
141147
}

MongoDB.DriverUnitTests/Linq/ExplainTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2012 10gen Inc.
1+
/* Copyright 2010-2013 10gen Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -15,12 +15,11 @@
1515

1616
using System;
1717
using System.Linq;
18-
using NUnit.Framework;
19-
2018
using MongoDB.Bson;
2119
using MongoDB.Driver;
2220
using MongoDB.Driver.Builders;
2321
using MongoDB.Driver.Linq;
22+
using NUnit.Framework;
2423

2524
namespace MongoDB.DriverUnitTests.Linq
2625
{
@@ -41,7 +40,6 @@ private class C
4140
public void Setup()
4241
{
4342
_server = Configuration.TestServer;
44-
_server.Connect();
4543
_collection = Configuration.TestCollection;
4644
}
4745

MongoDB.DriverUnitTests/Linq/WithIndexTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2012 10gen Inc.
1+
/* Copyright 2010-2013 10gen Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -15,12 +15,11 @@
1515

1616
using System;
1717
using System.Linq;
18-
using MongoDB.Driver.Builders;
19-
using NUnit.Framework;
20-
2118
using MongoDB.Bson;
2219
using MongoDB.Driver;
20+
using MongoDB.Driver.Builders;
2321
using MongoDB.Driver.Linq;
22+
using NUnit.Framework;
2423

2524
namespace MongoDB.DriverUnitTests.Linq
2625
{
@@ -47,6 +46,9 @@ public void Setup()
4746
_collection = Configuration.GetTestCollection<B>();
4847

4948
_collection.Drop();
49+
_collection.EnsureIndex(new IndexKeysBuilder().Ascending("a", "b"), IndexOptions.SetName("i"));
50+
_collection.EnsureIndex(new IndexKeysBuilder().Ascending("a", "b"), IndexOptions.SetName("i"));
51+
5052
_collection.Insert(new B { Id = ObjectId.GenerateNewId(), a = 1, b = 10, c = 100 });
5153
_collection.Insert(new B { Id = ObjectId.GenerateNewId(), a = 2, b = 20, c = 200 });
5254
_collection.Insert(new B { Id = ObjectId.GenerateNewId(), a = 3, b = 30, c = 300 });
@@ -103,7 +105,6 @@ public void TestQueryWithIndexBeforeConditionHasIndexNameHint()
103105
[Test]
104106
public void TestIndexNameHintIsUsedInQuery()
105107
{
106-
_collection.EnsureIndex(new IndexKeysBuilder().Ascending("a", "b"), IndexOptions.SetName("i") );
107108
var query = _collection.AsQueryable().Where(o => o.b == 1);
108109
var plan = query.Explain();
109110
Assert.AreEqual("BasicCursor", plan["cursor"].AsString); //Normally this query would use no index
@@ -164,7 +165,6 @@ public void TestQueryWithIndexBeforeConditionHasIndexDocumentHint()
164165
[Test]
165166
public void TestIndexDocumentHintIsUsedInQuery()
166167
{
167-
_collection.EnsureIndex(new IndexKeysBuilder().Ascending("a", "b"), IndexOptions.SetName("i"));
168168
var query = _collection.AsQueryable().Where(o => o.b == 1);
169169
var plan = query.Explain();
170170
Assert.AreEqual("BasicCursor", plan["cursor"].AsString); //Normally this query would use no index

MongoDB.DriverUnitTests/MongoDB.DriverUnitTests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
</AssemblyOriginatorKeyFile>
6060
</PropertyGroup>
6161
<ItemGroup>
62+
<Reference Include="nunit.framework, Version=2.5.9.10348, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
63+
<SpecificVersion>False</SpecificVersion>
64+
<HintPath>..\Tools\NUnit\nunit.framework.dll</HintPath>
65+
</Reference>
6266
<Reference Include="System" />
6367
<Reference Include="System.configuration" />
6468
<Reference Include="System.Core">
@@ -77,10 +81,6 @@
7781
<Reference Include="System.Xml" />
7882
<Reference Include="WindowsBase">
7983
</Reference>
80-
<Reference Include="nunit.framework, Version=2.5.9.10348, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77">
81-
<SpecificVersion>False</SpecificVersion>
82-
<HintPath>..\dependencies\NUnit\lib\nunit.framework.dll</HintPath>
83-
</Reference>
8484
</ItemGroup>
8585
<ItemGroup>
8686
<Compile Include="..\GlobalAssemblyInfo.cs">

0 commit comments

Comments
 (0)