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

131 lines
3.9 KiB
Java
Raw Normal View History

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;
2022-03-19 16:36:59 +01:00
import static it.cavallium.dbengine.database.LLUtils.generateCustomReadOptions;
2022-03-24 23:56:23 +01:00
import static it.cavallium.dbengine.database.LLUtils.isBoundedRange;
2021-04-03 19:09:06 +02:00
2022-10-02 03:09:50 +02:00
import io.netty5.buffer.Buffer;
2021-03-14 03:13:19 +01:00
import it.cavallium.dbengine.database.LLRange;
import it.cavallium.dbengine.database.LLUtils;
2022-07-19 23:45:39 +02:00
import java.util.function.Supplier;
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;
import org.rocksdb.RocksDBException;
2021-03-14 03:13:19 +01:00
import reactor.core.publisher.Flux;
2022-07-19 23:45:39 +02:00
import reactor.core.publisher.Mono;
2021-03-14 03:13:19 +01:00
2022-07-19 23:45:39 +02:00
public abstract class LLLocalReactiveRocksIterator<T> {
2021-03-14 03:13:19 +01:00
protected static final Logger logger = LogManager.getLogger(LLLocalReactiveRocksIterator.class);
2021-10-28 11:44:20 +02:00
2021-10-20 01:51:34 +02:00
private final RocksDBColumn db;
2022-07-19 23:45:39 +02:00
private final Mono<LLRange> rangeMono;
2021-06-29 23:31:02 +02:00
private final boolean allowNettyDirect;
2022-07-19 23:45:39 +02:00
private final Supplier<ReadOptions> readOptions;
2021-03-14 03:13:19 +01:00
private final boolean readValues;
2022-03-24 21:14:17 +01:00
private final boolean reverse;
2022-03-24 23:56:23 +01:00
private final boolean smallRange;
2021-03-14 03:13:19 +01:00
2021-10-20 01:51:34 +02:00
public LLLocalReactiveRocksIterator(RocksDBColumn db,
2022-07-19 23:45:39 +02:00
Mono<LLRange> rangeMono,
2021-06-29 23:31:02 +02:00
boolean allowNettyDirect,
2022-07-19 23:45:39 +02:00
Supplier<ReadOptions> readOptions,
2022-03-24 21:14:17 +01:00
boolean readValues,
2022-03-24 23:56:23 +01:00
boolean reverse,
boolean smallRange) {
this.db = db;
2022-07-19 23:45:39 +02:00
this.rangeMono = rangeMono;
this.allowNettyDirect = allowNettyDirect;
2022-07-19 23:45:39 +02:00
this.readOptions = readOptions != null ? readOptions : ReadOptions::new;
this.readValues = readValues;
this.reverse = reverse;
this.smallRange = smallRange;
2021-03-14 03:13:19 +01:00
}
2021-10-28 11:44:20 +02:00
public final Flux<T> flux() {
2022-07-19 23:45:39 +02:00
return Flux.usingWhen(rangeMono, range -> Flux.generate(() -> {
var readOptions = generateCustomReadOptions(this.readOptions.get(), true, isBoundedRange(range), smallRange);
2021-10-30 12:21:28 +02:00
if (logger.isTraceEnabled()) {
2022-07-19 23:45:39 +02:00
logger.trace(MARKER_ROCKSDB, "Range {} started", LLUtils.toStringSafe(range));
2021-10-30 12:21:28 +02:00
}
2022-07-19 23:45:39 +02:00
return new RocksIterWithReadOpts(readOptions, db.newRocksIterator(allowNettyDirect, readOptions, range, reverse));
2021-10-30 12:21:28 +02:00
}, (tuple, sink) -> {
try {
2022-05-12 19:14:27 +02:00
var rocksIterator = tuple.iter();
2021-10-30 12:21:28 +02:00
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
}
try {
2021-10-30 12:21:28 +02:00
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-05-02 19:18:15 +02:00
} else {
2021-10-30 12:21:28 +02:00
value = null;
}
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-07-19 23:45:39 +02:00
LLUtils.toStringSafe(range),
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 {
2022-03-24 21:14:17 +01:00
if (reverse) {
rocksIterator.prev();
} else {
rocksIterator.next();
}
sink.next(getEntry(key, value));
2022-07-20 02:00:08 +02:00
} catch (Throwable ex) {
if (value != null && value.isAccessible()) {
2022-07-20 02:00:08 +02:00
try {
value.close();
} catch (Throwable ex2) {
logger.error(ex2);
}
2021-10-30 12:21:28 +02:00
}
2022-07-20 02:00:08 +02:00
throw ex;
2021-10-30 12:21:28 +02:00
}
} catch (Throwable ex) {
if (key.isAccessible()) {
2022-07-20 02:00:08 +02:00
try {
key.close();
} catch (Throwable ex2) {
logger.error(ex2);
}
}
throw ex;
2021-10-30 12:21:28 +02:00
}
} else {
if (logger.isTraceEnabled()) {
2022-07-19 23:45:39 +02:00
logger.trace(MARKER_ROCKSDB, "Range {} ended", LLUtils.toStringSafe(range));
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-07-19 23:45:39 +02:00
logger.trace(MARKER_ROCKSDB, "Range {} failed", LLUtils.toStringSafe(range));
2021-10-30 12:21:28 +02:00
}
sink.error(ex);
}
return tuple;
2022-07-19 23:45:39 +02:00
}, RocksIterWithReadOpts::close), LLUtils::finalizeResource);
2021-03-14 03:13:19 +01:00
}
public abstract T getEntry(@Nullable Buffer key, @Nullable Buffer value);
2021-05-02 19:18:15 +02:00
2021-03-14 03:13:19 +01:00
}