2021-01-31 19:52:47 +01:00
|
|
|
package it.cavallium.dbengine.database.collections;
|
|
|
|
|
2022-10-02 03:09:50 +02:00
|
|
|
import io.netty5.buffer.Drop;
|
|
|
|
import io.netty5.buffer.Owned;
|
2022-07-15 02:44:50 +02:00
|
|
|
import io.netty5.util.Send;
|
2022-10-02 03:09:50 +02:00
|
|
|
import io.netty5.buffer.internal.ResourceSupport;
|
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;
|
2022-07-19 23:45:39 +02:00
|
|
|
import it.cavallium.dbengine.utils.SimpleResource;
|
2021-12-17 01:48:49 +01:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
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")
|
2022-07-19 23:45:39 +02:00
|
|
|
public class DatabaseSingleMapped<A, B> extends SimpleResource implements DatabaseStageEntry<A> {
|
2021-01-31 19:52:47 +01:00
|
|
|
|
2021-12-17 01:48:49 +01:00
|
|
|
private static final Logger logger = LogManager.getLogger(DatabaseSingleMapped.class);
|
2021-10-01 19:17:33 +02:00
|
|
|
|
2021-09-02 17:15:40 +02:00
|
|
|
private final Mapper<A, B> mapper;
|
2021-01-31 19:52:47 +01:00
|
|
|
|
2022-07-19 23:45:39 +02:00
|
|
|
private final DatabaseStageEntry<B> serializedSingle;
|
2021-09-23 20:57:28 +02:00
|
|
|
|
2021-10-01 19:17:33 +02:00
|
|
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
2021-09-23 20:57:28 +02:00
|
|
|
public DatabaseSingleMapped(DatabaseStageEntry<B> serializedSingle, Mapper<A, B> mapper,
|
|
|
|
Drop<DatabaseSingleMapped<A, B>> drop) {
|
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-10-01 19:17:33 +02:00
|
|
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
2022-07-19 23:45:39 +02:00
|
|
|
private DatabaseSingleMapped(DatabaseStage<B> serializedSingle, Mapper<A, B> mapper,
|
2021-09-23 20:57:28 +02:00
|
|
|
Drop<DatabaseSingleMapped<A, B>> drop) {
|
|
|
|
this.mapper = mapper;
|
|
|
|
|
2022-07-19 23:45:39 +02:00
|
|
|
this.serializedSingle = (DatabaseStageEntry<B>) serializedSingle;
|
2021-09-23 20:57:28 +02: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
|
2022-05-21 23:49:06 +02:00
|
|
|
public Mono<A> get(@Nullable CompositeSnapshot snapshot) {
|
2022-07-03 01:32:13 +02:00
|
|
|
return serializedSingle.get(snapshot).handle((value, sink) -> deserializeSink(value, sink));
|
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) {
|
2022-07-03 01:32:13 +02:00
|
|
|
return serializedSingle.get(snapshot).handle((B value, SynchronousSink<A> sink) -> deserializeSink(value, sink)).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))
|
2022-07-03 01:32:13 +02:00
|
|
|
.flatMap(value1 -> serializedSingle.set(value1));
|
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))
|
2022-07-03 01:32:13 +02:00
|
|
|
.flatMap(value2 -> serializedSingle.setAndGetPrevious(value2))
|
|
|
|
.handle((value1, sink) -> deserializeSink(value1, sink));
|
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))
|
2022-07-03 01:32:13 +02:00
|
|
|
.flatMap(value1 -> serializedSingle.setAndGetChanged(value1))
|
2021-08-22 21:23:22 +02:00
|
|
|
.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,
|
2022-03-02 18:33:58 +01:00
|
|
|
UpdateReturnMode updateReturnMode) {
|
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
|
|
|
}
|
2022-07-03 01:32:13 +02:00
|
|
|
}, updateReturnMode).handle((value, sink) -> deserializeSink(value, sink));
|
2021-05-08 03:09:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-03-02 18:33:58 +01:00
|
|
|
public Mono<Delta<A>> updateAndGetDelta(SerializationFunction<@Nullable A, @Nullable A> updater) {
|
2021-05-08 03:09:00 +02:00
|
|
|
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
|
|
|
}
|
2022-07-03 01:32:13 +02:00
|
|
|
}).transform(mono -> LLUtils.mapDelta(mono, bytes -> unMap(bytes)));
|
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() {
|
2022-07-03 01:32:13 +02:00
|
|
|
return serializedSingle.clearAndGetPrevious().handle((value, sink) -> deserializeSink(value, sink));
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Boolean> clearAndGetStatus() {
|
|
|
|
return serializedSingle.clearAndGetStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2021-09-23 20:57:28 +02:00
|
|
|
|
|
|
|
@Override
|
2022-07-19 23:45:39 +02:00
|
|
|
protected void onClose() {
|
|
|
|
serializedSingle.close();
|
2021-09-23 20:57:28 +02:00
|
|
|
}
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|