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

193 lines
5.6 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-09-23 11:30:44 +02:00
2022-03-16 13:47:56 +01:00
import io.netty5.buffer.api.Buffer;
import io.netty5.buffer.api.Drop;
import io.netty5.buffer.api.Owned;
import io.netty5.buffer.api.Send;
import io.netty5.buffer.api.internal.ResourceSupport;
2021-03-14 03:13:19 +01:00
import it.cavallium.dbengine.database.LLRange;
import it.cavallium.dbengine.database.LLUtils;
2021-03-14 03:13:19 +01:00
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.util.List;
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;
2021-10-28 11:44:20 +02:00
public abstract class LLLocalGroupedReactiveRocksIterator<T> extends
ResourceSupport<LLLocalGroupedReactiveRocksIterator<T>, LLLocalGroupedReactiveRocksIterator<T>> {
2021-03-14 03:13:19 +01:00
protected static final Logger logger = LogManager.getLogger(LLLocalGroupedReactiveRocksIterator.class);
2021-10-28 11:44:20 +02:00
private static final Drop<LLLocalGroupedReactiveRocksIterator<?>> DROP = new Drop<>() {
@Override
public void drop(LLLocalGroupedReactiveRocksIterator<?> obj) {
try {
if (obj.range != null) {
obj.range.close();
}
} catch (Throwable ex) {
logger.error("Failed to close range", ex);
}
try {
if (obj.readOptions != null) {
2022-05-10 16:57:41 +02:00
obj.readOptions.close();
2021-10-28 11:44:20 +02:00
}
} catch (Throwable ex) {
logger.error("Failed to close readOptions", ex);
}
}
@Override
public Drop<LLLocalGroupedReactiveRocksIterator<?>> fork() {
return this;
}
@Override
public void attach(LLLocalGroupedReactiveRocksIterator<?> obj) {
}
};
2021-10-20 01:51:34 +02:00
private final RocksDBColumn db;
2021-03-14 03:13:19 +01:00
private final int prefixLength;
2021-10-28 11:44:20 +02:00
private LLRange range;
2021-06-29 23:31:02 +02:00
private final boolean allowNettyDirect;
private ReadOptions readOptions;
2021-03-22 20:02:19 +01:00
private final boolean canFillCache;
2021-03-14 03:13:19 +01:00
private final boolean readValues;
2022-03-24 23:56:23 +01:00
private final boolean smallRange;
2021-03-14 03:13:19 +01:00
2021-10-28 11:44:20 +02:00
@SuppressWarnings({"unchecked", "rawtypes"})
2021-10-20 01:51:34 +02:00
public LLLocalGroupedReactiveRocksIterator(RocksDBColumn db,
2021-03-14 03:13:19 +01:00
int prefixLength,
LLRange range,
2021-06-29 23:31:02 +02:00
boolean allowNettyDirect,
ReadOptions readOptions,
2021-03-22 20:02:19 +01:00
boolean canFillCache,
2022-03-24 23:56:23 +01:00
boolean readValues,
boolean smallRange) {
2021-10-28 11:44:20 +02:00
super((Drop<LLLocalGroupedReactiveRocksIterator<T>>) (Drop) DROP);
this.db = db;
this.prefixLength = prefixLength;
this.range = range;
this.allowNettyDirect = allowNettyDirect;
this.readOptions = readOptions != null ? readOptions : new ReadOptions();
this.canFillCache = canFillCache;
this.readValues = readValues;
this.smallRange = smallRange;
2021-03-14 03:13:19 +01:00
}
2021-10-28 11:44:20 +02:00
public final Flux<List<T>> flux() {
2022-05-04 12:36:32 +02:00
return Flux.generate(() -> {
var readOptions = generateCustomReadOptions(this.readOptions, true, isBoundedRange(range), smallRange);
if (logger.isTraceEnabled()) {
logger.trace(MARKER_ROCKSDB, "Range {} started", LLUtils.toStringSafe(range));
}
2022-05-11 20:32:56 +02:00
return new RocksIterWithReadOpts(readOptions, db.newRocksIterator(allowNettyDirect, readOptions, range, false));
2022-05-04 12:36:32 +02:00
}, (tuple, sink) -> {
try {
2022-05-12 19:14:27 +02:00
var rocksIterator = tuple.iter();
2022-05-04 12:36:32 +02:00
ObjectArrayList<T> values = new ObjectArrayList<>();
Buffer firstGroupKey = null;
try {
while (rocksIterator.isValid()) {
try (Buffer key = LLUtils.readDirectNioBuffer(db.getAllocator(), rocksIterator::key)) {
if (firstGroupKey == null) {
firstGroupKey = key.copy();
} else if (!LLUtils.equals(firstGroupKey,
firstGroupKey.readerOffset(),
key,
key.readerOffset(),
prefixLength
)) {
break;
2021-05-02 19:18:15 +02:00
}
2022-05-04 12:36:32 +02:00
@Nullable Buffer value;
if (readValues) {
value = LLUtils.readDirectNioBuffer(db.getAllocator(), rocksIterator::value);
} else {
value = null;
}
2022-05-04 12:36:32 +02:00
2021-09-23 11:30:44 +02:00
if (logger.isTraceEnabled()) {
2022-05-04 12:36:32 +02:00
logger.trace(MARKER_ROCKSDB,
"Range {} is reading {}: {}",
LLUtils.toStringSafe(range),
LLUtils.toStringSafe(key),
LLUtils.toStringSafe(value)
);
}
try {
rocksIterator.next();
T entry = getEntry(key, value);
2022-05-04 12:36:32 +02:00
values.add(entry);
} finally {
if (value != null) {
value.close();
}
2021-09-23 11:30:44 +02:00
}
}
2021-03-14 03:13:19 +01:00
}
2022-05-04 12:36:32 +02:00
} finally {
if (firstGroupKey != null) {
firstGroupKey.close();
}
}
if (!values.isEmpty()) {
sink.next(values);
} else {
if (logger.isTraceEnabled()) {
logger.trace(MARKER_ROCKSDB, "Range {} ended", LLUtils.toStringSafe(range));
}
sink.complete();
}
} catch (RocksDBException ex) {
if (logger.isTraceEnabled()) {
logger.trace(MARKER_ROCKSDB, "Range {} failed", LLUtils.toStringSafe(range));
}
sink.error(ex);
}
return tuple;
2022-05-11 20:32:56 +02:00
}, RocksIterWithReadOpts::close);
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-10-28 11:44:20 +02:00
@Override
protected final RuntimeException createResourceClosedException() {
return new IllegalStateException("Closed");
}
@Override
protected Owned<LLLocalGroupedReactiveRocksIterator<T>> prepareSend() {
var range = this.range.send();
2022-05-10 16:57:41 +02:00
var readOptions = this.readOptions;
2021-10-28 11:44:20 +02:00
return drop -> new LLLocalGroupedReactiveRocksIterator<>(db,
prefixLength,
range.receive(),
2021-10-28 11:44:20 +02:00
allowNettyDirect,
readOptions,
canFillCache,
2022-05-10 16:57:41 +02:00
readValues,
smallRange
2021-10-28 11:44:20 +02:00
) {
@Override
public T getEntry(@Nullable Buffer key, @Nullable Buffer value) {
2021-10-28 11:44:20 +02:00
return LLLocalGroupedReactiveRocksIterator.this.getEntry(key, value);
}
};
}
protected void makeInaccessible() {
this.range = null;
this.readOptions = null;
2021-05-02 19:18:15 +02:00
}
2021-03-14 03:13:19 +01:00
}