2022-04-26 17:12:22 +02:00
|
|
|
package it.cavallium.dbengine.database.disk;
|
|
|
|
|
|
|
|
import static it.cavallium.dbengine.database.LLUtils.generateCustomReadOptions;
|
|
|
|
|
|
|
|
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;
|
|
|
|
import it.cavallium.dbengine.database.LLRange;
|
2022-05-12 19:14:27 +02:00
|
|
|
import it.cavallium.dbengine.database.disk.rocksdb.RocksObj;
|
2022-04-26 17:12:22 +02:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
import org.rocksdb.ReadOptions;
|
|
|
|
import org.rocksdb.RocksDBException;
|
|
|
|
import reactor.core.publisher.Flux;
|
2022-05-10 16:57:41 +02:00
|
|
|
import reactor.util.function.Tuples;
|
2022-04-26 17:12:22 +02:00
|
|
|
|
|
|
|
public final class LLLocalMigrationReactiveRocksIterator extends
|
|
|
|
ResourceSupport<LLLocalMigrationReactiveRocksIterator, LLLocalMigrationReactiveRocksIterator> {
|
|
|
|
|
2022-05-12 19:14:27 +02:00
|
|
|
private static final Logger logger = LogManager.getLogger(LLLocalMigrationReactiveRocksIterator.class);
|
2022-04-26 17:12:22 +02:00
|
|
|
private static final Drop<LLLocalMigrationReactiveRocksIterator> DROP = new Drop<>() {
|
|
|
|
@Override
|
|
|
|
public void drop(LLLocalMigrationReactiveRocksIterator obj) {
|
|
|
|
try {
|
|
|
|
if (obj.rangeShared != null) {
|
|
|
|
obj.rangeShared.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();
|
2022-04-26 17:12:22 +02:00
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
logger.error("Failed to close readOptions", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Drop<LLLocalMigrationReactiveRocksIterator> fork() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void attach(LLLocalMigrationReactiveRocksIterator obj) {
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private final RocksDBColumn db;
|
|
|
|
private LLRange rangeShared;
|
2022-05-12 19:14:27 +02:00
|
|
|
private RocksObj<ReadOptions> readOptions;
|
2022-04-26 17:12:22 +02:00
|
|
|
|
|
|
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
|
|
|
public LLLocalMigrationReactiveRocksIterator(RocksDBColumn db,
|
|
|
|
Send<LLRange> range,
|
2022-05-12 19:14:27 +02:00
|
|
|
Send<RocksObj<ReadOptions>> readOptions) {
|
2022-04-26 17:12:22 +02:00
|
|
|
super((Drop<LLLocalMigrationReactiveRocksIterator>) (Drop) DROP);
|
|
|
|
try (range) {
|
|
|
|
this.db = db;
|
|
|
|
this.rangeShared = range.receive();
|
2022-05-12 19:14:27 +02:00
|
|
|
this.readOptions = readOptions.receive();
|
2022-04-26 17:12:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public record ByteEntry(byte[] key, byte[] value) {}
|
|
|
|
|
|
|
|
public Flux<ByteEntry> flux() {
|
|
|
|
return Flux.generate(() -> {
|
|
|
|
var readOptions = generateCustomReadOptions(this.readOptions, false, false, false);
|
2022-05-11 20:32:56 +02:00
|
|
|
return new RocksIterWithReadOpts(readOptions, db.newRocksIterator(false, readOptions, rangeShared, false));
|
2022-04-26 17:12:22 +02:00
|
|
|
}, (tuple, sink) -> {
|
|
|
|
try {
|
2022-05-12 19:14:27 +02:00
|
|
|
var rocksIterator = tuple.iter();
|
2022-04-26 17:12:22 +02:00
|
|
|
if (rocksIterator.isValid()) {
|
|
|
|
byte[] key = rocksIterator.key();
|
|
|
|
byte[] value = rocksIterator.value();
|
|
|
|
rocksIterator.next(false);
|
|
|
|
sink.next(new ByteEntry(key, value));
|
|
|
|
} else {
|
|
|
|
sink.complete();
|
|
|
|
}
|
|
|
|
} catch (RocksDBException ex) {
|
|
|
|
sink.error(ex);
|
|
|
|
}
|
|
|
|
return tuple;
|
2022-05-11 20:32:56 +02:00
|
|
|
}, RocksIterWithReadOpts::close);
|
2022-04-26 17:12:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected final RuntimeException createResourceClosedException() {
|
|
|
|
return new IllegalStateException("Closed");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Owned<LLLocalMigrationReactiveRocksIterator> prepareSend() {
|
|
|
|
var range = this.rangeShared.send();
|
2022-05-12 19:14:27 +02:00
|
|
|
var readOptions = this.readOptions.send();
|
2022-04-26 17:12:22 +02:00
|
|
|
return drop -> new LLLocalMigrationReactiveRocksIterator(db,
|
|
|
|
range,
|
|
|
|
readOptions
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void makeInaccessible() {
|
|
|
|
this.rangeShared = null;
|
|
|
|
this.readOptions = null;
|
|
|
|
}
|
|
|
|
}
|