2021-07-18 19:37:24 +02:00
|
|
|
package it.cavallium.dbengine.database.memory;
|
|
|
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
|
import it.cavallium.dbengine.database.LLDictionaryResultType;
|
|
|
|
import it.cavallium.dbengine.database.LLSingleton;
|
|
|
|
import it.cavallium.dbengine.database.LLSnapshot;
|
|
|
|
import it.cavallium.dbengine.database.LLUtils;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
|
|
public class LLMemorySingleton implements LLSingleton {
|
|
|
|
|
|
|
|
private final LLMemoryDictionary dict;
|
|
|
|
private final byte[] singletonName;
|
2021-08-22 18:20:05 +02:00
|
|
|
private final Mono<ByteBuf> singletonNameBufMono;
|
2021-07-18 19:37:24 +02:00
|
|
|
|
|
|
|
public LLMemorySingleton(LLMemoryDictionary dict, byte[] singletonName) {
|
|
|
|
this.dict = dict;
|
|
|
|
this.singletonName = singletonName;
|
2021-08-22 18:20:05 +02:00
|
|
|
ByteBuf singletonNameBuf = Unpooled.wrappedBuffer(singletonName);
|
|
|
|
this.singletonNameBufMono = Mono.just(singletonNameBuf).map(ByteBuf::retain);
|
2021-07-18 19:37:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDatabaseName() {
|
|
|
|
return dict.getDatabaseName();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<byte[]> get(@Nullable LLSnapshot snapshot) {
|
2021-08-22 18:20:05 +02:00
|
|
|
return dict
|
|
|
|
.get(snapshot, singletonNameBufMono, false)
|
2021-07-18 19:37:24 +02:00
|
|
|
.map(b -> {
|
|
|
|
try {
|
|
|
|
return LLUtils.toArray(b);
|
|
|
|
} finally {
|
|
|
|
b.release();
|
|
|
|
}
|
2021-08-22 18:20:05 +02:00
|
|
|
});
|
2021-07-18 19:37:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Void> set(byte[] value) {
|
2021-08-22 18:20:05 +02:00
|
|
|
var bbKey = Mono.just(Unpooled.wrappedBuffer(singletonName)).map(ByteBuf::retain);
|
|
|
|
var bbVal = Mono.just(Unpooled.wrappedBuffer(value)).map(ByteBuf::retain);
|
|
|
|
return dict
|
|
|
|
.put(bbKey, bbVal, LLDictionaryResultType.VOID)
|
2021-07-18 19:37:24 +02:00
|
|
|
.then();
|
|
|
|
}
|
|
|
|
}
|