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

97 lines
3.0 KiB
Java
Raw Normal View History

package it.cavallium.dbengine.database.collections;
import io.netty.buffer.ByteBuf;
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-02-06 19:21:31 +01:00
import java.util.Optional;
import java.util.function.Function;
import org.jetbrains.annotations.Nullable;
import reactor.core.publisher.Mono;
import static io.netty.buffer.Unpooled.*;
2021-01-31 19:52:47 +01:00
public class DatabaseSingle<U> implements DatabaseStageEntry<U> {
private final LLDictionary dictionary;
private final ByteBuf key;
private final Serializer<U, ByteBuf> serializer;
public DatabaseSingle(LLDictionary dictionary, ByteBuf key, Serializer<U, ByteBuf> serializer) {
this.dictionary = dictionary;
if (!key.isDirect()) {
throw new IllegalArgumentException("Key must be direct");
}
this.key = key;
2021-01-31 19:52:47 +01:00
this.serializer = serializer;
}
private LLSnapshot resolveSnapshot(@Nullable CompositeSnapshot snapshot) {
if (snapshot == null) {
return null;
} else {
return snapshot.getSnapshot(dictionary);
}
}
@Override
2021-03-18 16:19:41 +01:00
public Mono<U> get(@Nullable CompositeSnapshot snapshot, boolean existsAlmostCertainly) {
return dictionary.get(resolveSnapshot(snapshot), key.retain(), existsAlmostCertainly).map(this::deserialize);
}
@Override
2021-01-31 19:52:47 +01:00
public Mono<U> setAndGetPrevious(U value) {
ByteBuf valueByteBuf = serialize(value);
return dictionary
.put(key.retain(), valueByteBuf.retain(), LLDictionaryResultType.PREVIOUS_VALUE)
.map(this::deserialize)
.doFinally(s -> valueByteBuf.release());
}
2021-02-06 19:21:31 +01:00
@Override
public Mono<Boolean> update(Function<@Nullable U, @Nullable U> updater, boolean existsAlmostCertainly) {
return dictionary.update(key.retain(), (oldValueSer) -> {
var result = updater.apply(oldValueSer == null ? null : this.deserialize(oldValueSer));
if (result == null) {
return null;
} else {
return this.serialize(result);
}
}, existsAlmostCertainly);
2021-02-06 19:21:31 +01:00
}
@Override
2021-01-31 19:52:47 +01:00
public Mono<U> clearAndGetPrevious() {
return dictionary.remove(key.retain(), LLDictionaryResultType.PREVIOUS_VALUE).map(this::deserialize);
}
@Override
2021-02-24 16:43:07 +01:00
public Mono<Long> leavesCount(@Nullable CompositeSnapshot snapshot, boolean fast) {
return dictionary
.isRangeEmpty(resolveSnapshot(snapshot), LLRange.single(key.retain()))
.map(empty -> empty ? 0L : 1L);
}
@Override
public Mono<Boolean> isEmpty(@Nullable CompositeSnapshot snapshot) {
return dictionary
.isRangeEmpty(resolveSnapshot(snapshot), LLRange.single(key.retain()));
}
2021-01-31 19:52:47 +01:00
//todo: temporary wrapper. convert the whole class to buffers
private U deserialize(ByteBuf bytes) {
return serializer.deserialize(bytes);
2021-01-31 19:52:47 +01:00
}
//todo: temporary wrapper. convert the whole class to buffers
private ByteBuf serialize(U bytes) {
return serializer.serialize(bytes);
2021-01-31 19:52:47 +01:00
}
@Override
public void release() {
key.release();
}
}