2021-02-04 00:48:49 +01:00
|
|
|
package it.cavallium.dbengine.database.collections;
|
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2021-02-04 00:48:49 +01:00
|
|
|
import it.cavallium.dbengine.client.CompositeSnapshot;
|
|
|
|
import it.cavallium.dbengine.database.LLDictionary;
|
|
|
|
import it.cavallium.dbengine.database.collections.DatabaseEmpty.Nothing;
|
|
|
|
import it.cavallium.dbengine.database.serialization.SerializerFixedBinaryLength;
|
|
|
|
import java.util.Map;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
|
|
public class SubStageGetterSet<T> implements SubStageGetter<Map<T, Nothing>, DatabaseSetDictionary<T>> {
|
|
|
|
|
|
|
|
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 SerializerFixedBinaryLength<T, ByteBuf> keySerializer;
|
2021-02-04 00:48:49 +01:00
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
public SubStageGetterSet(SerializerFixedBinaryLength<T, ByteBuf> keySerializer) {
|
2021-02-04 00:48:49 +01:00
|
|
|
this.keySerializer = keySerializer;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<DatabaseSetDictionary<T>> subStage(LLDictionary dictionary,
|
|
|
|
@Nullable CompositeSnapshot snapshot,
|
2021-04-30 19:15:04 +02:00
|
|
|
ByteBuf prefixKey,
|
|
|
|
Flux<ByteBuf> debuggingKeyFlux) {
|
|
|
|
try {
|
|
|
|
Mono<DatabaseSetDictionary<T>> result = Mono
|
|
|
|
.fromSupplier(() -> DatabaseSetDictionary.tail(dictionary, prefixKey.retain(), keySerializer));
|
|
|
|
if (assertsEnabled) {
|
|
|
|
return checkKeyFluxConsistency(prefixKey.retain(), debuggingKeyFlux).then(result);
|
|
|
|
} else {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
prefixKey.release();
|
2021-02-04 00:48:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-03-14 13:24:46 +01:00
|
|
|
public boolean isMultiKey() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean needsDebuggingKeyFlux() {
|
2021-02-04 00:48:49 +01:00
|
|
|
return assertsEnabled;
|
|
|
|
}
|
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
private Mono<Void> checkKeyFluxConsistency(ByteBuf prefixKey, Flux<ByteBuf> keyFlux) {
|
2021-02-04 00:48:49 +01:00
|
|
|
return keyFlux.doOnNext(key -> {
|
2021-04-30 19:15:04 +02:00
|
|
|
assert key.readableBytes() == prefixKey.readableBytes() + getKeyBinaryLength();
|
|
|
|
}).doFinally(s -> prefixKey.release()).then();
|
2021-02-04 00:48:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getKeyBinaryLength() {
|
|
|
|
return keySerializer.getSerializedBinaryLength();
|
|
|
|
}
|
|
|
|
}
|