2020-12-07 22:15:18 +01:00
|
|
|
package it.cavallium.dbengine.database.disk;
|
|
|
|
|
2021-02-11 22:27:43 +01:00
|
|
|
import it.cavallium.dbengine.database.LLSingleton;
|
|
|
|
import it.cavallium.dbengine.database.LLSnapshot;
|
2020-12-07 22:15:18 +01:00
|
|
|
import java.io.IOException;
|
2021-03-04 22:01:50 +01:00
|
|
|
import java.util.Arrays;
|
2020-12-07 22:15:18 +01:00
|
|
|
import java.util.function.Function;
|
|
|
|
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-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;
|
2020-12-07 22:15:18 +01:00
|
|
|
|
|
|
|
public class LLLocalSingleton implements LLSingleton {
|
|
|
|
|
|
|
|
private static final ReadOptions EMPTY_READ_OPTIONS = new ReadOptions();
|
|
|
|
private final RocksDB db;
|
|
|
|
private final ColumnFamilyHandle cfh;
|
|
|
|
private final Function<LLSnapshot, Snapshot> snapshotResolver;
|
|
|
|
private final byte[] name;
|
|
|
|
private final String databaseName;
|
2021-02-11 22:27:43 +01:00
|
|
|
private final Scheduler dbScheduler;
|
2020-12-07 22:15:18 +01:00
|
|
|
|
|
|
|
public LLLocalSingleton(RocksDB db, ColumnFamilyHandle singletonListColumn,
|
|
|
|
Function<LLSnapshot, Snapshot> snapshotResolver,
|
|
|
|
String databaseName,
|
|
|
|
byte[] name,
|
2021-02-11 22:27:43 +01:00
|
|
|
Scheduler dbScheduler,
|
2020-12-07 22:15:18 +01:00
|
|
|
byte[] defaultValue) throws RocksDBException {
|
|
|
|
this.db = db;
|
|
|
|
this.cfh = singletonListColumn;
|
|
|
|
this.databaseName = databaseName;
|
|
|
|
this.snapshotResolver = snapshotResolver;
|
|
|
|
this.name = name;
|
2021-02-11 22:27:43 +01:00
|
|
|
this.dbScheduler = dbScheduler;
|
2020-12-07 22:15:18 +01:00
|
|
|
if (db.get(cfh, this.name) == null) {
|
|
|
|
db.put(cfh, this.name, defaultValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private ReadOptions resolveSnapshot(LLSnapshot snapshot) {
|
|
|
|
if (snapshot != null) {
|
|
|
|
return new ReadOptions().setSnapshot(snapshotResolver.apply(snapshot));
|
|
|
|
} else {
|
|
|
|
return EMPTY_READ_OPTIONS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-30 10:52:14 +01:00
|
|
|
public Mono<byte[]> get(@Nullable LLSnapshot snapshot) {
|
|
|
|
return Mono
|
|
|
|
.fromCallable(() -> db.get(cfh, resolveSnapshot(snapshot), name))
|
2021-03-04 22:01:50 +01:00
|
|
|
.onErrorMap(cause -> new IOException("Failed to read " + Arrays.toString(name), cause))
|
2021-02-11 22:27:43 +01:00
|
|
|
.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(() -> {
|
|
|
|
db.put(cfh, name, value);
|
|
|
|
return null;
|
|
|
|
})
|
2021-03-04 22:01:50 +01:00
|
|
|
.onErrorMap(cause -> new IOException("Failed to write " + Arrays.toString(name), cause))
|
2021-02-11 22:27:43 +01:00
|
|
|
.subscribeOn(dbScheduler);
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDatabaseName() {
|
|
|
|
return databaseName;
|
|
|
|
}
|
|
|
|
}
|