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 48b1159..73d7300 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/RocksDBUtil.java +++ b/src/main/java/org/bdware/sc/boundry/utils/RocksDBUtil.java @@ -12,6 +12,8 @@ import wrp.jdk.nashorn.internal.runtime.ScriptObject; import wrp.jdk.nashorn.internal.scripts.JO; import java.io.File; +import java.util.HashMap; +import java.util.Map; @PermissionStub(permission = Permission.RocksDB) public class RocksDBUtil { @@ -19,32 +21,45 @@ public class RocksDBUtil { RocksDB.loadLibrary(); } + static Map cacheDB = new HashMap<>(); RocksDB rocksDB; + String path; public RocksDBUtil(String path, boolean readOnly) { try { + this.path = path; Options options = new Options(); options.setCreateIfMissing(true); File parent = new File("./ContractDB/" + ContractProcess.instance.getContractName()); - path = new File(parent, path).getAbsolutePath(); - File lockFile = new File(path, "LOCK"); + 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); } + } catch (RocksDBException e) { e.printStackTrace(); } } public static RocksDBUtil loadDB(String path, boolean readOnly) { - return new RocksDBUtil(path, readOnly); + if (cacheDB.containsKey(path)) return cacheDB.get(path); + RocksDBUtil ret = new RocksDBUtil(path, readOnly); + cacheDB.put(path, ret); + return ret; } public static RocksDBUtil loadDB(String path, String readOnly) { - return new RocksDBUtil(path, Boolean.parseBoolean(readOnly)); + return loadDB(path, Boolean.parseBoolean(readOnly)); + } + + public void close() { + rocksDB.close(); + cacheDB.remove(path); } public String get(String key) {