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

85 lines
2.4 KiB
Java
Raw Normal View History

2021-03-14 03:13:19 +01:00
package it.cavallium.dbengine.database.disk;
import it.cavallium.dbengine.database.LLRange;
2021-03-14 19:38:20 +01:00
import java.util.Optional;
2021-03-14 03:13:19 +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 03:13:19 +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 03:13:19 +01:00
2021-03-14 23:06:46 +01:00
public abstract class LLLocalReactiveRocksIterator<T> {
2021-03-14 03:13:19 +01:00
private static final byte[] EMPTY = new byte[0];
private final RocksDB db;
private final ColumnFamilyHandle cfh;
private final LLRange range;
private final ReadOptions readOptions;
private final boolean readValues;
2021-03-14 23:06:46 +01:00
public LLLocalReactiveRocksIterator(RocksDB db,
2021-03-14 03:13:19 +01:00
ColumnFamilyHandle cfh,
LLRange range,
ReadOptions readOptions,
boolean readValues) {
this.db = db;
this.cfh = cfh;
this.range = range;
this.readOptions = readOptions;
this.readValues = readValues;
}
2021-03-14 13:08:03 +01:00
public Flux<T> flux() {
return Flux
2021-03-14 03:13:19 +01:00
.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-21 13:06:54 +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 03:13:19 +01:00
}
2021-03-14 19:38:20 +01:00
if (range.hasMax()) {
sliceMax = new Slice(range.getMax());
readOptions.setIterateUpperBound(sliceMax);
} else {
sliceMax = null;
2021-03-14 14:16:07 +01:00
}
2021-03-14 19:38:20 +01:00
var rocksIterator = db.newIterator(cfh, readOptions);
if (!LLLocalDictionary.PREFER_SEEK_TO_FIRST && range.hasMin()) {
rocksIterator.seek(range.getMin());
} else {
rocksIterator.seekToFirst();
2021-03-14 03:13:19 +01:00
}
2021-03-14 19:38:20 +01:00
return Tuples.of(rocksIterator, Optional.ofNullable(sliceMin), Optional.ofNullable(sliceMax));
}, (tuple, sink) -> {
var rocksIterator = tuple.getT1();
if (rocksIterator.isValid()) {
byte[] key = rocksIterator.key();
byte[] value = readValues ? rocksIterator.value() : EMPTY;
rocksIterator.next();
sink.next(getEntry(key, value));
} else {
sink.complete();
}
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 03:13:19 +01:00
}
public abstract T getEntry(byte[] key, byte[] value);
}