2021-01-31 00:36:21 +01:00
|
|
|
package it.cavallium.dbengine.database.collections;
|
|
|
|
|
|
|
|
import it.cavallium.dbengine.client.CompositeSnapshot;
|
|
|
|
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-02-02 19:40:37 +01:00
|
|
|
import it.cavallium.dbengine.database.serialization.Serializer;
|
2021-01-31 00:36:21 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
2021-01-31 19:52:47 +01:00
|
|
|
public class DatabaseSingle<U> implements DatabaseStageEntry<U> {
|
2021-01-31 00:36:21 +01:00
|
|
|
|
|
|
|
private final LLDictionary dictionary;
|
|
|
|
private final byte[] key;
|
2021-02-01 12:19:17 +01:00
|
|
|
private final Serializer<U, byte[]> serializer;
|
2021-01-31 00:36:21 +01:00
|
|
|
|
2021-02-01 12:19:17 +01:00
|
|
|
public DatabaseSingle(LLDictionary dictionary, byte[] key, Serializer<U, byte[]> serializer) {
|
2021-01-31 00:36:21 +01:00
|
|
|
this.dictionary = dictionary;
|
|
|
|
this.key = key;
|
2021-01-31 19:52:47 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-31 19:52:47 +01:00
|
|
|
public Mono<U> get(@Nullable CompositeSnapshot snapshot) {
|
|
|
|
return dictionary.get(resolveSnapshot(snapshot), key).map(this::deserialize);
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-31 19:52:47 +01:00
|
|
|
public Mono<U> setAndGetPrevious(U value) {
|
|
|
|
return dictionary.put(key, serialize(value), LLDictionaryResultType.PREVIOUS_VALUE).map(this::deserialize);
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-31 19:52:47 +01:00
|
|
|
public Mono<U> clearAndGetPrevious() {
|
|
|
|
return dictionary.remove(key, LLDictionaryResultType.PREVIOUS_VALUE).map(this::deserialize);
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Long> size(@Nullable CompositeSnapshot snapshot, boolean fast) {
|
|
|
|
return dictionary
|
|
|
|
.isRangeEmpty(resolveSnapshot(snapshot), LLRange.single(key))
|
|
|
|
.map(empty -> empty ? 0L : 1L);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Boolean> isEmpty(@Nullable CompositeSnapshot snapshot) {
|
|
|
|
return dictionary
|
|
|
|
.isRangeEmpty(resolveSnapshot(snapshot), LLRange.single(key));
|
|
|
|
}
|
2021-01-31 19:52:47 +01:00
|
|
|
|
|
|
|
//todo: temporary wrapper. convert the whole class to buffers
|
|
|
|
private U deserialize(byte[] bytes) {
|
2021-02-01 12:19:17 +01:00
|
|
|
return serializer.deserialize(bytes);
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//todo: temporary wrapper. convert the whole class to buffers
|
|
|
|
private byte[] serialize(U bytes) {
|
2021-02-01 12:19:17 +01:00
|
|
|
return serializer.serialize(bytes);
|
2021-01-31 19:52:47 +01:00
|
|
|
}
|
2021-01-31 00:36:21 +01:00
|
|
|
}
|