CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/collections/SubStageGetterSingle.java

59 lines
1.7 KiB
Java
Raw Normal View History

package it.cavallium.dbengine.database.collections;
import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.database.LLDictionary;
2021-01-31 15:47:48 +01:00
import java.util.Arrays;
import org.jetbrains.annotations.Nullable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
2021-01-31 19:52:47 +01:00
public class SubStageGetterSingle<T> implements SubStageGetter<T, DatabaseStageEntry<T>> {
private final Serializer<T, byte[]> serializer;
2021-01-31 19:52:47 +01:00
public SubStageGetterSingle(Serializer<T, byte[]> serializer) {
2021-01-31 19:52:47 +01:00
this.serializer = serializer;
}
@Override
2021-01-31 19:52:47 +01:00
public Mono<DatabaseStageEntry<T>> subStage(LLDictionary dictionary,
@Nullable CompositeSnapshot snapshot,
byte[] keyPrefix,
Flux<byte[]> keyFlux) {
//System.out.println(Thread.currentThread() + "subStageGetterSingle1");
return keyFlux
.singleOrEmpty()
.flatMap(key -> Mono
.<DatabaseStageEntry<T>>fromCallable(() -> {
//System.out.println(Thread.currentThread() + "subStageGetterSingle2");
if (!Arrays.equals(keyPrefix, key)) {
throw new IndexOutOfBoundsException("Found more than one element!");
}
return null;
})
)
.then(Mono.fromSupplier(() -> {
//System.out.println(Thread.currentThread() + "subStageGetterSingle3");
return new DatabaseSingle<T>(dictionary,
keyPrefix,
serializer
);
}));
2021-01-31 19:52:47 +01:00
}
@Override
public boolean needsKeyFlux() {
return true;
}
2021-01-31 19:52:47 +01:00
//todo: temporary wrapper. convert the whole class to buffers
private T deserialize(byte[] bytes) {
return serializer.deserialize(bytes);
2021-01-31 19:52:47 +01:00
}
//todo: temporary wrapper. convert the whole class to buffers
private byte[] serialize(T bytes) {
return serializer.serialize(bytes);
}
}