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-09-02 17:15:40 +02:00
|
|
|
import it.cavallium.dbengine.client.Mapper;
|
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-08-22 21:23:22 +02:00
|
|
|
import it.cavallium.dbengine.database.serialization.SerializationException;
|
|
|
|
import it.cavallium.dbengine.database.serialization.SerializationFunction;
|
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-08-22 21:23:22 +02:00
|
|
|
import reactor.core.publisher.SynchronousSink;
|
2021-01-31 19:52:47 +01:00
|
|
|
|
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;
|
2021-09-02 17:15:40 +02:00
|
|
|
private final Mapper<A, B> mapper;
|
2021-01-31 19:52:47 +01:00
|
|
|
|
2021-09-02 17:15:40 +02:00
|
|
|
public DatabaseSingleMapped(DatabaseStageEntry<B> serializedSingle, Mapper<A, B> mapper) {
|
2021-01-31 19:52:47 +01:00
|
|
|
this.serializedSingle = serializedSingle;
|
2021-09-02 17:15:40 +02:00
|
|
|
this.mapper = mapper;
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
2021-08-22 21:23:22 +02:00
|
|
|
private void deserializeSink(B value, SynchronousSink<A> sink) {
|
|
|
|
try {
|
2021-09-02 17:15:40 +02:00
|
|
|
sink.next(this.unMap(value));
|
2021-08-22 21:23:22 +02:00
|
|
|
} catch (SerializationException ex) {
|
|
|
|
sink.error(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 19:52:47 +01:00
|
|
|
@Override
|
2021-04-12 17:09:55 +02:00
|
|
|
public Mono<A> get(@Nullable CompositeSnapshot snapshot, boolean existsAlmostCertainly) {
|
2021-08-22 21:23:22 +02:00
|
|
|
return serializedSingle.get(snapshot, existsAlmostCertainly).handle(this::deserializeSink);
|
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-08-22 21:23:22 +02:00
|
|
|
return serializedSingle.get(snapshot).handle(this::deserializeSink).switchIfEmpty(defaultValue);
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-04-12 17:09:55 +02:00
|
|
|
public Mono<Void> set(A value) {
|
2021-08-22 21:23:22 +02:00
|
|
|
return Mono
|
2021-09-02 17:15:40 +02:00
|
|
|
.fromCallable(() -> map(value))
|
2021-08-22 21:23:22 +02:00
|
|
|
.flatMap(serializedSingle::set);
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-04-12 17:09:55 +02:00
|
|
|
public Mono<A> setAndGetPrevious(A value) {
|
2021-08-22 21:23:22 +02:00
|
|
|
return Mono
|
2021-09-02 17:15:40 +02:00
|
|
|
.fromCallable(() -> map(value))
|
2021-08-22 21:23:22 +02:00
|
|
|
.flatMap(serializedSingle::setAndGetPrevious)
|
|
|
|
.handle(this::deserializeSink);
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-04-30 19:15:04 +02:00
|
|
|
public Mono<Boolean> setAndGetChanged(A value) {
|
2021-08-22 21:23:22 +02:00
|
|
|
return Mono
|
2021-09-02 17:15:40 +02:00
|
|
|
.fromCallable(() -> map(value))
|
2021-08-22 21:23:22 +02:00
|
|
|
.flatMap(serializedSingle::setAndGetChanged)
|
|
|
|
.single();
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
2021-02-06 19:21:31 +01:00
|
|
|
|
|
|
|
@Override
|
2021-08-22 21:23:22 +02:00
|
|
|
public Mono<A> update(SerializationFunction<@Nullable A, @Nullable A> updater,
|
2021-05-08 03:09:00 +02:00
|
|
|
UpdateReturnMode updateReturnMode,
|
|
|
|
boolean existsAlmostCertainly) {
|
2021-04-30 19:15:04 +02:00
|
|
|
return serializedSingle.update(oldValue -> {
|
2021-09-02 17:15:40 +02:00
|
|
|
var result = updater.apply(oldValue == null ? null : this.unMap(oldValue));
|
2021-04-30 19:15:04 +02:00
|
|
|
if (result == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
2021-09-02 17:15:40 +02:00
|
|
|
return this.map(result);
|
2021-04-30 19:15:04 +02:00
|
|
|
}
|
2021-08-22 21:23:22 +02:00
|
|
|
}, updateReturnMode, existsAlmostCertainly).handle(this::deserializeSink);
|
2021-05-08 03:09:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-22 21:23:22 +02:00
|
|
|
public Mono<Delta<A>> updateAndGetDelta(SerializationFunction<@Nullable A, @Nullable A> updater,
|
2021-05-08 03:09:00 +02:00
|
|
|
boolean existsAlmostCertainly) {
|
|
|
|
return serializedSingle.updateAndGetDelta(oldValue -> {
|
2021-09-02 17:15:40 +02:00
|
|
|
var result = updater.apply(oldValue == null ? null : this.unMap(oldValue));
|
2021-05-08 03:09:00 +02:00
|
|
|
if (result == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
2021-09-02 17:15:40 +02:00
|
|
|
return this.map(result);
|
2021-05-08 03:09:00 +02:00
|
|
|
}
|
2021-09-02 17:15:40 +02:00
|
|
|
}, existsAlmostCertainly).transform(mono -> LLUtils.mapDelta(mono, this::unMap));
|
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-08-22 21:23:22 +02:00
|
|
|
return serializedSingle.clearAndGetPrevious().handle(this::deserializeSink);
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@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-09-02 17:15:40 +02:00
|
|
|
private A unMap(B bytes) throws SerializationException {
|
|
|
|
return mapper.unmap(bytes);
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//todo: temporary wrapper. convert the whole class to buffers
|
2021-09-02 17:15:40 +02:00
|
|
|
private B map(A bytes) throws SerializationException {
|
|
|
|
return mapper.map(bytes);
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
}
|