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-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-04-12 17:53:29 +02:00
|
|
|
Mono<DatabaseMapDictionaryHashed<T, U, TH>> result = Mono.just(DatabaseMapDictionaryHashed.tail(dictionary,
|
|
|
|
prefixKey,
|
|
|
|
keySerializer,
|
|
|
|
valueSerializer,
|
|
|
|
keyHashFunction,
|
|
|
|
keyHashSerializer
|
|
|
|
));
|
|
|
|
if (assertsEnabled) {
|
|
|
|
return checkKeyFluxConsistency(prefixKey, debuggingKeyFlux).then(result);
|
|
|
|
} else {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-04-12 17:53:29 +02:00
|
|
|
return keyFlux.doOnNext(key -> {
|
2021-04-30 19:15:04 +02:00
|
|
|
assert key.readableBytes() == prefixKey.readableBytes() + getKeyHashBinaryLength();
|
2021-04-12 17:53:29 +02:00
|
|
|
}).then();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getKeyHashBinaryLength() {
|
|
|
|
return keyHashSerializer.getSerializedBinaryLength();
|
|
|
|
}
|
|
|
|
}
|