Skip to content

Commit

Permalink
fixed wrong ordering that caused bad dht selection
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@3646 6c8d7289-2bf4-0310-a012-ef5d649a1542
  • Loading branch information
orbiter committed May 4, 2007
1 parent 7cf8981 commit 191ef16
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
20 changes: 11 additions & 9 deletions source/de/anomic/kelondro/kelondroBase64Order.java
Expand Up @@ -285,7 +285,11 @@ public final long cardinal(byte[] key) {
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) {
Expand All @@ -301,11 +305,8 @@ public final int compare0(byte[] a, int aoffset, int alength, byte[] b, int boff
// 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) {
Expand All @@ -315,11 +316,9 @@ public final int compares(byte[] a, int aoffset, int alength, byte[] b, int boff
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];
Expand All @@ -336,6 +335,9 @@ public final int compares(byte[] a, int aoffset, int alength, byte[] b, int boff
// 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;
}
Expand Down
21 changes: 11 additions & 10 deletions source/de/anomic/kelondro/kelondroNaturalOrder.java
Expand Up @@ -96,7 +96,7 @@ public final long cardinal(byte[] key) {
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) {
Expand Down Expand Up @@ -131,7 +131,10 @@ public final static long decodeLong(byte[] s, int offset, int length) {
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
Expand All @@ -151,11 +154,8 @@ public final int compare0(byte[] a, int aoffset, int alength, byte[] b, int boff
// 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) {
Expand All @@ -168,17 +168,18 @@ public static final int compares(byte[] a, int aoffset, int alength, byte[] b, i
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;
if (aa < bb) return -1;
// 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;
}
Expand Down
1 change: 1 addition & 0 deletions source/de/anomic/kelondro/kelondroRowSet.java
Expand Up @@ -232,6 +232,7 @@ public rowIterator(boolean up, byte[] firstKey) {
p = 0;
} else {
p = binaryPosition(first, 0, first.length);
//System.out.println("binaryposition for key " + new String(firstKey) + " is " + p);
}
}

Expand Down
2 changes: 1 addition & 1 deletion source/de/anomic/yacy/yacySearch.java
Expand Up @@ -191,7 +191,7 @@ private static yacySeed[] selectDHTPeers(Set wordhashes, int seedcount) {
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--);
Expand Down
2 changes: 1 addition & 1 deletion source/migration.java
Expand Up @@ -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) {

}
Expand Down

0 comments on commit 191ef16

Please sign in to comment.