Apache Lucene open-source search software
LUCENE-4865: BytesRefArray#append returns wrong index
Simon Willnauer734c426fa711
diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/BytesRefArray.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/BytesRefArray.javaindex 4161d71b957..98f5291e9cc 100644--- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/BytesRefArray.java+++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/BytesRefArray.java@@ -70,7 +70,7 @@ public final class BytesRefArray {/*** Appends a copy of the given {@link BytesRef} to this {@link BytesRefArray}.* @param bytes the bytes to append- * @return the ordinal of the appended bytes+ * @return the index of the appended bytes*/public int append(BytesRef bytes) {if (lastElement >= offsets.length) {@@ -82,7 +82,7 @@ public final class BytesRefArray {pool.append(bytes);offsets[lastElement++] = currentOffset;currentOffset += bytes.length;- return lastElement;+ return lastElement-1;}/**@@ -96,21 +96,21 @@ public final class BytesRefArray {/*** Returns the <i>n'th</i> element of this {@link BytesRefArray}* @param spare a spare {@link BytesRef} instance- * @param ord the elements ordinal to retrieve+ * @param index the elements index to retrieve* @return the <i>n'th</i> element of this {@link BytesRefArray}*/- public BytesRef get(BytesRef spare, int ord) {- if (lastElement > ord) {- int offset = offsets[ord];- int length = ord == lastElement - 1 ? currentOffset - offset- : offsets[ord + 1] - offset;+ public BytesRef get(BytesRef spare, int index) {+ if (lastElement > index) {+ int offset = offsets[index];+ int length = index == lastElement - 1 ? currentOffset - offset+ : offsets[index + 1] - offset;assert spare.offset == 0;spare.grow(length);spare.length = length;pool.readBytes(offset, spare.bytes, spare.offset, spare.length);return spare;}- throw new IndexOutOfBoundsException("index " + ord+ throw new IndexOutOfBoundsException("index " + index+ " must be less than the size: " + lastElement);}@@ -130,20 +130,20 @@ public final class BytesRefArray {@Overrideprotected int compare(int i, int j) {- final int ord1 = orderedEntries[i], ord2 = orderedEntries[j];- return comp.compare(get(scratch1, ord1), get(scratch2, ord2));+ final int idx1 = orderedEntries[i], idx2 = orderedEntries[j];+ return comp.compare(get(scratch1, idx1), get(scratch2, idx2));}@Overrideprotected void setPivot(int i) {- final int ord = orderedEntries[i];- get(pivot, ord);+ final int index = orderedEntries[i];+ get(pivot, index);}@Overrideprotected int comparePivot(int j) {- final int ord = orderedEntries[j];- return comp.compare(pivot, get(scratch2, ord));+ final int index = orderedEntries[j];+ return comp.compare(pivot, get(scratch2, index));}private final BytesRef pivot = new BytesRef(), scratch1 = new BytesRef(),@@ -176,14 +176,14 @@ public final class BytesRefArray {public BytesRefIterator iterator(final Comparator<BytesRef> comp) {final BytesRef spare = new BytesRef();final int size = size();- final int[] ords = comp == null ? null : sort(comp);+ final int[] indices = comp == null ? null : sort(comp);return new BytesRefIterator() {int pos = 0;@Overridepublic BytesRef next() {if (pos < size) {- return get(spare, ords == null ? pos++ : ords[pos++]);+ return get(spare, indices == null ? pos++ : indices[pos++]);}return null;}diff --git a/lucene/suggest/src/test/org/apache/lucene/search/suggest/TestBytesRefArray.java b/lucene/suggest/src/test/org/apache/lucene/search/suggest/TestBytesRefArray.javaindex 90cae6fe218..49bc12d9581 100644--- a/lucene/suggest/src/test/org/apache/lucene/search/suggest/TestBytesRefArray.java+++ b/lucene/suggest/src/test/org/apache/lucene/search/suggest/TestBytesRefArray.java@@ -39,11 +39,12 @@ public class TestBytesRefArray extends LuceneTestCase {}int entries = atLeast(500);BytesRef spare = new BytesRef();+ int initSize = list.size();for (int i = 0; i < entries; i++) {String randomRealisticUnicodeString = _TestUtil.randomRealisticUnicodeString(random);spare.copyChars(randomRealisticUnicodeString);- list.append(spare);+ assertEquals(i+initSize, list.append(spare));stringList.add(randomRealisticUnicodeString);}for (int i = 0; i < entries; i++) {@@ -81,11 +82,12 @@ public class TestBytesRefArray extends LuceneTestCase {}int entries = atLeast(500);BytesRef spare = new BytesRef();+ final int initSize = list.size();for (int i = 0; i < entries; i++) {String randomRealisticUnicodeString = _TestUtil.randomRealisticUnicodeString(random);spare.copyChars(randomRealisticUnicodeString);- list.append(spare);+ assertEquals(initSize + i, list.append(spare));stringList.add(randomRealisticUnicodeString);}