2022-04-26 17:12:22 +02:00
|
|
|
package it.cavallium.dbengine.database.disk;
|
|
|
|
|
|
|
|
import static it.cavallium.dbengine.database.LLUtils.generateCustomReadOptions;
|
2023-02-22 22:31:36 +01:00
|
|
|
import static it.cavallium.dbengine.utils.StreamUtils.streamWhileNonNull;
|
2022-04-26 17:12:22 +02:00
|
|
|
|
2023-02-09 23:34:25 +01:00
|
|
|
import it.cavallium.dbengine.buffers.Buf;
|
|
|
|
import it.cavallium.dbengine.database.LLEntry;
|
2022-04-26 17:12:22 +02:00
|
|
|
import it.cavallium.dbengine.database.LLRange;
|
2022-07-19 23:45:39 +02:00
|
|
|
import it.cavallium.dbengine.database.LLUtils;
|
2023-02-09 23:34:25 +01:00
|
|
|
import it.cavallium.dbengine.database.disk.rocksdb.RocksIteratorObj;
|
|
|
|
import it.cavallium.dbengine.utils.DBException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.concurrent.CompletionException;
|
2022-05-20 10:20:00 +02:00
|
|
|
import java.util.function.Supplier;
|
2023-02-09 23:34:25 +01:00
|
|
|
import java.util.stream.Stream;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
2022-04-26 17:12:22 +02:00
|
|
|
import org.rocksdb.ReadOptions;
|
|
|
|
import org.rocksdb.RocksDBException;
|
|
|
|
|
2022-07-19 23:45:39 +02:00
|
|
|
public final class LLLocalMigrationReactiveRocksIterator {
|
2022-04-26 17:12:22 +02:00
|
|
|
|
|
|
|
private final RocksDBColumn db;
|
2023-02-09 23:34:25 +01:00
|
|
|
private LLRange range;
|
2022-05-20 10:20:00 +02:00
|
|
|
private Supplier<ReadOptions> readOptions;
|
2022-04-26 17:12:22 +02:00
|
|
|
|
|
|
|
public LLLocalMigrationReactiveRocksIterator(RocksDBColumn db,
|
2023-02-09 23:34:25 +01:00
|
|
|
LLRange range,
|
2022-05-20 10:20:00 +02:00
|
|
|
Supplier<ReadOptions> readOptions) {
|
2022-07-19 23:45:39 +02:00
|
|
|
this.db = db;
|
2023-02-09 23:34:25 +01:00
|
|
|
this.range = range;
|
2022-07-19 23:45:39 +02:00
|
|
|
this.readOptions = readOptions;
|
2022-04-26 17:12:22 +02:00
|
|
|
}
|
|
|
|
|
2023-02-09 23:34:25 +01:00
|
|
|
public Stream<LLEntry> stream() {
|
|
|
|
var readOptions = generateCustomReadOptions(this.readOptions.get(), false, false, false);
|
|
|
|
RocksIteratorObj rocksIterator;
|
|
|
|
try {
|
|
|
|
rocksIterator = db.newRocksIterator(readOptions, range, false);
|
|
|
|
} catch (RocksDBException e) {
|
|
|
|
throw new DBException("Failed to open iterator", e);
|
|
|
|
}
|
2023-02-22 22:31:36 +01:00
|
|
|
return streamWhileNonNull(() -> {
|
2022-04-26 17:12:22 +02:00
|
|
|
try {
|
|
|
|
if (rocksIterator.isValid()) {
|
2023-02-09 23:34:25 +01:00
|
|
|
var key = rocksIterator.keyBuf().copy();
|
|
|
|
var value = rocksIterator.valueBuf().copy();
|
2022-04-26 17:12:22 +02:00
|
|
|
rocksIterator.next(false);
|
2023-02-09 23:34:25 +01:00
|
|
|
return LLEntry.of(key, value);
|
2022-04-26 17:12:22 +02:00
|
|
|
} else {
|
2023-02-09 23:34:25 +01:00
|
|
|
return null;
|
2022-04-26 17:12:22 +02:00
|
|
|
}
|
|
|
|
} catch (RocksDBException ex) {
|
2023-02-09 23:34:25 +01:00
|
|
|
throw new CompletionException(new DBException("Failed to iterate", ex));
|
2022-04-26 17:12:22 +02:00
|
|
|
}
|
2023-02-22 22:31:36 +01:00
|
|
|
}).onClose(() -> {
|
2023-02-09 23:34:25 +01:00
|
|
|
rocksIterator.close();
|
|
|
|
readOptions.close();
|
|
|
|
});
|
2022-04-26 17:12:22 +02:00
|
|
|
}
|
|
|
|
}
|