package it.cavallium.dbengine.database.collections; import it.cavallium.dbengine.client.CompositeSnapshot; import it.cavallium.dbengine.database.serialization.Serializer; import java.util.Optional; import java.util.function.Function; import org.jetbrains.annotations.Nullable; import reactor.core.publisher.Mono; public class DatabaseSingleMapped implements DatabaseStageEntry { private final DatabaseSingle serializedSingle; private final Serializer serializer; public DatabaseSingleMapped(DatabaseSingle serializedSingle, Serializer serializer) { this.serializedSingle = serializedSingle; this.serializer = serializer; } @Override public Mono get(@Nullable CompositeSnapshot snapshot) { return serializedSingle.get(snapshot).map(this::deserialize); } @Override public Mono getOrDefault(@Nullable CompositeSnapshot snapshot, Mono defaultValue) { return serializedSingle.get(snapshot).map(this::deserialize).switchIfEmpty(defaultValue); } @Override public Mono set(U value) { return serializedSingle.set(serialize(value)); } @Override public Mono setAndGetPrevious(U value) { return serializedSingle.setAndGetPrevious(serialize(value)).map(this::deserialize); } @Override public Mono setAndGetStatus(U value) { return serializedSingle.setAndGetStatus(serialize(value)); } @Override public Mono update(Function, Optional> updater) { return serializedSingle.update(oldValue -> updater.apply(oldValue.map(this::deserialize)).map(this::serialize)); } @Override public Mono clear() { return serializedSingle.clear(); } @Override public Mono clearAndGetPrevious() { return serializedSingle.clearAndGetPrevious().map(this::deserialize); } @Override public Mono clearAndGetStatus() { return serializedSingle.clearAndGetStatus(); } @Override public Mono close() { return serializedSingle.close(); } @Override public Mono size(@Nullable CompositeSnapshot snapshot, boolean fast) { return serializedSingle.size(snapshot, fast); } @Override public Mono isEmpty(@Nullable CompositeSnapshot snapshot) { return serializedSingle.isEmpty(snapshot); } @Override public DatabaseStageEntry entry() { return this; } //todo: temporary wrapper. convert the whole class to buffers private U deserialize(byte[] bytes) { return serializer.deserialize(bytes); } //todo: temporary wrapper. convert the whole class to buffers private byte[] serialize(U bytes) { return serializer.serialize(bytes); } }