CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/collections/DatabaseMapSingle.java

229 lines
6.8 KiB
Java
Raw Normal View History

package it.cavallium.dbengine.database.collections;
2022-03-16 13:47:56 +01:00
import io.netty5.buffer.api.Buffer;
import io.netty5.buffer.api.Drop;
import io.netty5.buffer.api.Owned;
import io.netty5.buffer.api.Send;
import io.netty5.buffer.api.internal.ResourceSupport;
2021-06-26 02:35:33 +02:00
import it.cavallium.dbengine.client.BadBlock;
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.LLDictionary;
import it.cavallium.dbengine.database.LLDictionaryResultType;
import it.cavallium.dbengine.database.LLRange;
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-03-11 17:59:46 +01:00
import java.util.concurrent.atomic.AtomicLong;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
2021-06-26 02:35:33 +02:00
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
2021-08-22 21:23:22 +02:00
import reactor.core.publisher.SynchronousSink;
2022-03-20 14:33:27 +01:00
public class DatabaseMapSingle<U> extends ResourceSupport<DatabaseStage<U>, DatabaseMapSingle<U>> implements
2021-09-23 20:57:28 +02:00
DatabaseStageEntry<U> {
2022-03-20 14:33:27 +01:00
private static final Logger LOG = LogManager.getLogger(DatabaseMapSingle.class);
2021-10-01 19:17:33 +02:00
2022-03-11 17:59:46 +01:00
private final AtomicLong totalZeroBytesErrors = new AtomicLong();
2022-03-20 14:33:27 +01:00
private static final Drop<DatabaseMapSingle<?>> DROP = new Drop<>() {
2021-10-01 19:17:33 +02:00
@Override
2022-03-20 14:33:27 +01:00
public void drop(DatabaseMapSingle<?> obj) {
2021-10-01 19:17:33 +02:00
try {
obj.key.close();
} catch (Throwable ex) {
2022-03-11 17:59:46 +01:00
LOG.error("Failed to close key", ex);
2021-10-01 19:17:33 +02:00
}
if (obj.onClose != null) {
obj.onClose.run();
}
}
@Override
2022-03-20 14:33:27 +01:00
public Drop<DatabaseMapSingle<?>> fork() {
2021-10-01 19:17:33 +02:00
return this;
}
@Override
2022-03-20 14:33:27 +01:00
public void attach(DatabaseMapSingle<?> obj) {
2021-10-01 19:17:33 +02:00
}
};
private final LLDictionary dictionary;
2021-08-29 23:18:03 +02:00
private final Mono<Send<Buffer>> keyMono;
2021-09-02 17:15:40 +02:00
private final Serializer<U> serializer;
2021-09-23 20:57:28 +02:00
private Buffer key;
2021-10-01 19:17:33 +02:00
private Runnable onClose;
2021-09-23 20:57:28 +02:00
2021-10-01 19:17:33 +02:00
@SuppressWarnings({"unchecked", "rawtypes"})
2022-03-20 14:33:27 +01:00
public DatabaseMapSingle(LLDictionary dictionary, Buffer key, Serializer<U> serializer,
2021-10-01 19:17:33 +02:00
Runnable onClose) {
2022-03-20 14:33:27 +01:00
super((Drop<DatabaseMapSingle<U>>) (Drop) DROP);
2021-11-08 16:33:41 +01:00
this.dictionary = dictionary;
this.key = key;
this.keyMono = LLUtils.lazyRetain(this.key);
this.serializer = serializer;
this.onClose = onClose;
}
private LLSnapshot resolveSnapshot(@Nullable CompositeSnapshot snapshot) {
if (snapshot == null) {
return null;
} else {
return snapshot.getSnapshot(dictionary);
}
}
2021-08-29 23:18:03 +02:00
private void deserializeValue(Send<Buffer> value, SynchronousSink<U> sink) {
2021-10-19 00:22:05 +02:00
try {
U deserializedValue;
try (var valueBuf = value.receive()) {
deserializedValue = serializer.deserialize(valueBuf);
}
sink.next(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 ")) {
LOG.error("Unexpected zero-bytes value at "
+ dictionary.getDatabaseName() + ":" + dictionary.getColumnName() + ":" + LLUtils.toStringSafe(key));
2022-03-11 17:59:46 +01:00
sink.complete();
} else {
sink.error(ex);
}
2021-08-22 21:23:22 +02:00
} catch (SerializationException ex) {
sink.error(ex);
}
}
2021-10-19 00:22:05 +02:00
private Send<Buffer> serializeValue(U value) throws SerializationException {
var valSizeHint = serializer.getSerializedSizeHint();
if (valSizeHint == -1) valSizeHint = 128;
try (var valBuf = dictionary.getAllocator().allocate(valSizeHint)) {
serializer.serialize(value, valBuf);
return valBuf.send();
}
}
@Override
2021-03-18 16:19:41 +01:00
public Mono<U> get(@Nullable CompositeSnapshot snapshot, boolean existsAlmostCertainly) {
return dictionary
2022-03-24 21:14:17 +01:00
.get(resolveSnapshot(snapshot), keyMono)
2021-08-22 21:23:22 +02:00
.handle(this::deserializeValue);
}
@Override
2021-01-31 19:52:47 +01:00
public Mono<U> setAndGetPrevious(U value) {
2021-08-29 23:18:03 +02:00
return dictionary
2021-10-19 00:22:05 +02:00
.put(keyMono, Mono.fromCallable(() -> serializeValue(value)), LLDictionaryResultType.PREVIOUS_VALUE)
2021-08-29 23:18:03 +02:00
.handle(this::deserializeValue);
}
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) {
return dictionary
.update(keyMono, (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 {
U deserializedValue;
try (var valueBuf = oldValueSer.receive()) {
deserializedValue = serializer.deserialize(valueBuf);
}
result = updater.apply(deserializedValue);
}
2021-09-08 00:22:39 +02:00
if (result == null) {
return null;
} else {
2021-11-08 16:33:41 +01:00
return serializeValue(result).receive();
2021-09-08 00:22:39 +02:00
}
2021-05-12 19:02:51 +02:00
}
2022-03-02 18:33:58 +01:00
}, updateReturnMode)
2021-08-22 21:23:22 +02:00
.handle(this::deserializeValue);
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) {
return dictionary
.updateAndGetDelta(keyMono, (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 {
U deserializedValue;
try (var valueBuf = oldValueSer.receive()) {
deserializedValue = serializer.deserialize(valueBuf);
}
result = updater.apply(deserializedValue);
}
2021-09-08 00:22:39 +02:00
if (result == null) {
return null;
} else {
2021-11-08 16:33:41 +01:00
return serializeValue(result).receive();
2021-09-08 00:22:39 +02:00
}
2021-05-12 19:02:51 +02:00
}
2022-03-02 18:33:58 +01:00
}).transform(mono -> LLUtils.mapLLDelta(mono, serialized -> {
2021-10-19 00:22:05 +02:00
try (var valueBuf = serialized.receive()) {
return serializer.deserialize(valueBuf);
}
}));
2021-02-06 19:21:31 +01:00
}
@Override
2021-01-31 19:52:47 +01:00
public Mono<U> clearAndGetPrevious() {
return dictionary
.remove(keyMono, LLDictionaryResultType.PREVIOUS_VALUE)
2021-08-22 21:23:22 +02:00
.handle(this::deserializeValue);
}
@Override
2021-02-24 16:43:07 +01:00
public Mono<Long> leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) {
return dictionary
.isRangeEmpty(resolveSnapshot(snapshot), keyMono.map(LLRange::single).map(ResourceSupport::send), false)
.map(empty -> empty ? 0L : 1L);
}
@Override
public Mono<Boolean> isEmpty(@Nullable CompositeSnapshot snapshot) {
return dictionary
.isRangeEmpty(resolveSnapshot(snapshot), keyMono.map(LLRange::single).map(ResourceSupport::send), true);
2021-01-31 19:52:47 +01:00
}
@Override
2021-09-23 20:57:28 +02:00
public Flux<BadBlock> badBlocks() {
return dictionary.badBlocks(keyMono.map(LLRange::single).map(ResourceSupport::send));
}
2021-06-26 02:35:33 +02:00
@Override
2021-09-23 20:57:28 +02:00
protected RuntimeException createResourceClosedException() {
throw new IllegalStateException("Closed");
}
@Override
2022-03-20 14:33:27 +01:00
protected Owned<DatabaseMapSingle<U>> prepareSend() {
2021-11-08 16:33:41 +01:00
var keySend = this.key.send();
2021-10-01 19:17:33 +02:00
var onClose = this.onClose;
return drop -> {
2021-11-08 16:33:41 +01:00
var key = keySend.receive();
2022-03-20 14:33:27 +01:00
var instance = new DatabaseMapSingle<>(dictionary, key, serializer, onClose);
2021-10-01 19:17:33 +02:00
drop.attach(instance);
return instance;
};
2021-09-23 20:57:28 +02:00
}
@Override
protected void makeInaccessible() {
this.key = null;
2021-10-01 19:17:33 +02:00
this.onClose = null;
2021-06-26 02:35:33 +02:00
}
}