CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/disk/rocksdb/RocksIteratorObj.java

205 lines
5.1 KiB
Java
Raw Normal View History

2022-05-12 19:14:27 +02:00
package it.cavallium.dbengine.database.disk.rocksdb;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Timer;
2023-03-06 12:19:08 +01:00
import it.cavallium.buffer.Buf;
import it.cavallium.dbengine.database.LLUtils;
2023-05-22 19:12:05 +02:00
import it.cavallium.dbengine.database.disk.IteratorMetrics;
2022-07-19 23:45:39 +02:00
import it.cavallium.dbengine.utils.SimpleResource;
import java.nio.ByteBuffer;
2022-05-12 19:14:27 +02:00
import org.rocksdb.AbstractSlice;
import org.rocksdb.RocksDBException;
import org.rocksdb.RocksIterator;
2022-07-19 23:45:39 +02:00
public class RocksIteratorObj extends SimpleResource {
2022-05-12 19:14:27 +02:00
2023-05-22 19:12:05 +02:00
private LLReadOptions readOptions;
private final RocksIterator rocksIterator;
private final Counter startedIterSeek;
private final Counter endedIterSeek;
private final Timer iterSeekTime;
private final Counter startedIterNext;
private final Counter endedIterNext;
private final Timer iterNextTime;
2023-05-22 19:12:05 +02:00
private byte[] seekingFrom;
private byte[] seekingTo;
RocksIteratorObj(RocksIterator rocksIterator,
LLReadOptions readOptions, IteratorMetrics iteratorMetrics) {
super(rocksIterator::close);
this.readOptions = readOptions;
this.rocksIterator = rocksIterator;
2023-05-22 19:12:05 +02:00
this.startedIterSeek = iteratorMetrics.startedIterSeek();
this.startedIterNext = iteratorMetrics.startedIterNext();
this.iterSeekTime = iteratorMetrics.iterSeekTime();
this.endedIterNext = iteratorMetrics.endedIterNext();
this.endedIterSeek = iteratorMetrics.endedIterSeek();
this.iterNextTime = iteratorMetrics.iterNextTime();
}
public synchronized void seek(ByteBuffer seekBuf) throws RocksDBException {
2022-07-19 23:45:39 +02:00
ensureOpen();
startedIterSeek.increment();
try {
iterSeekTime.record(() -> rocksIterator.seek(seekBuf));
} finally {
endedIterSeek.increment();
}
rocksIterator.status();
}
public synchronized void seek(byte[] seekArray) throws RocksDBException {
2022-07-19 23:45:39 +02:00
ensureOpen();
2023-09-20 00:56:21 +02:00
rocksIterator.status();
startedIterSeek.increment();
try {
iterSeekTime.record(() -> rocksIterator.seek(seekArray));
} finally {
endedIterSeek.increment();
}
rocksIterator.status();
}
public synchronized void seekToFirst() throws RocksDBException {
2022-07-19 23:45:39 +02:00
ensureOpen();
startedIterSeek.increment();
try {
iterSeekTime.record(rocksIterator::seekToFirst);
} finally {
endedIterSeek.increment();
}
rocksIterator.status();
}
public synchronized void seekToLast() throws RocksDBException {
2022-07-19 23:45:39 +02:00
ensureOpen();
startedIterSeek.increment();
try {
iterSeekTime.record(rocksIterator::seekToLast);
} finally {
endedIterSeek.increment();
}
rocksIterator.status();
}
/**
* Useful for reverse iterations
*/
2023-09-20 00:56:21 +02:00
public synchronized void seekFrom(Buf key) throws RocksDBException {
2022-07-19 23:45:39 +02:00
ensureOpen();
var keyArray = LLUtils.asArray(key);
2023-09-20 00:56:21 +02:00
try {
rocksIterator.seekForPrev(keyArray);
} finally {
}
rocksIterator.status();
// This is useful to retain the key buffer in memory and avoid deallocations
this.seekingFrom = keyArray;
}
/**
* Useful for forward iterations
*/
2023-09-20 00:56:21 +02:00
public synchronized void seekTo(Buf key) throws RocksDBException {
2022-07-19 23:45:39 +02:00
ensureOpen();
2023-09-20 00:56:21 +02:00
rocksIterator.status();
var keyArray = LLUtils.asArray(key);
startedIterSeek.increment();
2023-09-20 00:56:21 +02:00
try {
iterSeekTime.record(() -> rocksIterator.seek(keyArray));
} finally {
endedIterSeek.increment();
}
rocksIterator.status();
// This is useful to retain the key buffer in memory and avoid deallocations
this.seekingTo = keyArray;
}
public synchronized boolean isValid() {
2022-07-19 23:45:39 +02:00
ensureOpen();
return rocksIterator.isValid();
}
@Deprecated(forRemoval = true)
public synchronized int key(ByteBuffer buffer) {
2022-07-19 23:45:39 +02:00
ensureOpen();
return rocksIterator.key(buffer);
}
@Deprecated(forRemoval = true)
public synchronized int value(ByteBuffer buffer) {
2022-07-19 23:45:39 +02:00
ensureOpen();
return rocksIterator.value(buffer);
}
/**
* The returned buffer may change when calling next() or when the iterator is not valid anymore
*/
public synchronized byte[] key() {
2022-07-19 23:45:39 +02:00
ensureOpen();
return rocksIterator.key();
}
/**
* The returned buffer may change when calling next() or when the iterator is not valid anymore
*/
public synchronized byte[] value() {
2022-07-19 23:45:39 +02:00
ensureOpen();
return rocksIterator.value();
}
/**
* The returned buffer may change when calling next() or when the iterator is not valid anymore
*/
public Buf keyBuf() {
return Buf.wrap(this.key());
}
/**
* The returned buffer may change when calling next() or when the iterator is not valid anymore
*/
public Buf valueBuf() {
return Buf.wrap(this.value());
}
public void next() throws RocksDBException {
next(true);
}
public synchronized void next(boolean traceStats) {
2022-07-19 23:45:39 +02:00
ensureOpen();
if (traceStats) {
startedIterNext.increment();
iterNextTime.record(rocksIterator::next);
endedIterNext.increment();
} else {
rocksIterator.next();
}
}
public void prev() throws RocksDBException {
prev(true);
}
public synchronized void prev(boolean traceStats) {
2022-07-19 23:45:39 +02:00
ensureOpen();
if (traceStats) {
startedIterNext.increment();
iterNextTime.record(rocksIterator::prev);
endedIterNext.increment();
} else {
rocksIterator.prev();
}
}
2022-05-12 19:14:27 +02:00
@Override
protected synchronized void onClose() {
2022-07-19 23:45:39 +02:00
if (rocksIterator != null) {
rocksIterator.close();
}
2023-05-22 19:12:05 +02:00
seekingFrom = null;
seekingTo = null;
readOptions = null;
2022-05-12 19:14:27 +02:00
}
}