2021-03-14 03:13:19 +01:00
|
|
|
package it.cavallium.dbengine.database.disk;
|
|
|
|
|
2021-09-23 11:30:44 +02:00
|
|
|
import static it.cavallium.dbengine.database.LLUtils.MARKER_ROCKSDB;
|
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;
|
2021-10-28 11:44:20 +02:00
|
|
|
import io.net5.buffer.api.Drop;
|
|
|
|
import io.net5.buffer.api.Owned;
|
2021-09-17 16:56:28 +02:00
|
|
|
import io.net5.buffer.api.Send;
|
2021-10-28 11:44:20 +02:00
|
|
|
import io.net5.buffer.api.internal.ResourceSupport;
|
2021-03-14 03:13:19 +01:00
|
|
|
import it.cavallium.dbengine.database.LLRange;
|
2021-04-30 19:15:04 +02:00
|
|
|
import it.cavallium.dbengine.database.LLUtils;
|
2021-12-17 01:48:49 +01:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2021-09-22 18:33:28 +02:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2021-03-14 03:13:19 +01:00
|
|
|
import org.rocksdb.ReadOptions;
|
2021-06-27 11:58:12 +02:00
|
|
|
import org.rocksdb.RocksDBException;
|
2021-03-14 03:13:19 +01:00
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
|
2021-10-28 11:44:20 +02:00
|
|
|
public abstract class LLLocalReactiveRocksIterator<T> extends
|
|
|
|
ResourceSupport<LLLocalReactiveRocksIterator<T>, LLLocalReactiveRocksIterator<T>> {
|
2021-03-14 03:13:19 +01:00
|
|
|
|
2021-12-17 01:48:49 +01:00
|
|
|
protected static final Logger logger = LogManager.getLogger(LLLocalReactiveRocksIterator.class);
|
2021-10-28 11:44:20 +02:00
|
|
|
private static final Drop<LLLocalReactiveRocksIterator<?>> DROP = new Drop<>() {
|
|
|
|
@Override
|
|
|
|
public void drop(LLLocalReactiveRocksIterator<?> obj) {
|
|
|
|
try {
|
2022-01-26 19:03:51 +01:00
|
|
|
if (obj.rangeShared != null) {
|
|
|
|
obj.rangeShared.close();
|
2021-10-28 11:44:20 +02:00
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
logger.error("Failed to close range", ex);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (obj.readOptions != null) {
|
2021-10-29 00:11:25 +02:00
|
|
|
if (!(obj.readOptions instanceof UnreleasableReadOptions)) {
|
|
|
|
obj.readOptions.close();
|
|
|
|
}
|
2021-10-28 11:44:20 +02:00
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
logger.error("Failed to close readOptions", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Drop<LLLocalReactiveRocksIterator<?>> fork() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void attach(LLLocalReactiveRocksIterator<?> obj) {
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-20 01:51:34 +02:00
|
|
|
private final RocksDBColumn db;
|
2022-01-26 19:03:51 +01:00
|
|
|
private LLRange rangeShared;
|
2021-06-29 23:31:02 +02:00
|
|
|
private final boolean allowNettyDirect;
|
2021-10-28 11:44:20 +02:00
|
|
|
private ReadOptions readOptions;
|
2021-03-14 03:13:19 +01:00
|
|
|
private final boolean readValues;
|
|
|
|
|
2021-10-28 11:44:20 +02:00
|
|
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
2021-10-20 01:51:34 +02:00
|
|
|
public LLLocalReactiveRocksIterator(RocksDBColumn db,
|
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-10-28 11:44:20 +02:00
|
|
|
super((Drop<LLLocalReactiveRocksIterator<T>>) (Drop) DROP);
|
2022-01-26 19:03:51 +01:00
|
|
|
try (range) {
|
|
|
|
this.db = db;
|
|
|
|
this.rangeShared = range.receive();
|
|
|
|
this.allowNettyDirect = allowNettyDirect;
|
|
|
|
this.readOptions = readOptions;
|
|
|
|
this.readValues = readValues;
|
|
|
|
}
|
2021-03-14 03:13:19 +01:00
|
|
|
}
|
|
|
|
|
2021-10-28 11:44:20 +02:00
|
|
|
public final Flux<T> flux() {
|
2021-10-30 12:21:28 +02:00
|
|
|
return Flux.generate(() -> {
|
|
|
|
var readOptions = new ReadOptions(this.readOptions);
|
2022-01-26 19:03:51 +01:00
|
|
|
if (!rangeShared.hasMin() || !rangeShared.hasMax()) {
|
2021-10-30 12:21:28 +02:00
|
|
|
readOptions.setReadaheadSize(32 * 1024); // 32KiB
|
|
|
|
readOptions.setFillCache(false);
|
|
|
|
}
|
|
|
|
if (logger.isTraceEnabled()) {
|
2022-01-26 19:03:51 +01:00
|
|
|
logger.trace(MARKER_ROCKSDB, "Range {} started", LLUtils.toStringSafe(rangeShared));
|
2021-10-30 12:21:28 +02:00
|
|
|
}
|
2022-01-26 19:03:51 +01:00
|
|
|
return getRocksIterator(allowNettyDirect, readOptions, rangeShared, db);
|
2021-10-30 12:21:28 +02:00
|
|
|
}, (tuple, sink) -> {
|
|
|
|
try {
|
2022-01-26 19:03:51 +01:00
|
|
|
var rocksIterator = tuple.iterator();
|
2021-10-30 12:21:28 +02:00
|
|
|
rocksIterator.status();
|
|
|
|
if (rocksIterator.isValid()) {
|
|
|
|
Buffer key;
|
|
|
|
if (allowNettyDirect) {
|
|
|
|
key = LLUtils.readDirectNioBuffer(db.getAllocator(), rocksIterator::key);
|
|
|
|
} else {
|
|
|
|
key = LLUtils.fromByteArray(db.getAllocator(), rocksIterator.key());
|
2021-09-23 11:30:44 +02:00
|
|
|
}
|
2021-10-30 12:21:28 +02:00
|
|
|
try (key) {
|
|
|
|
Buffer value;
|
|
|
|
if (readValues) {
|
2021-09-02 17:15:40 +02:00
|
|
|
if (allowNettyDirect) {
|
2021-10-30 12:21:28 +02:00
|
|
|
value = LLUtils.readDirectNioBuffer(db.getAllocator(), rocksIterator::value);
|
2021-09-02 17:15:40 +02:00
|
|
|
} else {
|
2021-10-30 12:21:28 +02:00
|
|
|
value = LLUtils.fromByteArray(db.getAllocator(), rocksIterator.value());
|
2021-04-30 19:15:04 +02:00
|
|
|
}
|
2021-05-02 19:18:15 +02:00
|
|
|
} else {
|
2021-10-30 12:21:28 +02:00
|
|
|
value = null;
|
2021-04-30 19:15:04 +02:00
|
|
|
}
|
2021-10-30 12:21:28 +02:00
|
|
|
|
2021-09-23 11:30:44 +02:00
|
|
|
if (logger.isTraceEnabled()) {
|
2021-10-30 12:21:28 +02:00
|
|
|
logger.trace(MARKER_ROCKSDB,
|
|
|
|
"Range {} is reading {}: {}",
|
2022-01-26 19:03:51 +01:00
|
|
|
LLUtils.toStringSafe(rangeShared),
|
2021-10-30 12:21:28 +02:00
|
|
|
LLUtils.toStringSafe(key),
|
|
|
|
LLUtils.toStringSafe(value)
|
|
|
|
);
|
2021-09-23 11:30:44 +02:00
|
|
|
}
|
2021-10-30 12:21:28 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
rocksIterator.next();
|
|
|
|
rocksIterator.status();
|
|
|
|
sink.next(getEntry(key.send(), value == null ? null : value.send()));
|
|
|
|
} finally {
|
|
|
|
if (value != null) {
|
|
|
|
value.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (logger.isTraceEnabled()) {
|
2022-01-26 19:03:51 +01:00
|
|
|
logger.trace(MARKER_ROCKSDB, "Range {} ended", LLUtils.toStringSafe(rangeShared));
|
2021-03-14 19:38:20 +01:00
|
|
|
}
|
2021-10-30 12:21:28 +02:00
|
|
|
sink.complete();
|
|
|
|
}
|
|
|
|
} catch (RocksDBException ex) {
|
|
|
|
if (logger.isTraceEnabled()) {
|
2022-01-26 19:03:51 +01:00
|
|
|
logger.trace(MARKER_ROCKSDB, "Range {} failed", LLUtils.toStringSafe(rangeShared));
|
2021-10-30 12:21:28 +02:00
|
|
|
}
|
|
|
|
sink.error(ex);
|
|
|
|
}
|
|
|
|
return tuple;
|
2022-01-26 19:03:51 +01:00
|
|
|
}, RocksIteratorTuple::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
|
|
|
|
2021-10-28 11:44:20 +02:00
|
|
|
@Override
|
|
|
|
protected final RuntimeException createResourceClosedException() {
|
|
|
|
return new IllegalStateException("Closed");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Owned<LLLocalReactiveRocksIterator<T>> prepareSend() {
|
2022-01-26 19:03:51 +01:00
|
|
|
var range = this.rangeShared.send();
|
2021-10-28 11:44:20 +02:00
|
|
|
var readOptions = new ReadOptions(this.readOptions);
|
|
|
|
return drop -> new LLLocalReactiveRocksIterator<>(db, range, allowNettyDirect, readOptions, readValues) {
|
|
|
|
@Override
|
|
|
|
public T getEntry(@Nullable Send<Buffer> key, @Nullable Send<Buffer> value) {
|
|
|
|
return LLLocalReactiveRocksIterator.this.getEntry(key, value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void makeInaccessible() {
|
2022-01-26 19:03:51 +01:00
|
|
|
this.rangeShared = null;
|
2021-10-28 11:44:20 +02:00
|
|
|
this.readOptions = null;
|
2021-05-02 19:18:15 +02:00
|
|
|
}
|
2021-03-14 03:13:19 +01:00
|
|
|
}
|