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;
|
2021-11-12 02:05:44 +01:00
|
|
|
import io.net5.buffer.api.BufferAllocator;
|
2021-09-17 16:56:28 +02:00
|
|
|
import io.net5.buffer.api.Send;
|
2021-11-12 02:05:44 +01:00
|
|
|
import it.cavallium.dbengine.database.LLDelta;
|
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;
|
2021-11-12 02:05:44 +01:00
|
|
|
import it.cavallium.dbengine.database.UpdateMode;
|
|
|
|
import it.cavallium.dbengine.database.UpdateReturnMode;
|
|
|
|
import it.cavallium.dbengine.database.disk.UpdateAtomicResult;
|
|
|
|
import it.cavallium.dbengine.database.disk.UpdateAtomicResultCurrent;
|
|
|
|
import it.cavallium.dbengine.database.disk.UpdateAtomicResultMode;
|
|
|
|
import it.cavallium.dbengine.database.disk.UpdateAtomicResultPrevious;
|
|
|
|
import it.cavallium.dbengine.database.serialization.SerializationException;
|
|
|
|
import it.cavallium.dbengine.database.serialization.SerializationFunction;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
2021-07-18 19:37:24 +02:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import reactor.core.publisher.Mono;
|
2021-11-12 02:05:44 +01:00
|
|
|
import reactor.core.scheduler.Schedulers;
|
2021-07-18 19:37:24 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-11-12 02:05:44 +01:00
|
|
|
@Override
|
|
|
|
public BufferAllocator getAllocator() {
|
|
|
|
return dict.getAllocator();
|
|
|
|
}
|
|
|
|
|
2021-07-18 19:37:24 +02:00
|
|
|
@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 15:50:11 +02:00
|
|
|
try (var buf = b.receive()) {
|
|
|
|
return LLUtils.toArray(buf);
|
2021-07-18 19:37:24 +02:00
|
|
|
}
|
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;
|
2021-08-31 15:50:11 +02:00
|
|
|
var bbVal = Mono.fromCallable(() -> dict.getAllocator().allocate(value.length).writeBytes(value).send());
|
2021-08-22 18:20:05 +02:00
|
|
|
return dict
|
|
|
|
.put(bbKey, bbVal, LLDictionaryResultType.VOID)
|
2021-07-18 19:37:24 +02:00
|
|
|
.then();
|
|
|
|
}
|
2021-11-12 02:05:44 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Send<Buffer>> update(SerializationFunction<@Nullable Send<Buffer>, @Nullable Buffer> updater,
|
|
|
|
UpdateReturnMode updateReturnMode) {
|
|
|
|
return dict.update(singletonNameBufMono, updater, updateReturnMode, true);
|
|
|
|
}
|
2021-07-18 19:37:24 +02:00
|
|
|
}
|