LUCENE-4865: BytesRefArray#append returns wrong index

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1459051 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Willnauer 2013-03-20 21:01:48 +00:00
parent affb12e11b
commit 734c426fa7
2 changed files with 21 additions and 19 deletions

View File

@ -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 {
@Override
protected 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));
}
@Override
protected void setPivot(int i) {
final int ord = orderedEntries[i];
get(pivot, ord);
final int index = orderedEntries[i];
get(pivot, index);
}
@Override
protected 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;
@Override
public BytesRef next() {
if (pos < size) {
return get(spare, ords == null ? pos++ : ords[pos++]);
return get(spare, indices == null ? pos++ : indices[pos++]);
}
return null;
}

View File

@ -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);
}