fix: fix bugs in RocksDBUtil

use correct path in RocksDB.open
This commit is contained in:
Frank.R.Wu 2021-11-18 12:57:05 +08:00
parent 4e7863b38b
commit 2f7411d5e6
2 changed files with 22 additions and 13 deletions

View File

@ -119,7 +119,6 @@ public class ContractProcess {
}
System.out.println("[Create CP]");
instance = new ContractProcess(port, cmi);
;
}
public static boolean isArchiveFile(File file) {
@ -169,8 +168,9 @@ public class ContractProcess {
}
public static String getContractDir() {
if (instance!=null && instance.cn!=null && instance.cn.getContractName()!=null)
if (null != instance && null != instance.cn && null != instance.cn.getContractName()) {
return instance.cn.getContractName();
}
return "debug";
}

View File

@ -1,5 +1,7 @@
package org.bdware.sc.boundry.utils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.sc.ContractProcess;
import org.bdware.sc.compiler.PermissionStub;
import org.bdware.sc.node.Permission;
@ -17,11 +19,14 @@ import java.util.Map;
@PermissionStub(permission = Permission.RocksDB)
public class RocksDBUtil {
private static final Logger LOGGER = LogManager.getLogger(RocksDBUtil.class);
static Map<String, RocksDBUtil> cacheDB = new HashMap<>();
static {
RocksDB.loadLibrary();
}
static Map<String, RocksDBUtil> cacheDB = new HashMap<>();
RocksDB rocksDB;
String path;
@ -33,22 +38,26 @@ public class RocksDBUtil {
File parent = new File("./ContractDB/" + ContractProcess.getContractDir());
File dir = new File(parent, path);
if (!dir.exists()) dir.mkdirs();
File lockFile = new File(dir, "LOCK");
lockFile.delete();
if (readOnly) {
rocksDB = RocksDB.openReadOnly(options, path);
} else {
rocksDB = RocksDB.open(options, path);
LOGGER.info("init RocksDB in " + dir.getAbsolutePath());
if (!dir.exists()) {
LOGGER.trace("create directory " + dir.getAbsolutePath() + ": " + dir.mkdirs());
}
File lockFile = new File(dir, "LOCK");
LOGGER.trace("delete file" + lockFile.getAbsolutePath() + ": " + lockFile.delete());
if (readOnly) {
rocksDB = RocksDB.openReadOnly(options, dir.getAbsolutePath());
} else {
rocksDB = RocksDB.open(options, dir.getAbsolutePath());
}
} catch (RocksDBException e) {
e.printStackTrace();
}
}
public static RocksDBUtil loadDB(String path, boolean readOnly) {
if (cacheDB.containsKey(path)) return cacheDB.get(path);
if (cacheDB.containsKey(path)) {
return cacheDB.get(path);
}
RocksDBUtil ret = new RocksDBUtil(path, readOnly);
cacheDB.put(path, ret);
return ret;