2021-03-14 13:24:46 +01:00
|
|
|
package it.cavallium.dbengine.database.disk;
|
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.ByteBufAllocator;
|
|
|
|
import io.netty.buffer.ByteBufUtil;
|
2021-03-14 13:24:46 +01:00
|
|
|
import it.cavallium.dbengine.database.LLRange;
|
2021-04-30 19:15:04 +02:00
|
|
|
import it.cavallium.dbengine.database.LLUtils;
|
2021-03-14 13:24:46 +01:00
|
|
|
import java.util.Arrays;
|
|
|
|
import org.rocksdb.ColumnFamilyHandle;
|
|
|
|
import org.rocksdb.ReadOptions;
|
|
|
|
import org.rocksdb.RocksDB;
|
2021-03-14 19:38:20 +01:00
|
|
|
import org.rocksdb.RocksMutableObject;
|
2021-03-14 13:24:46 +01:00
|
|
|
import reactor.core.publisher.Flux;
|
2021-04-30 19:15:04 +02:00
|
|
|
import static io.netty.buffer.Unpooled.*;
|
2021-03-14 13:24:46 +01:00
|
|
|
|
2021-03-14 23:06:46 +01:00
|
|
|
public class LLLocalKeyPrefixReactiveRocksIterator {
|
2021-03-14 13:24:46 +01:00
|
|
|
|
|
|
|
private final RocksDB db;
|
2021-04-30 19:15:04 +02:00
|
|
|
private final ByteBufAllocator alloc;
|
2021-03-14 13:24:46 +01:00
|
|
|
private final ColumnFamilyHandle cfh;
|
|
|
|
private final int prefixLength;
|
|
|
|
private final LLRange range;
|
|
|
|
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-04-30 19:15:04 +02:00
|
|
|
public LLLocalKeyPrefixReactiveRocksIterator(RocksDB db, ByteBufAllocator alloc, ColumnFamilyHandle cfh,
|
2021-03-14 13:24:46 +01:00
|
|
|
int prefixLength,
|
|
|
|
LLRange range,
|
|
|
|
ReadOptions readOptions,
|
2021-03-22 20:02:19 +01:00
|
|
|
boolean canFillCache,
|
2021-03-14 13:24:46 +01:00
|
|
|
String debugName) {
|
|
|
|
this.db = db;
|
2021-04-30 19:15:04 +02:00
|
|
|
this.alloc = alloc;
|
2021-03-14 13:24:46 +01:00
|
|
|
this.cfh = cfh;
|
|
|
|
this.prefixLength = prefixLength;
|
|
|
|
this.range = range;
|
|
|
|
this.readOptions = readOptions;
|
2021-03-22 20:02:19 +01:00
|
|
|
this.canFillCache = canFillCache;
|
2021-03-14 13:24:46 +01:00
|
|
|
this.debugName = debugName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
public Flux<ByteBuf> flux() {
|
2021-03-14 13:24:46 +01:00
|
|
|
return Flux
|
|
|
|
.generate(() -> {
|
2021-03-14 19:38:20 +01:00
|
|
|
var readOptions = new ReadOptions(this.readOptions);
|
2021-03-17 01:02:02 +01:00
|
|
|
if (!range.hasMin() || !range.hasMax()) {
|
2021-05-29 01:45:03 +02:00
|
|
|
readOptions.setReadaheadSize(32 * 1024); // 32KiB
|
2021-03-22 20:02:19 +01:00
|
|
|
readOptions.setFillCache(canFillCache);
|
2021-03-17 01:02:02 +01:00
|
|
|
}
|
2021-04-30 19:15:04 +02:00
|
|
|
return LLLocalDictionary.getRocksIterator(readOptions, range.retain(), db, cfh);
|
2021-03-14 19:38:20 +01:00
|
|
|
}, (tuple, sink) -> {
|
2021-05-02 19:18:15 +02:00
|
|
|
range.retain();
|
2021-04-30 19:15:04 +02:00
|
|
|
try {
|
2021-05-02 19:18:15 +02:00
|
|
|
var rocksIterator = tuple.getT1();
|
|
|
|
ByteBuf firstGroupKey = null;
|
|
|
|
try {
|
|
|
|
while (rocksIterator.isValid()) {
|
|
|
|
ByteBuf key = LLUtils.readDirectNioBuffer(alloc, rocksIterator::key);
|
|
|
|
try {
|
|
|
|
if (firstGroupKey == null) {
|
|
|
|
firstGroupKey = key.retain();
|
2021-05-03 00:29:26 +02:00
|
|
|
} else if (!ByteBufUtil.equals(firstGroupKey, firstGroupKey.readerIndex(), key, key.readerIndex(), prefixLength)) {
|
2021-05-02 19:18:15 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
rocksIterator.next();
|
|
|
|
} finally {
|
|
|
|
key.release();
|
2021-04-30 19:15:04 +02:00
|
|
|
}
|
2021-05-02 19:18:15 +02:00
|
|
|
}
|
|
|
|
if (firstGroupKey != null) {
|
|
|
|
var groupKeyPrefix = firstGroupKey.slice(0, prefixLength);
|
|
|
|
sink.next(groupKeyPrefix.retain());
|
|
|
|
} else {
|
|
|
|
sink.complete();
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (firstGroupKey != null) {
|
|
|
|
firstGroupKey.release();
|
2021-04-30 19:15:04 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-02 19:18:15 +02:00
|
|
|
return tuple;
|
2021-04-30 19:15:04 +02:00
|
|
|
} finally {
|
2021-05-02 19:18:15 +02:00
|
|
|
range.release();
|
2021-03-14 13:24:46 +01:00
|
|
|
}
|
2021-03-14 19:38:20 +01:00
|
|
|
}, tuple -> {
|
|
|
|
var rocksIterator = tuple.getT1();
|
|
|
|
rocksIterator.close();
|
2021-04-30 19:15:04 +02:00
|
|
|
tuple.getT2().release();
|
|
|
|
tuple.getT3().release();
|
2021-03-14 14:16:07 +01:00
|
|
|
});
|
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() {
|
|
|
|
range.release();
|
|
|
|
}
|
2021-03-14 13:24:46 +01:00
|
|
|
}
|