From 191ef16499101e8d3656eb8c00036fbaaa7f0121 Mon Sep 17 00:00:00 2001 From: orbiter Date: Fri, 4 May 2007 14:11:50 +0000 Subject: [PATCH] fixed wrong ordering that caused bad dht selection git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@3646 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- .../anomic/kelondro/kelondroBase64Order.java | 20 ++++++++++-------- .../anomic/kelondro/kelondroNaturalOrder.java | 21 ++++++++++--------- source/de/anomic/kelondro/kelondroRowSet.java | 1 + source/de/anomic/yacy/yacySearch.java | 2 +- source/migration.java | 2 +- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/source/de/anomic/kelondro/kelondroBase64Order.java b/source/de/anomic/kelondro/kelondroBase64Order.java index 18d1d4301..857596c53 100644 --- a/source/de/anomic/kelondro/kelondroBase64Order.java +++ b/source/de/anomic/kelondro/kelondroBase64Order.java @@ -285,7 +285,11 @@ public class kelondroBase64Order extends kelondroAbstractOrder implements kelond long zeroCardinal = cardinalI(this.zero); long keyCardinal = cardinalI(key); if (keyCardinal > zeroCardinal) return keyCardinal - zeroCardinal; - return Long.MAX_VALUE - keyCardinal + zeroCardinal + 1; + return Long.MAX_VALUE - keyCardinal + zeroCardinal; + } + + private static final int sig(int x) { + return (x > 0) ? 1 : (x < 0) ? -1 : 0; } public final int compare(byte[] a, byte[] b) { @@ -301,11 +305,8 @@ public class kelondroBase64Order extends kelondroAbstractOrder implements kelond // we have an artificial start point. check all combinations int az = compares(a, aoffset, alength, zero, 0, Math.min(alength, zero.length)); // -1 if a < z; 0 if a == z; 1 if a > z int bz = compares(b, boffset, blength, zero, 0, Math.min(blength, zero.length)); // -1 if b < z; 0 if b == z; 1 if b > z - if ((az == 0) && (bz == 0)) return 0; - if (az == 0) return -1; - if (bz == 0) return 1; - if (az == bz) return compares(a, aoffset, alength, b, boffset, blength); - return bz; + if (az == bz) return compares(a, aoffset, alength, b, boffset, blength); + return sig(az - bz); } public final int compares(byte[] a, int aoffset, int alength, byte[] b, int boffset, int blength) { @@ -315,11 +316,9 @@ public class kelondroBase64Order extends kelondroAbstractOrder implements kelond int i = 0; final int al = Math.min(alength, a.length - aoffset); final int bl = Math.min(blength, b.length - boffset); - if (al > bl) return 1; - if (al < bl) return -1; byte ac, bc; byte acc, bcc; - while (i < al) { + while ((i < al) && (i < bl)) { assert (i + aoffset < a.length) : "i = " + i + ", aoffset = " + aoffset + ", a.length = " + a.length + ", a = " + serverLog.arrayList(a, aoffset, al); assert (i + boffset < b.length) : "i = " + i + ", boffset = " + boffset + ", b.length = " + b.length + ", b = " + serverLog.arrayList(b, boffset, al); ac = a[aoffset + i]; @@ -336,6 +335,9 @@ public class kelondroBase64Order extends kelondroAbstractOrder implements kelond // else the bytes are equal and it may go on yet undecided i++; } + // compare length + if (al > bl) return 1; + if (al < bl) return -1; // they are equal return 0; } diff --git a/source/de/anomic/kelondro/kelondroNaturalOrder.java b/source/de/anomic/kelondro/kelondroNaturalOrder.java index e3bfb4a8a..cf8997d7d 100644 --- a/source/de/anomic/kelondro/kelondroNaturalOrder.java +++ b/source/de/anomic/kelondro/kelondroNaturalOrder.java @@ -96,7 +96,7 @@ public class kelondroNaturalOrder extends kelondroAbstractOrder implements kelon long zeroCardinal = cardinalI(this.zero); long keyCardinal = cardinalI(key); if (keyCardinal > zeroCardinal) return keyCardinal - zeroCardinal; - return Long.MAX_VALUE - keyCardinal + zeroCardinal + 1; + return Long.MAX_VALUE - keyCardinal + zeroCardinal; } public final static byte[] encodeLong(long c, int length) { @@ -131,7 +131,10 @@ public class kelondroNaturalOrder extends kelondroAbstractOrder implements kelon while (offset < m) c = (c << 8) | ((long) s[offset++] & 0xFF); return c; } - + + private static final int sig(int x) { + return (x > 0) ? 1 : (x < 0) ? -1 : 0; + } // Compares its two arguments for order. // Returns -1, 0, or 1 as the first argument @@ -151,11 +154,8 @@ public class kelondroNaturalOrder extends kelondroAbstractOrder implements kelon // we have an artificial start point. check all combinations int az = compares(a, aoffset, alength, zero, 0, zero.length); // -1 if a < z; 0 if a == z; 1 if a > z int bz = compares(b, boffset, blength, zero, 0, zero.length); // -1 if b < z; 0 if b == z; 1 if b > z - if ((az == 0) && (bz == 0)) return 0; - if (az == 0) return -1; - if (bz == 0) return 1; - if (az == bz) return compares(a, aoffset, alength, b, boffset, blength); - return bz; + if (az == bz) return compares(a, aoffset, alength, b, boffset, blength); + return sig(az - bz); } public static final boolean equal(byte[] a, byte[] b) { @@ -168,10 +168,8 @@ public class kelondroNaturalOrder extends kelondroAbstractOrder implements kelon int i = 0; final int al = Math.min(alength, a.length - aoffset); final int bl = Math.min(blength, b.length - boffset); - if (al > bl) return 1; - if (al < bl) return -1; int aa, bb; - while (i < al) { + while ((i < al) && (i < bl)) { aa = 0xff & (int) a[i + aoffset]; bb = 0xff & (int) b[i + boffset]; if (aa > bb) return 1; @@ -179,6 +177,9 @@ public class kelondroNaturalOrder extends kelondroAbstractOrder implements kelon // else the bytes are equal and it may go on yet undecided i++; } + // compare length + if (al > bl) return 1; + if (al < bl) return -1; // they are equal return 0; } diff --git a/source/de/anomic/kelondro/kelondroRowSet.java b/source/de/anomic/kelondro/kelondroRowSet.java index 9cad04ecb..95afd9e5c 100644 --- a/source/de/anomic/kelondro/kelondroRowSet.java +++ b/source/de/anomic/kelondro/kelondroRowSet.java @@ -232,6 +232,7 @@ public class kelondroRowSet extends kelondroRowCollection implements kelondroInd p = 0; } else { p = binaryPosition(first, 0, first.length); + //System.out.println("binaryposition for key " + new String(firstKey) + " is " + p); } } diff --git a/source/de/anomic/yacy/yacySearch.java b/source/de/anomic/yacy/yacySearch.java index 7d6094d52..a8d17989b 100644 --- a/source/de/anomic/yacy/yacySearch.java +++ b/source/de/anomic/yacy/yacySearch.java @@ -191,7 +191,7 @@ public class yacySearch extends Thread { seed = (yacySeed) dhtEnum.nextElement(); if (seed == null) continue; distance = yacyDHTAction.dhtDistance(seed.hash, wordhash); - if (distance > 0.9) continue; // catch bug in peer selection + if (distance > 0.2) continue; // catch bug in peer selection if (!seed.getFlagAcceptRemoteIndex()) continue; // probably a robinson peer serverLog.logFine("PLASMA", "selectPeers/DHTorder: " + seed.hash + ":" + seed.getName() + "/" + distance + " for wordhash " + wordhash + ", score " + c); ranking.addScore(seed.hash, c--); diff --git a/source/migration.java b/source/migration.java index b40c4297e..9ee42379c 100644 --- a/source/migration.java +++ b/source/migration.java @@ -51,7 +51,7 @@ public class migration { //SVN constants public static final int USE_WORK_DIR=1389; //wiki & messages in DATA/WORK public static final int TAGDB_WITH_TAGHASH=1635; //tagDB keys are tagHashes instead of plain tagname. - public static final int NEW_OVERLAYS=2299; + public static final int NEW_OVERLAYS=3624; public static void main(String[] args) { }