diff --git a/source/de/anomic/kelondro/order/Base64Order.java b/source/de/anomic/kelondro/order/Base64Order.java index c61ede8d9..755cdbfb6 100644 --- a/source/de/anomic/kelondro/order/Base64Order.java +++ b/source/de/anomic/kelondro/order/Base64Order.java @@ -262,6 +262,16 @@ public class Base64Order extends AbstractOrder implements ByteOrder, Cod c = c << 3; return c; } + + private final long cardinalI(final String key) { + // returns a cardinal number in the range of 0 .. Long.MAX_VALUE + long c = 0; + int p = 0; + while ((p < 10) && (p < key.length())) c = (c << 6) | ahpla[key.charAt(p++)]; + while (p++ < 10) c = (c << 6); + c = c << 3; + return c; + } public final byte[] uncardinal(long c) { c = c >> 3; @@ -281,6 +291,14 @@ public class Base64Order extends AbstractOrder implements ByteOrder, Cod return Long.MAX_VALUE - keyCardinal + zeroCardinal; } + public final long cardinal(final String key) { + if (this.zero == null) return cardinalI(key); + final long zeroCardinal = cardinalI(this.zero); + final long keyCardinal = cardinalI(key); + if (keyCardinal > zeroCardinal) return keyCardinal - zeroCardinal; + return Long.MAX_VALUE - keyCardinal + zeroCardinal; + } + private static final int sig(final int x) { return (x > 0) ? 1 : (x < 0) ? -1 : 0; } diff --git a/source/de/anomic/yacy/dht/FlatWordPartitionScheme.java b/source/de/anomic/yacy/dht/FlatWordPartitionScheme.java index d7c87fb55..98759eee0 100755 --- a/source/de/anomic/yacy/dht/FlatWordPartitionScheme.java +++ b/source/de/anomic/yacy/dht/FlatWordPartitionScheme.java @@ -43,10 +43,7 @@ public class FlatWordPartitionScheme implements PartitionScheme { public long dhtPosition(String wordHash, String urlHash) { // the urlHash has no relevance here // normalized to Long.MAX_VALUE - long c = Base64Order.enhancedCoder.cardinal(wordHash.getBytes()); - assert c != Long.MAX_VALUE; - if (c == Long.MAX_VALUE) return Long.MAX_VALUE - 1; - return c; + return Base64Order.enhancedCoder.cardinal(wordHash); } public final long dhtDistance(final String word, final String urlHash, final yacySeed peer) { @@ -78,8 +75,9 @@ public class FlatWordPartitionScheme implements PartitionScheme { } public final static long dhtDistance(final long fromPos, final long toPos) { - final long d = toPos - fromPos; - return (d >= 0) ? d : (d + Long.MAX_VALUE) + 1; + return (toPos >= fromPos) ? + toPos - fromPos : + (Long.MAX_VALUE - fromPos) + toPos + 1; } public static String positionToHash(final long l) {