Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ API Changes
* GITHUB#12592: Add RandomAccessInput#length method to the RandomAccessInput interface. In addition deprecate
ByteBuffersDataInput#size in favour of this new method. (Ignacio Vera)

* GITHUB#12599: Add RandomAccessInput#readBytes method to the RandomAccessInput interface. (Ignacio Vera)

New Features
---------------------
(No changes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,27 @@ public final byte readByte(long pos) throws IOException {
return buffer.get((int) index);
}

@Override
public void readBytes(long pos, byte[] bytes, int offset, int len) throws IOException {
if (len <= bufferSize) {
// the buffer is big enough to satisfy this request
if (len > 0) { // to allow b to be null if len is 0...
long index = resolvePositionInBuffer(pos, len);
buffer.get((int) index, bytes, offset, len);
}
} else {
while (len > bufferSize) {
long index = resolvePositionInBuffer(pos, bufferSize);
buffer.get((int) index, bytes, offset, bufferSize);
len -= bufferSize;
offset += bufferSize;
pos += bufferSize;
}
long index = resolvePositionInBuffer(pos, len);
buffer.get((int) index, bytes, offset, len);
}
}

@Override
public final short readShort(long pos) throws IOException {
long index = resolvePositionInBuffer(pos, Short.BYTES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ private void ensureValid() {
}
}

public void getBytes(ByteBuffer receiver, int pos, byte[] dst, int offset, int length) {
ensureValid();
receiver.get(pos, dst, offset, length);
}

public void getBytes(ByteBuffer receiver, byte[] dst, int offset, int length) {
ensureValid();
receiver.get(dst, offset, length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,29 @@ private void setPos(long pos, int bi) throws IOException {
}
}

@Override
public void readBytes(long pos, byte[] bytes, int offset, int len) throws IOException {
int bi = (int) (pos >> chunkSizePower);
int bufferPos = (int) (pos & chunkSizeMask);
try {
int curAvail = Math.min(buffers[bi].capacity() - bufferPos, len);
while (len > curAvail) {
guard.getBytes(buffers[bi], bufferPos, bytes, offset, curAvail);
len -= curAvail;
offset += curAvail;
bi++;
if (bi >= buffers.length) {
throw new EOFException("read past EOF: " + this);
}
bufferPos = 0;
curAvail = Math.min(len, buffers[bi].capacity());
}
guard.getBytes(buffers[bi], bufferPos, bytes, offset, curAvail);
} catch (NullPointerException e) {
throw alreadyClosed(e);
}
}

@Override
public short readShort(long pos) throws IOException {
final int bi = (int) (pos >> chunkSizePower);
Expand Down Expand Up @@ -569,6 +592,17 @@ public byte readByte(long pos) throws IOException {
}
}

@Override
public void readBytes(long pos, byte[] bytes, int offset, int len) throws IOException {
try {
guard.getBytes(curBuf, (int) pos, bytes, offset, len);
} catch (IllegalArgumentException e) {
throw handlePositionalIOOBE(e, "read", pos);
} catch (NullPointerException e) {
throw alreadyClosed(e);
}
}

@Override
public short readShort(long pos) throws IOException {
try {
Expand Down Expand Up @@ -645,6 +679,11 @@ public byte readByte(long pos) throws IOException {
return super.readByte(pos + offset);
}

@Override
public void readBytes(long pos, byte[] bytes, int offset, int len) throws IOException {
super.readBytes(pos + this.offset, bytes, offset, len);
}

@Override
public short readShort(long pos) throws IOException {
return super.readShort(pos + offset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,34 @@ public byte readByte(long pos) {
return blocks[blockIndex(pos)].get(blockOffset(pos));
}

@Override
public void readBytes(long pos, byte[] bytes, int offset, int len) throws IOException {
long absPos = this.offset + pos;
try {
while (len > 0) {
ByteBuffer block = blocks[blockIndex(absPos)];
int blockPosition = blockOffset(absPos);
int chunk = Math.min(len, block.capacity() - blockPosition);
if (chunk == 0) {
throw new EOFException();
}

// Update pos early on for EOF detection, then try to get buffer content.
block.get(blockPosition, bytes, offset, chunk);

absPos += chunk;
len -= chunk;
offset += chunk;
}
} catch (BufferUnderflowException | ArrayIndexOutOfBoundsException e) {
if (absPos >= length()) {
throw new EOFException();
} else {
throw e; // Something is wrong.
}
}
}

@Override
public short readShort(long pos) {
long absPos = offset + pos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ public byte readByte(long pos) throws IOException {
return in.readByte(pos);
}

@Override
public void readBytes(long pos, byte[] bytes, int offset, int length) throws IOException {
ensureOpen();
in.readBytes(pos, bytes, offset, length);
}

@Override
public short readShort(long pos) throws IOException {
ensureOpen();
Expand Down
6 changes: 6 additions & 0 deletions lucene/core/src/java/org/apache/lucene/store/IndexInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ public byte readByte(long pos) throws IOException {
return slice.readByte();
}

@Override
public void readBytes(long pos, byte[] bytes, int offset, int length) throws IOException {
slice.seek(pos);
slice.readBytes(bytes, offset, length);
}

@Override
public short readShort(long pos) throws IOException {
slice.seek(pos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ public interface RandomAccessInput {
* @see DataInput#readByte
*/
public byte readByte(long pos) throws IOException;

/**
* Reads a specified number of bytes starting at a given position into an array at the specified
* offset.
*
* @see DataInput#readBytes
*/
default void readBytes(long pos, byte[] bytes, int offset, int length) throws IOException {
for (int i = 0; i < length; i++) {
bytes[offset + i] = readByte(pos + i);
}
}

/**
* Reads a short (LE byte order) at the given position in the file
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,31 @@ public byte readByte(long pos) throws IOException {
}
}

@Override
public void readBytes(long pos, byte[] b, int offset, int len) throws IOException {
try {
int si = (int) (pos >> chunkSizePower);
pos = pos & chunkSizeMask;
long curAvail = segments[si].byteSize() - pos;
while (len > curAvail) {
MemorySegment.copy(segments[si], LAYOUT_BYTE, pos, b, offset, (int) curAvail);
len -= curAvail;
offset += curAvail;
si++;
if (si >= segments.length) {
throw new EOFException("read past EOF: " + this);
}
pos = 0L;
curAvail = segments[si].byteSize();
}
MemorySegment.copy(segments[si], LAYOUT_BYTE, pos, b, offset, len);
} catch (IndexOutOfBoundsException ioobe) {
throw handlePositionalIOOBE(ioobe, "read", pos);
} catch (NullPointerException | IllegalStateException e) {
throw alreadyClosed(e);
}
}

// used only by random access methods to handle reads across boundaries
private void setPos(long pos, int si) throws IOException {
try {
Expand Down Expand Up @@ -490,6 +515,17 @@ public byte readByte(long pos) throws IOException {
}
}

@Override
public void readBytes(long pos, byte[] bytes, int offset, int length) throws IOException {
try {
MemorySegment.copy(curSegment, LAYOUT_BYTE, pos, bytes, offset, length);
} catch (IndexOutOfBoundsException e) {
throw handlePositionalIOOBE(e, "read", pos);
} catch (NullPointerException | IllegalStateException e) {
throw alreadyClosed(e);
}
}

@Override
public short readShort(long pos) throws IOException {
try {
Expand Down Expand Up @@ -567,6 +603,11 @@ public byte readByte(long pos) throws IOException {
return super.readByte(pos + offset);
}

@Override
public void readBytes(long pos, byte[] bytes, int offset, int length) throws IOException {
super.readBytes(pos + this.offset, bytes, offset, length);
}

@Override
public short readShort(long pos) throws IOException {
return super.readShort(pos + offset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,31 @@ public byte readByte(long pos) throws IOException {
}
}

@Override
public void readBytes(long pos, byte[] b, int offset, int len) throws IOException {
try {
int si = (int) (pos >> chunkSizePower);
pos = pos & chunkSizeMask;
long curAvail = segments[si].byteSize() - pos;
while (len > curAvail) {
MemorySegment.copy(segments[si], LAYOUT_BYTE, pos, b, offset, (int) curAvail);
len -= curAvail;
offset += curAvail;
si++;
if (si >= segments.length) {
throw new EOFException("read past EOF: " + this);
}
pos = 0L;
curAvail = segments[si].byteSize();
}
MemorySegment.copy(segments[si], LAYOUT_BYTE, pos, b, offset, len);
} catch (IndexOutOfBoundsException ioobe) {
throw handlePositionalIOOBE(ioobe, "read", pos);
} catch (NullPointerException | IllegalStateException e) {
throw alreadyClosed(e);
}
}

// used only by random access methods to handle reads across boundaries
private void setPos(long pos, int si) throws IOException {
try {
Expand Down Expand Up @@ -488,6 +513,17 @@ public byte readByte(long pos) throws IOException {
}
}

@Override
public void readBytes(long pos, byte[] bytes, int offset, int length) throws IOException {
try {
MemorySegment.copy(curSegment, LAYOUT_BYTE, pos, bytes, offset, length);
} catch (IndexOutOfBoundsException e) {
throw handlePositionalIOOBE(e, "read", pos);
} catch (NullPointerException | IllegalStateException e) {
throw alreadyClosed(e);
}
}

@Override
public short readShort(long pos) throws IOException {
try {
Expand Down Expand Up @@ -565,6 +601,11 @@ public byte readByte(long pos) throws IOException {
return super.readByte(pos + offset);
}

@Override
public void readBytes(long pos, byte[] bytes, int offset, int length) throws IOException {
super.readBytes(pos + this.offset, bytes, offset, length);
}

@Override
public short readShort(long pos) throws IOException {
return super.readShort(pos + offset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,31 @@ public byte readByte(long pos) throws IOException {
}
}

@Override
public void readBytes(long pos, byte[] b, int offset, int len) throws IOException {
try {
int si = (int) (pos >> chunkSizePower);
pos = pos & chunkSizeMask;
long curAvail = segments[si].byteSize() - pos;
while (len > curAvail) {
MemorySegment.copy(segments[si], LAYOUT_BYTE, pos, b, offset, (int) curAvail);
len -= curAvail;
offset += curAvail;
si++;
if (si >= segments.length) {
throw new EOFException("read past EOF: " + this);
}
pos = 0L;
curAvail = segments[si].byteSize();
}
MemorySegment.copy(segments[si], LAYOUT_BYTE, pos, b, offset, len);
} catch (IndexOutOfBoundsException ioobe) {
throw handlePositionalIOOBE(ioobe, "read", pos);
} catch (NullPointerException | IllegalStateException e) {
throw alreadyClosed(e);
}
}

// used only by random access methods to handle reads across boundaries
private void setPos(long pos, int si) throws IOException {
try {
Expand Down Expand Up @@ -488,6 +513,17 @@ public byte readByte(long pos) throws IOException {
}
}

@Override
public void readBytes(long pos, byte[] bytes, int offset, int length) throws IOException {
try {
MemorySegment.copy(curSegment, LAYOUT_BYTE, pos, bytes, offset, length);
} catch (IndexOutOfBoundsException e) {
throw handlePositionalIOOBE(e, "read", pos);
} catch (NullPointerException | IllegalStateException e) {
throw alreadyClosed(e);
}
}

@Override
public short readShort(long pos) throws IOException {
try {
Expand Down Expand Up @@ -565,6 +601,11 @@ public byte readByte(long pos) throws IOException {
return super.readByte(pos + offset);
}

@Override
public void readBytes(long pos, byte[] bytes, int offset, int length) throws IOException {
super.readBytes(pos + this.offset, bytes, offset, length);
}

@Override
public short readShort(long pos) throws IOException {
return super.readShort(pos + offset);
Expand Down
Loading