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;
|
2021-09-23 11:30:44 +02:00
|
|
|
|
2023-02-09 23:34:25 +01:00
|
|
|
import it.cavallium.dbengine.buffers.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-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;
|
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;
|
2022-07-19 23:45:39 +02:00
|
|
|
private final Supplier<ReadOptions> 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,
|
2022-07-19 23:45:39 +02:00
|
|
|
Supplier<ReadOptions> 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;
|
2022-07-19 23:45:39 +02:00
|
|
|
this.readOptions = readOptions != null ? readOptions : ReadOptions::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() {
|
|
|
|
var readOptions = generateCustomReadOptions(this.readOptions.get(), canFillCache, isBoundedRange(range), smallRange);
|
|
|
|
if (logger.isTraceEnabled()) {
|
|
|
|
logger.trace(MARKER_ROCKSDB, "Range {} started", LLUtils.toStringSafe(range));
|
|
|
|
}
|
2022-05-04 12:36:32 +02:00
|
|
|
|
2023-02-09 23:34:25 +01:00
|
|
|
RocksIteratorObj rocksIterator;
|
|
|
|
try {
|
|
|
|
rocksIterator = db.newRocksIterator(readOptions, range, false);
|
|
|
|
} catch (RocksDBException e) {
|
|
|
|
readOptions.close();
|
|
|
|
throw new DBException("Failed to iterate the range", e);
|
|
|
|
}
|
2022-05-04 12:36:32 +02:00
|
|
|
|
2023-02-22 22:31:36 +01:00
|
|
|
return StreamUtils.<List<T>>streamWhileNonNull(() -> {
|
2023-02-09 23:34:25 +01:00
|
|
|
try {
|
|
|
|
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;
|
2021-03-14 03:13:19 +01:00
|
|
|
}
|
2023-02-09 23:34:25 +01:00
|
|
|
// Note that the underlying array is subject to changes!
|
|
|
|
@Nullable Buf value;
|
|
|
|
if (readValues) {
|
|
|
|
value = rocksIterator.valueBuf();
|
|
|
|
} else {
|
|
|
|
value = null;
|
2022-05-04 12:36:32 +02:00
|
|
|
}
|
2023-02-09 23:34:25 +01:00
|
|
|
|
|
|
|
if (logger.isTraceEnabled()) {
|
|
|
|
logger.trace(MARKER_ROCKSDB,
|
|
|
|
"Range {} is reading {}: {}",
|
|
|
|
LLUtils.toStringSafe(range),
|
|
|
|
LLUtils.toStringSafe(key),
|
|
|
|
LLUtils.toStringSafe(value)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
rocksIterator.next();
|
|
|
|
T entry = getEntry(key, value);
|
|
|
|
values.add(entry);
|
2022-05-04 12:36:32 +02:00
|
|
|
}
|
|
|
|
if (!values.isEmpty()) {
|
2023-02-09 23:34:25 +01:00
|
|
|
return values;
|
2022-05-04 12:36:32 +02:00
|
|
|
} else {
|
|
|
|
if (logger.isTraceEnabled()) {
|
|
|
|
logger.trace(MARKER_ROCKSDB, "Range {} ended", LLUtils.toStringSafe(range));
|
|
|
|
}
|
2023-02-09 23:34:25 +01:00
|
|
|
return null;
|
2022-05-04 12:36:32 +02:00
|
|
|
}
|
|
|
|
} catch (RocksDBException ex) {
|
|
|
|
if (logger.isTraceEnabled()) {
|
|
|
|
logger.trace(MARKER_ROCKSDB, "Range {} failed", LLUtils.toStringSafe(range));
|
|
|
|
}
|
2023-02-09 23:34:25 +01:00
|
|
|
throw new CompletionException(new DBException("Range failed", ex));
|
2022-05-04 12:36:32 +02:00
|
|
|
}
|
2023-02-22 22:31:36 +01:00
|
|
|
}).onClose(() -> {
|
2023-02-09 23:34:25 +01:00
|
|
|
rocksIterator.close();
|
|
|
|
readOptions.close();
|
|
|
|
});
|
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
|
|
|
}
|