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

52 lines
1.5 KiB
Java
Raw Normal View History

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;
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-29 23:18:03 +02:00
Buffer singletonNameBuf = Unpooled.wrappedBuffer(singletonName);
this.singletonNameBufMono = Mono.just(singletonNameBuf).map(Buffer::retain);
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 -> {
try {
return LLUtils.toArray(b);
} finally {
b.release();
}
});
2021-07-18 19:37:24 +02:00
}
@Override
public Mono<Void> set(byte[] value) {
2021-08-29 23:18:03 +02:00
var bbKey = Mono.just(Unpooled.wrappedBuffer(singletonName)).map(Buffer::retain);
var bbVal = Mono.just(Unpooled.wrappedBuffer(value)).map(Buffer::retain);
return dict
.put(bbKey, bbVal, LLDictionaryResultType.VOID)
2021-07-18 19:37:24 +02:00
.then();
}
}