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-01-31 15:47:48 +01:00
|
|
|
import java.util.Arrays;
|
2021-01-31 00:36:21 +01:00
|
|
|
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>> {
|
|
|
|
|
2021-02-01 12:19:17 +01:00
|
|
|
private final Serializer<T, byte[]> serializer;
|
2021-01-31 19:52:47 +01:00
|
|
|
|
2021-02-01 12:19:17 +01:00
|
|
|
public SubStageGetterSingle(Serializer<T, byte[]> serializer) {
|
2021-01-31 19:52:47 +01:00
|
|
|
this.serializer = serializer;
|
|
|
|
}
|
2021-01-31 00:36:21 +01:00
|
|
|
|
|
|
|
@Override
|
2021-01-31 19:52:47 +01:00
|
|
|
public Mono<DatabaseStageEntry<T>> subStage(LLDictionary dictionary,
|
2021-01-31 00:36:21 +01:00
|
|
|
@Nullable CompositeSnapshot snapshot,
|
|
|
|
byte[] keyPrefix,
|
|
|
|
Flux<byte[]> keyFlux) {
|
2021-02-02 00:09:46 +01:00
|
|
|
//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
|
|
|
}
|
|
|
|
|
2021-02-02 15:36:11 +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) {
|
2021-02-01 12:19:17 +01:00
|
|
|
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) {
|
2021-02-01 12:19:17 +01:00
|
|
|
return serializer.serialize(bytes);
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|
|
|
|
}
|