mirror of
https://gitee.com/BDWare/cp.git
synced 2025-01-10 01:44:08 +00:00
fix RocksDBUtil bugs
This commit is contained in:
parent
3c92f29d63
commit
32c882ec07
@ -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<String, RocksDBUtil> 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) {
|
||||
|
Loading…
Reference in New Issue
Block a user