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;
|
2021-09-23 11:30:44 +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;
|
2023-02-22 22:31:36 +01:00
|
|
|
import it.cavallium.dbengine.utils.StreamUtils;
|
2021-03-14 03:13:19 +01:00
|
|
|
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
2023-02-09 23:34:25 +01:00
|
|
|
import java.io.IOException;
|
2021-03-14 03:13:19 +01:00
|
|
|
import java.util.List;
|
2023-02-09 23:34:25 +01:00
|
|
|
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;
|
2023-05-24 01:44:06 +02:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
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 LLLocalGroupedReactiveRocksIterator<T> {
|
2021-03-14 03:13:19 +01:00
|
|
|
|
2021-12-17 01:48:49 +01:00
|
|
|
protected static final Logger logger = LogManager.getLogger(LLLocalGroupedReactiveRocksIterator.class);
|
2021-10-28 11:44:20 +02:00
|
|
|
|
2021-10-20 01:51:34 +02:00
|
|
|
private final RocksDBColumn db;
|
2021-03-14 03:13:19 +01:00
|
|
|
private final int prefixLength;
|
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-22 20:02:19 +01:00
|
|
|
private final boolean canFillCache;
|
2021-03-14 03:13:19 +01:00
|
|
|
private final boolean readValues;
|
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 LLLocalGroupedReactiveRocksIterator(RocksDBColumn db,
|
2021-03-14 03:13:19 +01:00
|
|
|
int prefixLength,
|
2023-02-09 23:34:25 +01:00
|
|
|
LLRange range,
|
2023-05-22 19:12:05 +02:00
|
|
|
Supplier<LLReadOptions> readOptions,
|
2021-03-22 20:02:19 +01:00
|
|
|
boolean canFillCache,
|
2022-03-24 23:56:23 +01:00
|
|
|
boolean readValues,
|
|
|
|
boolean smallRange) {
|
2022-05-20 10:20:00 +02:00
|
|
|
this.db = db;
|
|
|
|
this.prefixLength = prefixLength;
|
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.canFillCache = canFillCache;
|
|
|
|
this.readValues = readValues;
|
|
|
|
this.smallRange = smallRange;
|
2021-03-14 03:13:19 +01:00
|
|
|
}
|
|
|
|
|
2023-02-09 23:34:25 +01:00
|
|
|
public final Stream<List<T>> stream() {
|
2023-05-24 01:44:06 +02:00
|
|
|
try {
|
|
|
|
return resourceStream(
|
|
|
|
() -> generateCustomReadOptions(this.readOptions.get(), canFillCache, isBoundedRange(range), smallRange),
|
|
|
|
readOptions -> StreamUtils.resourceStream(
|
|
|
|
() -> db.newRocksIterator(readOptions, range, false),
|
|
|
|
rocksIterator -> StreamUtils.streamWhileNonNull(() -> {
|
|
|
|
try {
|
|
|
|
if (logger.isTraceEnabled()) {
|
|
|
|
logger.trace(MARKER_ROCKSDB, "Range {} started", LLUtils.toStringSafe(range));
|
|
|
|
}
|
|
|
|
ObjectArrayList<T> values = new ObjectArrayList<>();
|
|
|
|
Buf firstGroupKey = null;
|
|
|
|
while (rocksIterator.isValid()) {
|
|
|
|
// Note that the underlying array is subject to changes!
|
|
|
|
Buf key = rocksIterator.keyBuf();
|
|
|
|
if (firstGroupKey == null) {
|
|
|
|
firstGroupKey = key.copy();
|
|
|
|
} else if (!LLUtils.equals(firstGroupKey, 0, key, 0, prefixLength)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Note that the underlying array is subject to changes!
|
|
|
|
@Nullable Buf value;
|
|
|
|
if (readValues) {
|
|
|
|
value = rocksIterator.valueBuf();
|
|
|
|
} else {
|
|
|
|
value = null;
|
|
|
|
}
|
2023-02-09 23:34:25 +01: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)
|
|
|
|
);
|
|
|
|
}
|
2023-02-09 23:34:25 +01:00
|
|
|
|
2023-05-24 01:44:06 +02:00
|
|
|
rocksIterator.next();
|
|
|
|
T entry = getEntry(key, value);
|
|
|
|
values.add(entry);
|
|
|
|
}
|
|
|
|
if (!values.isEmpty()) {
|
|
|
|
return values;
|
|
|
|
} 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
|
|
|
}
|