Skip to content

Commit

Permalink
Pass cancellation token and throw if requested, apache#922
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Dec 31, 2024
1 parent bb781d5 commit b137e19
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/Lucene.Net.Misc/Index/Sorter/BlockJoinComparatorSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Lucene.Net.Util;
using System;
using System.IO;
using System.Threading;

namespace Lucene.Net.Index.Sorter
{
Expand All @@ -28,7 +29,7 @@ namespace Lucene.Net.Index.Sorter
/// Note that this class is intended to used with <see cref="SortingMergePolicy"/>,
/// and for other purposes has some limitations:
/// <list type="bullet">
/// <item><description>Cannot yet be used with <see cref="IndexSearcher.SearchAfter(ScoreDoc, Query, Filter, int, Sort)">
/// <item><description>Cannot yet be used with <see cref="IndexSearcher.SearchAfter(ScoreDoc, Query, Filter, int, Sort, CancellationToken)">
/// IndexSearcher.SearchAfter</see></description></item>
/// <item><description>Filling sort field values is not yet supported.</description></item>
/// </list>
Expand Down Expand Up @@ -266,4 +267,4 @@ public override string ToString()
return "blockJoin(parentSort=" + parentSort + ",childSort=" + childSort + ")";
}
}
}
}
20 changes: 16 additions & 4 deletions src/Lucene.Net/Search/IndexSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ namespace Lucene.Net.Search
/// synchronize on the <see cref="IndexSearcher"/> instance;
/// use your own (non-Lucene) objects instead.</p>
/// </summary>
/// <remarks>
/// LUCENENET Specific - Search methods have had an optional <see cref="CancellationToken"/> parameter added
/// to allow for cancellation of the search operation. For multithreaded search operations, the
/// <see cref="TaskScheduler"/> passed to the constructor will be used to execute the search operations
/// and the <see cref="CancellationToken"/> will be passed to the awaited tasks. If the <see cref="TaskScheduler"/>
/// is <c>null</c>, the search operations will be executed synchronously, and the <see cref="CancellationToken"/>
/// will throw if cancellation is requested upon entry to each leaf reader.
/// </remarks>
public class IndexSearcher
{
internal readonly IndexReader reader; // package private for testing!
Expand Down Expand Up @@ -527,7 +535,7 @@ protected virtual TopDocs Search(Weight weight, ScoreDoc? after, int nDocs, Canc

HitQueue hq = new HitQueue(nDocs, prePopulate: false);
ReentrantLock @lock = new ReentrantLock();
ExecutionHelper<TopDocs> runner = new ExecutionHelper<TopDocs>(executor);
ExecutionHelper<TopDocs> runner = new ExecutionHelper<TopDocs>(executor, cancellationToken);

for (int i = 0; i < m_leafSlices.Length; i++) // search each sub
{
Expand Down Expand Up @@ -637,7 +645,7 @@ protected virtual TopFieldDocs Search(Weight weight, FieldDoc? after, int nDocs,
TopFieldCollector topCollector = TopFieldCollector.Create(sort, nDocs, after, fillFields, doDocScores, doMaxScore, false);

ReentrantLock @lock = new ReentrantLock();
ExecutionHelper<TopFieldDocs> runner = new ExecutionHelper<TopFieldDocs>(executor);
ExecutionHelper<TopFieldDocs> runner = new ExecutionHelper<TopFieldDocs>(executor, cancellationToken);

for (int i = 0; i < m_leafSlices.Length; i++) // search each leaf slice
{
Expand Down Expand Up @@ -722,6 +730,8 @@ protected virtual void Search(IList<AtomicReaderContext> leaves, Weight weight,
// always use single thread:
foreach (AtomicReaderContext ctx in leaves) // search each subreader
{
cancellationToken.ThrowIfCancellationRequested(); // LUCENENET specific - cancellation support at leaf level

try
{
collector.SetNextReader(ctx);
Expand Down Expand Up @@ -960,12 +970,14 @@ public TopFieldDocs Call()
private sealed class ExecutionHelper<T> : IEnumerator<T>, IEnumerable<T>
{
private readonly TaskSchedulerCompletionService<T> service;
private readonly CancellationToken cancellationToken;
private int numTasks;
private T current;

internal ExecutionHelper(TaskScheduler executor)
internal ExecutionHelper(TaskScheduler executor, CancellationToken cancellationToken)
{
this.service = new TaskSchedulerCompletionService<T>(executor);
this.cancellationToken = cancellationToken;
}

public T Current => current;
Expand Down Expand Up @@ -995,7 +1007,7 @@ public bool MoveNext()
try
{
var awaitable = service.Take();
awaitable.Wait();
awaitable.Wait(cancellationToken);
current = awaitable.Result;

return true;
Expand Down

0 comments on commit b137e19

Please sign in to comment.