Skip to content

Commit

Permalink
Add CancellationToken parameter to IndexSearcher methods, apache#922
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Dec 31, 2024
1 parent d96dc9e commit bb781d5
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 109 deletions.
7 changes: 4 additions & 3 deletions src/Lucene.Net.TestFramework/Search/AssertingIndexSearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using RandomizedTesting.Generators;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Lucene.Net.Search
Expand Down Expand Up @@ -100,15 +101,15 @@ protected override Query WrapFilter(Query query, Filter filter)
return (filter is null) ? query : new FilteredQuery(query, filter, TestUtil.RandomFilterStrategy(random));
}

protected override void Search(IList<AtomicReaderContext> leaves, Weight weight, ICollector collector)
protected override void Search(IList<AtomicReaderContext> leaves, Weight weight, ICollector collector, CancellationToken cancellationToken = default)
{
// TODO: shouldn't we AssertingCollector.wrap(collector) here?
base.Search(leaves, AssertingWeight.Wrap(random, weight), collector);
base.Search(leaves, AssertingWeight.Wrap(random, weight), collector, cancellationToken);
}

public override string ToString()
{
return "AssertingIndexSearcher(" + base.ToString() + ")";
}
}
}
}
17 changes: 9 additions & 8 deletions src/Lucene.Net.TestFramework/Search/CheckHits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Globalization;
using System.Text;
using System.Threading;
using JCG = J2N.Collections.Generic;
using Assert = Lucene.Net.TestFramework.Assert;

Expand Down Expand Up @@ -450,28 +451,28 @@ protected virtual void CheckExplanations(Query q)
base.Search(q, null, new ExplanationAsserter(q, null, this));
}

public override TopFieldDocs Search(Query query, Filter filter, int n, Sort sort)
public override TopFieldDocs Search(Query query, Filter filter, int n, Sort sort, CancellationToken cancellationToken = default)
{
CheckExplanations(query);
return base.Search(query, filter, n, sort);
return base.Search(query, filter, n, sort, cancellationToken);
}

public override void Search(Query query, ICollector results)
public override void Search(Query query, ICollector results, CancellationToken cancellationToken = default)
{
CheckExplanations(query);
base.Search(query, results);
base.Search(query, results, cancellationToken);
}

public override void Search(Query query, Filter filter, ICollector results)
public override void Search(Query query, Filter filter, ICollector results, CancellationToken cancellationToken = default)
{
CheckExplanations(query);
base.Search(query, filter, results);
base.Search(query, filter, results, cancellationToken);
}

public override TopDocs Search(Query query, Filter filter, int n)
public override TopDocs Search(Query query, Filter filter, int n, CancellationToken cancellationToken = default)
{
CheckExplanations(query);
return base.Search(query, filter, n);
return base.Search(query, filter, n, cancellationToken);
}
}

Expand Down
25 changes: 13 additions & 12 deletions src/Lucene.Net.TestFramework/Search/ShardSearchingTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using JCG = J2N.Collections.Generic;
using Console = Lucene.Net.Util.SystemConsole;
#if FEATURE_SERIALIZABLE_EXCEPTIONS
Expand Down Expand Up @@ -453,7 +454,7 @@ public override CollectionStatistics CollectionStatistics(string field)
return new CollectionStatistics(field, maxDoc, docCount, sumTotalTermFreq, sumDocFreq);
}

public override TopDocs Search(Query query, int numHits)
public override TopDocs Search(Query query, int numHits, CancellationToken cancellationToken = default)
{
TopDocs[] shardHits = new TopDocs[nodeVersions.Length];
for (int nodeID = 0; nodeID < nodeVersions.Length; nodeID++)
Expand All @@ -462,7 +463,7 @@ public override TopDocs Search(Query query, int numHits)
{
// My node; run using local shard searcher we
// already aquired:
shardHits[nodeID] = LocalSearch(query, numHits);
shardHits[nodeID] = LocalSearch(query, numHits, cancellationToken);
}
else
{
Expand All @@ -474,12 +475,12 @@ public override TopDocs Search(Query query, int numHits)
return TopDocs.Merge(null, numHits, shardHits);
}

public virtual TopDocs LocalSearch(Query query, int numHits)
public virtual TopDocs LocalSearch(Query query, int numHits, CancellationToken cancellationToken = default)
{
return base.Search(query, numHits);
return base.Search(query, numHits, cancellationToken);
}

public override TopDocs SearchAfter(ScoreDoc after, Query query, int numHits)
public override TopDocs SearchAfter(ScoreDoc after, Query query, int numHits, CancellationToken cancellationToken = default)
{
TopDocs[] shardHits = new TopDocs[nodeVersions.Length];
// results are merged in that order: score, shardIndex, doc. therefore we set
Expand Down Expand Up @@ -526,7 +527,7 @@ public override TopDocs SearchAfter(ScoreDoc after, Query query, int numHits)
{
// My node; run using local shard searcher we
// already aquired:
shardHits[nodeID] = LocalSearchAfter(shardAfter, query, numHits);
shardHits[nodeID] = LocalSearchAfter(shardAfter, query, numHits, cancellationToken);
}
else
{
Expand All @@ -539,12 +540,12 @@ public override TopDocs SearchAfter(ScoreDoc after, Query query, int numHits)
return TopDocs.Merge(null, numHits, shardHits);
}

public virtual TopDocs LocalSearchAfter(ScoreDoc after, Query query, int numHits)
public virtual TopDocs LocalSearchAfter(ScoreDoc after, Query query, int numHits, CancellationToken cancellationToken = default)
{
return base.SearchAfter(after, query, numHits);
return base.SearchAfter(after, query, numHits, cancellationToken);
}

public override TopFieldDocs Search(Query query, int numHits, Sort sort)
public override TopFieldDocs Search(Query query, int numHits, Sort sort, CancellationToken cancellationToken = default)
{
if (Debugging.AssertsEnabled) Debugging.Assert(sort != null);
TopDocs[] shardHits = new TopDocs[nodeVersions.Length];
Expand All @@ -554,7 +555,7 @@ public override TopFieldDocs Search(Query query, int numHits, Sort sort)
{
// My node; run using local shard searcher we
// already aquired:
shardHits[nodeID] = LocalSearch(query, numHits, sort);
shardHits[nodeID] = LocalSearch(query, numHits, sort, cancellationToken);
}
else
{
Expand All @@ -566,9 +567,9 @@ public override TopFieldDocs Search(Query query, int numHits, Sort sort)
return (TopFieldDocs)TopDocs.Merge(sort, numHits, shardHits);
}

public virtual TopFieldDocs LocalSearch(Query query, int numHits, Sort sort)
public virtual TopFieldDocs LocalSearch(Query query, int numHits, Sort sort, CancellationToken cancellationToken = default)
{
return base.Search(query, numHits, sort);
return base.Search(query, numHits, sort, cancellationToken);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/Lucene.Net.Tests/Search/TestBooleanQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using JCG = J2N.Collections.Generic;
using Assert = Lucene.Net.TestFramework.Assert;
Expand Down Expand Up @@ -396,10 +397,10 @@ public IndexSearcherAnonymousClass(IndexReader r)
{
}

protected override void Search(IList<AtomicReaderContext> leaves, Weight weight, ICollector collector)
protected override void Search(IList<AtomicReaderContext> leaves, Weight weight, ICollector collector, CancellationToken cancellationToken = default)
{
Assert.AreEqual(-1, collector.GetType().Name.IndexOf("OutOfOrder", StringComparison.Ordinal));
base.Search(leaves, weight, collector);
base.Search(leaves, weight, collector, cancellationToken);
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/Lucene.Net.Tests/Search/TestCustomSearcherSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Assert = Lucene.Net.TestFramework.Assert;
using Console = Lucene.Net.Util.SystemConsole;
using JCG = J2N.Collections.Generic;
Expand Down Expand Up @@ -213,20 +214,20 @@ public CustomSearcher(IndexReader r, int switcher)
this.switcher = switcher;
}

public override TopFieldDocs Search(Query query, Filter filter, int nDocs, Sort sort)
public override TopFieldDocs Search(Query query, Filter filter, int nDocs, Sort sort, CancellationToken cancellationToken = default)
{
BooleanQuery bq = new BooleanQuery();
bq.Add(query, Occur.MUST);
bq.Add(new TermQuery(new Term("mandant", Convert.ToString(switcher))), Occur.MUST);
return base.Search(bq, filter, nDocs, sort);
return base.Search(bq, filter, nDocs, sort, cancellationToken);
}

public override TopDocs Search(Query query, Filter filter, int nDocs)
public override TopDocs Search(Query query, Filter filter, int nDocs, CancellationToken cancellationToken = default)
{
BooleanQuery bq = new BooleanQuery();
bq.Add(query, Occur.MUST);
bq.Add(new TermQuery(new Term("mandant", Convert.ToString(switcher))), Occur.MUST);
return base.Search(bq, filter, nDocs);
return base.Search(bq, filter, nDocs, cancellationToken);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/Lucene.Net/Search/CollectionTerminatedException.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;

#if FEATURE_SERIALIZABLE_EXCEPTIONS
using System.ComponentModel;
Expand Down Expand Up @@ -29,10 +30,10 @@ namespace Lucene.Net.Search
/// terminate collection of the current leaf.
/// <para/>Note: <see cref="IndexSearcher"/> swallows this exception and never re-throws it.
/// As a consequence, you should not catch it when calling any overload of
/// <see cref="IndexSearcher.Search(Weight, FieldDoc, int, Sort, bool, bool, bool)"/> as it is unnecessary and might hide misuse
/// <see cref="IndexSearcher.Search(Weight, FieldDoc, int, Sort, bool, bool, bool, CancellationToken)"/> as it is unnecessary and might hide misuse
/// of this exception.
/// </summary>
// LUCENENET: It is no longer good practice to use binary serialization.
// LUCENENET: It is no longer good practice to use binary serialization.
// See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568
#if FEATURE_SERIALIZABLE_EXCEPTIONS
[Serializable]
Expand Down
22 changes: 12 additions & 10 deletions src/Lucene.Net/Search/FieldComparator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Lucene.Net.Diagnostics;
using Lucene.Net.Index;
using Lucene.Net.Support;
using System;
using System.IO;
using System.Threading;
using JCG = J2N.Collections.Generic;
using Number = J2N.Numerics.Number;

Expand Down Expand Up @@ -403,7 +405,7 @@ public override FieldComparer SetNextReader(AtomicReaderContext context)

/// <summary>
/// Parses field's values as <see cref="byte"/> (using
/// <see cref="IFieldCache.GetBytes(Index.AtomicReader, string, FieldCache.IByteParser, bool)"/> and sorts by ascending value
/// <see cref="IFieldCache.GetBytes(AtomicReader, string, FieldCache.IByteParser, bool)"/> and sorts by ascending value
/// </summary>
[Obsolete, CLSCompliant(false)] // LUCENENET NOTE: marking non-CLS compliant because of sbyte - it is obsolete, anyway
public sealed class ByteComparer : NumericComparer<J2N.Numerics.SByte>
Expand Down Expand Up @@ -488,7 +490,7 @@ public override int CompareTop(int doc)

/// <summary>
/// Parses field's values as <see cref="double"/> (using
/// <see cref="IFieldCache.GetDoubles(Index.AtomicReader, string, FieldCache.IDoubleParser, bool)"/> and sorts by ascending value
/// <see cref="IFieldCache.GetDoubles(AtomicReader, string, FieldCache.IDoubleParser, bool)"/> and sorts by ascending value
/// </summary>
public sealed class DoubleComparer : NumericComparer<J2N.Numerics.Double>
{
Expand Down Expand Up @@ -581,7 +583,7 @@ public override int CompareTop(int doc)

/// <summary>
/// Parses field's values as <see cref="float"/> (using
/// <see cref="IFieldCache.GetSingles(Index.AtomicReader, string, FieldCache.ISingleParser, bool)"/> and sorts by ascending value
/// <see cref="IFieldCache.GetSingles(AtomicReader, string, FieldCache.ISingleParser, bool)"/> and sorts by ascending value
/// <para/>
/// NOTE: This was FloatComparator in Lucene
/// </summary>
Expand Down Expand Up @@ -677,7 +679,7 @@ public override int CompareTop(int doc)

/// <summary>
/// Parses field's values as <see cref="short"/> (using
/// <see cref="IFieldCache.GetInt16s(Index.AtomicReader, string, FieldCache.IInt16Parser, bool)"/> and sorts by ascending value
/// <see cref="IFieldCache.GetInt16s(AtomicReader, string, FieldCache.IInt16Parser, bool)"/> and sorts by ascending value
/// <para/>
/// NOTE: This was ShortComparator in Lucene
/// </summary>
Expand Down Expand Up @@ -765,7 +767,7 @@ public override int CompareTop(int doc)

/// <summary>
/// Parses field's values as <see cref="int"/> (using
/// <see cref="IFieldCache.GetInt32s(Index.AtomicReader, string, FieldCache.IInt32Parser, bool)"/> and sorts by ascending value
/// <see cref="IFieldCache.GetInt32s(AtomicReader, string, FieldCache.IInt32Parser, bool)"/> and sorts by ascending value
/// <para/>
/// NOTE: This was IntComparator in Lucene
/// </summary>
Expand Down Expand Up @@ -849,7 +851,7 @@ public override int CompareTop(int doc)

/// <summary>
/// Parses field's values as <see cref="long"/> (using
/// <see cref="IFieldCache.GetInt64s(Index.AtomicReader, string, FieldCache.IInt64Parser, bool)"/> and sorts by ascending value
/// <see cref="IFieldCache.GetInt64s(AtomicReader, string, FieldCache.IInt64Parser, bool)"/> and sorts by ascending value
/// <para/>
/// NOTE: This was LongComparator in Lucene
/// </summary>
Expand Down Expand Up @@ -941,7 +943,7 @@ public override int CompareTop(int doc)
/// sorting only by descending relevance and then
/// secondarily by ascending docID, performance is faster
/// using <see cref="TopScoreDocCollector"/> directly (which all overloads of
/// <see cref="IndexSearcher.Search(Query, int)"/> use when no <see cref="Sort"/> is
/// <see cref="IndexSearcher.Search(Query, int, CancellationToken)"/> use when no <see cref="Sort"/> is
/// specified).
/// </summary>
public sealed class RelevanceComparer : FieldComparer<J2N.Numerics.Single>
Expand Down Expand Up @@ -1105,11 +1107,11 @@ public override int CompareTop(int doc)
}

/// <summary>
/// Sorts by field's natural <see cref="Index.Term"/> sort order, using
/// Sorts by field's natural <see cref="Term"/> sort order, using
/// ordinals. This is functionally equivalent to
/// <see cref="Lucene.Net.Search.FieldComparer.TermValComparer"/>, but it first resolves the string
/// to their relative ordinal positions (using the index
/// returned by <see cref="IFieldCache.GetTermsIndex(Index.AtomicReader, string, float)"/>), and
/// returned by <see cref="IFieldCache.GetTermsIndex(AtomicReader, string, float)"/>), and
/// does most comparisons using the ordinals. For medium
/// to large results, this comparer will be much faster
/// than <see cref="Lucene.Net.Search.FieldComparer.TermValComparer"/>. For very small
Expand Down Expand Up @@ -1447,7 +1449,7 @@ public override int CompareValues(BytesRef val1, BytesRef val2)
}

/// <summary>
/// Sorts by field's natural <see cref="Index.Term"/> sort order. All
/// Sorts by field's natural <see cref="Term"/> sort order. All
/// comparisons are done using <see cref="BytesRef.CompareTo(BytesRef)"/>, which is
/// slow for medium to large result sets but possibly
/// very fast for very small results sets.
Expand Down
5 changes: 3 additions & 2 deletions src/Lucene.Net/Search/FieldDoc.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Lucene.Net.Support;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Threading;

namespace Lucene.Net.Search
{
Expand Down Expand Up @@ -48,7 +49,7 @@ public class FieldDoc : ScoreDoc
/// the <see cref="FieldComparer.GetValue(int)"/> method corresponding
/// FieldComparer used to sort this field. </summary>
/// <seealso cref="Sort"/>
/// <seealso cref="IndexSearcher.Search(Query,Filter,int,Sort)"/>
/// <seealso cref="IndexSearcher.Search(Query,Filter,int,Sort,CancellationToken)"/>
public object[] Fields;

/// <summary>
Expand Down Expand Up @@ -87,4 +88,4 @@ public override string ToString()
return sb.ToString();
}
}
}
}
7 changes: 4 additions & 3 deletions src/Lucene.Net/Search/FieldValueHitQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.IO;
using System.Threading;

namespace Lucene.Net.Search
{
Expand Down Expand Up @@ -196,7 +197,7 @@ public static FieldValueHitQueue<T> Create<T>(SortField[] fields, int size)
/// <para/>
/// @lucene.experimental
/// @since 2.9 </summary>
/// <seealso cref="IndexSearcher.Search(Query,Filter,int,Sort)"/>
/// <seealso cref="IndexSearcher.Search(Query,Filter,int,Sort,CancellationToken)"/>
/// <seealso cref="FieldCache"/>
public abstract class FieldValueHitQueue<T> : PriorityQueue<T>
where T : FieldValueHitQueue.Entry
Expand Down Expand Up @@ -259,7 +260,7 @@ public virtual void SetComparer(int pos, FieldComparer comparer)
/// </summary>
/// <param name="entry"> The <see cref="FieldValueHitQueue.Entry"/> used to create a <see cref="FieldDoc"/> </param>
/// <returns> The newly created <see cref="FieldDoc"/> </returns>
/// <seealso cref="IndexSearcher.Search(Query,Filter,int,Sort)"/>
/// <seealso cref="IndexSearcher.Search(Query,Filter,int,Sort,CancellationToken)"/>
internal virtual FieldDoc FillFields(FieldValueHitQueue.Entry entry)
{
int n = m_comparers.Length;
Expand All @@ -278,4 +279,4 @@ internal virtual FieldDoc FillFields(FieldValueHitQueue.Entry entry)
[SuppressMessage("Microsoft.Performance", "CA1819", Justification = "Lucene's design requires some writable array properties")]
internal virtual SortField[] Fields => m_fields;
}
}
}
Loading

0 comments on commit bb781d5

Please sign in to comment.