feat: update DHTUtil

update DHTUtil.getClusterByKey to ignore prefix '04' of nodeIds
This commit is contained in:
Frank.R.Wu 2021-12-09 15:04:39 +08:00
parent 896cbfeb5b
commit 14e918fa18

View File

@ -16,14 +16,14 @@ public class DHTUtil {
if (nodes.length == 0) { if (nodes.length == 0) {
return null; return null;
} }
if (nodes.length <= k) { if (nodes.length == 1) {
return nodes; return nodes;
} }
String hash = HashUtil.sha3ToFixedLen(key, nodes[0].length()); String hash = HashUtil.sha3ToFixedLen(key, nodes[0].length() - 2);
int l = 0, r = nodes.length - 1, m, int l = 0, r = nodes.length - 1, m,
h2l = hash.compareTo(nodes[l]), r2h = nodes[r].compareTo(hash), h2l = hash.compareTo(nodes[l].substring(2)), r2h = nodes[r].substring(2).compareTo(hash),
h2m; h2m;
BigInteger bigH = null; BigInteger bigH = null;
String selected; String selected;
@ -41,7 +41,7 @@ public class DHTUtil {
if (l + 1 == r) { if (l + 1 == r) {
BigInteger bigL = getBigInteger(nodes[l]), BigInteger bigL = getBigInteger(nodes[l]),
bigR = getBigInteger(nodes[r]); bigR = getBigInteger(nodes[r]);
bigH = new BigInteger(hash.substring(2)); bigH = new BigInteger(hash);
if (bigR.subtract(bigH).compareTo(bigH.subtract(bigL)) > -1) { if (bigR.subtract(bigH).compareTo(bigH.subtract(bigL)) > -1) {
selected = nodes[l]; selected = nodes[l];
m = l; m = l;
@ -52,7 +52,7 @@ public class DHTUtil {
break; break;
} }
m = (l + r) >> 1; m = (l + r) >> 1;
h2m = hash.compareTo(nodes[m]); h2m = hash.compareTo(nodes[m].substring(2));
if (h2m < 1) { if (h2m < 1) {
r = m; r = m;
r2h = -h2m; r2h = -h2m;
@ -76,7 +76,7 @@ public class DHTUtil {
ret.add(nodes[l--]); ret.add(nodes[l--]);
} else { } else {
if (null == bigH) { if (null == bigH) {
bigH = new BigInteger(hash.substring(2)); bigH = new BigInteger(hash);
} }
if (getBigInteger(nodes[r]).subtract(bigH) if (getBigInteger(nodes[r]).subtract(bigH)
.compareTo(bigH.subtract(getBigInteger(nodes[l]))) > -1) { .compareTo(bigH.subtract(getBigInteger(nodes[l]))) > -1) {