mirror of
https://gitee.com/BDWare/cm
synced 2025-01-10 01:44:04 +00:00
refactor: prune unused code
This commit is contained in:
parent
9a5d3acf26
commit
ec7ee3f302
@ -1,14 +1,7 @@
|
||||
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 java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.bdware.sc.ContractManager.instance;
|
||||
|
||||
/**
|
||||
@ -24,92 +17,16 @@ public class EventCenter {
|
||||
* @return id of the node
|
||||
*/
|
||||
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) {
|
||||
return null;
|
||||
}
|
||||
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
|
||||
*
|
||||
|
@ -45,7 +45,7 @@ public class ContractConsumer implements IEventConsumer {
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return HashUtil.sha3(contract + handler);
|
||||
return HashUtil.sha3(contract, handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -34,7 +34,7 @@ public class MultiContractMeta implements IDSerializable {
|
||||
public transient PriorityQueue<ContractRequest> queue; // contract request
|
||||
public transient Map<Integer, String> uniReqIDMap; // 用于请求
|
||||
public transient Map<Integer, ResultCallback> resultMap; // 用于请求
|
||||
public transient PriorityQueue<TransRecord> trans_queue; // transRecord
|
||||
public transient PriorityQueue<TransRecord> transQueue; // transRecord
|
||||
AtomicInteger masterOrder = new AtomicInteger(-1);
|
||||
private String contractID;
|
||||
private boolean isPrivate = false;
|
||||
@ -54,7 +54,7 @@ public class MultiContractMeta implements IDSerializable {
|
||||
|
||||
public void initQueue() {
|
||||
queue = new PriorityQueue<>();
|
||||
trans_queue = new PriorityQueue<>();
|
||||
transQueue = new PriorityQueue<>();
|
||||
uniReqIDMap = new ConcurrentHashMap<>();
|
||||
resultMap = new ConcurrentHashMap<>();
|
||||
}
|
||||
@ -74,15 +74,15 @@ public class MultiContractMeta implements IDSerializable {
|
||||
public void init(MultiContractMeta old) {
|
||||
if (old != null && old.hasQueues()) {
|
||||
queue = old.queue;
|
||||
trans_queue = old.trans_queue;
|
||||
transQueue = old.transQueue;
|
||||
uniReqIDMap = old.uniReqIDMap;
|
||||
resultMap = old.resultMap;
|
||||
} else {
|
||||
if (null == queue) {
|
||||
queue = new PriorityQueue<>();
|
||||
}
|
||||
if (null == trans_queue) {
|
||||
trans_queue = new PriorityQueue<>();
|
||||
if (null == transQueue) {
|
||||
transQueue = new PriorityQueue<>();
|
||||
}
|
||||
if (null == uniReqIDMap) {
|
||||
uniReqIDMap = new ConcurrentHashMap<>();
|
||||
@ -94,7 +94,7 @@ public class MultiContractMeta implements IDSerializable {
|
||||
}
|
||||
|
||||
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() {
|
||||
|
Loading…
Reference in New Issue
Block a user