2022-04-06 02:41:32 +02:00
|
|
|
package it.cavallium.dbengine.database.disk;
|
|
|
|
|
|
|
|
import static it.cavallium.dbengine.database.disk.LLTempHugePqEnv.getColumnOptions;
|
|
|
|
|
|
|
|
import com.google.common.primitives.Ints;
|
|
|
|
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
|
|
|
|
import io.netty5.buffer.api.BufferAllocator;
|
2022-06-30 13:54:55 +02:00
|
|
|
import it.cavallium.dbengine.utils.SimpleResource;
|
2022-04-06 02:41:32 +02:00
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
|
|
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
|
|
|
import java.io.Closeable;
|
|
|
|
import java.io.IOException;
|
2022-06-30 13:54:55 +02:00
|
|
|
import java.io.UncheckedIOException;
|
2022-04-06 02:41:32 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Objects;
|
2022-04-28 23:23:26 +02:00
|
|
|
import java.util.concurrent.locks.ReentrantLock;
|
|
|
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
2022-04-30 01:50:24 +02:00
|
|
|
import java.util.concurrent.locks.StampedLock;
|
2022-04-06 02:41:32 +02:00
|
|
|
import org.rocksdb.AbstractComparator;
|
|
|
|
import org.rocksdb.ColumnFamilyDescriptor;
|
|
|
|
import org.rocksdb.ColumnFamilyHandle;
|
|
|
|
import org.rocksdb.RocksDB;
|
|
|
|
import org.rocksdb.RocksDBException;
|
|
|
|
|
2022-06-30 13:54:55 +02:00
|
|
|
public class HugePqEnv extends SimpleResource {
|
2022-04-06 02:41:32 +02:00
|
|
|
|
|
|
|
private final RocksDB db;
|
2022-05-20 10:20:00 +02:00
|
|
|
private final ArrayList<ColumnFamilyHandle> defaultCfh;
|
|
|
|
private final Int2ObjectMap<ColumnFamilyHandle> cfhs = new Int2ObjectOpenHashMap<>();
|
2022-04-06 02:41:32 +02:00
|
|
|
|
2022-05-20 10:20:00 +02:00
|
|
|
public HugePqEnv(RocksDB db, ArrayList<ColumnFamilyHandle> defaultCfh) {
|
2022-04-06 02:41:32 +02:00
|
|
|
this.db = db;
|
|
|
|
this.defaultCfh = defaultCfh;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-06-30 13:54:55 +02:00
|
|
|
protected void onClose() {
|
2022-05-12 19:14:27 +02:00
|
|
|
for (var cfh : defaultCfh) {
|
2022-05-20 10:20:00 +02:00
|
|
|
db.destroyColumnFamilyHandle(cfh);
|
2022-04-06 02:41:32 +02:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
db.closeE();
|
|
|
|
} catch (RocksDBException e) {
|
2022-06-30 13:54:55 +02:00
|
|
|
throw new IllegalStateException(e);
|
2022-04-06 02:41:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int createColumnFamily(int name, AbstractComparator comparator) throws RocksDBException {
|
2022-05-20 10:20:00 +02:00
|
|
|
var cfh = db.createColumnFamily(new ColumnFamilyDescriptor(Ints.toByteArray(name), getColumnOptions(comparator)));
|
2022-04-06 02:41:32 +02:00
|
|
|
synchronized (cfhs) {
|
|
|
|
var prev = cfhs.put(name, cfh);
|
|
|
|
if (prev != null) {
|
|
|
|
throw new UnsupportedOperationException("Db " + name + " already exists");
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteColumnFamily(int db) throws RocksDBException {
|
2022-05-20 10:20:00 +02:00
|
|
|
ColumnFamilyHandle cfh;
|
2022-04-06 02:41:32 +02:00
|
|
|
synchronized (cfhs) {
|
|
|
|
cfh = cfhs.remove(db);
|
|
|
|
}
|
|
|
|
if (cfh != null) {
|
2022-05-20 10:20:00 +02:00
|
|
|
this.db.dropColumnFamily(cfh);
|
|
|
|
this.db.destroyColumnFamilyHandle(cfh);
|
2022-04-06 02:41:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public StandardRocksDBColumn openDb(int hugePqId) {
|
2022-05-20 10:20:00 +02:00
|
|
|
ColumnFamilyHandle cfh;
|
2022-04-06 02:41:32 +02:00
|
|
|
synchronized (cfhs) {
|
|
|
|
cfh = Objects.requireNonNull(cfhs.get(hugePqId), () -> "column " + hugePqId + " does not exist");
|
|
|
|
}
|
|
|
|
return new StandardRocksDBColumn(db,
|
|
|
|
true,
|
|
|
|
BufferAllocator.offHeapPooled(),
|
|
|
|
db.getName(),
|
|
|
|
cfh,
|
2022-04-28 23:23:26 +02:00
|
|
|
new CompositeMeterRegistry(),
|
2022-04-30 01:50:24 +02:00
|
|
|
new StampedLock()
|
2022-04-06 02:41:32 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|