2021-03-14 13:24:46 +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-09-17 16:56:28 +02:00
|
|
|
import io.net5.buffer.api.Buffer;
|
|
|
|
import io.net5.buffer.api.BufferAllocator;
|
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 13:24:46 +01:00
|
|
|
import it.cavallium.dbengine.database.LLRange;
|
2021-04-30 19:15:04 +02:00
|
|
|
import it.cavallium.dbengine.database.LLUtils;
|
2021-10-28 11:44:20 +02:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2021-03-14 13:24:46 +01:00
|
|
|
import org.rocksdb.ColumnFamilyHandle;
|
|
|
|
import org.rocksdb.ReadOptions;
|
|
|
|
import org.rocksdb.RocksDB;
|
2021-06-27 11:58:12 +02:00
|
|
|
import org.rocksdb.RocksDBException;
|
2021-09-23 11:30:44 +02:00
|
|
|
import org.warp.commonutils.log.Logger;
|
|
|
|
import org.warp.commonutils.log.LoggerFactory;
|
2021-03-14 13:24:46 +01:00
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
|
2021-10-28 11:44:20 +02:00
|
|
|
public class LLLocalKeyPrefixReactiveRocksIterator extends
|
|
|
|
ResourceSupport<LLLocalKeyPrefixReactiveRocksIterator, LLLocalKeyPrefixReactiveRocksIterator> {
|
2021-03-14 13:24:46 +01:00
|
|
|
|
2021-09-23 11:30:44 +02:00
|
|
|
protected static final Logger logger = LoggerFactory.getLogger(LLLocalKeyPrefixReactiveRocksIterator.class);
|
2021-10-28 11:44:20 +02:00
|
|
|
private static final Drop<LLLocalKeyPrefixReactiveRocksIterator> DROP = new Drop<>() {
|
|
|
|
@Override
|
|
|
|
public void drop(LLLocalKeyPrefixReactiveRocksIterator obj) {
|
|
|
|
try {
|
|
|
|
if (obj.range != null) {
|
|
|
|
obj.range.close();
|
|
|
|
}
|
|
|
|
} 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<LLLocalKeyPrefixReactiveRocksIterator> fork() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void attach(LLLocalKeyPrefixReactiveRocksIterator obj) {
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-20 01:51:34 +02:00
|
|
|
private final RocksDBColumn db;
|
2021-03-14 13:24:46 +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;
|
2021-10-28 11:44:20 +02:00
|
|
|
private ReadOptions readOptions;
|
2021-03-22 20:02:19 +01:00
|
|
|
private final boolean canFillCache;
|
2021-03-14 13:24:46 +01:00
|
|
|
|
2021-10-20 01:51:34 +02:00
|
|
|
public LLLocalKeyPrefixReactiveRocksIterator(RocksDBColumn db,
|
2021-03-14 13:24:46 +01:00
|
|
|
int prefixLength,
|
2021-08-29 23:18:03 +02:00
|
|
|
Send<LLRange> range,
|
2021-06-29 23:31:02 +02:00
|
|
|
boolean allowNettyDirect,
|
2021-03-14 13:24:46 +01:00
|
|
|
ReadOptions readOptions,
|
2021-10-28 11:44:20 +02:00
|
|
|
boolean canFillCache) {
|
|
|
|
super(DROP);
|
2021-08-31 09:14:46 +02:00
|
|
|
try (range) {
|
|
|
|
this.db = db;
|
|
|
|
this.prefixLength = prefixLength;
|
|
|
|
this.range = range.receive();
|
|
|
|
this.allowNettyDirect = allowNettyDirect;
|
|
|
|
this.readOptions = readOptions;
|
|
|
|
this.canFillCache = canFillCache;
|
|
|
|
}
|
2021-03-14 13:24:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-29 23:18:03 +02:00
|
|
|
public Flux<Send<Buffer>> flux() {
|
2021-09-02 21:14:26 +02:00
|
|
|
return Flux.using(
|
|
|
|
() -> range.copy().send(),
|
|
|
|
rangeSend -> Flux
|
|
|
|
.generate(() -> {
|
|
|
|
var readOptions = new ReadOptions(this.readOptions);
|
|
|
|
if (!range.hasMin() || !range.hasMax()) {
|
|
|
|
readOptions.setReadaheadSize(32 * 1024); // 32KiB
|
|
|
|
readOptions.setFillCache(canFillCache);
|
|
|
|
}
|
2021-09-23 11:30:44 +02:00
|
|
|
if (logger.isTraceEnabled()) {
|
|
|
|
logger.trace(MARKER_ROCKSDB, "Range {} started", LLUtils.toStringSafe(range));
|
|
|
|
}
|
2021-10-20 01:51:34 +02:00
|
|
|
return LLLocalDictionary.getRocksIterator(db.getAllocator(), allowNettyDirect, readOptions, rangeSend, db);
|
2021-09-02 21:14:26 +02:00
|
|
|
}, (tuple, sink) -> {
|
|
|
|
try {
|
|
|
|
var rocksIterator = tuple.getT1();
|
|
|
|
rocksIterator.status();
|
|
|
|
Buffer firstGroupKey = null;
|
|
|
|
try {
|
|
|
|
while (rocksIterator.isValid()) {
|
|
|
|
Buffer key;
|
|
|
|
if (allowNettyDirect) {
|
2021-10-20 01:51:34 +02:00
|
|
|
key = LLUtils.readDirectNioBuffer(db.getAllocator(), rocksIterator::key);
|
2021-09-02 21:14:26 +02:00
|
|
|
} else {
|
2021-10-20 01:51:34 +02:00
|
|
|
key = LLUtils.fromByteArray(db.getAllocator(), rocksIterator.key());
|
2021-09-02 21:14:26 +02:00
|
|
|
}
|
|
|
|
try (key) {
|
|
|
|
if (firstGroupKey == null) {
|
|
|
|
firstGroupKey = key.copy();
|
|
|
|
} else if (!LLUtils.equals(firstGroupKey, firstGroupKey.readerOffset(), key, key.readerOffset(),
|
|
|
|
prefixLength)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rocksIterator.next();
|
|
|
|
rocksIterator.status();
|
|
|
|
}
|
|
|
|
}
|
2021-09-23 11:30:44 +02:00
|
|
|
|
2021-09-02 21:14:26 +02:00
|
|
|
if (firstGroupKey != null) {
|
2021-10-19 00:22:05 +02:00
|
|
|
assert firstGroupKey.isAccessible();
|
2021-09-02 21:14:26 +02:00
|
|
|
var groupKeyPrefix = firstGroupKey.copy(firstGroupKey.readerOffset(), prefixLength);
|
|
|
|
assert groupKeyPrefix.isAccessible();
|
2021-09-23 11:30:44 +02:00
|
|
|
|
|
|
|
if (logger.isTraceEnabled()) {
|
|
|
|
logger.trace(MARKER_ROCKSDB,
|
|
|
|
"Range {} is reading prefix {}",
|
|
|
|
LLUtils.toStringSafe(range),
|
|
|
|
LLUtils.toStringSafe(groupKeyPrefix)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-02 21:14:26 +02:00
|
|
|
sink.next(groupKeyPrefix.send());
|
|
|
|
} else {
|
2021-09-23 11:30:44 +02:00
|
|
|
if (logger.isTraceEnabled()) {
|
|
|
|
logger.trace(MARKER_ROCKSDB, "Range {} ended", LLUtils.toStringSafe(range));
|
|
|
|
}
|
2021-09-02 21:14:26 +02:00
|
|
|
sink.complete();
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (firstGroupKey != null) {
|
|
|
|
firstGroupKey.close();
|
2021-05-02 19:18:15 +02:00
|
|
|
}
|
2021-04-30 19:15:04 +02:00
|
|
|
}
|
2021-09-02 21:14:26 +02:00
|
|
|
} catch (RocksDBException ex) {
|
2021-09-23 11:30:44 +02:00
|
|
|
if (logger.isTraceEnabled()) {
|
|
|
|
logger.trace(MARKER_ROCKSDB, "Range {} failed", LLUtils.toStringSafe(range));
|
|
|
|
}
|
2021-09-02 21:14:26 +02:00
|
|
|
sink.error(ex);
|
2021-05-02 19:18:15 +02:00
|
|
|
}
|
2021-09-02 21:14:26 +02:00
|
|
|
return tuple;
|
|
|
|
}, tuple -> {
|
|
|
|
var rocksIterator = tuple.getT1();
|
|
|
|
rocksIterator.close();
|
|
|
|
tuple.getT2().close();
|
|
|
|
tuple.getT3().close();
|
|
|
|
tuple.getT4().close();
|
|
|
|
}),
|
2021-10-19 00:22:05 +02:00
|
|
|
resource -> resource.close()
|
2021-09-02 21:14:26 +02:00
|
|
|
);
|
2021-03-14 13:24:46 +01:00
|
|
|
}
|
2021-04-03 19:09:06 +02:00
|
|
|
|
2021-10-28 11:44:20 +02:00
|
|
|
@Override
|
|
|
|
protected final RuntimeException createResourceClosedException() {
|
|
|
|
return new IllegalStateException("Closed");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Owned<LLLocalKeyPrefixReactiveRocksIterator> prepareSend() {
|
|
|
|
var range = this.range.send();
|
|
|
|
var readOptions = new ReadOptions(this.readOptions);
|
|
|
|
return drop -> new LLLocalKeyPrefixReactiveRocksIterator(db,
|
|
|
|
prefixLength,
|
|
|
|
range,
|
|
|
|
allowNettyDirect,
|
|
|
|
readOptions,
|
|
|
|
canFillCache
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void makeInaccessible() {
|
|
|
|
this.range = null;
|
|
|
|
this.readOptions = null;
|
2021-05-02 19:18:15 +02:00
|
|
|
}
|
2021-03-14 13:24:46 +01:00
|
|
|
}
|