2021-01-31 00:36:21 +01:00
|
|
|
package it.cavallium.dbengine.database.collections;
|
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2021-01-31 00:36:21 +01:00
|
|
|
import it.cavallium.dbengine.client.CompositeSnapshot;
|
|
|
|
import it.cavallium.dbengine.database.LLDictionary;
|
2021-04-30 19:15:04 +02:00
|
|
|
import it.cavallium.dbengine.database.LLUtils;
|
2021-02-02 19:40:37 +01:00
|
|
|
import it.cavallium.dbengine.database.serialization.Serializer;
|
2021-01-31 15:47:48 +01:00
|
|
|
import java.util.Arrays;
|
2021-05-03 00:29:26 +02:00
|
|
|
import java.util.List;
|
2021-01-31 00:36:21 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
2021-01-31 19:52:47 +01:00
|
|
|
public class SubStageGetterSingle<T> implements SubStageGetter<T, DatabaseStageEntry<T>> {
|
|
|
|
|
2021-03-14 13:24:46 +01:00
|
|
|
private static final boolean assertsEnabled;
|
|
|
|
static {
|
|
|
|
boolean assertsEnabledTmp = false;
|
|
|
|
//noinspection AssertWithSideEffects
|
|
|
|
assert assertsEnabledTmp = true;
|
|
|
|
//noinspection ConstantConditions
|
|
|
|
assertsEnabled = assertsEnabledTmp;
|
|
|
|
}
|
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
private final Serializer<T, ByteBuf> serializer;
|
2021-01-31 19:52:47 +01:00
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
public SubStageGetterSingle(Serializer<T, ByteBuf> serializer) {
|
2021-01-31 19:52:47 +01:00
|
|
|
this.serializer = serializer;
|
|
|
|
}
|
2021-01-31 00:36:21 +01:00
|
|
|
|
|
|
|
@Override
|
2021-01-31 19:52:47 +01:00
|
|
|
public Mono<DatabaseStageEntry<T>> subStage(LLDictionary dictionary,
|
2021-01-31 00:36:21 +01:00
|
|
|
@Nullable CompositeSnapshot snapshot,
|
2021-08-22 18:20:05 +02:00
|
|
|
Mono<ByteBuf> keyPrefixMono,
|
|
|
|
Flux<ByteBuf> debuggingKeysFlux) {
|
|
|
|
return Mono.usingWhen(
|
|
|
|
keyPrefixMono,
|
|
|
|
keyPrefix -> Mono
|
|
|
|
.<DatabaseStageEntry<T>>fromSupplier(() -> new DatabaseSingle<>(dictionary, keyPrefix.retain(), serializer))
|
|
|
|
.transform(mono -> {
|
|
|
|
if (assertsEnabled && needsDebuggingKeyFlux()) {
|
|
|
|
return debuggingKeysFlux.handle((key, sink) -> {
|
|
|
|
try {
|
|
|
|
if (!LLUtils.equals(keyPrefix, key)) {
|
|
|
|
sink.error(new IndexOutOfBoundsException("Found more than one element!"));
|
|
|
|
} else {
|
|
|
|
sink.complete();
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
key.release();
|
|
|
|
}
|
|
|
|
}).then(mono);
|
|
|
|
} else {
|
|
|
|
return mono;
|
2021-05-12 19:02:51 +02:00
|
|
|
}
|
2021-08-22 18:20:05 +02:00
|
|
|
}),
|
|
|
|
keyPrefix -> Mono.fromRunnable(keyPrefix::release)
|
|
|
|
);
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
2021-02-02 15:36:11 +01:00
|
|
|
@Override
|
2021-03-14 13:24:46 +01:00
|
|
|
public boolean isMultiKey() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean needsDebuggingKeyFlux() {
|
|
|
|
return assertsEnabled;
|
2021-02-02 15:36:11 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|