fix bytesref problems in readBytes, just take byte[],int,int

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene4547@1441543 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2013-02-01 17:21:03 +00:00
parent 040ca47778
commit 00f66bf975
4 changed files with 18 additions and 14 deletions

View File

@ -112,7 +112,9 @@ class BinaryDocValuesWriter extends DocValuesWriter {
} }
if (upto < size) { if (upto < size) {
int length = (int) lengthsIterator.next(); int length = (int) lengthsIterator.next();
pool.readBytes(value, byteOffset, length); value.grow(length);
value.length = length;
pool.readBytes(byteOffset, value.bytes, value.offset, value.length);
byteOffset += length; byteOffset += length;
} else { } else {
// This is to handle last N documents not having // This is to handle last N documents not having

View File

@ -327,32 +327,29 @@ public final class ByteBlockPool {
/** /**
* Reads bytes bytes out of the pool starting at the given offset with the given * Reads bytes bytes out of the pool starting at the given offset with the given
* length into the given {@link BytesRef} at offset <tt>0</tt>. * length into the given byte array at offset <tt>off</tt>.
* <p>Note: this method allows to copy across block boundaries.</p> * <p>Note: this method allows to copy across block boundaries.</p>
*/ */
public void readBytes(final BytesRef bytes, final long offset, final int length) { public void readBytes(final long offset, final byte bytes[], final int off, final int length) {
bytes.offset = 0;
bytes.grow(length);
bytes.length = length;
if (length == 0) { if (length == 0) {
return; return;
} }
int bytesOffset = off;
int bytesLength = length;
int bufferIndex = (int) (offset >> BYTE_BLOCK_SHIFT); int bufferIndex = (int) (offset >> BYTE_BLOCK_SHIFT);
byte[] buffer = buffers[bufferIndex]; byte[] buffer = buffers[bufferIndex];
int pos = (int) (offset & BYTE_BLOCK_MASK); int pos = (int) (offset & BYTE_BLOCK_MASK);
int overflow = (pos + length) - BYTE_BLOCK_SIZE; int overflow = (pos + length) - BYTE_BLOCK_SIZE;
do { do {
if (overflow <= 0) { if (overflow <= 0) {
System.arraycopy(buffer, pos, bytes.bytes, bytes.offset, bytes.length); System.arraycopy(buffer, pos, bytes, bytesOffset, bytesLength);
bytes.length = length;
bytes.offset = 0;
break; break;
} else { } else {
final int bytesToCopy = length - overflow; final int bytesToCopy = length - overflow;
System.arraycopy(buffer, pos, bytes.bytes, bytes.offset, bytesToCopy); System.arraycopy(buffer, pos, bytes, bytesOffset, bytesToCopy);
pos = 0; pos = 0;
bytes.length -= bytesToCopy; bytesLength -= bytesToCopy;
bytes.offset += bytesToCopy; bytesOffset += bytesToCopy;
buffer = buffers[++bufferIndex]; buffer = buffers[++bufferIndex];
overflow = overflow - BYTE_BLOCK_SIZE; overflow = overflow - BYTE_BLOCK_SIZE;
} }

View File

@ -43,7 +43,9 @@ public class TestByteBlockPool extends LuceneTestCase {
// verify // verify
long position = 0; long position = 0;
for (BytesRef expected : list) { for (BytesRef expected : list) {
pool.readBytes(ref, position, expected.length); ref.grow(expected.length);
ref.length = expected.length;
pool.readBytes(position, ref.bytes, ref.offset, ref.length);
assertEquals(expected, ref); assertEquals(expected, ref);
position += ref.length; position += ref.length;
} }

View File

@ -104,7 +104,10 @@ public final class BytesRefArray {
int offset = offsets[ord]; int offset = offsets[ord];
int length = ord == lastElement - 1 ? currentOffset - offset int length = ord == lastElement - 1 ? currentOffset - offset
: offsets[ord + 1] - offset; : offsets[ord + 1] - offset;
pool.readBytes(spare, offset, length); assert spare.offset == 0;
spare.grow(length);
spare.length = length;
pool.readBytes(offset, spare.bytes, spare.offset, spare.length);
return spare; return spare;
} }
throw new IndexOutOfBoundsException("index " + ord throw new IndexOutOfBoundsException("index " + ord