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

113 lines
3.2 KiB
Java
Raw Normal View History

2021-03-14 03:13:19 +01:00
package it.cavallium.dbengine.database.disk;
2021-04-03 19:09:06 +02:00
import static it.cavallium.dbengine.database.disk.LLLocalDictionary.getRocksIterator;
2021-09-17 16:56:28 +02:00
import io.net5.buffer.api.Buffer;
import io.net5.buffer.api.BufferAllocator;
import io.net5.buffer.api.Send;
import io.net5.util.IllegalReferenceCountException;
2021-03-14 03:13:19 +01:00
import it.cavallium.dbengine.database.LLRange;
import it.cavallium.dbengine.database.LLUtils;
2021-06-19 12:14:14 +02:00
import java.util.concurrent.atomic.AtomicBoolean;
2021-09-22 18:33:28 +02:00
import org.jetbrains.annotations.Nullable;
2021-03-14 03:13:19 +01:00
import org.rocksdb.ColumnFamilyHandle;
import org.rocksdb.ReadOptions;
import org.rocksdb.RocksDB;
import org.rocksdb.RocksDBException;
2021-03-14 03:13:19 +01:00
import reactor.core.publisher.Flux;
2021-03-14 23:06:46 +01:00
public abstract class LLLocalReactiveRocksIterator<T> {
2021-03-14 03:13:19 +01:00
2021-06-19 12:14:14 +02:00
private final AtomicBoolean released = new AtomicBoolean(false);
2021-03-14 03:13:19 +01:00
private final RocksDB db;
2021-08-29 23:18:03 +02:00
private final BufferAllocator alloc;
2021-03-14 03:13:19 +01:00
private final ColumnFamilyHandle cfh;
private final LLRange range;
2021-06-29 23:31:02 +02:00
private final boolean allowNettyDirect;
2021-03-14 03:13:19 +01:00
private final ReadOptions readOptions;
private final boolean readValues;
2021-03-14 23:06:46 +01:00
public LLLocalReactiveRocksIterator(RocksDB db,
2021-08-29 23:18:03 +02:00
BufferAllocator alloc,
2021-03-14 03:13:19 +01:00
ColumnFamilyHandle cfh,
2021-08-29 23:18:03 +02:00
Send<LLRange> range,
2021-06-29 23:31:02 +02:00
boolean allowNettyDirect,
2021-03-14 03:13:19 +01:00
ReadOptions readOptions,
2021-09-22 18:33:28 +02:00
boolean readValues) {
2021-03-14 03:13:19 +01:00
this.db = db;
this.alloc = alloc;
2021-03-14 03:13:19 +01:00
this.cfh = cfh;
2021-08-29 23:18:03 +02:00
this.range = range.receive();
2021-06-29 23:31:02 +02:00
this.allowNettyDirect = allowNettyDirect;
2021-03-14 03:13:19 +01:00
this.readOptions = readOptions;
this.readValues = readValues;
}
2021-03-14 13:08:03 +01:00
public Flux<T> flux() {
return Flux
2021-08-29 23:18:03 +02: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-05-28 16:04:59 +02:00
readOptions.setReadaheadSize(32 * 1024); // 32KiB
2021-03-17 01:02:02 +01:00
readOptions.setFillCache(false);
}
2021-09-01 00:01:56 +02:00
return getRocksIterator(alloc, allowNettyDirect, readOptions, range.copy().send(), db, cfh);
2021-03-14 19:38:20 +01:00
}, (tuple, sink) -> {
2021-05-02 19:18:15 +02:00
try {
var rocksIterator = tuple.getT1();
rocksIterator.status();
2021-05-02 19:18:15 +02:00
if (rocksIterator.isValid()) {
2021-09-02 17:15:40 +02:00
Buffer key;
if (allowNettyDirect) {
2021-09-22 18:33:28 +02:00
key = LLUtils.readDirectNioBuffer(alloc, rocksIterator::key);
2021-09-02 17:15:40 +02:00
} else {
key = LLUtils.fromByteArray(alloc, rocksIterator.key());
}
try (key) {
2021-08-29 23:18:03 +02:00
Buffer value;
2021-05-02 19:18:15 +02:00
if (readValues) {
2021-09-02 17:15:40 +02:00
if (allowNettyDirect) {
2021-09-22 18:33:28 +02:00
value = LLUtils.readDirectNioBuffer(alloc, rocksIterator::value);
2021-09-02 17:15:40 +02:00
} else {
value = LLUtils.fromByteArray(alloc, rocksIterator.value());
}
2021-05-02 19:18:15 +02:00
} else {
2021-09-22 18:33:28 +02:00
value = null;
2021-05-02 19:18:15 +02:00
}
try {
rocksIterator.next();
rocksIterator.status();
2021-09-22 18:33:28 +02:00
sink.next(getEntry(key.send(), value == null ? null : value.send()));
2021-05-02 19:18:15 +02:00
} finally {
2021-09-22 18:33:28 +02:00
if (value != null) {
value.close();
}
2021-05-02 19:18:15 +02:00
}
}
2021-05-02 19:18:15 +02:00
} else {
sink.complete();
}
} catch (RocksDBException ex) {
sink.error(ex);
2021-03-14 19:38:20 +01:00
}
return tuple;
2021-03-14 19:38:20 +01:00
}, tuple -> {
var rocksIterator = tuple.getT1();
rocksIterator.close();
2021-08-29 23:18:03 +02:00
tuple.getT2().close();
tuple.getT3().close();
2021-09-01 00:01:56 +02:00
tuple.getT4().close();
});
2021-03-14 03:13:19 +01:00
}
2021-09-22 18:33:28 +02:00
public abstract T getEntry(@Nullable Send<Buffer> key, @Nullable Send<Buffer> value);
2021-05-02 19:18:15 +02:00
public void release() {
2021-06-19 12:14:14 +02:00
if (released.compareAndSet(false, true)) {
2021-08-29 23:18:03 +02:00
range.close();
2021-06-19 12:14:14 +02:00
} else {
throw new IllegalReferenceCountException(0, -1);
2021-06-19 12:14:14 +02:00
}
2021-05-02 19:18:15 +02:00
}
2021-03-14 03:13:19 +01:00
}