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]"); System.out.println("[Create CP]");
instance = new ContractProcess(port, cmi); instance = new ContractProcess(port, cmi);
;
} }
public static boolean isArchiveFile(File file) { public static boolean isArchiveFile(File file) {
@ -169,8 +168,9 @@ public class ContractProcess {
} }
public static String getContractDir() { 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 instance.cn.getContractName();
}
return "debug"; return "debug";
} }

View File

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