2020-12-07 22:15:18 +01:00
|
|
|
package it.cavallium.dbengine.database.disk;
|
|
|
|
|
2021-11-12 02:05:44 +01:00
|
|
|
import io.net5.buffer.api.Buffer;
|
|
|
|
import io.net5.buffer.api.BufferAllocator;
|
|
|
|
import io.net5.buffer.api.Send;
|
|
|
|
import it.cavallium.dbengine.database.Delta;
|
2021-02-11 22:27:43 +01:00
|
|
|
import it.cavallium.dbengine.database.LLSingleton;
|
|
|
|
import it.cavallium.dbengine.database.LLSnapshot;
|
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.serialization.SerializationFunction;
|
|
|
|
import it.unimi.dsi.fastutil.bytes.ByteList;
|
2020-12-07 22:15:18 +01:00
|
|
|
import java.io.IOException;
|
2021-03-04 22:01:50 +01:00
|
|
|
import java.util.Arrays;
|
2021-11-12 02:05:44 +01:00
|
|
|
import java.util.concurrent.Callable;
|
2020-12-07 22:15:18 +01:00
|
|
|
import java.util.function.Function;
|
2021-11-12 02:05:44 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2020-12-07 22:15:18 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import org.rocksdb.ColumnFamilyHandle;
|
|
|
|
import org.rocksdb.ReadOptions;
|
|
|
|
import org.rocksdb.RocksDB;
|
|
|
|
import org.rocksdb.RocksDBException;
|
|
|
|
import org.rocksdb.Snapshot;
|
2021-11-12 02:05:44 +01:00
|
|
|
import org.rocksdb.WriteOptions;
|
2021-01-30 10:52:14 +01:00
|
|
|
import reactor.core.publisher.Mono;
|
2021-02-11 22:27:43 +01:00
|
|
|
import reactor.core.scheduler.Scheduler;
|
2021-09-05 14:23:46 +02:00
|
|
|
import reactor.core.scheduler.Schedulers;
|
2020-12-07 22:15:18 +01:00
|
|
|
|
|
|
|
public class LLLocalSingleton implements LLSingleton {
|
|
|
|
|
2021-11-12 02:05:44 +01:00
|
|
|
static final ReadOptions EMPTY_READ_OPTIONS = new UnreleasableReadOptions(new UnmodifiableReadOptions());
|
|
|
|
static final WriteOptions EMPTY_WRITE_OPTIONS = new UnreleasableWriteOptions(new UnmodifiableWriteOptions());
|
|
|
|
private final RocksDBColumn db;
|
2020-12-07 22:15:18 +01:00
|
|
|
private final Function<LLSnapshot, Snapshot> snapshotResolver;
|
|
|
|
private final byte[] name;
|
2021-11-12 02:05:44 +01:00
|
|
|
private final Mono<Send<Buffer>> nameMono;
|
2020-12-07 22:15:18 +01:00
|
|
|
private final String databaseName;
|
2021-09-07 11:26:10 +02:00
|
|
|
private final Scheduler dbScheduler;
|
2020-12-07 22:15:18 +01:00
|
|
|
|
2021-11-12 02:05:44 +01:00
|
|
|
public LLLocalSingleton(RocksDBColumn db,
|
2020-12-07 22:15:18 +01:00
|
|
|
Function<LLSnapshot, Snapshot> snapshotResolver,
|
|
|
|
String databaseName,
|
|
|
|
byte[] name,
|
2021-09-07 11:26:10 +02:00
|
|
|
Scheduler dbScheduler,
|
2020-12-07 22:15:18 +01:00
|
|
|
byte[] defaultValue) throws RocksDBException {
|
|
|
|
this.db = db;
|
|
|
|
this.databaseName = databaseName;
|
|
|
|
this.snapshotResolver = snapshotResolver;
|
|
|
|
this.name = name;
|
2021-11-12 02:05:44 +01:00
|
|
|
this.nameMono = Mono.fromCallable(() -> {
|
|
|
|
var alloc = db.getAllocator();
|
|
|
|
try (var nameBuf = alloc.allocate(this.name.length)) {
|
|
|
|
nameBuf.writeBytes(this.name);
|
|
|
|
return nameBuf.send();
|
|
|
|
}
|
|
|
|
});
|
2021-09-07 11:26:10 +02:00
|
|
|
this.dbScheduler = dbScheduler;
|
2021-09-05 14:23:46 +02:00
|
|
|
if (Schedulers.isInNonBlockingThread()) {
|
|
|
|
throw new UnsupportedOperationException("Initialized in a nonblocking thread");
|
|
|
|
}
|
2021-11-12 02:05:44 +01:00
|
|
|
if (db.get(EMPTY_READ_OPTIONS, this.name, true) == null) {
|
|
|
|
db.put(EMPTY_WRITE_OPTIONS, this.name, defaultValue);
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 02:05:44 +01:00
|
|
|
private <T> @NotNull Mono<T> runOnDb(Callable<@Nullable T> callable) {
|
|
|
|
return Mono.fromCallable(callable).subscribeOn(dbScheduler);
|
|
|
|
}
|
|
|
|
|
2020-12-07 22:15:18 +01:00
|
|
|
private ReadOptions resolveSnapshot(LLSnapshot snapshot) {
|
|
|
|
if (snapshot != null) {
|
|
|
|
return new ReadOptions().setSnapshot(snapshotResolver.apply(snapshot));
|
|
|
|
} else {
|
|
|
|
return EMPTY_READ_OPTIONS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 02:05:44 +01:00
|
|
|
@Override
|
|
|
|
public BufferAllocator getAllocator() {
|
|
|
|
return db.getAllocator();
|
|
|
|
}
|
|
|
|
|
2020-12-07 22:15:18 +01:00
|
|
|
@Override
|
2021-01-30 10:52:14 +01:00
|
|
|
public Mono<byte[]> get(@Nullable LLSnapshot snapshot) {
|
|
|
|
return Mono
|
2021-09-05 14:23:46 +02:00
|
|
|
.fromCallable(() -> {
|
|
|
|
if (Schedulers.isInNonBlockingThread()) {
|
|
|
|
throw new UnsupportedOperationException("Called get in a nonblocking thread");
|
|
|
|
}
|
2021-11-12 02:05:44 +01:00
|
|
|
return db.get(resolveSnapshot(snapshot), name, true);
|
2021-09-05 14:23:46 +02:00
|
|
|
})
|
2021-09-07 11:26:10 +02:00
|
|
|
.onErrorMap(cause -> new IOException("Failed to read " + Arrays.toString(name), cause))
|
|
|
|
.subscribeOn(dbScheduler);
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-30 10:52:14 +01:00
|
|
|
public Mono<Void> set(byte[] value) {
|
|
|
|
return Mono
|
|
|
|
.<Void>fromCallable(() -> {
|
2021-09-05 14:23:46 +02:00
|
|
|
if (Schedulers.isInNonBlockingThread()) {
|
|
|
|
throw new UnsupportedOperationException("Called set in a nonblocking thread");
|
|
|
|
}
|
2021-11-12 02:05:44 +01:00
|
|
|
db.put(EMPTY_WRITE_OPTIONS, name, value);
|
2021-01-30 10:52:14 +01:00
|
|
|
return null;
|
|
|
|
})
|
2021-09-07 11:26:10 +02:00
|
|
|
.onErrorMap(cause -> new IOException("Failed to write " + Arrays.toString(name), cause))
|
|
|
|
.subscribeOn(dbScheduler);
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
|
2021-11-12 02:05:44 +01:00
|
|
|
@Override
|
|
|
|
public Mono<Send<Buffer>> update(SerializationFunction<@Nullable Send<Buffer>, @Nullable Buffer> updater,
|
|
|
|
UpdateReturnMode updateReturnMode) {
|
|
|
|
return Mono.usingWhen(nameMono, keySend -> runOnDb(() -> {
|
|
|
|
if (Schedulers.isInNonBlockingThread()) {
|
|
|
|
throw new UnsupportedOperationException("Called update in a nonblocking thread");
|
|
|
|
}
|
|
|
|
UpdateAtomicResultMode returnMode = switch (updateReturnMode) {
|
|
|
|
case NOTHING -> UpdateAtomicResultMode.NOTHING;
|
|
|
|
case GET_NEW_VALUE -> UpdateAtomicResultMode.CURRENT;
|
|
|
|
case GET_OLD_VALUE -> UpdateAtomicResultMode.PREVIOUS;
|
|
|
|
};
|
|
|
|
UpdateAtomicResult result = db.updateAtomic(EMPTY_READ_OPTIONS, EMPTY_WRITE_OPTIONS, keySend, updater,
|
|
|
|
true, returnMode);
|
|
|
|
return switch (updateReturnMode) {
|
|
|
|
case NOTHING -> null;
|
|
|
|
case GET_NEW_VALUE -> ((UpdateAtomicResultCurrent) result).current();
|
|
|
|
case GET_OLD_VALUE -> ((UpdateAtomicResultPrevious) result).previous();
|
|
|
|
};
|
|
|
|
}).onErrorMap(cause -> new IOException("Failed to read or write", cause)),
|
|
|
|
keySend -> Mono.fromRunnable(keySend::close));
|
|
|
|
}
|
|
|
|
|
2020-12-07 22:15:18 +01:00
|
|
|
@Override
|
|
|
|
public String getDatabaseName() {
|
|
|
|
return databaseName;
|
|
|
|
}
|
|
|
|
}
|