Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
9 changes: 5 additions & 4 deletions src/Lucene.Net.Tests/Util/Packed/TestPackedInts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1195,16 +1195,17 @@ public virtual void TestAppendingLongBuffer()
Assert.AreEqual(arr[i], buf.Get(i));
}

AbstractAppendingInt64Buffer.Iterator it = buf.GetIterator();
using var it = buf.GetEnumerator();
for (int i = 0; i < arr.Length; ++i)
{
bool hasNext = it.MoveNext();
if (Random.NextBoolean())
{
Assert.IsTrue(it.HasNext);
Assert.IsTrue(hasNext);
}
Assert.AreEqual(arr[i], it.Next());
Assert.AreEqual(arr[i], it.Current);
}
Assert.IsFalse(it.HasNext);
Assert.IsFalse(it.MoveNext());


long[] target = new long[arr.Length + 1024]; // check the request for more is OK.
Expand Down
7 changes: 5 additions & 2 deletions src/Lucene.Net/Index/BinaryDocValuesWriter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Lucene.Net.Diagnostics;
using System;
using System.Collections.Generic;

Expand Down Expand Up @@ -136,7 +137,7 @@ private IEnumerable<BytesRef> GetBytesIterator(int maxDocParam)
{
// Use yield return instead of ucsom IEnumerable
var value = new BytesRef();
AppendingDeltaPackedInt64Buffer.Iterator lengthsIterator = lengths.GetIterator();
using var lengthsEnumerator = lengths.GetEnumerator();
int size = (int)lengths.Count;
DataInput bytesIterator = bytes.GetDataInput();
int maxDoc = maxDocParam;
Expand All @@ -147,7 +148,9 @@ private IEnumerable<BytesRef> GetBytesIterator(int maxDocParam)
BytesRef v = null;
if (upto < size)
{
int length = (int)lengthsIterator.Next();
bool moved = lengthsEnumerator.MoveNext();
if (Debugging.AssertsEnabled) Debugging.Assert(moved);
int length = (int)lengthsEnumerator.Current;
value.Grow(length);
value.Length = length;
try
Expand Down
7 changes: 5 additions & 2 deletions src/Lucene.Net/Index/NumericDocValuesWriter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Lucene.Net.Diagnostics;
using Lucene.Net.Util.Packed;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -102,7 +103,7 @@ public override void Flush(SegmentWriteState state, DocValuesConsumer dvConsumer
private IEnumerable<long?> GetNumericIterator(int maxDoc)
{
// LUCENENET specific: using yield return instead of custom iterator type. Much less code.
AbstractAppendingInt64Buffer.Iterator iter = pending.GetIterator();
using var enumerator = pending.GetEnumerator();
int size = (int)pending.Count;
int upto = 0;

Expand All @@ -111,7 +112,9 @@ public override void Flush(SegmentWriteState state, DocValuesConsumer dvConsumer
long? value;
if (upto < size)
{
var v = iter.Next();
bool moved = enumerator.MoveNext();
if (Debugging.AssertsEnabled) Debugging.Assert(moved);
var v = enumerator.Current;
if (docsWithField is null || docsWithField.Get(upto))
{
value = v;
Expand Down
14 changes: 8 additions & 6 deletions src/Lucene.Net/Index/SortedDocValuesWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,16 @@ public override void Flush(SegmentWriteState state, DocValuesConsumer dvConsumer
ordMap[sortedValues[ord]] = ord;
}

dvConsumer.AddSortedField(fieldInfo, GetBytesRefEnumberable(valueCount, sortedValues),
dvConsumer.AddSortedField(fieldInfo, GetBytesRefEnumerable(valueCount, sortedValues),
// doc -> ord
GetOrdsEnumberable(maxDoc, ordMap));
GetOrdsEnumerable(maxDoc, ordMap));
}

public override void Abort()
{
}

private IEnumerable<BytesRef> GetBytesRefEnumberable(int valueCount, int[] sortedValues)
private IEnumerable<BytesRef> GetBytesRefEnumerable(int valueCount, int[] sortedValues)
{
var scratch = new BytesRef();

Expand All @@ -142,14 +142,16 @@ private IEnumerable<BytesRef> GetBytesRefEnumberable(int valueCount, int[] sorte
}
}

private IEnumerable<long?> GetOrdsEnumberable(int maxDoc, int[] ordMap)
private IEnumerable<long?> GetOrdsEnumerable(int maxDoc, int[] ordMap)
{
AppendingDeltaPackedInt64Buffer.Iterator iter = pending.GetIterator();
using var enumerator = pending.GetEnumerator();
if (Debugging.AssertsEnabled) Debugging.Assert(pending.Count == maxDoc);

for (int i = 0; i < maxDoc; ++i)
{
int ord = (int)iter.Next();
bool moved = enumerator.MoveNext();
if (Debugging.AssertsEnabled) Debugging.Assert(moved);
int ord = (int)enumerator.Current;
yield return ord == -1 ? ord : ordMap[ord];
Comment thread
paulirwin marked this conversation as resolved.
}
}
Expand Down
18 changes: 12 additions & 6 deletions src/Lucene.Net/Index/SortedSetDocValuesWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,22 +202,24 @@ private IEnumerable<BytesRef> GetValuesEnumerable(int valueCount, int[] sortedVa
[SuppressMessage("ReSharper", "AccessToStaticMemberViaDerivedType", Justification = "Matches Lucene")]
private IEnumerable<long?> GetOrdCountEnumerable(int maxDoc)
{
AppendingDeltaPackedInt64Buffer.Iterator iter = pendingCounts.GetIterator();
using var enumerator = pendingCounts.GetEnumerator();

if (Debugging.AssertsEnabled) Debugging.Assert(pendingCounts.Count == maxDoc, "MaxDoc: {0}, pending.Count: {1}", maxDoc, pending.Count);

for (int docUpto = 0; docUpto < maxDoc; ++docUpto)
{
yield return iter.Next();
bool moved = enumerator.MoveNext();
if (Debugging.AssertsEnabled) Debugging.Assert(moved);
yield return enumerator.Current;
}
Comment thread
paulirwin marked this conversation as resolved.
}

[SuppressMessage("ReSharper", "AccessToStaticMemberViaDerivedType", Justification = "Matches Lucene")]
private IEnumerable<long?> GetOrdsEnumerable(int[] ordMap, int maxCountPerDoc)
{
int currentUpTo = 0, currentLength = 0;
AppendingPackedInt64Buffer.Iterator iter = pending.GetIterator();
AppendingDeltaPackedInt64Buffer.Iterator counts = pendingCounts.GetIterator();
using var enumerator = pending.GetEnumerator();
using var counts = pendingCounts.GetEnumerator();
int[] cd = new int[maxCountPerDoc]; // LUCENENET specific - renamed from currentDoc to cd to prevent conflict

for (long ordUpto = 0; ordUpto < pending.Count; ++ordUpto)
Expand All @@ -226,10 +228,14 @@ private IEnumerable<BytesRef> GetValuesEnumerable(int valueCount, int[] sortedVa
{
// refill next doc, and sort remapped ords within the doc.
currentUpTo = 0;
currentLength = (int)counts.Next();
bool countsMoved = counts.MoveNext();
if (Debugging.AssertsEnabled) Debugging.Assert(countsMoved);
currentLength = (int)counts.Current;
for (int j = 0; j < currentLength; j++)
Comment thread
paulirwin marked this conversation as resolved.
{
cd[j] = ordMap[(int)iter.Next()];
bool moved = enumerator.MoveNext();
if (Debugging.AssertsEnabled) Debugging.Assert(moved);
cd[j] = ordMap[(int)enumerator.Current];
}
Comment thread
paulirwin marked this conversation as resolved.
Array.Sort(cd, 0, currentLength);
}
Expand Down
80 changes: 56 additions & 24 deletions src/Lucene.Net/Util/Packed/AbstractAppendingLongBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Lucene.Net.Diagnostics;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace Lucene.Net.Util.Packed
Expand All @@ -26,7 +28,8 @@ namespace Lucene.Net.Util.Packed
/// <para/>
/// NOTE: This was AbstractAppendingLongBuffer in Lucene
/// </summary>
public abstract class AbstractAppendingInt64Buffer : Int64Values // LUCENENET NOTE: made public rather than internal because has public subclasses
public abstract class AbstractAppendingInt64Buffer : Int64Values, // LUCENENET NOTE: made public rather than internal because has public subclasses
IEnumerable<long> // LUCENENET specific
{
internal const int MIN_PAGE_SIZE = 64;

Expand Down Expand Up @@ -105,7 +108,7 @@ public void Add(long l)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal virtual void Grow(int newBlockCount)
{
Array.Resize<PackedInt32s.Reader>(ref values, newBlockCount);
Array.Resize(ref values, newBlockCount);
}

internal abstract void PackPendingValues();
Expand Down Expand Up @@ -144,33 +147,27 @@ public int Get(long index, long[] arr, int off, int len)
/// <summary>
/// Return an iterator over the values of this buffer.
/// </summary>
public virtual Iterator GetIterator()
public virtual Enumerator GetEnumerator()
{
return new Iterator(this);
return new Enumerator(this);
}

public sealed class Iterator
IEnumerator<long> IEnumerable<long>.GetEnumerator() => GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

public sealed class Enumerator : IEnumerator<long>
{
Comment thread
paulirwin marked this conversation as resolved.
private readonly AbstractAppendingInt64Buffer outerInstance;

internal long[] currentValues;
internal int vOff, pOff;
internal int currentCount; // number of entries of the current page

internal Iterator(AbstractAppendingInt64Buffer outerInstance)
internal Enumerator(AbstractAppendingInt64Buffer outerInstance)
{
this.outerInstance = outerInstance;
vOff = pOff = 0;
if (outerInstance.valuesOff == 0)
{
currentValues = outerInstance.pending;
currentCount = outerInstance.pendingOff;
}
else
{
currentValues = new long[outerInstance.values[0].Count];
FillValues();
}
Reset(); // LUCENENET specific - moved from ctor
}

internal void FillValues()
Expand All @@ -191,15 +188,24 @@ internal void FillValues()
}

/// <summary>
/// Whether or not there are remaining values. </summary>
public bool HasNext => pOff < currentCount;
/// Gets the current value.
/// </summary>
public long Current { get; private set; }

object IEnumerator.Current => Current;

/// <summary>
/// Return the next long in the buffer. </summary>
public long Next()
/// Advances the enumerator to the next value.
/// </summary>
public bool MoveNext()
{
if (Debugging.AssertsEnabled) Debugging.Assert(HasNext);
long result = currentValues[pOff++];
if (pOff >= currentCount)
{
return false;
}

Current = currentValues[pOff++];

if (pOff == currentCount)
{
vOff += 1;
Expand All @@ -213,7 +219,33 @@ public long Next()
currentCount = 0;
}
}
return result;

return true;
}

/// <summary>
/// Resets the enumerator to its initial position.
/// </summary>
public void Reset()
{
vOff = pOff = 0;
if (outerInstance.valuesOff == 0)
{
currentValues = outerInstance.pending;
currentCount = outerInstance.pendingOff;
}
else
{
currentValues = new long[outerInstance.values[0].Count];
FillValues();
}
}

/// <summary>
/// Dispose of the resources used by the <see cref="Enumerator"/>.
/// </summary>
public void Dispose()
{
}
}

Expand Down
Loading