Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,7 @@ internal SimpleTVTermsEnum(JCG.SortedDictionary<BytesRef, SimpleTVPostings> term

public override SeekStatus SeekCeil(BytesRef text)
{
var newTerms = new JCG.SortedDictionary<BytesRef, SimpleTVPostings>(_terms.Comparer);
foreach (var p in _terms)
if (p.Key.CompareTo(text) >= 0)
newTerms.Add(p.Key, p.Value);

_iterator = newTerms.GetEnumerator();
_iterator = _terms.GetViewAfter(text).GetEnumerator();

// LUCENENET specific: Since in .NET we don't have a HasNext() method, we need
// to call MoveNext(). Since we need
Expand Down
42 changes: 15 additions & 27 deletions src/Lucene.Net.Grouping/AbstractFirstPassGroupingCollector.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using Lucene.Net.Diagnostics;
using Lucene.Net.Index;
using Lucene.Net.Support;
using Lucene.Net.Support.Threading;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using JCG = J2N.Collections.Generic;

namespace Lucene.Net.Search.Grouping
Expand Down Expand Up @@ -241,18 +239,7 @@ public virtual void Collect(int doc)

// We already tested that the document is competitive, so replace
// the bottom group with this new group.
//CollectedSearchGroup<TGroupValue> bottomGroup = orderedGroups.PollLast();
CollectedSearchGroup<TGroupValue> bottomGroup;
UninterruptableMonitor.Enter(m_orderedGroups);
try
{
bottomGroup = m_orderedGroups.Last();
m_orderedGroups.Remove(bottomGroup);
}
finally
{
UninterruptableMonitor.Exit(m_orderedGroups);
}
m_orderedGroups.RemoveLast(out CollectedSearchGroup<TGroupValue> bottomGroup);
if (Debugging.AssertsEnabled) Debugging.Assert(m_orderedGroups.Count == topNGroups - 1);

groupMap.Remove(bottomGroup.GroupValue);
Expand All @@ -270,7 +257,9 @@ public virtual void Collect(int doc)
m_orderedGroups.Add(bottomGroup);
if (Debugging.AssertsEnabled) Debugging.Assert(m_orderedGroups.Count == topNGroups);

int lastComparerSlot = m_orderedGroups.Last().ComparerSlot;
// LUCENENET: We know this call cannot fail because we just added a group, so we can safely ignore the return value.
m_orderedGroups.TryGetLast(out CollectedSearchGroup<TGroupValue> lastGroup);
Comment thread
NightOwl888 marked this conversation as resolved.
Outdated
int lastComparerSlot = lastGroup.ComparerSlot;
foreach (FieldComparer fc in comparers)
{
fc.SetBottom(lastComparerSlot);
Expand Down Expand Up @@ -315,16 +304,8 @@ public virtual void Collect(int doc)
CollectedSearchGroup<TGroupValue> prevLast;
if (m_orderedGroups != null)
{
UninterruptableMonitor.Enter(m_orderedGroups);
try
{
prevLast = m_orderedGroups.Last();
m_orderedGroups.Remove(group);
}
finally
{
UninterruptableMonitor.Exit(m_orderedGroups);
}
m_orderedGroups.TryGetLast(out prevLast);
m_orderedGroups.Remove(group);
if (Debugging.AssertsEnabled) Debugging.Assert(m_orderedGroups.Count == topNGroups - 1);
}
else
Expand All @@ -344,7 +325,11 @@ public virtual void Collect(int doc)
{
m_orderedGroups.Add(group);
if (Debugging.AssertsEnabled) Debugging.Assert(m_orderedGroups.Count == topNGroups);
var newLast = m_orderedGroups.Last();
if (!m_orderedGroups.TryGetLast(out CollectedSearchGroup<TGroupValue> newLast))
{
// LUCENENET: Added because Java would throw NoSuchElementException if orderedGroups is empty.
throw new InvalidOperationException("orderedGroups must not be empty");
}
// If we changed the value of the last group, or changed which group was last, then update bottom:
if (group == newLast || prevLast != newLast)
{
Expand Down Expand Up @@ -390,7 +375,10 @@ private void BuildSortedSet()

foreach (FieldComparer fc in comparers)
{
fc.SetBottom(m_orderedGroups.Last().ComparerSlot);
if (!m_orderedGroups.TryGetLast(out CollectedSearchGroup<TGroupValue> lastGroup))
// LUCENENET: Added because Java would throw NoSuchElementException if orderedGroups is empty.
throw new InvalidOperationException("orderedGroups must not be empty");
fc.SetBottom(lastGroup.ComparerSlot);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/Lucene.Net.Grouping/AbstractGroupFacetCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,16 @@ public virtual void AddFacetCount(BytesRef facetValue, int count)
{
return;
}
var max = facetEntries.Max;
if (max != null)
facetEntries.Remove(max);
facetEntries.RemoveLast(out _);
}
facetEntries.Add(facetEntry);

if (facetEntries.Count == maxSize)
{
var max = facetEntries.Max;
currentMin = max != null ? max.Count : 0;
// LUCENENET: We can safely ignore the return value of TryGetLast() here, because we
// know that the collection is not empty (we just added an entry to it).
facetEntries.TryGetLast(out FacetEntry last);
Comment thread
NightOwl888 marked this conversation as resolved.
Outdated
currentMin = last.Count;
}
}

Expand Down
7 changes: 2 additions & 5 deletions src/Lucene.Net.Grouping/SearchGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,16 +392,14 @@ private void UpdateNextGroup(int topN, ShardIter<T> shard)
// Prune un-competitive groups:
while (queue.Count > topN)
{
MergedGroup<T> group = queue.Max;
queue.Remove(group);
queue.RemoveLast(out MergedGroup<T> group);
//System.out.println("PRUNE: " + group);
group.IsInQueue = false;
}
}

public virtual ICollection<SearchGroup<T>> Merge(IList<ICollection<SearchGroup<T>>> shards, int offset, int topN)
{

int maxQueueSize = offset + topN;

//System.out.println("merge");
Expand All @@ -423,8 +421,7 @@ public virtual ICollection<SearchGroup<T>> Merge(IList<ICollection<SearchGroup<T

while (queue.Count != 0)
{
MergedGroup<T> group = queue.Min;
queue.Remove(group);
queue.RemoveFirst(out MergedGroup<T> group);
group.IsProcessed = true;
//System.out.println(" pop: shards=" + group.shards + " group=" + (group.groupValue is null ? "null" : (((BytesRef) group.groupValue).utf8ToString())) + " sortValues=" + Arrays.toString(group.topValues));
if (count++ >= offset)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,8 @@ protected internal virtual IDictionary<string, object[]> HighlightFieldsAsObject
Term floor = new Term(field, "");
Term ceiling = new Term(field, UnicodeUtil.BIG_TERM);

// LUCENENET: Call custom GetViewBetween overload to mimic Java's exclusive upper bound behavior.
var fieldTerms = queryTerms.GetViewBetween(floor, lowerValueInclusive: true, ceiling, upperValueInclusive: false);
// LUCENENET: Call J2N's GetView overload to mimic Java's exclusive upper bound behavior.
var fieldTerms = queryTerms.GetView(floor, fromInclusive: true, ceiling, toInclusive: false);

// TODO: should we have some reasonable defaults for term pruning? (e.g. stopwords)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,12 @@ private static void BoundedTreeAdd(JCG.SortedSet<Lookup.LookupResult> results, L
{
if (results.Count >= num)
{
var first = results.Min; // "get" our first object so we don't cross threads
if (!results.TryGetFirst(out Lookup.LookupResult first)) // "get" our first object so we don't cross threads
// LUCENENET: Java would throw NoSuchElementException here, so we are also throwing in this case.
throw new InvalidOperationException("Expected at least one result in the set, but there were none.");
if (first.Value < result.Value)
// Code similar to the java TreeMap class
results.Remove(first);
results.Remove(first); // LUCENENET: Calling RemoveFirst() would be redundant here, since we already have the value.
else
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Lucene.Net.TestFramework/Analysis/MockCharFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ protected override int Correct(int currentOff)
{
int ret;
// LUCENENET NOTE: TryGetPredecessor is equivalent to TreeMap.lowerEntry() in Java
if (corrections.TryGetPredecessor(currentOff + 1, out KeyValuePair<int, int> lastEntry))
if (corrections.TryGetPredecessor(currentOff + 1, out _, out int lastEntryValue))
{
ret = currentOff + lastEntry.Value;
ret = currentOff + lastEntryValue;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,10 @@ private void AddFixedDerefBytesField(FieldInfo field, IndexOutput data, IndexOut
{
brefDummy = new BytesRef();
}
//int ord = dictionary.HeadSet(brefDummy).Size();
// LUCENENET: This LINQ call will actually be less overhead than the
// equivalent check using dictionary.GetViewBefore(brefDummy, false).Count
// due to all of the range checks during the tree walk to determine the count.
// In this case, LINQ doesn't allocate anything so will always be faster.
int ord = dictionary.Count(@ref => @ref.CompareTo(brefDummy) < 0);
w.Add(ord);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using JCG = J2N.Collections.Generic;

namespace Lucene.Net.Codecs.RAMOnly
Expand Down Expand Up @@ -391,8 +390,7 @@ private void EnsureEnumeratorInitialized() // LUCENENET specific - factored out
}
else
{
//It = RamField.TermToDocs.tailMap(Current).Keys.GetEnumerator();
it = ramField.termToDocs.Where(kvpair => string.CompareOrdinal(kvpair.Key, current) >= 0).Select(pair => pair.Key).GetEnumerator();
it = ramField.termToDocs.GetViewAfter(current).Keys.GetEnumerator();
}
}
}
Expand All @@ -407,7 +405,12 @@ public override SeekStatus SeekCeil(BytesRef term)
}
else
{
if (current.CompareToOrdinal(ramField.termToDocs.Last().Key) > 0)
if (!ramField.termToDocs.TryGetLast(out string lastKey, out _))
// LUCENENET: Java would throw a NoSuchElementException here, so we throw InvalidOperationException
// to indicate that the dictionary is empty when it shouldn't be.
throw new InvalidOperationException("The termToDocs dictionary is empty, but it should have at least one term.");

if (current.CompareToOrdinal(lastKey) > 0)
{
return SeekStatus.END;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,14 @@ private int[] FlushFieldNums()

int numDistinctFields = fieldNums.Count;
if (Debugging.AssertsEnabled) Debugging.Assert(numDistinctFields > 0);
int bitsRequired = PackedInt32s.BitsRequired(fieldNums.Max);
// LUCENENET specific - Java would throw a NoSuchElementException, but in .NET we throw an
// InvalidOperationException instead, since the collection is empty and .NET doesn't throw
// in this case. In practice, this exception should never happen.
if (!fieldNums.TryGetLast(out int last))
{
throw new InvalidOperationException("fieldNums must not be empty");
}
int bitsRequired = PackedInt32s.BitsRequired(last);
int token = (Math.Min(numDistinctFields - 1, 0x07) << 5) | bitsRequired;
vectorsStream.WriteByte((byte)token);
if (numDistinctFields - 1 >= 0x07)
Expand Down
13 changes: 5 additions & 8 deletions src/Lucene.Net/Util/Fst/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ protected virtual void AddIfCompetitive(FSTPath<T> path)

if (queue.Count == maxQueueDepth)
{
FSTPath<T> bottom = queue.Max;
if (!queue.TryGetLast(out FSTPath<T> bottom))
// LUCENENET: Java would throw NoSuchElementException in this case, so we are throwing also.
throw new InvalidOperationException("Expected the queue to contain an element, but it was empty.");
int comp = comparer.Compare(cost, bottom.Cost);
if (comp > 0)
{
Expand Down Expand Up @@ -436,7 +438,7 @@ protected virtual void AddIfCompetitive(FSTPath<T> path)

if (queue.Count == maxQueueDepth + 1)
{
queue.Remove(queue.Max);
queue.RemoveLast(out _);
}
}

Expand Down Expand Up @@ -505,12 +507,7 @@ public virtual TopResults<T> Search()

// Remove top path since we are now going to
// pursue it:
path = queue.Min;
if (path != null)
{
queue.Remove(path);
}
else
if (!queue.RemoveFirst(out path) || path is null)
{
// There were less than topN paths available:
//System.out.println(" break no more paths");
Expand Down
Loading