2021-01-31 00:36:21 +01:00
|
|
|
package it.cavallium.dbengine.database.collections;
|
|
|
|
|
|
|
|
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-02-01 12:19:17 +01:00
|
|
|
private final SerializerFixedBinaryLength<T, byte[]> 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-02-01 12:19:17 +01:00
|
|
|
SerializerFixedBinaryLength<T, byte[]> 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,
|
|
|
|
byte[] prefixKey,
|
2021-03-14 13:24:46 +01:00
|
|
|
Flux<byte[]> debuggingKeyFlux) {
|
2021-02-02 18:42:18 +01:00
|
|
|
Mono<DatabaseMapDictionaryDeep<T, U, US>> result = Mono.just(DatabaseMapDictionaryDeep.deepIntermediate(dictionary,
|
2021-01-31 12:43:28 +01:00
|
|
|
prefixKey,
|
2021-02-02 19:40:37 +01:00
|
|
|
keySerializer,
|
|
|
|
subStageGetter,
|
2021-01-31 12:43:28 +01:00
|
|
|
keyExtLength
|
|
|
|
));
|
2021-02-02 15:36:11 +01:00
|
|
|
if (assertsEnabled) {
|
2021-03-14 13:24:46 +01:00
|
|
|
return checkKeyFluxConsistency(prefixKey, debuggingKeyFlux).then(result);
|
2021-02-02 15:36:11 +01:00
|
|
|
} else {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-02-02 15:36:11 +01:00
|
|
|
private Mono<Void> checkKeyFluxConsistency(byte[] prefixKey, Flux<byte[]> keyFlux) {
|
|
|
|
return keyFlux.doOnNext(key -> {
|
|
|
|
assert key.length == prefixKey.length + getKeyBinaryLength();
|
|
|
|
}).then();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getKeyBinaryLength() {
|
|
|
|
return keySerializer.getSerializedBinaryLength() + keyExtLength;
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|
|
|
|
}
|