2021-07-18 19:37:24 +02:00
|
|
|
package it.cavallium.dbengine.database.memory;
|
|
|
|
|
2021-08-29 23:18:03 +02:00
|
|
|
import io.netty.buffer.api.Buffer;
|
2021-07-18 19:37:24 +02:00
|
|
|
import io.netty.buffer.Unpooled;
|
2021-08-31 09:14:46 +02:00
|
|
|
import io.netty.buffer.api.Send;
|
2021-07-18 19:37:24 +02:00
|
|
|
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-29 23:18:03 +02:00
|
|
|
private final Mono<Buffer> singletonNameBufMono;
|
2021-07-18 19:37:24 +02:00
|
|
|
|
|
|
|
public LLMemorySingleton(LLMemoryDictionary dict, byte[] singletonName) {
|
|
|
|
this.dict = dict;
|
|
|
|
this.singletonName = singletonName;
|
2021-08-31 09:14:46 +02:00
|
|
|
this.singletonNameBufMono = Mono.fromCallable(() -> dict
|
|
|
|
.getAllocator()
|
|
|
|
.allocate(singletonName.length)
|
|
|
|
.writeBytes(singletonName));
|
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 -> {
|
2021-08-31 09:14:46 +02:00
|
|
|
try (b) {
|
2021-07-18 19:37:24 +02:00
|
|
|
return LLUtils.toArray(b);
|
|
|
|
}
|
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-31 09:14:46 +02:00
|
|
|
var bbKey = singletonNameBufMono;
|
|
|
|
var bbVal = Mono.fromCallable(() -> dict.getAllocator().allocate(value.length).writeBytes(value));
|
2021-08-22 18:20:05 +02:00
|
|
|
return dict
|
|
|
|
.put(bbKey, bbVal, LLDictionaryResultType.VOID)
|
2021-07-18 19:37:24 +02:00
|
|
|
.then();
|
|
|
|
}
|
|
|
|
}
|