CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/memory/LLMemorySingleton.java

53 lines
1.5 KiB
Java
Raw Normal View History

2021-07-18 19:37:24 +02:00
package it.cavallium.dbengine.database.memory;
2021-09-17 16:56:28 +02:00
import io.net5.buffer.api.Buffer;
import io.net5.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-31 15:50:11 +02:00
private final Mono<Send<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)
2021-08-31 15:50:11 +02:00
.writeBytes(singletonName)
.send());
2021-07-18 19:37:24 +02:00
}
@Override
public String getDatabaseName() {
return dict.getDatabaseName();
}
@Override
public Mono<byte[]> get(@Nullable LLSnapshot snapshot) {
return dict
.get(snapshot, singletonNameBufMono, false)
2021-07-18 19:37:24 +02:00
.map(b -> {
2021-08-31 15:50:11 +02:00
try (var buf = b.receive()) {
return LLUtils.toArray(buf);
2021-07-18 19:37:24 +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;
2021-08-31 15:50:11 +02:00
var bbVal = Mono.fromCallable(() -> dict.getAllocator().allocate(value.length).writeBytes(value).send());
return dict
.put(bbKey, bbVal, LLDictionaryResultType.VOID)
2021-07-18 19:37:24 +02:00
.then();
}
}