2021-01-31 19:52:47 +01:00
|
|
|
package it.cavallium.dbengine.database.collections;
|
|
|
|
|
2023-09-26 00:34:44 +02:00
|
|
|
import it.cavallium.dbengine.client.VerificationProgress;
|
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.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;
|
2023-02-09 23:34:25 +01:00
|
|
|
import java.util.stream.Stream;
|
2021-01-31 19:52:47 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
|
2023-02-09 23:34:25 +01:00
|
|
|
public class DatabaseSingleMapped<A, B> implements DatabaseStageEntry<A> {
|
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
|
|
|
|
2023-02-09 23:34:25 +01:00
|
|
|
public DatabaseSingleMapped(DatabaseStageEntry<B> serializedSingle, Mapper<A, B> mapper) {
|
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
|
|
|
}
|
|
|
|
|
2023-02-09 23:34:25 +01:00
|
|
|
private DatabaseSingleMapped(DatabaseStage<B> serializedSingle, Mapper<A, B> mapper) {
|
2021-09-23 20:57:28 +02:00
|
|
|
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-01-31 19:52:47 +01:00
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
public A get(@Nullable CompositeSnapshot snapshot) {
|
|
|
|
var data = serializedSingle.get(snapshot);
|
|
|
|
if (data == null) return null;
|
|
|
|
return this.unMap(data);
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
public A getOrDefault(@Nullable CompositeSnapshot snapshot, A defaultValue) {
|
|
|
|
var value = serializedSingle.get(snapshot);
|
|
|
|
if (value == null) return defaultValue;
|
|
|
|
return this.unMap(value);
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
public void set(A value) {
|
|
|
|
B mappedValue = value != null ? map(value) : null;
|
|
|
|
serializedSingle.set(mappedValue);
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
public A setAndGetPrevious(A value) {
|
|
|
|
var mappedValue = value != null ? map(value) : null;
|
|
|
|
var prev = serializedSingle.setAndGetPrevious(mappedValue);
|
|
|
|
return prev != null ? unMap(prev) : null;
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
public boolean setAndGetChanged(A value) {
|
|
|
|
var mappedValue = value != null ? map(value) : null;
|
|
|
|
return serializedSingle.setAndGetChanged(mappedValue);
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
2021-02-06 19:21:31 +01:00
|
|
|
|
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
public A update(SerializationFunction<@Nullable A, @Nullable A> updater, UpdateReturnMode updateReturnMode) {
|
2023-03-27 22:00:32 +02:00
|
|
|
var mappedUpdater = new CachedSerializationFunction<>(updater, this::map, this::unMap);
|
|
|
|
serializedSingle.update(mappedUpdater, UpdateReturnMode.NOTHING);
|
|
|
|
return mappedUpdater.getResult(updateReturnMode);
|
2021-05-08 03:09:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
public Delta<A> updateAndGetDelta(SerializationFunction<@Nullable A, @Nullable A> updater) {
|
2023-03-27 22:00:32 +02:00
|
|
|
var mappedUpdater = new CachedSerializationFunction<>(updater, this::map, this::unMap);
|
|
|
|
serializedSingle.update(mappedUpdater, UpdateReturnMode.NOTHING);
|
|
|
|
return mappedUpdater.getDelta();
|
2021-02-06 19:21:31 +01:00
|
|
|
}
|
2021-01-31 19:52:47 +01:00
|
|
|
|
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
public void clear() {
|
|
|
|
serializedSingle.clear();
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
public A clearAndGetPrevious() {
|
|
|
|
var prev = serializedSingle.clearAndGetPrevious();
|
|
|
|
return prev != null ? unMap(prev) : null;
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
public boolean clearAndGetStatus() {
|
2021-01-31 19:52:47 +01:00
|
|
|
return serializedSingle.clearAndGetStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
public long leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) {
|
2021-02-24 16:43:07 +01:00
|
|
|
return serializedSingle.leavesCount(snapshot, fast);
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-02-09 23:34:25 +01:00
|
|
|
public boolean isEmpty(@Nullable CompositeSnapshot snapshot) {
|
2021-01-31 19:52:47 +01:00
|
|
|
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
|
2023-09-26 00:34:44 +02:00
|
|
|
public Stream<VerificationProgress> badBlocks() {
|
2021-06-26 02:35:33 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|