mirror of
https://gitee.com/BDWare/cp.git
synced 2025-01-10 01:44:08 +00:00
fix: binarySearch bugs in TimeSerialIndex
feat: support multi tag time index util
This commit is contained in:
parent
afad80ee2b
commit
b5ec0caccb
@ -0,0 +1,136 @@
|
||||
package org.bdware.sc.boundry.utils;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
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.db.MultiIndexTimeRocksDBUtil;
|
||||
import org.bdware.sc.node.Permission;
|
||||
import org.rocksdb.RocksDB;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@PermissionStub(permission = Permission.MultiTagIndexDB)
|
||||
public class MultiTagIndexDBUtil {
|
||||
private static final Logger LOGGER = LogManager.getLogger(MultiIndexTimeRocksDBUtil.class);
|
||||
|
||||
static Map<String, MultiTagIndexDBUtil> cacheDB = new HashMap<>();
|
||||
|
||||
static {
|
||||
RocksDB.loadLibrary();
|
||||
}
|
||||
|
||||
MultiIndexTimeRocksDBUtil rocksDB;
|
||||
String path;
|
||||
|
||||
public MultiTagIndexDBUtil(String path, String tableName) {
|
||||
try {
|
||||
this.path = path;
|
||||
File parent = new File("./ContractDB/" + ContractProcess.getContractDir());
|
||||
File dir = new File(parent, 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());
|
||||
rocksDB = new MultiIndexTimeRocksDBUtil(dir.getAbsolutePath(), tableName);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static MultiTagIndexDBUtil loadDB(String path, String tableName) {
|
||||
if (cacheDB.containsKey(path)) {
|
||||
return cacheDB.get(path);
|
||||
}
|
||||
MultiTagIndexDBUtil ret = new MultiTagIndexDBUtil(path, tableName);
|
||||
cacheDB.put(path, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
rocksDB.close();
|
||||
cacheDB.remove(path);
|
||||
}
|
||||
|
||||
public String get(String key) {
|
||||
try {
|
||||
return new String(rocksDB.get(key));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public long size() {
|
||||
try {
|
||||
return rocksDB.size();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void put(String label, String value) {
|
||||
try {
|
||||
rocksDB.put(label, value);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> getTags(String prefix) {
|
||||
return rocksDB.getIndexStartWith(prefix);
|
||||
}
|
||||
|
||||
public List<String> getAllTags() {
|
||||
List<String> list = rocksDB.getAllIndexKey();
|
||||
return list;
|
||||
}
|
||||
|
||||
public long getSize(String tag) {
|
||||
try {
|
||||
return rocksDB.size(tag);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
return 0L;
|
||||
}
|
||||
|
||||
public long queryOffset(String tag, long startTimestamp) {
|
||||
return rocksDB.queryOffset(tag, startTimestamp);
|
||||
}
|
||||
|
||||
public List<JsonObject> queryByOffset(String tag, long offset, int count) {
|
||||
return rocksDB.queryByOffset(tag, offset, count);
|
||||
}
|
||||
|
||||
public List<Long> countInInterval(String tag, long startTime, long endTime, long interval) {
|
||||
List<Long> ret = new ArrayList<>();
|
||||
if (interval <= 0) return ret;
|
||||
long start = rocksDB.queryOffset(tag, startTime);
|
||||
long delta;
|
||||
startTime += interval;
|
||||
for (; startTime < endTime; startTime += interval) {
|
||||
delta = rocksDB.queryOffset(tag, startTime);
|
||||
ret.add(delta - start);
|
||||
start = delta;
|
||||
}
|
||||
delta = rocksDB.queryOffset(tag, endTime);
|
||||
ret.add(delta - start);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public List<String> queryInInterval(String tag, long startTime, long endTime) {
|
||||
return rocksDB.queryByDateAsString(tag, startTime, endTime);
|
||||
}
|
||||
}
|
@ -6,6 +6,8 @@ import wrp.jdk.nashorn.internal.runtime.PropertyMap;
|
||||
import wrp.jdk.nashorn.internal.scripts.JO;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class JSONTool {
|
||||
@ -44,7 +46,40 @@ public class JSONTool {
|
||||
if (obj == null) return JsonNull.INSTANCE;
|
||||
if (obj.getClass() == wrp.jdk.nashorn.internal.runtime.Undefined.class)
|
||||
return JsonNull.INSTANCE;
|
||||
if (obj.getClass() == ScriptObjectMirror.class) {
|
||||
if (obj instanceof JsonElement) return (JsonElement) obj;
|
||||
if (obj.getClass().isArray()) {
|
||||
Object[] arr = (Object[]) obj;
|
||||
recorded.add(obj);
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
jsonArray.add(convertMirrorToJsonInternal(arr[i], recorded));
|
||||
}
|
||||
return jsonArray;
|
||||
} else if (List.class.isAssignableFrom(obj.getClass())) {
|
||||
List arr = (List) obj;
|
||||
recorded.add(arr);
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
for (int i = 0; i < arr.size(); i++) {
|
||||
jsonArray.add(convertMirrorToJsonInternal(arr.get(i), recorded));
|
||||
}
|
||||
return jsonArray;
|
||||
} else if (List.class.isAssignableFrom(obj.getClass())) {
|
||||
List arr = (List) obj;
|
||||
recorded.add(arr);
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
for (int i = 0; i < arr.size(); i++) {
|
||||
jsonArray.add(convertMirrorToJsonInternal(arr.get(i), recorded));
|
||||
}
|
||||
return jsonArray;
|
||||
} else if (Set.class.isAssignableFrom(obj.getClass())) {
|
||||
Set arr = (Set) obj;
|
||||
recorded.add(arr);
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
for (Object k : arr) {
|
||||
jsonArray.add(convertMirrorToJsonInternal(k, recorded));
|
||||
}
|
||||
return jsonArray;
|
||||
} else if (obj.getClass() == ScriptObjectMirror.class) {
|
||||
recorded.add(obj);
|
||||
ScriptObjectMirror som = (ScriptObjectMirror) obj;
|
||||
if (som.isFunction()) {
|
||||
@ -52,7 +87,6 @@ public class JSONTool {
|
||||
}
|
||||
if (som.isArray()) {
|
||||
JsonArray jarray = new JsonArray();
|
||||
|
||||
for (String str : som.getOwnKeys(true)) {
|
||||
try {
|
||||
if (Integer.parseInt(str) >= 0)
|
||||
@ -95,6 +129,14 @@ public class JSONTool {
|
||||
}
|
||||
return jo;
|
||||
}
|
||||
} else if (Map.class.isAssignableFrom(obj.getClass())) {
|
||||
Map arr = (Map) obj;
|
||||
recorded.add(arr);
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
for (Object k : arr.keySet()) {
|
||||
jsonObject.add(k.toString(), convertMirrorToJsonInternal(k, recorded));
|
||||
}
|
||||
return jsonObject;
|
||||
} else if (obj.getClass() == jdk.internal.dynalink.beans.StaticClass.class) {
|
||||
return JsonNull.INSTANCE;
|
||||
} else if (obj instanceof Number) {
|
||||
|
Loading…
Reference in New Issue
Block a user