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;
|
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;
|
2022-07-15 02:44:50 +02:00
|
|
|
import io.netty5.util.Send;
|
2022-03-16 13:47:56 +01:00
|
|
|
import io.netty5.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;
|
2022-07-19 23:45:39 +02:00
|
|
|
import java.util.function.Supplier;
|
2021-12-17 01:48:49 +01:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2021-03-14 13:24:46 +01:00
|
|
|
import org.rocksdb.ReadOptions;
|
2021-06-27 11:58:12 +02:00
|
|
|
import org.rocksdb.RocksDBException;
|
2021-03-14 13:24:46 +01:00
|
|
|
import reactor.core.publisher.Flux;
|
2022-07-19 23:45:39 +02:00
|
|
|
import reactor.core.publisher.Mono;
|
2022-05-10 16:57:41 +02:00
|
|
|
import reactor.util.function.Tuples;
|
2021-03-14 13:24:46 +01:00
|
|
|
|
2022-07-19 23:45:39 +02:00
|
|
|
public class LLLocalKeyPrefixReactiveRocksIterator {
|
2021-03-14 13:24:46 +01:00
|
|
|
|
2021-12-17 01:48:49 +01:00
|
|
|
protected static final Logger logger = LogManager.getLogger(LLLocalKeyPrefixReactiveRocksIterator.class);
|
2021-10-28 11:44:20 +02:00
|
|
|
|
2021-10-20 01:51:34 +02:00
|
|
|
private final RocksDBColumn db;
|
2021-03-14 13:24:46 +01:00
|
|
|
private final int prefixLength;
|
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-22 20:02:19 +01:00
|
|
|
private final boolean canFillCache;
|
2022-03-24 23:56:23 +01:00
|
|
|
private final boolean smallRange;
|
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,
|
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 23:56:23 +01:00
|
|
|
boolean canFillCache,
|
|
|
|
boolean smallRange) {
|
2022-05-20 10:20:00 +02:00
|
|
|
this.db = db;
|
|
|
|
this.prefixLength = prefixLength;
|
2022-07-19 23:45:39 +02:00
|
|
|
this.rangeMono = rangeMono;
|
2022-05-20 10:20:00 +02:00
|
|
|
this.allowNettyDirect = allowNettyDirect;
|
2022-07-19 23:45:39 +02:00
|
|
|
this.readOptions = readOptions != null ? readOptions : ReadOptions::new;
|
2022-05-20 10:20:00 +02:00
|
|
|
this.canFillCache = canFillCache;
|
|
|
|
this.smallRange = smallRange;
|
2021-03-14 13:24:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-20 10:20:00 +02:00
|
|
|
public Flux<Buffer> flux() {
|
2022-07-19 23:45:39 +02:00
|
|
|
return Flux.usingWhen(rangeMono, range -> Flux.generate(() -> {
|
|
|
|
var readOptions
|
|
|
|
= generateCustomReadOptions(this.readOptions.get(), canFillCache, isBoundedRange(range), smallRange);
|
2022-01-26 19:03:51 +01:00
|
|
|
if (logger.isTraceEnabled()) {
|
2022-07-19 23:45:39 +02:00
|
|
|
logger.trace(MARKER_ROCKSDB, "Range {} started", LLUtils.toStringSafe(range));
|
2022-01-26 19:03:51 +01:00
|
|
|
}
|
2022-07-19 23:45:39 +02:00
|
|
|
return new RocksIterWithReadOpts(readOptions, db.newRocksIterator(allowNettyDirect, readOptions, range, false));
|
2022-01-26 19:03:51 +01:00
|
|
|
}, (tuple, sink) -> {
|
|
|
|
try {
|
2022-05-12 19:14:27 +02:00
|
|
|
var rocksIterator = tuple.iter();
|
2022-01-26 19:03:51 +01:00
|
|
|
Buffer firstGroupKey = null;
|
|
|
|
try {
|
|
|
|
while (rocksIterator.isValid()) {
|
|
|
|
Buffer key;
|
|
|
|
if (allowNettyDirect) {
|
2022-07-03 01:32:13 +02:00
|
|
|
key = LLUtils.readDirectNioBuffer(db.getAllocator(), buffer -> rocksIterator.key(buffer));
|
2022-01-26 19:03:51 +01:00
|
|
|
} else {
|
|
|
|
key = LLUtils.fromByteArray(db.getAllocator(), rocksIterator.key());
|
|
|
|
}
|
|
|
|
try (key) {
|
2022-07-04 20:03:10 +02:00
|
|
|
var keyLen = key.readableBytes();
|
|
|
|
if (keyLen >= prefixLength) {
|
|
|
|
if (firstGroupKey == null) {
|
|
|
|
firstGroupKey = key.copy();
|
|
|
|
assert firstGroupKey == null || firstGroupKey.readableBytes() >= prefixLength;
|
|
|
|
} else if (!LLUtils.equals(firstGroupKey,
|
|
|
|
firstGroupKey.readerOffset(),
|
|
|
|
key,
|
|
|
|
key.readerOffset(),
|
|
|
|
prefixLength
|
|
|
|
)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.error("Skipped a key with length {}, the expected minimum prefix key length is {}!"
|
|
|
|
+ " This key will be dropped", key.readableBytes(), prefixLength);
|
2021-05-02 19:18:15 +02:00
|
|
|
}
|
2022-01-26 19:03:51 +01:00
|
|
|
rocksIterator.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (firstGroupKey != null) {
|
|
|
|
assert firstGroupKey.isAccessible();
|
|
|
|
var groupKeyPrefix = firstGroupKey.copy(firstGroupKey.readerOffset(), prefixLength);
|
|
|
|
assert groupKeyPrefix.isAccessible();
|
|
|
|
|
|
|
|
if (logger.isTraceEnabled()) {
|
|
|
|
logger.trace(MARKER_ROCKSDB,
|
|
|
|
"Range {} is reading prefix {}",
|
2022-07-19 23:45:39 +02:00
|
|
|
LLUtils.toStringSafe(range),
|
2022-01-26 19:03:51 +01:00
|
|
|
LLUtils.toStringSafe(groupKeyPrefix)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:20:00 +02:00
|
|
|
sink.next(groupKeyPrefix);
|
2022-01-26 19:03:51 +01:00
|
|
|
} else {
|
|
|
|
if (logger.isTraceEnabled()) {
|
2022-07-19 23:45:39 +02:00
|
|
|
logger.trace(MARKER_ROCKSDB, "Range {} ended", LLUtils.toStringSafe(range));
|
2022-01-26 19:03:51 +01:00
|
|
|
}
|
|
|
|
sink.complete();
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (firstGroupKey != null) {
|
|
|
|
firstGroupKey.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (RocksDBException ex) {
|
|
|
|
if (logger.isTraceEnabled()) {
|
2022-07-19 23:45:39 +02:00
|
|
|
logger.trace(MARKER_ROCKSDB, "Range {} failed", LLUtils.toStringSafe(range));
|
2022-01-26 19:03:51 +01:00
|
|
|
}
|
|
|
|
sink.error(ex);
|
|
|
|
}
|
|
|
|
return tuple;
|
2022-07-19 23:45:39 +02:00
|
|
|
}, RocksIterWithReadOpts::close), LLUtils::finalizeResource);
|
2021-10-28 11:44:20 +02:00
|
|
|
}
|
|
|
|
|
2021-03-14 13:24:46 +01:00
|
|
|
}
|