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-05-02 19:18:15 +02:00
|
|
|
import io.netty.util.ReferenceCounted;
|
2021-01-31 00:36:21 +01:00
|
|
|
import it.cavallium.dbengine.client.CompositeSnapshot;
|
|
|
|
import it.cavallium.dbengine.database.LLDictionary;
|
2021-02-02 19:40:37 +01:00
|
|
|
import it.cavallium.dbengine.database.serialization.SerializerFixedBinaryLength;
|
2021-01-31 00:36:21 +01:00
|
|
|
import java.util.Map;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
2021-02-02 18:42:18 +01:00
|
|
|
public class SubStageGetterMapDeep<T, U, US extends DatabaseStage<U>> implements SubStageGetter<Map<T, U>, DatabaseMapDictionaryDeep<T, U, US>> {
|
2021-01-31 00:36:21 +01:00
|
|
|
|
2021-02-02 15:36:11 +01:00
|
|
|
private static final boolean assertsEnabled;
|
|
|
|
static {
|
|
|
|
boolean assertsEnabledTmp = false;
|
|
|
|
//noinspection AssertWithSideEffects
|
|
|
|
assert assertsEnabledTmp = true;
|
|
|
|
//noinspection ConstantConditions
|
|
|
|
assertsEnabled = assertsEnabledTmp;
|
|
|
|
}
|
|
|
|
|
2021-01-31 00:36:21 +01:00
|
|
|
private final SubStageGetter<U, US> subStageGetter;
|
2021-04-30 19:15:04 +02:00
|
|
|
private final SerializerFixedBinaryLength<T, ByteBuf> keySerializer;
|
2021-01-31 00:36:21 +01:00
|
|
|
private final int keyExtLength;
|
|
|
|
|
2021-01-31 12:43:28 +01:00
|
|
|
public SubStageGetterMapDeep(SubStageGetter<U, US> subStageGetter,
|
2021-04-30 19:15:04 +02:00
|
|
|
SerializerFixedBinaryLength<T, ByteBuf> keySerializer,
|
2021-01-31 12:43:28 +01:00
|
|
|
int keyExtLength) {
|
2021-01-31 00:36:21 +01:00
|
|
|
this.subStageGetter = subStageGetter;
|
2021-01-31 12:43:28 +01:00
|
|
|
this.keySerializer = keySerializer;
|
2021-01-31 00:36:21 +01:00
|
|
|
this.keyExtLength = keyExtLength;
|
2021-02-02 15:36:11 +01:00
|
|
|
assert keyExtConsistency();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private boolean keyExtConsistency() {
|
|
|
|
if (subStageGetter instanceof SubStageGetterMapDeep) {
|
|
|
|
return keyExtLength == ((SubStageGetterMapDeep<?, ?, ?>) subStageGetter).getKeyBinaryLength();
|
|
|
|
} else if (subStageGetter instanceof SubStageGetterMap) {
|
|
|
|
return keyExtLength == ((SubStageGetterMap<?, ?>) subStageGetter).getKeyBinaryLength();
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-02 18:42:18 +01:00
|
|
|
public Mono<DatabaseMapDictionaryDeep<T, U, US>> subStage(LLDictionary dictionary,
|
2021-01-31 00:36:21 +01:00
|
|
|
@Nullable CompositeSnapshot snapshot,
|
2021-04-30 19:15:04 +02:00
|
|
|
ByteBuf prefixKey,
|
|
|
|
Flux<ByteBuf> debuggingKeyFlux) {
|
2021-05-02 19:18:15 +02:00
|
|
|
return Flux
|
|
|
|
.defer(() -> {
|
|
|
|
if (assertsEnabled) {
|
|
|
|
return this
|
|
|
|
.checkKeyFluxConsistency(prefixKey.retain(), debuggingKeyFlux);
|
|
|
|
} else {
|
|
|
|
return debuggingKeyFlux.flatMap(buf -> Mono.fromRunnable(buf::release));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(Mono
|
|
|
|
.fromSupplier(() -> DatabaseMapDictionaryDeep.deepIntermediate(dictionary,
|
|
|
|
prefixKey.retain(),
|
|
|
|
keySerializer,
|
|
|
|
subStageGetter,
|
|
|
|
keyExtLength
|
|
|
|
))
|
|
|
|
)
|
|
|
|
.doFinally(s -> prefixKey.release());
|
2021-02-02 15:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-03-14 13:24:46 +01:00
|
|
|
public boolean isMultiKey() {
|
2021-03-14 13:08:03 +01:00
|
|
|
return true;
|
2021-02-02 15:36:11 +01:00
|
|
|
}
|
|
|
|
|
2021-03-14 13:24:46 +01:00
|
|
|
@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() + getKeyBinaryLength();
|
|
|
|
})
|
|
|
|
.flatMap(key -> Mono.fromRunnable(key::release))
|
|
|
|
.doOnDiscard(ByteBuf.class, ReferenceCounted::release)
|
|
|
|
.then()
|
|
|
|
.doFinally(s -> prefixKey.release());
|
2021-02-02 15:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getKeyBinaryLength() {
|
|
|
|
return keySerializer.getSerializedBinaryLength() + keyExtLength;
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|
|
|
|
}
|