2021-01-31 19:52:47 +01:00
|
|
|
package it.cavallium.dbengine.database.collections;
|
|
|
|
|
2021-06-26 02:35:33 +02:00
|
|
|
import it.cavallium.dbengine.client.BadBlock;
|
2021-01-31 19:52:47 +01:00
|
|
|
import it.cavallium.dbengine.client.CompositeSnapshot;
|
2021-05-08 03:09:00 +02:00
|
|
|
import it.cavallium.dbengine.database.Delta;
|
|
|
|
import it.cavallium.dbengine.database.LLUtils;
|
|
|
|
import it.cavallium.dbengine.database.UpdateReturnMode;
|
2021-02-02 19:40:37 +01:00
|
|
|
import it.cavallium.dbengine.database.serialization.Serializer;
|
2021-02-06 19:21:31 +01:00
|
|
|
import java.util.function.Function;
|
2021-01-31 19:52:47 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2021-06-26 02:35:33 +02:00
|
|
|
import reactor.core.publisher.Flux;
|
2021-01-31 19:52:47 +01:00
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
2021-04-12 17:09:55 +02:00
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public class DatabaseSingleMapped<A, B> implements DatabaseStageEntry<A> {
|
2021-01-31 19:52:47 +01:00
|
|
|
|
2021-04-12 17:09:55 +02:00
|
|
|
private final DatabaseStageEntry<B> serializedSingle;
|
|
|
|
private final Serializer<A, B> serializer;
|
2021-01-31 19:52:47 +01:00
|
|
|
|
2021-04-12 17:09:55 +02:00
|
|
|
public DatabaseSingleMapped(DatabaseStageEntry<B> serializedSingle, Serializer<A, B> serializer) {
|
2021-01-31 19:52:47 +01:00
|
|
|
this.serializedSingle = serializedSingle;
|
|
|
|
this.serializer = serializer;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-04-12 17:09:55 +02:00
|
|
|
public Mono<A> get(@Nullable CompositeSnapshot snapshot, boolean existsAlmostCertainly) {
|
2021-03-18 16:19:41 +01:00
|
|
|
return serializedSingle.get(snapshot, existsAlmostCertainly).map(this::deserialize);
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-04-12 17:09:55 +02:00
|
|
|
public Mono<A> getOrDefault(@Nullable CompositeSnapshot snapshot, Mono<A> defaultValue) {
|
2021-01-31 19:52:47 +01:00
|
|
|
return serializedSingle.get(snapshot).map(this::deserialize).switchIfEmpty(defaultValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-04-12 17:09:55 +02:00
|
|
|
public Mono<Void> set(A value) {
|
2021-01-31 19:52:47 +01:00
|
|
|
return serializedSingle.set(serialize(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-04-12 17:09:55 +02:00
|
|
|
public Mono<A> setAndGetPrevious(A value) {
|
2021-01-31 19:52:47 +01:00
|
|
|
return serializedSingle.setAndGetPrevious(serialize(value)).map(this::deserialize);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-04-30 19:15:04 +02:00
|
|
|
public Mono<Boolean> setAndGetChanged(A value) {
|
|
|
|
return serializedSingle.setAndGetChanged(serialize(value)).single();
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
2021-02-06 19:21:31 +01:00
|
|
|
|
|
|
|
@Override
|
2021-05-08 03:09:00 +02:00
|
|
|
public Mono<A> update(Function<@Nullable A, @Nullable A> updater,
|
|
|
|
UpdateReturnMode updateReturnMode,
|
|
|
|
boolean existsAlmostCertainly) {
|
2021-04-30 19:15:04 +02:00
|
|
|
return serializedSingle.update(oldValue -> {
|
|
|
|
var result = updater.apply(oldValue == null ? null : this.deserialize(oldValue));
|
|
|
|
if (result == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return this.serialize(result);
|
|
|
|
}
|
2021-05-08 03:09:00 +02:00
|
|
|
}, updateReturnMode, existsAlmostCertainly).map(this::deserialize);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Delta<A>> updateAndGetDelta(Function<@Nullable A, @Nullable A> updater,
|
|
|
|
boolean existsAlmostCertainly) {
|
|
|
|
return serializedSingle.updateAndGetDelta(oldValue -> {
|
|
|
|
var result = updater.apply(oldValue == null ? null : this.deserialize(oldValue));
|
|
|
|
if (result == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return this.serialize(result);
|
|
|
|
}
|
|
|
|
}, existsAlmostCertainly).transform(mono -> LLUtils.mapDelta(mono, this::deserialize));
|
2021-02-06 19:21:31 +01:00
|
|
|
}
|
2021-01-31 19:52:47 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Void> clear() {
|
|
|
|
return serializedSingle.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-04-12 17:09:55 +02:00
|
|
|
public Mono<A> clearAndGetPrevious() {
|
2021-01-31 19:52:47 +01:00
|
|
|
return serializedSingle.clearAndGetPrevious().map(this::deserialize);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Boolean> clearAndGetStatus() {
|
|
|
|
return serializedSingle.clearAndGetStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Void> close() {
|
|
|
|
return serializedSingle.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-24 16:43:07 +01:00
|
|
|
public Mono<Long> leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) {
|
|
|
|
return serializedSingle.leavesCount(snapshot, fast);
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Boolean> isEmpty(@Nullable CompositeSnapshot snapshot) {
|
|
|
|
return serializedSingle.isEmpty(snapshot);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-04-12 17:09:55 +02:00
|
|
|
public DatabaseStageEntry<A> entry() {
|
2021-01-31 19:52:47 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-06-26 02:35:33 +02:00
|
|
|
@Override
|
|
|
|
public Flux<BadBlock> badBlocks() {
|
|
|
|
return this.serializedSingle.badBlocks();
|
|
|
|
}
|
|
|
|
|
2021-04-30 19:15:04 +02:00
|
|
|
@Override
|
|
|
|
public void release() {
|
|
|
|
serializedSingle.release();
|
|
|
|
}
|
|
|
|
|
2021-01-31 19:52:47 +01:00
|
|
|
//todo: temporary wrapper. convert the whole class to buffers
|
2021-04-12 17:09:55 +02:00
|
|
|
private A deserialize(B 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
|
2021-04-12 17:09:55 +02:00
|
|
|
private B serialize(A bytes) {
|
2021-02-01 12:19:17 +01:00
|
|
|
return serializer.serialize(bytes);
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
}
|