2022-02-26 22:51:22 +01:00
|
|
|
package it.cavallium.dbengine.lucene.directory;
|
|
|
|
|
2022-03-09 02:29:38 +01:00
|
|
|
import io.net5.buffer.api.Buffer;
|
|
|
|
import io.net5.buffer.api.BufferAllocator;
|
2022-02-26 22:51:22 +01:00
|
|
|
import java.io.EOFException;
|
|
|
|
import java.io.IOException;
|
2022-02-28 03:20:24 +01:00
|
|
|
import org.apache.lucene.store.IndexInput;
|
2022-02-26 22:51:22 +01:00
|
|
|
|
|
|
|
public class RocksdbInputStream extends IndexInput {
|
|
|
|
|
|
|
|
private final int bufferSize;
|
|
|
|
|
|
|
|
private long position;
|
|
|
|
|
|
|
|
private final long length;
|
|
|
|
|
2022-03-09 02:29:38 +01:00
|
|
|
private Buffer currentBuffer;
|
|
|
|
|
|
|
|
private int currentBufferIndex;
|
|
|
|
|
|
|
|
private boolean closed = false;
|
2022-02-26 22:51:22 +01:00
|
|
|
|
|
|
|
private final RocksdbFileStore store;
|
|
|
|
|
|
|
|
private final String name;
|
|
|
|
|
|
|
|
public RocksdbInputStream(String name, RocksdbFileStore store, int bufferSize) throws IOException {
|
|
|
|
this(name, store, bufferSize, store.getSize(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
public RocksdbInputStream(String name, RocksdbFileStore store, int bufferSize, long length) {
|
2022-02-28 03:20:24 +01:00
|
|
|
this(name,
|
|
|
|
store,
|
|
|
|
bufferSize,
|
|
|
|
length,
|
2022-03-09 02:29:38 +01:00
|
|
|
null
|
2022-02-28 03:20:24 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-09 02:29:38 +01:00
|
|
|
private RocksdbInputStream(String name, RocksdbFileStore store, int bufferSize, long length, Buffer currentBuffer) {
|
2022-02-26 22:51:22 +01:00
|
|
|
super("RocksdbInputStream(name=" + name + ")");
|
|
|
|
this.name = name;
|
|
|
|
this.store = store;
|
|
|
|
this.bufferSize = bufferSize;
|
2022-02-28 03:20:24 +01:00
|
|
|
this.currentBuffer = currentBuffer;
|
2022-03-09 02:29:38 +01:00
|
|
|
this.currentBufferIndex = bufferSize;
|
2022-02-26 22:51:22 +01:00
|
|
|
this.position = 0;
|
|
|
|
this.length = length;
|
2022-03-09 02:29:38 +01:00
|
|
|
if (currentBuffer != null && bufferSize > currentBuffer.capacity()) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"BufferSize is " + bufferSize + " but the buffer has only a capacity of " + currentBuffer.capacity());
|
|
|
|
}
|
2022-02-26 22:51:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() throws IOException {
|
2022-03-09 02:29:38 +01:00
|
|
|
if (!closed) {
|
|
|
|
closed = true;
|
|
|
|
if (currentBuffer != null) {
|
|
|
|
currentBuffer.close();
|
|
|
|
}
|
|
|
|
}
|
2022-02-26 22:51:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getFilePointer() {
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void seek(long pos) {
|
|
|
|
if (pos < 0 || pos > length) {
|
|
|
|
throw new IllegalArgumentException("pos must be between 0 and " + length);
|
|
|
|
}
|
|
|
|
position = pos;
|
2022-03-09 02:29:38 +01:00
|
|
|
currentBufferIndex = this.bufferSize;
|
2022-02-26 22:51:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long length() {
|
|
|
|
return this.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IndexInput slice(String sliceDescription, final long offset, final long length) throws IOException {
|
|
|
|
|
|
|
|
if (offset < 0 || length < 0 || offset + length > this.length) {
|
|
|
|
throw new IllegalArgumentException("slice() " + sliceDescription + " out of bounds: " + this);
|
|
|
|
}
|
|
|
|
|
2022-03-09 02:29:38 +01:00
|
|
|
return new RocksDBSliceInputStream(name,
|
2022-02-28 03:20:24 +01:00
|
|
|
store,
|
|
|
|
bufferSize,
|
2022-03-09 02:29:38 +01:00
|
|
|
offset + length
|
2022-02-28 03:20:24 +01:00
|
|
|
) {
|
2022-02-26 22:51:22 +01:00
|
|
|
{
|
|
|
|
seek(0L);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void seek(long pos) {
|
|
|
|
if (pos < 0L) {
|
|
|
|
throw new IllegalArgumentException("Seeking to negative position: " + this);
|
|
|
|
}
|
|
|
|
|
|
|
|
super.seek(pos + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getFilePointer() {
|
|
|
|
return super.getFilePointer() - offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long length() {
|
|
|
|
return super.length() - offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IndexInput slice(String sliceDescription, long ofs, long len) throws IOException {
|
|
|
|
return super.slice(sliceDescription, offset + ofs, len);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public byte readByte() throws IOException {
|
|
|
|
|
|
|
|
if (position >= length) {
|
|
|
|
throw new EOFException("Read end");
|
|
|
|
}
|
|
|
|
loadBufferIfNeed();
|
2022-03-09 02:29:38 +01:00
|
|
|
byte b = currentBuffer.getByte(currentBufferIndex++);
|
2022-02-26 22:51:22 +01:00
|
|
|
position++;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void loadBufferIfNeed() throws IOException {
|
2022-03-09 02:29:38 +01:00
|
|
|
if (currentBuffer == null) {
|
|
|
|
currentBuffer = store.bufferAllocator.allocate(bufferSize).writerOffset(bufferSize);
|
|
|
|
}
|
|
|
|
if (this.currentBufferIndex == this.bufferSize) {
|
2022-02-26 22:51:22 +01:00
|
|
|
int n = store.load(name, position, currentBuffer, 0, bufferSize);
|
|
|
|
if (n == -1) {
|
|
|
|
throw new EOFException("Read end");
|
|
|
|
}
|
2022-03-09 02:29:38 +01:00
|
|
|
this.currentBufferIndex = 0;
|
2022-02-26 22:51:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void readBytes(byte[] b, int offset, int len) throws IOException {
|
|
|
|
|
|
|
|
if (position >= length) {
|
|
|
|
throw new EOFException("Read end");
|
|
|
|
}
|
|
|
|
|
|
|
|
int f = offset;
|
|
|
|
int n = Math.min((int) (length - position), len);
|
|
|
|
do {
|
|
|
|
loadBufferIfNeed();
|
|
|
|
|
2022-03-09 02:29:38 +01:00
|
|
|
int r = Math.min(bufferSize - currentBufferIndex, n);
|
2022-02-26 22:51:22 +01:00
|
|
|
|
2022-03-09 02:29:38 +01:00
|
|
|
currentBuffer.copyInto(currentBufferIndex, b, f, r);
|
2022-02-26 22:51:22 +01:00
|
|
|
|
|
|
|
f += r;
|
|
|
|
position += r;
|
2022-03-09 02:29:38 +01:00
|
|
|
currentBufferIndex += r;
|
2022-02-26 22:51:22 +01:00
|
|
|
n -= r;
|
|
|
|
|
|
|
|
} while (n != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IndexInput clone() {
|
2022-03-09 02:29:38 +01:00
|
|
|
return super.clone();
|
2022-02-26 22:51:22 +01:00
|
|
|
}
|
|
|
|
}
|