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;
|
2022-07-15 02:44:50 +02:00
|
|
|
import io.netty5.util.Send;
|
2022-04-26 17:12:22 +02:00
|
|
|
import io.netty5.buffer.api.internal.ResourceSupport;
|
|
|
|
import it.cavallium.dbengine.database.LLRange;
|
2022-07-19 23:45:39 +02:00
|
|
|
import it.cavallium.dbengine.database.LLUtils;
|
|
|
|
import it.cavallium.dbengine.utils.SimpleResource;
|
2022-05-20 10:20:00 +02:00
|
|
|
import java.util.function.Supplier;
|
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-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;
|
2022-04-26 17:12:22 +02:00
|
|
|
|
2022-07-19 23:45:39 +02:00
|
|
|
public final class LLLocalMigrationReactiveRocksIterator {
|
2022-04-26 17:12:22 +02:00
|
|
|
|
|
|
|
private final RocksDBColumn db;
|
2022-07-19 23:45:39 +02:00
|
|
|
private Mono<LLRange> rangeMono;
|
2022-05-20 10:20:00 +02:00
|
|
|
private Supplier<ReadOptions> readOptions;
|
2022-04-26 17:12:22 +02:00
|
|
|
|
|
|
|
public LLLocalMigrationReactiveRocksIterator(RocksDBColumn db,
|
2022-07-19 23:45:39 +02:00
|
|
|
Mono<LLRange> rangeMono,
|
2022-05-20 10:20:00 +02:00
|
|
|
Supplier<ReadOptions> readOptions) {
|
2022-07-19 23:45:39 +02:00
|
|
|
this.db = db;
|
|
|
|
this.rangeMono = rangeMono;
|
|
|
|
this.readOptions = readOptions;
|
2022-04-26 17:12:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public record ByteEntry(byte[] key, byte[] value) {}
|
|
|
|
|
|
|
|
public Flux<ByteEntry> flux() {
|
2022-07-19 23:45:39 +02:00
|
|
|
return Flux.usingWhen(rangeMono, range -> Flux.generate(() -> {
|
2022-05-20 10:20:00 +02:00
|
|
|
var readOptions = generateCustomReadOptions(this.readOptions.get(), false, false, false);
|
2022-07-19 23:45:39 +02:00
|
|
|
return new RocksIterWithReadOpts(readOptions, db.newRocksIterator(false, readOptions, range, 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-07-19 23:45:39 +02:00
|
|
|
}, RocksIterWithReadOpts::close), LLUtils::finalizeResource);
|
2022-04-26 17:12:22 +02:00
|
|
|
}
|
|
|
|
}
|