refactor: prune unused code

This commit is contained in:
Frank.R.Wu 2021-11-01 15:04:57 +08:00
parent 9a5d3acf26
commit ec7ee3f302
3 changed files with 11 additions and 94 deletions

View File

@ -1,14 +1,7 @@
package org.bdware.sc.event; package org.bdware.sc.event;
import org.bdware.sc.NodeCenterConn;
import org.bdware.sc.NodeCenterConn.NodeKey;
import org.bdware.sc.util.HashUtil;
import org.bdware.sc.util.JsonUtil; import org.bdware.sc.util.JsonUtil;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import static org.bdware.sc.ContractManager.instance; import static org.bdware.sc.ContractManager.instance;
/** /**
@ -24,92 +17,16 @@ public class EventCenter {
* @return id of the node * @return id of the node
*/ */
public String getCenterByTopic(String topic) { public String getCenterByTopic(String topic) {
String[] centers = getCentersByTopic(topic, 1); if (null == instance.nodeCenterConn) {
return null;
}
String[] centers = instance.nodeCenterConn.getClusterByKey(topic, 1);
if (null == centers) { if (null == centers) {
return null; return null;
} }
return centers[0]; return centers[0];
} }
/**
* get k nearest nodes to the topic in the hash function range
*
* @param topic the topic
* @param k the number of required node ids
* @return ids of k nearest nodes
*/
public String[] getCentersByTopic(String topic, int k) {
NodeCenterConn nodeCenterConn = instance.nodeCenterConn;
if (null == nodeCenterConn) {
return null;
}
NodeKey[] nodes = nodeCenterConn.listNodes();
if (nodes.length == 0) {
return null;
}
if (nodes.length == 1) {
return new String[]{nodeCenterConn.getNodeId(nodes[0].id)};
}
// get hash with enough length
String hash = HashUtil.sha3ToFixedLen(topic, nodes[0].id.length());
BigInteger biH = new BigInteger(hash, 16);
// binary search, to find the nearest node with hash
int l = 0, r = nodes.length - 1, m = 0,
comL = biH.compareTo(nodes[l].biId), comR = nodes[r].biId.compareTo(biH),
comM;
String selected;
do {
if (comL < 1) {
selected = nodes[l].id;
break;
}
if (comR < 1) {
selected = nodes[r].id;
break;
}
if (l + 1 == r) {
if (biH.subtract(nodes[l].biId).compareTo(nodes[r].biId.subtract(biH)) < 1) {
selected = nodes[l].id;
} else {
selected = nodes[r].id;
}
break;
}
m = (l + r) >> 1;
comM = biH.compareTo(nodes[m].biId);
if (comM < 1) {
r = m;
comR = -comM;
} else {
l = m;
comL = comM;
}
} while (true);
List<String> ret = new ArrayList<>();
ret.add(nodeCenterConn.getNodeId(selected));
if (k > 1) {
l = m - 1;
r = m + 1;
while (ret.size() < k && (l >= 0 || r < nodes.length)) {
if (l < 0) {
ret.add(nodeCenterConn.getNodeId(nodes[r++].id));
} else if (r >= nodes.length) {
ret.add(nodeCenterConn.getNodeId(nodes[l--].id));
} else {
if (biH.subtract(nodes[l].biId).compareTo(nodes[r].biId.subtract(biH)) < 1) {
ret.add(nodeCenterConn.getNodeId(nodes[l--].id));
} else {
ret.add(nodeCenterConn.getNodeId(nodes[r++].id));
}
}
}
}
return ret.toArray(new String[0]);
}
/** /**
* subscribe a topic in center * subscribe a topic in center
* *

View File

@ -45,7 +45,7 @@ public class ContractConsumer implements IEventConsumer {
@Override @Override
public String getId() { public String getId() {
return HashUtil.sha3(contract + handler); return HashUtil.sha3(contract, handler);
} }
@Override @Override

View File

@ -34,7 +34,7 @@ public class MultiContractMeta implements IDSerializable {
public transient PriorityQueue<ContractRequest> queue; // contract request public transient PriorityQueue<ContractRequest> queue; // contract request
public transient Map<Integer, String> uniReqIDMap; // 用于请求 public transient Map<Integer, String> uniReqIDMap; // 用于请求
public transient Map<Integer, ResultCallback> resultMap; // 用于请求 public transient Map<Integer, ResultCallback> resultMap; // 用于请求
public transient PriorityQueue<TransRecord> trans_queue; // transRecord public transient PriorityQueue<TransRecord> transQueue; // transRecord
AtomicInteger masterOrder = new AtomicInteger(-1); AtomicInteger masterOrder = new AtomicInteger(-1);
private String contractID; private String contractID;
private boolean isPrivate = false; private boolean isPrivate = false;
@ -54,7 +54,7 @@ public class MultiContractMeta implements IDSerializable {
public void initQueue() { public void initQueue() {
queue = new PriorityQueue<>(); queue = new PriorityQueue<>();
trans_queue = new PriorityQueue<>(); transQueue = new PriorityQueue<>();
uniReqIDMap = new ConcurrentHashMap<>(); uniReqIDMap = new ConcurrentHashMap<>();
resultMap = new ConcurrentHashMap<>(); resultMap = new ConcurrentHashMap<>();
} }
@ -74,15 +74,15 @@ public class MultiContractMeta implements IDSerializable {
public void init(MultiContractMeta old) { public void init(MultiContractMeta old) {
if (old != null && old.hasQueues()) { if (old != null && old.hasQueues()) {
queue = old.queue; queue = old.queue;
trans_queue = old.trans_queue; transQueue = old.transQueue;
uniReqIDMap = old.uniReqIDMap; uniReqIDMap = old.uniReqIDMap;
resultMap = old.resultMap; resultMap = old.resultMap;
} else { } else {
if (null == queue) { if (null == queue) {
queue = new PriorityQueue<>(); queue = new PriorityQueue<>();
} }
if (null == trans_queue) { if (null == transQueue) {
trans_queue = new PriorityQueue<>(); transQueue = new PriorityQueue<>();
} }
if (null == uniReqIDMap) { if (null == uniReqIDMap) {
uniReqIDMap = new ConcurrentHashMap<>(); uniReqIDMap = new ConcurrentHashMap<>();
@ -94,7 +94,7 @@ public class MultiContractMeta implements IDSerializable {
} }
private boolean hasQueues() { private boolean hasQueues() {
return null != queue && null != trans_queue && null != uniReqIDMap && null != resultMap; return null != queue && null != transQueue && null != uniReqIDMap && null != resultMap;
} }
public int getLastExeSeq() { public int getLastExeSeq() {