From ec7ee3f30200f3d8525b1a980edb1d3a08f045df Mon Sep 17 00:00:00 2001 From: "Frank.R.Wu" Date: Mon, 1 Nov 2021 15:04:57 +0800 Subject: [PATCH] refactor: prune unused code --- .../java/org/bdware/sc/event/EventCenter.java | 91 +------------------ .../sc/event/clients/ContractConsumer.java | 2 +- .../bdware/sc/units/MultiContractMeta.java | 12 +-- 3 files changed, 11 insertions(+), 94 deletions(-) diff --git a/src/main/java/org/bdware/sc/event/EventCenter.java b/src/main/java/org/bdware/sc/event/EventCenter.java index f64b202..ab5c6c7 100644 --- a/src/main/java/org/bdware/sc/event/EventCenter.java +++ b/src/main/java/org/bdware/sc/event/EventCenter.java @@ -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 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 * diff --git a/src/main/java/org/bdware/sc/event/clients/ContractConsumer.java b/src/main/java/org/bdware/sc/event/clients/ContractConsumer.java index 260ea15..2d6642f 100644 --- a/src/main/java/org/bdware/sc/event/clients/ContractConsumer.java +++ b/src/main/java/org/bdware/sc/event/clients/ContractConsumer.java @@ -45,7 +45,7 @@ public class ContractConsumer implements IEventConsumer { @Override public String getId() { - return HashUtil.sha3(contract + handler); + return HashUtil.sha3(contract, handler); } @Override diff --git a/src/main/java/org/bdware/sc/units/MultiContractMeta.java b/src/main/java/org/bdware/sc/units/MultiContractMeta.java index 2a619cb..9771b77 100644 --- a/src/main/java/org/bdware/sc/units/MultiContractMeta.java +++ b/src/main/java/org/bdware/sc/units/MultiContractMeta.java @@ -34,7 +34,7 @@ public class MultiContractMeta implements IDSerializable { public transient PriorityQueue queue; // contract request public transient Map uniReqIDMap; // 用于请求 public transient Map resultMap; // 用于请求 - public transient PriorityQueue trans_queue; // transRecord + public transient PriorityQueue 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() {