Skip to content

Commit

Permalink
Fix random access reads from ByteArrayIndexInput (elastic#107885)
Browse files Browse the repository at this point in the history
Fix random access read behavior for ByteArrayIndexInput and enhance our
generic test to test random access reads a little if it's a random
access input. Also, this adjustment to how `pos` works should maybe come
with a mild performance gain.
  • Loading branch information
original-brownbear authored Apr 25, 2024
1 parent 6dfb78f commit b331e8f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public ByteArrayIndexInput(String resourceDesc, byte[] bytes, int offset, int le
super(resourceDesc);
this.bytes = bytes;
this.offset = offset;
this.pos = offset;
this.length = length;
}

Expand All @@ -43,7 +44,7 @@ public void close() throws IOException {}

@Override
public long getFilePointer() {
return pos;
return pos - offset;
}

@Override
Expand All @@ -57,7 +58,7 @@ private int position(long p) throws EOFException {
} else if (p > length) {
throw new EOFException("seek past EOF");
}
return (int) p;
return (int) p + offset;
}

@Override
Expand Down Expand Up @@ -108,22 +109,22 @@ public byte readByte() throws IOException {
if (pos >= offset + length) {
throw new EOFException("seek past EOF");
}
return bytes[offset + pos++];
return bytes[pos++];
}

@Override
public void readBytes(final byte[] b, final int offset, int len) throws IOException {
if (pos + len > this.offset + length) {
throw new EOFException("seek past EOF");
}
System.arraycopy(bytes, this.offset + pos, b, offset, len);
System.arraycopy(bytes, pos, b, offset, len);
pos += len;
}

@Override
public short readShort() throws IOException {
try {
return (short) BitUtil.VH_LE_SHORT.get(bytes, pos + offset);
return (short) BitUtil.VH_LE_SHORT.get(bytes, pos);
} finally {
pos += Short.BYTES;
}
Expand All @@ -132,7 +133,7 @@ public short readShort() throws IOException {
@Override
public int readInt() throws IOException {
try {
return (int) BitUtil.VH_LE_INT.get(bytes, pos + offset);
return (int) BitUtil.VH_LE_INT.get(bytes, pos);
} finally {
pos += Integer.BYTES;
}
Expand All @@ -141,7 +142,7 @@ public int readInt() throws IOException {
@Override
public long readLong() throws IOException {
try {
return (long) BitUtil.VH_LE_LONG.get(bytes, pos + offset);
return (long) BitUtil.VH_LE_LONG.get(bytes, pos);
} finally {
pos += Long.BYTES;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package org.elasticsearch.common.lucene.store;

import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.RandomAccessInput;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.PlainActionFuture;
import org.elasticsearch.common.settings.Settings;
Expand Down Expand Up @@ -73,16 +74,36 @@ protected byte[] randomReadAndSlice(IndexInput indexInput, int length) throws IO
switch (readStrategy) {
case 0, 1, 2, 3:
if (length - readPos >= Long.BYTES && readStrategy <= 0) {
ByteBuffer.wrap(output, readPos, Long.BYTES).order(ByteOrder.LITTLE_ENDIAN).putLong(indexInput.readLong());
long read = indexInput.readLong();
ByteBuffer.wrap(output, readPos, Long.BYTES).order(ByteOrder.LITTLE_ENDIAN).putLong(read);
readPos += Long.BYTES;
if (indexInput instanceof RandomAccessInput randomAccessInput) {
assertEquals(read, randomAccessInput.readLong(indexInput.getFilePointer() - Long.BYTES));
indexInput.seek(readPos);
}
} else if (length - readPos >= Integer.BYTES && readStrategy <= 1) {
ByteBuffer.wrap(output, readPos, Integer.BYTES).order(ByteOrder.LITTLE_ENDIAN).putInt(indexInput.readInt());
int read = indexInput.readInt();
ByteBuffer.wrap(output, readPos, Integer.BYTES).order(ByteOrder.LITTLE_ENDIAN).putInt(read);
readPos += Integer.BYTES;
if (indexInput instanceof RandomAccessInput randomAccessInput) {
assertEquals(read, randomAccessInput.readInt(indexInput.getFilePointer() - Integer.BYTES));
indexInput.seek(readPos);
}
} else if (length - readPos >= Short.BYTES && readStrategy <= 2) {
ByteBuffer.wrap(output, readPos, Short.BYTES).order(ByteOrder.LITTLE_ENDIAN).putShort(indexInput.readShort());
short read = indexInput.readShort();
ByteBuffer.wrap(output, readPos, Short.BYTES).order(ByteOrder.LITTLE_ENDIAN).putShort(read);
readPos += Short.BYTES;
if (indexInput instanceof RandomAccessInput randomAccessInput) {
assertEquals(read, randomAccessInput.readShort(indexInput.getFilePointer() - Short.BYTES));
indexInput.seek(readPos);
}
} else {
output[readPos++] = indexInput.readByte();
byte read = indexInput.readByte();
output[readPos++] = read;
if (indexInput instanceof RandomAccessInput randomAccessInput) {
assertEquals(read, randomAccessInput.readByte(indexInput.getFilePointer() - 1));
indexInput.seek(readPos);
}
}
break;
case 4:
Expand Down

0 comments on commit b331e8f

Please sign in to comment.