2021-01-31 00:36:21 +01:00
|
|
|
package it.cavallium.dbengine.database.collections;
|
|
|
|
|
2022-10-02 03:09:50 +02:00
|
|
|
import io.netty5.buffer.Buffer;
|
|
|
|
import io.netty5.buffer.Drop;
|
|
|
|
import io.netty5.buffer.Owned;
|
|
|
|
import io.netty5.buffer.internal.ResourceSupport;
|
2021-06-26 02:35:33 +02:00
|
|
|
import it.cavallium.dbengine.client.BadBlock;
|
2021-01-31 00:36:21 +01:00
|
|
|
import it.cavallium.dbengine.client.CompositeSnapshot;
|
2021-05-08 03:09:00 +02:00
|
|
|
import it.cavallium.dbengine.database.Delta;
|
2022-03-20 14:33:27 +01:00
|
|
|
import it.cavallium.dbengine.database.LLSingleton;
|
2021-01-31 00:36:21 +01:00
|
|
|
import it.cavallium.dbengine.database.LLSnapshot;
|
2021-05-08 03:09:00 +02:00
|
|
|
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-02-02 19:40:37 +01:00
|
|
|
import it.cavallium.dbengine.database.serialization.Serializer;
|
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 00:36:21 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2021-06-26 02:35:33 +02:00
|
|
|
import reactor.core.publisher.Flux;
|
2021-01-31 00:36:21 +01:00
|
|
|
import reactor.core.publisher.Mono;
|
2021-08-22 21:23:22 +02:00
|
|
|
import reactor.core.publisher.SynchronousSink;
|
2021-01-31 00:36:21 +01:00
|
|
|
|
2022-07-19 23:45:39 +02:00
|
|
|
public class DatabaseSingleton<U> extends SimpleResource implements DatabaseStageEntry<U> {
|
2021-01-31 00:36:21 +01:00
|
|
|
|
2022-03-20 14:33:27 +01:00
|
|
|
private static final Logger LOG = LogManager.getLogger(DatabaseSingleton.class);
|
2021-10-01 19:17:33 +02:00
|
|
|
|
2022-03-20 14:33:27 +01:00
|
|
|
private final LLSingleton singleton;
|
2021-09-02 17:15:40 +02:00
|
|
|
private final Serializer<U> serializer;
|
2021-01-31 00:36:21 +01:00
|
|
|
|
2021-10-01 19:17:33 +02:00
|
|
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
2022-07-19 23:45:39 +02:00
|
|
|
public DatabaseSingleton(LLSingleton singleton, Serializer<U> serializer) {
|
2022-03-20 14:33:27 +01:00
|
|
|
this.singleton = singleton;
|
2021-11-08 16:33:41 +01:00
|
|
|
this.serializer = serializer;
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private LLSnapshot resolveSnapshot(@Nullable CompositeSnapshot snapshot) {
|
|
|
|
if (snapshot == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
2022-03-20 14:33:27 +01:00
|
|
|
return snapshot.getSnapshot(singleton);
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 13:13:14 +02:00
|
|
|
private U deserializeValue(Buffer value) {
|
2021-10-19 00:22:05 +02:00
|
|
|
try {
|
|
|
|
U deserializedValue;
|
2022-05-20 10:20:00 +02:00
|
|
|
try (value) {
|
|
|
|
deserializedValue = serializer.deserialize(value);
|
2021-10-19 00:22:05 +02:00
|
|
|
}
|
2022-05-26 13:13:14 +02:00
|
|
|
return deserializedValue;
|
2022-03-11 17:59:46 +01:00
|
|
|
} catch (IndexOutOfBoundsException ex) {
|
|
|
|
var exMessage = ex.getMessage();
|
|
|
|
if (exMessage != null && exMessage.contains("read 0 to 0, write 0 to ")) {
|
2022-05-26 13:13:14 +02:00
|
|
|
LOG.error("Unexpected zero-bytes value at " + singleton.getDatabaseName()
|
|
|
|
+ ":" + singleton.getColumnName() + ":" + singleton.getName());
|
|
|
|
return null;
|
2022-03-11 17:59:46 +01:00
|
|
|
} else {
|
2022-05-26 13:13:14 +02:00
|
|
|
throw ex;
|
2022-03-11 17:59:46 +01:00
|
|
|
}
|
2021-08-22 21:23:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:20:00 +02:00
|
|
|
private Buffer serializeValue(U value) throws SerializationException {
|
2021-10-19 00:22:05 +02:00
|
|
|
var valSizeHint = serializer.getSerializedSizeHint();
|
|
|
|
if (valSizeHint == -1) valSizeHint = 128;
|
2022-05-20 10:20:00 +02:00
|
|
|
var valBuf = singleton.getAllocator().allocate(valSizeHint);
|
|
|
|
try {
|
2021-10-19 00:22:05 +02:00
|
|
|
serializer.serialize(value, valBuf);
|
2022-05-20 10:20:00 +02:00
|
|
|
return valBuf;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
valBuf.close();
|
|
|
|
throw ex;
|
2021-10-19 00:22:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 00:36:21 +01:00
|
|
|
@Override
|
2022-05-21 23:49:06 +02:00
|
|
|
public Mono<U> get(@Nullable CompositeSnapshot snapshot) {
|
2022-05-26 13:13:14 +02:00
|
|
|
var resultMono = singleton.get(resolveSnapshot(snapshot));
|
|
|
|
return Mono.usingWhen(resultMono,
|
|
|
|
result -> Mono.fromSupplier(() -> this.deserializeValue(result)),
|
2022-06-20 12:30:33 +02:00
|
|
|
LLUtils::finalizeResource
|
2022-05-26 13:13:14 +02:00
|
|
|
);
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 14:45:48 +01:00
|
|
|
@Override
|
|
|
|
public Mono<Void> set(U value) {
|
|
|
|
return singleton.set(Mono.fromCallable(() -> serializeValue(value)));
|
|
|
|
}
|
|
|
|
|
2021-01-31 00:36:21 +01:00
|
|
|
@Override
|
2021-01-31 19:52:47 +01:00
|
|
|
public Mono<U> setAndGetPrevious(U value) {
|
2022-05-26 13:13:14 +02:00
|
|
|
var resultMono = Flux
|
2022-03-20 14:33:27 +01:00
|
|
|
.concat(singleton.get(null), singleton.set(Mono.fromCallable(() -> serializeValue(value))).then(Mono.empty()))
|
2022-05-26 13:13:14 +02:00
|
|
|
.last();
|
|
|
|
return Mono.usingWhen(resultMono,
|
|
|
|
result -> Mono.fromSupplier(() -> this.deserializeValue(result)),
|
2022-06-20 12:30:33 +02:00
|
|
|
LLUtils::finalizeResource
|
2022-05-26 13:13:14 +02:00
|
|
|
);
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|
|
|
|
|
2021-02-06 19:21:31 +01:00
|
|
|
@Override
|
2021-08-22 21:23:22 +02:00
|
|
|
public Mono<U> update(SerializationFunction<@Nullable U, @Nullable U> updater,
|
2022-03-02 18:33:58 +01:00
|
|
|
UpdateReturnMode updateReturnMode) {
|
2022-05-26 13:13:14 +02:00
|
|
|
var resultMono = singleton
|
|
|
|
.update((oldValueSer) -> {
|
|
|
|
try (oldValueSer) {
|
|
|
|
U result;
|
|
|
|
if (oldValueSer == null) {
|
|
|
|
result = updater.apply(null);
|
|
|
|
} else {
|
|
|
|
U deserializedValue = serializer.deserialize(oldValueSer);
|
|
|
|
result = updater.apply(deserializedValue);
|
|
|
|
}
|
|
|
|
if (result == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return serializeValue(result);
|
2021-05-12 19:02:51 +02:00
|
|
|
}
|
2022-05-26 13:13:14 +02:00
|
|
|
}
|
|
|
|
}, updateReturnMode);
|
|
|
|
return Mono.usingWhen(resultMono,
|
|
|
|
result -> Mono.fromSupplier(() -> this.deserializeValue(result)),
|
2022-06-20 12:30:33 +02:00
|
|
|
LLUtils::finalizeResource
|
2022-05-26 13:13:14 +02:00
|
|
|
);
|
2021-05-08 03:09:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-03-02 18:33:58 +01:00
|
|
|
public Mono<Delta<U>> updateAndGetDelta(SerializationFunction<@Nullable U, @Nullable U> updater) {
|
2022-03-20 14:33:27 +01:00
|
|
|
return singleton
|
|
|
|
.updateAndGetDelta((oldValueSer) -> {
|
2021-09-08 00:22:39 +02:00
|
|
|
try (oldValueSer) {
|
2021-10-19 00:22:05 +02:00
|
|
|
U result;
|
|
|
|
if (oldValueSer == null) {
|
|
|
|
result = updater.apply(null);
|
|
|
|
} else {
|
2022-04-01 01:30:56 +02:00
|
|
|
U deserializedValue = serializer.deserialize(oldValueSer);
|
2021-10-19 00:22:05 +02:00
|
|
|
result = updater.apply(deserializedValue);
|
|
|
|
}
|
2021-09-08 00:22:39 +02:00
|
|
|
if (result == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
2022-05-20 10:20:00 +02:00
|
|
|
return serializeValue(result);
|
2021-09-08 00:22:39 +02:00
|
|
|
}
|
2021-05-12 19:02:51 +02:00
|
|
|
}
|
2022-07-03 01:32:13 +02:00
|
|
|
}).transform(mono -> LLUtils.mapLLDelta(mono, serialized -> serializer.deserialize(serialized)));
|
2021-02-06 19:21:31 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 14:45:48 +01:00
|
|
|
@Override
|
|
|
|
public Mono<Void> clear() {
|
|
|
|
return singleton.set(Mono.empty());
|
|
|
|
}
|
|
|
|
|
2021-01-31 00:36:21 +01:00
|
|
|
@Override
|
2021-01-31 19:52:47 +01:00
|
|
|
public Mono<U> clearAndGetPrevious() {
|
2022-05-26 13:13:14 +02:00
|
|
|
var resultMono = Flux.concat(singleton.get(null), singleton.set(Mono.empty()).then(Mono.empty())).last();
|
|
|
|
return Mono.usingWhen(resultMono,
|
|
|
|
result -> Mono.fromSupplier(() -> this.deserializeValue(result)),
|
2022-06-20 12:30:33 +02:00
|
|
|
LLUtils::finalizeResource
|
2022-05-26 13:13:14 +02:00
|
|
|
);
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-24 16:43:07 +01:00
|
|
|
public Mono<Long> leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) {
|
2022-03-20 14:33:27 +01:00
|
|
|
return singleton.get(null).map(unused -> 1L).defaultIfEmpty(0L);
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Boolean> isEmpty(@Nullable CompositeSnapshot snapshot) {
|
2022-03-20 14:33:27 +01:00
|
|
|
return singleton.get(null).map(t -> false).defaultIfEmpty(true);
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
2021-04-30 19:15:04 +02:00
|
|
|
|
|
|
|
@Override
|
2021-09-23 20:57:28 +02:00
|
|
|
public Flux<BadBlock> badBlocks() {
|
2022-03-20 14:33:27 +01:00
|
|
|
return Flux.empty();
|
2021-04-30 19:15:04 +02:00
|
|
|
}
|
2021-06-26 02:35:33 +02:00
|
|
|
|
|
|
|
@Override
|
2022-07-19 23:45:39 +02:00
|
|
|
protected void onClose() {
|
2021-09-23 20:57:28 +02:00
|
|
|
|
2021-06-26 02:35:33 +02:00
|
|
|
}
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|