2022-03-05 15:46:40 +01:00
|
|
|
package it.cavallium.dbengine.lucene;
|
|
|
|
|
2022-03-16 13:47:56 +01:00
|
|
|
import io.netty5.buffer.api.BufferAllocator;
|
2022-03-05 15:46:40 +01:00
|
|
|
import it.cavallium.dbengine.lucene.directory.RocksDBInstance;
|
|
|
|
import it.cavallium.dbengine.lucene.directory.RocksdbFileStore;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
|
|
|
|
public class LuceneRocksDBManager {
|
|
|
|
|
|
|
|
private static final Logger LOG = LogManager.getLogger(LuceneRocksDBManager.class);
|
|
|
|
private final List<Map.Entry<Path, RocksDBInstance>> dbs = new ArrayList<>();
|
2022-03-09 02:29:38 +01:00
|
|
|
private BufferAllocator bufferAllocator;
|
|
|
|
|
|
|
|
public synchronized BufferAllocator getAllocator() {
|
|
|
|
if (bufferAllocator == null) {
|
|
|
|
bufferAllocator = BufferAllocator.offHeapPooled();
|
|
|
|
}
|
|
|
|
return bufferAllocator;
|
|
|
|
}
|
2022-03-05 15:46:40 +01:00
|
|
|
|
|
|
|
public synchronized RocksDBInstance getOrCreate(Path path) {
|
|
|
|
try {
|
|
|
|
for (var entry : dbs) {
|
|
|
|
if (Files.isSameFile(path, entry.getKey())) {
|
|
|
|
return entry.getValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RocksDBInstance db = RocksdbFileStore.createEmpty(path);
|
|
|
|
dbs.add(Map.entry(path, db));
|
|
|
|
return db;
|
|
|
|
} catch (IOException ex) {
|
|
|
|
throw new UnsupportedOperationException("Can't load RocksDB database at path: " + path, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void closeAll() {
|
|
|
|
for (Entry<Path, RocksDBInstance> db : dbs) {
|
|
|
|
try {
|
|
|
|
db.getValue().db().closeE();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
LOG.error("Failed to close lucene RocksDB database", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dbs.clear();
|
2022-03-09 02:29:38 +01:00
|
|
|
if (bufferAllocator != null) {
|
|
|
|
bufferAllocator.close();
|
|
|
|
}
|
|
|
|
bufferAllocator = null;
|
2022-03-05 15:46:40 +01:00
|
|
|
}
|
|
|
|
}
|