This repository has been archived by the owner on Sep 13, 2023. It is now read-only.
forked from chriseldredge/Lucene.Net.Linq
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CaptureStatistics extension method to allow clients to be called …
…back with statistics whenever a query is executed.
- Loading branch information
1 parent
97749c4
commit 389b965
Showing
16 changed files
with
348 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
source/Lucene.Net.Linq.Tests/Integration/StatisticTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using NUnit.Framework; | ||
|
||
namespace Lucene.Net.Linq.Tests.Integration | ||
{ | ||
[TestFixture] | ||
public class StatisticTests : IntegrationTestBase | ||
{ | ||
private IQueryable<SampleDocument> documents; | ||
|
||
[SetUp] | ||
public void AddDocuments() | ||
{ | ||
AddDocument(new SampleDocument { Name = "c", Scalar = 3, Flag = true, Version = new Version(100, 0, 0) }); | ||
AddDocument(new SampleDocument { Name = "a", Scalar = 1, Version = new Version(20, 0, 0) }); | ||
AddDocument(new SampleDocument { Name = "b", Scalar = 2, Flag = true, Version = new Version(3, 0, 0) }); | ||
|
||
documents = provider.AsQueryable<SampleDocument>(); | ||
} | ||
|
||
[Test] | ||
public void CountsTotalHits() | ||
{ | ||
LuceneQueryStatistics stats = null; | ||
|
||
documents.CaptureStatistics(s => { stats = s; }).Skip(1).Take(1).Count(); | ||
|
||
Assert.That(stats, Is.Not.Null, "stats"); | ||
Assert.That(stats.TotalHits, Is.EqualTo(documents.Count())); | ||
} | ||
|
||
[Test] | ||
public void InvokesOncePerExecution() | ||
{ | ||
var list = new List<LuceneQueryStatistics>(); | ||
|
||
documents = documents.CaptureStatistics(list.Add); | ||
|
||
documents.Where(doc => doc.Scalar == 1).ToList(); | ||
documents.Where(doc => doc.Scalar != 1).ToList(); | ||
|
||
Assert.That(list.Count, Is.EqualTo(2)); | ||
Assert.That(list[0].TotalHits, Is.EqualTo(1)); | ||
Assert.That(list[1].TotalHits, Is.EqualTo(2)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
source/Lucene.Net.Linq/Clauses/ExpressionNodes/QueryStatisticsCallbackExpressionNode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using Remotion.Linq; | ||
using Remotion.Linq.Parsing.Structure.IntermediateModel; | ||
|
||
namespace Lucene.Net.Linq.Clauses.ExpressionNodes | ||
{ | ||
internal class QueryStatisticsCallbackExpressionNode : MethodCallExpressionNodeBase | ||
{ | ||
public static readonly MethodInfo[] SupportedMethods = | ||
{ | ||
GetSupportedMethod (() => LuceneMethods.CaptureStatistics<object>(null, null)) | ||
}; | ||
|
||
private readonly ConstantExpression callback; | ||
|
||
public QueryStatisticsCallbackExpressionNode(MethodCallExpressionParseInfo parseInfo, ConstantExpression callback) | ||
: base(parseInfo) | ||
{ | ||
this.callback = callback; | ||
} | ||
|
||
public override Expression Resolve(ParameterExpression inputParameter, Expression expressionToBeResolved, ClauseGenerationContext clauseGenerationContext) | ||
{ | ||
return Source.Resolve(inputParameter, expressionToBeResolved, clauseGenerationContext); | ||
} | ||
|
||
protected override QueryModel ApplyNodeSpecificSemantics(QueryModel queryModel, ClauseGenerationContext clauseGenerationContext) | ||
{ | ||
queryModel.BodyClauses.Add(new QueryStatisticsCallbackClause(callback)); | ||
return queryModel; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
source/Lucene.Net.Linq/Clauses/QueryStatisticsCallbackClause.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
using Remotion.Linq; | ||
using Remotion.Linq.Clauses; | ||
|
||
namespace Lucene.Net.Linq.Clauses | ||
{ | ||
internal class QueryStatisticsCallbackClause : ExtensionClause<ConstantExpression> | ||
{ | ||
public QueryStatisticsCallbackClause(ConstantExpression expression) | ||
: base(expression) | ||
{ | ||
} | ||
|
||
public Action<LuceneQueryStatistics> Callback | ||
{ | ||
get { return (Action<LuceneQueryStatistics>) expression.Value; } | ||
} | ||
|
||
protected override void Accept(ILuceneQueryModelVisitor visitor, QueryModel queryModel, int index) | ||
{ | ||
visitor.VisitQueryStatisticsCallbackClause(this, queryModel, index); | ||
} | ||
|
||
public override IBodyClause Clone(CloneContext cloneContext) | ||
{ | ||
return new QueryStatisticsCallbackClause(expression); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.