2021-03-14 03:13:19 +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;
|
2023-05-24 01:44:06 +02:00
|
|
|
import static it.cavallium.dbengine.utils.StreamUtils.resourceStream;
|
2023-02-22 22:31:36 +01:00
|
|
|
import static it.cavallium.dbengine.utils.StreamUtils.streamWhileNonNull;
|
2021-04-03 19:09:06 +02:00
|
|
|
|
2023-03-06 12:19:08 +01:00
|
|
|
import it.cavallium.buffer.Buf;
|
2021-03-14 03:13:19 +01:00
|
|
|
import it.cavallium.dbengine.database.LLRange;
|
2021-04-30 19:15:04 +02:00
|
|
|
import it.cavallium.dbengine.database.LLUtils;
|
2023-05-22 19:12:05 +02:00
|
|
|
import it.cavallium.dbengine.database.disk.rocksdb.LLReadOptions;
|
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-07-19 23:45:39 +02:00
|
|
|
import java.util.function.Supplier;
|
2023-02-09 23:34:25 +01:00
|
|
|
import java.util.stream.Stream;
|
2021-12-17 01:48:49 +01:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2021-09-22 18:33:28 +02:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2021-03-14 03:13:19 +01:00
|
|
|
import org.rocksdb.ReadOptions;
|
2021-06-27 11:58:12 +02:00
|
|
|
import org.rocksdb.RocksDBException;
|
2021-03-14 03:13:19 +01:00
|
|
|
|
2022-07-19 23:45:39 +02:00
|
|
|
public abstract class LLLocalReactiveRocksIterator<T> {
|
2021-03-14 03:13:19 +01:00
|
|
|
|
2021-12-17 01:48:49 +01:00
|
|
|
protected static final Logger logger = LogManager.getLogger(LLLocalReactiveRocksIterator.class);
|
2021-10-28 11:44:20 +02:00
|
|
|
|
2021-10-20 01:51:34 +02:00
|
|
|
private final RocksDBColumn db;
|
2023-02-09 23:34:25 +01:00
|
|
|
private final LLRange range;
|
2023-05-22 19:12:05 +02:00
|
|
|
private final Supplier<LLReadOptions> readOptions;
|
2021-03-14 03:13:19 +01:00
|
|
|
private final boolean readValues;
|
2022-03-24 21:14:17 +01:00
|
|
|
private final boolean reverse;
|
2022-03-24 23:56:23 +01:00
|
|
|
private final boolean smallRange;
|
2021-03-14 03:13:19 +01:00
|
|
|
|
2021-10-20 01:51:34 +02:00
|
|
|
public LLLocalReactiveRocksIterator(RocksDBColumn db,
|
2023-02-09 23:34:25 +01:00
|
|
|
LLRange range,
|
2023-05-22 19:12:05 +02:00
|
|
|
Supplier<LLReadOptions> readOptions,
|
2022-03-24 21:14:17 +01:00
|
|
|
boolean readValues,
|
2022-03-24 23:56:23 +01:00
|
|
|
boolean reverse,
|
|
|
|
boolean smallRange) {
|
2022-05-20 10:20:00 +02:00
|
|
|
this.db = db;
|
2023-02-09 23:34:25 +01:00
|
|
|
this.range = range;
|
2023-05-22 19:12:05 +02:00
|
|
|
this.readOptions = readOptions != null ? readOptions : LLReadOptions::new;
|
2022-05-20 10:20:00 +02:00
|
|
|
this.readValues = readValues;
|
|
|
|
this.reverse = reverse;
|
|
|
|
this.smallRange = smallRange;
|
2021-03-14 03:13:19 +01:00
|
|
|
}
|
|
|
|
|
2023-02-09 23:34:25 +01:00
|
|
|
public final Stream<T> stream() {
|
2023-05-24 01:44:06 +02:00
|
|
|
try {
|
|
|
|
return resourceStream(
|
|
|
|
() -> generateCustomReadOptions(this.readOptions.get(), true, isBoundedRange(range), smallRange),
|
|
|
|
readOptions -> resourceStream(
|
|
|
|
() -> db.newRocksIterator(readOptions, range, reverse),
|
|
|
|
rocksIterator -> streamWhileNonNull(() -> {
|
|
|
|
try {
|
|
|
|
if (logger.isTraceEnabled()) {
|
|
|
|
logger.trace(MARKER_ROCKSDB, "Range {} started", LLUtils.toStringSafe(range));
|
|
|
|
}
|
|
|
|
if (rocksIterator.isValid()) {
|
|
|
|
// Note that the underlying array is subject to changes!
|
|
|
|
Buf key;
|
|
|
|
key = rocksIterator.keyBuf();
|
|
|
|
// Note that the underlying array is subject to changes!
|
|
|
|
Buf value;
|
|
|
|
if (readValues) {
|
|
|
|
value = rocksIterator.valueBuf();
|
|
|
|
} else {
|
|
|
|
value = null;
|
|
|
|
}
|
2021-10-30 12:21:28 +02:00
|
|
|
|
2023-05-24 01:44:06 +02:00
|
|
|
if (logger.isTraceEnabled()) {
|
|
|
|
logger.trace(MARKER_ROCKSDB,
|
|
|
|
"Range {} is reading {}: {}",
|
|
|
|
LLUtils.toStringSafe(range),
|
|
|
|
LLUtils.toStringSafe(key),
|
|
|
|
LLUtils.toStringSafe(value)
|
|
|
|
);
|
|
|
|
}
|
2021-10-30 12:21:28 +02:00
|
|
|
|
2023-05-24 01:44:06 +02:00
|
|
|
if (reverse) {
|
|
|
|
rocksIterator.prev();
|
|
|
|
} else {
|
|
|
|
rocksIterator.next();
|
|
|
|
}
|
|
|
|
return getEntry(key, value);
|
|
|
|
} else {
|
|
|
|
if (logger.isTraceEnabled()) {
|
|
|
|
logger.trace(MARKER_ROCKSDB, "Range {} ended", LLUtils.toStringSafe(range));
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} catch (RocksDBException ex) {
|
|
|
|
throw new CompletionException(generateRangeFailedException(ex));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} catch (RocksDBException ex) {
|
|
|
|
throw generateRangeFailedException(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private DBException generateRangeFailedException(RocksDBException ex) {
|
|
|
|
if (logger.isTraceEnabled()) {
|
|
|
|
logger.trace(MARKER_ROCKSDB, "Range {} failed", LLUtils.toStringSafe(range));
|
|
|
|
}
|
|
|
|
throw new DBException("Range failed", ex);
|
2021-03-14 03:13:19 +01:00
|
|
|
}
|
|
|
|
|
2023-02-09 23:34:25 +01:00
|
|
|
/**
|
|
|
|
* @param key this buffer content will be changed during the next iteration
|
|
|
|
* @param value this buffer content will be changed during the next iteration
|
|
|
|
*/
|
|
|
|
public abstract T getEntry(@Nullable Buf key, @Nullable Buf value);
|
2021-05-02 19:18:15 +02:00
|
|
|
|
2021-03-14 03:13:19 +01:00
|
|
|
}
|