CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/disk/LLLocalKeyPrefixReactiveRocksIterator.java

113 lines
3.3 KiB
Java
Raw Normal View History

2021-03-14 13:24:46 +01:00
package it.cavallium.dbengine.database.disk;
2021-09-04 02:19:10 +02:00
import io.netty5.buffer.api.Buffer;
import io.netty5.buffer.api.BufferAllocator;
import io.netty5.buffer.api.Send;
2021-03-14 13:24:46 +01:00
import it.cavallium.dbengine.database.LLRange;
import it.cavallium.dbengine.database.LLUtils;
2021-03-14 13:24:46 +01:00
import org.rocksdb.ColumnFamilyHandle;
import org.rocksdb.ReadOptions;
import org.rocksdb.RocksDB;
import org.rocksdb.RocksDBException;
2021-03-14 13:24:46 +01:00
import reactor.core.publisher.Flux;
2021-03-14 23:06:46 +01:00
public class LLLocalKeyPrefixReactiveRocksIterator {
2021-03-14 13:24:46 +01:00
private final RocksDB db;
2021-08-29 23:18:03 +02:00
private final BufferAllocator alloc;
2021-03-14 13:24:46 +01:00
private final ColumnFamilyHandle cfh;
private final int prefixLength;
private final LLRange range;
2021-06-29 23:31:02 +02:00
private final boolean allowNettyDirect;
2021-03-14 13:24:46 +01:00
private final ReadOptions readOptions;
2021-03-22 20:02:19 +01:00
private final boolean canFillCache;
2021-03-14 13:24:46 +01:00
private final String debugName;
2021-08-29 23:18:03 +02:00
public LLLocalKeyPrefixReactiveRocksIterator(RocksDB db, BufferAllocator alloc, ColumnFamilyHandle cfh,
2021-03-14 13:24:46 +01:00
int prefixLength,
2021-08-29 23:18:03 +02:00
Send<LLRange> range,
2021-06-29 23:31:02 +02:00
boolean allowNettyDirect,
2021-03-14 13:24:46 +01:00
ReadOptions readOptions,
2021-03-22 20:02:19 +01:00
boolean canFillCache,
2021-03-14 13:24:46 +01:00
String debugName) {
2021-08-31 09:14:46 +02:00
try (range) {
this.db = db;
this.alloc = alloc;
this.cfh = cfh;
this.prefixLength = prefixLength;
this.range = range.receive();
this.allowNettyDirect = allowNettyDirect;
this.readOptions = readOptions;
this.canFillCache = canFillCache;
this.debugName = debugName;
}
2021-03-14 13:24:46 +01:00
}
2021-08-29 23:18:03 +02:00
public Flux<Send<Buffer>> flux() {
2021-09-02 21:14:26 +02:00
return Flux.using(
() -> range.copy().send(),
rangeSend -> Flux
.generate(() -> {
var readOptions = new ReadOptions(this.readOptions);
if (!range.hasMin() || !range.hasMax()) {
readOptions.setReadaheadSize(32 * 1024); // 32KiB
readOptions.setFillCache(canFillCache);
}
return LLLocalDictionary.getRocksIterator(alloc, allowNettyDirect, readOptions, rangeSend, db, cfh);
}, (tuple, sink) -> {
try {
var rocksIterator = tuple.getT1();
rocksIterator.status();
Buffer firstGroupKey = null;
try {
while (rocksIterator.isValid()) {
Buffer key;
if (allowNettyDirect) {
key = LLUtils.readDirectNioBuffer(alloc, rocksIterator::key).receive();
} else {
key = LLUtils.fromByteArray(alloc, rocksIterator.key());
}
try (key) {
if (firstGroupKey == null) {
firstGroupKey = key.copy();
} else if (!LLUtils.equals(firstGroupKey, firstGroupKey.readerOffset(), key, key.readerOffset(),
prefixLength)) {
break;
}
rocksIterator.next();
rocksIterator.status();
}
}
if (firstGroupKey != null) {
var groupKeyPrefix = firstGroupKey.copy(firstGroupKey.readerOffset(), prefixLength);
assert groupKeyPrefix.isAccessible();
sink.next(groupKeyPrefix.send());
} else {
sink.complete();
}
} finally {
if (firstGroupKey != null) {
firstGroupKey.close();
2021-05-02 19:18:15 +02:00
}
}
2021-09-02 21:14:26 +02:00
} catch (RocksDBException ex) {
sink.error(ex);
2021-05-02 19:18:15 +02:00
}
2021-09-02 21:14:26 +02:00
return tuple;
}, tuple -> {
var rocksIterator = tuple.getT1();
rocksIterator.close();
tuple.getT2().close();
tuple.getT3().close();
tuple.getT4().close();
}),
Send::close
);
2021-03-14 13:24:46 +01:00
}
2021-04-03 19:09:06 +02:00
2021-05-02 19:18:15 +02:00
public void release() {
2021-08-31 09:14:46 +02:00
range.close();
2021-05-02 19:18:15 +02:00
}
2021-03-14 13:24:46 +01:00
}