2021-04-12 17:53:29 +02:00
|
|
|
package it.cavallium.dbengine.database.collections;
|
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2021-05-02 19:18:15 +02:00
|
|
|
import io.netty.util.ReferenceCounted;
|
2021-04-12 17:53:29 +02:00
|
|
|
import it.cavallium.dbengine.client.CompositeSnapshot;
|
|
|
|
import it.cavallium.dbengine.database.LLDictionary;
|
|
|
|
import it.cavallium.dbengine.database.serialization.Serializer;
|
|
|
|
import it.cavallium.dbengine.database.serialization.SerializerFixedBinaryLength;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.function.Function;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public class SubStageGetterHashMap<T, U, TH> implements
|
|
|
|
SubStageGetter<Map<T, U>, DatabaseMapDictionaryHashed<T, U, TH>> {
|
|
|
|
|
|
|
|
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> keySerializer;
|
|
|
|
private final Serializer<U, ByteBuf> valueSerializer;
|
2021-04-12 17:53:29 +02:00
|
|
|
private final Function<T, TH> keyHashFunction;
|
2021-04-30 19:15:04 +02:00
|
|
|
private final SerializerFixedBinaryLength<TH, ByteBuf> keyHashSerializer;
|
2021-04-12 17:53:29 +02:00
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
public SubStageGetterHashMap(Serializer<T, ByteBuf> keySerializer,
|
|
|
|
Serializer<U, ByteBuf> valueSerializer,
|
2021-04-12 17:53:29 +02:00
|
|
|
Function<T, TH> keyHashFunction,
|
2021-04-30 19:15:04 +02:00
|
|
|
SerializerFixedBinaryLength<TH, ByteBuf> keyHashSerializer) {
|
2021-04-12 17:53:29 +02:00
|
|
|
this.keySerializer = keySerializer;
|
|
|
|
this.valueSerializer = valueSerializer;
|
|
|
|
this.keyHashFunction = keyHashFunction;
|
|
|
|
this.keyHashSerializer = keyHashSerializer;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<DatabaseMapDictionaryHashed<T, U, TH>> subStage(LLDictionary dictionary,
|
|
|
|
@Nullable CompositeSnapshot snapshot,
|
2021-04-30 19:15:04 +02:00
|
|
|
ByteBuf prefixKey,
|
|
|
|
Flux<ByteBuf> debuggingKeyFlux) {
|
2021-05-02 19:18:15 +02:00
|
|
|
Mono<DatabaseMapDictionaryHashed<T, U, TH>> result = Mono.fromSupplier(() -> DatabaseMapDictionaryHashed.tail(dictionary,
|
|
|
|
prefixKey.retain(),
|
2021-04-12 17:53:29 +02:00
|
|
|
keySerializer,
|
|
|
|
valueSerializer,
|
|
|
|
keyHashFunction,
|
|
|
|
keyHashSerializer
|
|
|
|
));
|
|
|
|
if (assertsEnabled) {
|
2021-05-02 19:18:15 +02:00
|
|
|
return checkKeyFluxConsistency(prefixKey.retain(), debuggingKeyFlux)
|
|
|
|
.then(result)
|
|
|
|
.doFinally(s -> prefixKey.release());
|
2021-04-12 17:53:29 +02:00
|
|
|
} else {
|
2021-05-02 19:18:15 +02:00
|
|
|
return debuggingKeyFlux
|
|
|
|
.flatMap(key -> Mono.fromRunnable(key::release))
|
|
|
|
.then(result)
|
|
|
|
.doFinally(s -> prefixKey.release());
|
2021-04-12 17:53:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isMultiKey() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean needsDebuggingKeyFlux() {
|
|
|
|
return assertsEnabled;
|
|
|
|
}
|
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
private Mono<Void> checkKeyFluxConsistency(ByteBuf prefixKey, Flux<ByteBuf> keyFlux) {
|
2021-05-02 19:18:15 +02:00
|
|
|
return keyFlux
|
|
|
|
.doOnNext(key -> {
|
|
|
|
assert key.readableBytes() == prefixKey.readableBytes() + getKeyHashBinaryLength();
|
|
|
|
})
|
|
|
|
.flatMap(key -> Mono.fromRunnable(key::release))
|
|
|
|
.doOnDiscard(ByteBuf.class, ReferenceCounted::release)
|
|
|
|
.then()
|
|
|
|
.doFinally(s -> prefixKey.release());
|
2021-04-12 17:53:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getKeyHashBinaryLength() {
|
|
|
|
return keyHashSerializer.getSerializedBinaryLength();
|
|
|
|
}
|
|
|
|
}
|