2021-01-31 00:36:21 +01:00
|
|
|
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;
|
2023-09-26 00:34:44 +02:00
|
|
|
import it.cavallium.dbengine.client.VerificationProgress;
|
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;
|
2021-01-31 00:36:21 +01:00
|
|
|
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;
|
2023-02-09 23:34:25 +01:00
|
|
|
import java.util.stream.Stream;
|
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;
|
|
|
|
|
2023-02-09 23:34:25 +01:00
|
|
|
public final class DatabaseMapSingle<U> 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(DatabaseMapSingle.class);
|
2021-10-01 19:17:33 +02:00
|
|
|
|
2021-01-31 00:36:21 +01:00
|
|
|
private final LLDictionary dictionary;
|
2023-02-09 23:34:25 +01:00
|
|
|
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
|
|
|
|
2023-02-09 23:34:25 +01:00
|
|
|
public DatabaseMapSingle(LLDictionary dictionary, Buf key, Serializer<U> serializer) {
|
2021-11-08 16:33:41 +01:00
|
|
|
this.dictionary = dictionary;
|
2023-02-09 23:34:25 +01:00
|
|
|
this.key = key;
|
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 {
|
|
|
|
return snapshot.getSnapshot(dictionary);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 23:34:25 +01:00
|
|
|
private U deserializeValue(Buf value) {
|
2021-10-19 00:22:05 +02:00
|
|
|
try {
|
2023-02-09 23:34:25 +01:00
|
|
|
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 ")) {
|
2023-02-09 23:34:25 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 23:34:25 +01: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
|
|
|
}
|
|
|
|
|
2021-01-31 00:36:21 +01:00
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
public U get(@Nullable CompositeSnapshot snapshot) {
|
|
|
|
var result = dictionary.get(resolveSnapshot(snapshot), key);
|
|
|
|
if (result != null) {
|
|
|
|
return deserializeValue(result);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
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-01-31 00:36:21 +01:00
|
|
|
}
|
|
|
|
|
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
|
2023-02-09 23:34:25 +01:00
|
|
|
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);
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
public U clearAndGetPrevious() {
|
|
|
|
return deserializeValue(dictionary.remove(key, LLDictionaryResultType.PREVIOUS_VALUE));
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
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
|
|
|
}
|
2021-04-30 19:15:04 +02:00
|
|
|
|
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
public boolean isEmpty(@Nullable CompositeSnapshot snapshot) {
|
|
|
|
return dictionary.isRangeEmpty(resolveSnapshot(snapshot), LLRange.single(key), true);
|
2021-04-30 19:15:04 +02:00
|
|
|
}
|
2021-06-26 02:35:33 +02:00
|
|
|
|
|
|
|
@Override
|
2023-09-26 01:18:24 +02:00
|
|
|
public Stream<VerificationProgress> verifyChecksum() {
|
|
|
|
return dictionary.verifyChecksum(LLRange.single(key));
|
2021-06-26 02:35:33 +02:00
|
|
|
}
|
2023-02-09 23:34:25 +01:00
|
|
|
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|