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

140 lines
4.5 KiB
Java
Raw Normal View History

package it.cavallium.dbengine.database.collections;
2023-03-06 12:19:08 +01:00
import it.cavallium.buffer.Buf;
import it.cavallium.buffer.BufDataInput;
import it.cavallium.buffer.BufDataOutput;
import it.cavallium.dbengine.client.CompositeSnapshot;
import it.cavallium.dbengine.client.DbProgress;
import it.cavallium.dbengine.client.SSTVerificationProgress;
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;
2023-03-27 22:00:32 +02:00
import it.cavallium.dbengine.database.disk.CachedSerializationFunction;
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;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Stream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
public final class DatabaseMapSingle<U> implements 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
private final LLDictionary dictionary;
private final Buf key;
2021-09-02 17:15:40 +02:00
private final Serializer<U> serializer;
2021-09-23 20:57:28 +02:00
public DatabaseMapSingle(LLDictionary dictionary, Buf key, Serializer<U> serializer) {
2021-11-08 16:33:41 +01:00
this.dictionary = dictionary;
this.key = key;
2021-11-08 16:33:41 +01:00
this.serializer = serializer;
}
private LLSnapshot resolveSnapshot(@Nullable CompositeSnapshot snapshot) {
if (snapshot == null) {
return null;
} else {
return snapshot.getSnapshot(dictionary);
}
}
private U deserializeValue(Buf value) {
2021-10-19 00:22:05 +02:00
try {
return serializer.deserialize(BufDataInput.create(value));
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 %s:%s:%s".formatted(dictionary.getDatabaseName(),
dictionary.getColumnName(),
LLUtils.toStringSafe(key)
));
2022-05-21 01:06:55 +02:00
return null;
2022-03-11 17:59:46 +01:00
} else {
2022-05-21 01:06:55 +02:00
throw ex;
2022-03-11 17:59:46 +01:00
}
2021-08-22 21:23:22 +02:00
}
}
private Buf serializeValue(U value) throws SerializationException {
BufDataOutput valBuf = BufDataOutput.create(serializer.getSerializedSizeHint());
serializer.serialize(value, valBuf);
return valBuf.asList();
2021-10-19 00:22:05 +02:00
}
@Override
public ForkJoinPool getDbReadPool() {
return dictionary.getDbReadPool();
}
@Override
public ForkJoinPool getDbWritePool() {
return dictionary.getDbWritePool();
}
@Override
public U get(@Nullable CompositeSnapshot snapshot) {
var result = dictionary.get(resolveSnapshot(snapshot), key);
if (result != null) {
return deserializeValue(result);
} else {
return null;
}
}
@Override
public U setAndGetPrevious(U value) {
var serializedKey = value != null ? serializeValue(value) : null;
var result = dictionary.put(key, serializedKey, LLDictionaryResultType.PREVIOUS_VALUE);
if (result != null) {
return deserializeValue(result);
} else {
return null;
}
}
2021-02-06 19:21:31 +01:00
@Override
2023-02-22 22:31:36 +01:00
public U update(SerializationFunction<@Nullable U, @Nullable U> updater, UpdateReturnMode updateReturnMode) {
2023-03-27 22:00:32 +02:00
var serializedUpdater = createUpdater(updater);
dictionary.update(key, serializedUpdater, UpdateReturnMode.NOTHING);
return serializedUpdater.getResult(updateReturnMode);
2021-05-08 03:09:00 +02:00
}
@Override
public Delta<U> updateAndGetDelta(SerializationFunction<@Nullable U, @Nullable U> updater) {
2023-03-27 22:00:32 +02:00
var serializedUpdater = createUpdater(updater);
dictionary.update(key, serializedUpdater, UpdateReturnMode.NOTHING);
return serializedUpdater.getDelta();
2021-02-06 19:21:31 +01:00
}
2023-03-27 22:00:32 +02:00
private CachedSerializationFunction<U, Buf, Buf> createUpdater(SerializationFunction<U, U> updater) {
return new CachedSerializationFunction<>(updater, this::serializeValue, this::deserializeValue);
}
@Override
public U clearAndGetPrevious() {
return deserializeValue(dictionary.remove(key, LLDictionaryResultType.PREVIOUS_VALUE));
}
@Override
public long leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) {
return dictionary.isRangeEmpty(resolveSnapshot(snapshot), LLRange.single(key), false) ? 0L : 1L;
2021-01-31 19:52:47 +01:00
}
@Override
public boolean isEmpty(@Nullable CompositeSnapshot snapshot) {
return dictionary.isRangeEmpty(resolveSnapshot(snapshot), LLRange.single(key), true);
}
2021-06-26 02:35:33 +02:00
@Override
public Stream<DbProgress<SSTVerificationProgress>> verifyChecksum() {
return dictionary.verifyChecksum(LLRange.single(key));
2021-06-26 02:35:33 +02:00
}
}