Skip to content

Commit d1f6fef

Browse files
Fix random access reads from ByteArrayIndexInput
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.
1 parent b687ef7 commit d1f6fef

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

server/src/main/java/org/elasticsearch/common/lucene/store/ByteArrayIndexInput.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public ByteArrayIndexInput(String resourceDesc, byte[] bytes, int offset, int le
3535
super(resourceDesc);
3636
this.bytes = bytes;
3737
this.offset = offset;
38+
this.pos = offset;
3839
this.length = length;
3940
}
4041

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

4445
@Override
4546
public long getFilePointer() {
46-
return pos;
47+
return pos - offset;
4748
}
4849

4950
@Override
@@ -57,7 +58,7 @@ private int position(long p) throws EOFException {
5758
} else if (p > length) {
5859
throw new EOFException("seek past EOF");
5960
}
60-
return (int) p;
61+
return (int) p + offset;
6162
}
6263

6364
@Override
@@ -108,22 +109,22 @@ public byte readByte() throws IOException {
108109
if (pos >= offset + length) {
109110
throw new EOFException("seek past EOF");
110111
}
111-
return bytes[offset + pos++];
112+
return bytes[pos++];
112113
}
113114

114115
@Override
115116
public void readBytes(final byte[] b, final int offset, int len) throws IOException {
116117
if (pos + len > this.offset + length) {
117118
throw new EOFException("seek past EOF");
118119
}
119-
System.arraycopy(bytes, this.offset + pos, b, offset, len);
120+
System.arraycopy(bytes, pos, b, offset, len);
120121
pos += len;
121122
}
122123

123124
@Override
124125
public short readShort() throws IOException {
125126
try {
126-
return (short) BitUtil.VH_LE_SHORT.get(bytes, pos + offset);
127+
return (short) BitUtil.VH_LE_SHORT.get(bytes, pos);
127128
} finally {
128129
pos += Short.BYTES;
129130
}
@@ -132,7 +133,7 @@ public short readShort() throws IOException {
132133
@Override
133134
public int readInt() throws IOException {
134135
try {
135-
return (int) BitUtil.VH_LE_INT.get(bytes, pos + offset);
136+
return (int) BitUtil.VH_LE_INT.get(bytes, pos);
136137
} finally {
137138
pos += Integer.BYTES;
138139
}
@@ -141,7 +142,7 @@ public int readInt() throws IOException {
141142
@Override
142143
public long readLong() throws IOException {
143144
try {
144-
return (long) BitUtil.VH_LE_LONG.get(bytes, pos + offset);
145+
return (long) BitUtil.VH_LE_LONG.get(bytes, pos);
145146
} finally {
146147
pos += Long.BYTES;
147148
}

test/framework/src/main/java/org/elasticsearch/common/lucene/store/ESIndexInputTestCase.java

+21-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package org.elasticsearch.common.lucene.store;
99

1010
import org.apache.lucene.store.IndexInput;
11+
import org.apache.lucene.store.RandomAccessInput;
1112
import org.elasticsearch.action.ActionListener;
1213
import org.elasticsearch.action.support.PlainActionFuture;
1314
import org.elasticsearch.common.settings.Settings;
@@ -73,16 +74,32 @@ protected byte[] randomReadAndSlice(IndexInput indexInput, int length) throws IO
7374
switch (readStrategy) {
7475
case 0, 1, 2, 3:
7576
if (length - readPos >= Long.BYTES && readStrategy <= 0) {
76-
ByteBuffer.wrap(output, readPos, Long.BYTES).order(ByteOrder.LITTLE_ENDIAN).putLong(indexInput.readLong());
77+
long read = indexInput.readLong();
78+
ByteBuffer.wrap(output, readPos, Long.BYTES).order(ByteOrder.LITTLE_ENDIAN).putLong(read);
7779
readPos += Long.BYTES;
80+
if (indexInput instanceof RandomAccessInput randomAccessInput) {
81+
assertEquals(read, randomAccessInput.readLong(indexInput.getFilePointer() - Long.BYTES));
82+
}
7883
} else if (length - readPos >= Integer.BYTES && readStrategy <= 1) {
79-
ByteBuffer.wrap(output, readPos, Integer.BYTES).order(ByteOrder.LITTLE_ENDIAN).putInt(indexInput.readInt());
84+
int read = indexInput.readInt();
85+
ByteBuffer.wrap(output, readPos, Integer.BYTES).order(ByteOrder.LITTLE_ENDIAN).putInt(read);
8086
readPos += Integer.BYTES;
87+
if (indexInput instanceof RandomAccessInput randomAccessInput) {
88+
assertEquals(read, randomAccessInput.readInt(indexInput.getFilePointer() - Integer.BYTES));
89+
}
8190
} else if (length - readPos >= Short.BYTES && readStrategy <= 2) {
82-
ByteBuffer.wrap(output, readPos, Short.BYTES).order(ByteOrder.LITTLE_ENDIAN).putShort(indexInput.readShort());
91+
short read = indexInput.readShort();
92+
ByteBuffer.wrap(output, readPos, Short.BYTES).order(ByteOrder.LITTLE_ENDIAN).putShort(read);
8393
readPos += Short.BYTES;
94+
if (indexInput instanceof RandomAccessInput randomAccessInput) {
95+
assertEquals(read, randomAccessInput.readShort(indexInput.getFilePointer() - Short.BYTES));
96+
}
8497
} else {
85-
output[readPos++] = indexInput.readByte();
98+
byte read = indexInput.readByte();
99+
output[readPos++] = read;
100+
if (indexInput instanceof RandomAccessInput randomAccessInput) {
101+
assertEquals(read, randomAccessInput.readByte(indexInput.getFilePointer() - 1));
102+
}
86103
}
87104
break;
88105
case 4:

0 commit comments

Comments
 (0)