From 2f7411d5e6454dbf11ddad14d40e604347264752 Mon Sep 17 00:00:00 2001 From: "Frank.R.Wu" Date: Thu, 18 Nov 2021 12:57:05 +0800 Subject: [PATCH] fix: fix bugs in RocksDBUtil use correct path in RocksDB.open --- .../java/org/bdware/sc/ContractProcess.java | 4 +-- .../bdware/sc/boundry/utils/RocksDBUtil.java | 31 ++++++++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/bdware/sc/ContractProcess.java b/src/main/java/org/bdware/sc/ContractProcess.java index a28c86b..641ea24 100644 --- a/src/main/java/org/bdware/sc/ContractProcess.java +++ b/src/main/java/org/bdware/sc/ContractProcess.java @@ -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"; } diff --git a/src/main/java/org/bdware/sc/boundry/utils/RocksDBUtil.java b/src/main/java/org/bdware/sc/boundry/utils/RocksDBUtil.java index b79b71d..a46306b 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/RocksDBUtil.java +++ b/src/main/java/org/bdware/sc/boundry/utils/RocksDBUtil.java @@ -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 cacheDB = new HashMap<>(); + static { RocksDB.loadLibrary(); } - static Map cacheDB = new HashMap<>(); RocksDB rocksDB; String path; @@ -31,24 +36,28 @@ public class RocksDBUtil { Options options = new Options(); options.setCreateIfMissing(true); - File parent = new File("./ContractDB/" + ContractProcess.getContractDir()); + 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;