Fixed pruneKey calculation

This commit is contained in:
armin 2018-04-28 18:53:08 +02:00
parent 7c283aa827
commit e209e5039c
5 changed files with 22 additions and 12 deletions

View file

@ -20,7 +20,7 @@ public class CnC {
public static final int MAX_BITS = 62; public static final int MAX_BITS = 62;
public static void main(String[] args) { public static void main(String[] args) {
KeyServer kts = KSFactory.build(54, 30, 90, TimeUnit.MINUTES, 0.62f); KeyServer kts = KSFactory.build(54, 30, 15, TimeUnit.MINUTES, 0.62f);
CommServer server = new CommServer(PORT, kts); CommServer server = new CommServer(PORT, kts);
try { try {
server.listen(); server.listen();

View file

@ -5,6 +5,7 @@ import org.btcollider.cnc.keysrv.impl.keytree.KeyTreeServer;
import org.btcollider.cnc.keysrv.impl.multistage.MultiStagedServer; import org.btcollider.cnc.keysrv.impl.multistage.MultiStagedServer;
public class KSFactory { public class KSFactory {
private KSFactory() {}
public static KeyServer build(int index, int depth, long maxWorkSpan) { public static KeyServer build(int index, int depth, long maxWorkSpan) {
return build(index, depth, maxWorkSpan, TimeUnit.MILLISECONDS); return build(index, depth, maxWorkSpan, TimeUnit.MILLISECONDS);
@ -16,10 +17,13 @@ public class KSFactory {
public static KeyServer build(int index, int depth, long maxWorkSpan, TimeUnit timeUnit, public static KeyServer build(int index, int depth, long maxWorkSpan, TimeUnit timeUnit,
float pruneFraction) { float pruneFraction) {
assert (pruneFraction >= 0.0 && pruneFraction <= 1.0); assert (pruneFraction >= 0.0 && pruneFraction <= 1.0);
long maxKey = ((long) 1 << index) | (((long) 1 << index) - 1); long maxKey = ((long) 1 << index) | (((long) 1 << index) - 1);
long pruneKey = (long) Math.floor(maxKey * pruneFraction); long minKey = ((long) 1 << index);
long pruneKey = (long) Math.floor((maxKey - minKey) * pruneFraction) + minKey;
return build(index, depth, maxWorkSpan, timeUnit, pruneKey); return build(index, depth, maxWorkSpan, timeUnit, pruneKey);
} }

View file

@ -16,8 +16,10 @@ public class KTConcierge implements Runnable {
@Override @Override
public void run() { public void run() {
synchronized (keyTree) {
recCheckInWork(keyTree); recCheckInWork(keyTree);
} }
}
private void recCheckInWork(KeyTree node) { private void recCheckInWork(KeyTree node) {
if (node == null) if (node == null)

View file

@ -134,11 +134,11 @@ public class KeyTreeServer implements KeyServer {
} }
private void recPrune(BitSet pruneKey, KeyTree node, int curIdx) { private void recPrune(BitSet pruneKey, KeyTree node, int curIdx) {
if (node.getValue() && !pruneKey.get(curIdx)) { // node[ix] = 0 and pruneK[ix] = 1 -> path if (!node.getValue() && pruneKey.get(curIdx)) { // node[ix] = 0 and pruneK[ix] = 1 -> path
// smaller // smaller
node.setSearched(true); node.setSearched(true);
return; return;
} else if (!node.getValue() && pruneKey.get(curIdx)) { // node[ix] = 1 and pruneK[ix] = 0 -> } else if (node.getValue() && !pruneKey.get(curIdx)) { // node[ix] = 1 and pruneK[ix] = 0 ->
// path // path
return; return;
} }

View file

@ -81,24 +81,28 @@ public class MultiStagedServer implements KeyServer {
keyRangeBuffer.setInWork(keyRange); keyRangeBuffer.setInWork(keyRange);
// strip out partition bits -> get subspace KeyRange only // strip out partition bits -> get subspace KeyRange only
long mask = (long) Math.pow(2, (subIndex + 1)) - 1; long mask = ((long) 1 << subIndex) - 1;
// add leading bit // add leading bit
long lb = (long) Math.pow(2, (subIndex + 1)); long lb = ((long) 1 << subIndex);
KeyRange secKR =
new KeyRange(((keyRange.getStart() & mask) | lb), ((keyRange.getEnd() & mask) | lb));
KeyRange secKR = new KeyRange(keyRange.getStart() & mask | lb, keyRange.getEnd() & mask | lb);
secKTS.setInWork(secKR); secKTS.setInWork(secKR);
} }
@Override @Override
public void setSearched(KeyRange keyRange) { public void setSearched(KeyRange keyRange) {
keyRangeBuffer.setSearched(keyRange); keyRangeBuffer.setSearched(keyRange);
// strip out partition bits -> get subspace KeyRange only // strip out partition bits -> get subspace KeyRange only
long mask = (long) Math.pow(2, (subIndex + 1)) - 1; long mask = ((long) 1 << subIndex) - 1;
// add leading bit // add leading bit
long lb = (long) Math.pow(2, (subIndex + 1)); long lb = ((long) 1 << subIndex);
KeyRange secKR =
new KeyRange(((keyRange.getStart() & mask) | lb), ((keyRange.getEnd() & mask) | lb));
KeyRange secKR = new KeyRange(keyRange.getStart() & mask | lb, keyRange.getEnd() & mask | lb);
secKTS.setSearched(secKR); secKTS.setSearched(secKR);
} }