2021-03-14 13:24:46 +01:00
|
|
|
package it.cavallium.dbengine.database.disk;
|
|
|
|
|
|
|
|
import it.cavallium.dbengine.database.LLRange;
|
|
|
|
import java.util.Arrays;
|
2021-03-14 19:38:20 +01:00
|
|
|
import java.util.Optional;
|
2021-03-14 13:24:46 +01:00
|
|
|
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 org.rocksdb.Slice;
|
|
|
|
import reactor.core.publisher.Flux;
|
2021-03-14 19:38:20 +01:00
|
|
|
import reactor.util.function.Tuples;
|
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 static final byte[] EMPTY = new byte[0];
|
|
|
|
|
|
|
|
private final RocksDB db;
|
|
|
|
private final ColumnFamilyHandle cfh;
|
|
|
|
private final int prefixLength;
|
|
|
|
private final LLRange range;
|
|
|
|
private final ReadOptions readOptions;
|
|
|
|
private final String debugName;
|
|
|
|
|
2021-03-14 23:06:46 +01:00
|
|
|
public LLLocalKeyPrefixReactiveRocksIterator(RocksDB db,
|
2021-03-14 13:24:46 +01:00
|
|
|
ColumnFamilyHandle cfh,
|
|
|
|
int prefixLength,
|
|
|
|
LLRange range,
|
|
|
|
ReadOptions readOptions,
|
|
|
|
String debugName) {
|
|
|
|
this.db = db;
|
|
|
|
this.cfh = cfh;
|
|
|
|
this.prefixLength = prefixLength;
|
|
|
|
this.range = range;
|
|
|
|
this.readOptions = readOptions;
|
|
|
|
this.debugName = debugName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("Convert2MethodRef")
|
|
|
|
public Flux<byte[]> flux() {
|
|
|
|
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-03-19 20:55:38 +01:00
|
|
|
// readOptions.setReadaheadSize(2 * 1024 * 1024);
|
2021-03-17 01:02:02 +01:00
|
|
|
readOptions.setFillCache(false);
|
|
|
|
}
|
2021-03-14 19:38:20 +01:00
|
|
|
Slice sliceMin;
|
|
|
|
Slice sliceMax;
|
|
|
|
if (range.hasMin()) {
|
|
|
|
sliceMin = new Slice(range.getMin());
|
|
|
|
readOptions.setIterateLowerBound(sliceMin);
|
|
|
|
} else {
|
|
|
|
sliceMin = null;
|
2021-03-14 13:24:46 +01:00
|
|
|
}
|
2021-03-14 19:38:20 +01:00
|
|
|
if (range.hasMax()) {
|
|
|
|
sliceMax = new Slice(range.getMax());
|
|
|
|
readOptions.setIterateUpperBound(sliceMax);
|
|
|
|
} else {
|
|
|
|
sliceMax = null;
|
|
|
|
}
|
|
|
|
var rocksIterator = db.newIterator(cfh, readOptions);
|
|
|
|
if (!LLLocalDictionary.PREFER_SEEK_TO_FIRST && range.hasMin()) {
|
|
|
|
rocksIterator.seek(range.getMin());
|
|
|
|
} else {
|
|
|
|
rocksIterator.seekToFirst();
|
|
|
|
}
|
|
|
|
return Tuples.of(rocksIterator, Optional.ofNullable(sliceMin), Optional.ofNullable(sliceMax));
|
|
|
|
}, (tuple, sink) -> {
|
|
|
|
var rocksIterator = tuple.getT1();
|
|
|
|
byte[] firstGroupKey = null;
|
2021-03-14 13:24:46 +01:00
|
|
|
|
2021-03-14 19:38:20 +01:00
|
|
|
while (rocksIterator.isValid()) {
|
|
|
|
byte[] key = rocksIterator.key();
|
|
|
|
if (firstGroupKey == null) {
|
|
|
|
firstGroupKey = key;
|
|
|
|
} else if (!Arrays.equals(firstGroupKey, 0, prefixLength, key, 0, prefixLength)) {
|
|
|
|
break;
|
2021-03-14 13:24:46 +01:00
|
|
|
}
|
2021-03-14 19:38:20 +01:00
|
|
|
rocksIterator.next();
|
2021-03-14 13:24:46 +01:00
|
|
|
}
|
2021-03-14 19:38:20 +01:00
|
|
|
if (firstGroupKey != null) {
|
|
|
|
var groupKeyPrefix = Arrays.copyOf(firstGroupKey, prefixLength);
|
|
|
|
sink.next(groupKeyPrefix);
|
|
|
|
} else {
|
|
|
|
sink.complete();
|
2021-03-14 13:24:46 +01:00
|
|
|
}
|
2021-03-14 19:38:20 +01:00
|
|
|
return tuple;
|
|
|
|
}, tuple -> {
|
|
|
|
var rocksIterator = tuple.getT1();
|
|
|
|
rocksIterator.close();
|
|
|
|
tuple.getT2().ifPresent(RocksMutableObject::close);
|
|
|
|
tuple.getT3().ifPresent(RocksMutableObject::close);
|
2021-03-14 14:16:07 +01:00
|
|
|
});
|
2021-03-14 13:24:46 +01:00
|
|
|
}
|
|
|
|
}
|