package it.cavallium.dbengine.database.collections; import it.cavallium.dbengine.client.CompositeSnapshot; import it.cavallium.dbengine.database.LLDictionary; import it.cavallium.dbengine.database.serialization.Serializer; import java.util.Arrays; import org.jetbrains.annotations.Nullable; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; public class SubStageGetterSingle implements SubStageGetter> { private final Serializer serializer; public SubStageGetterSingle(Serializer serializer) { this.serializer = serializer; } @Override public Mono> subStage(LLDictionary dictionary, @Nullable CompositeSnapshot snapshot, byte[] keyPrefix, Flux keyFlux) { return keyFlux .singleOrEmpty() .flatMap(key -> Mono .>fromCallable(() -> { if (!Arrays.equals(keyPrefix, key)) { throw new IndexOutOfBoundsException("Found more than one element!"); } return null; }) ) .then(Mono.fromSupplier(() -> { return new DatabaseSingle<>(dictionary, keyPrefix, serializer ); })); } @Override public boolean needsKeyFlux() { return true; } }