Author: orbiter
Date: 2008-03-11 00:28:05 +0100 (Tue, 11 Mar 2008)
New Revision: 4548

Modified:
   trunk/source/de/anomic/kelondro/kelondroAbstractRecords.java
   trunk/source/de/anomic/yacy/yacySeed.java
Log:
new method for best hash computation

Modified: trunk/source/de/anomic/kelondro/kelondroAbstractRecords.java
===================================================================
--- trunk/source/de/anomic/kelondro/kelondroAbstractRecords.java        
2008-03-10 23:23:04 UTC (rev 4547)
+++ trunk/source/de/anomic/kelondro/kelondroAbstractRecords.java        
2008-03-10 23:28:05 UTC (rev 4548)
@@ -870,47 +870,7 @@
     public final Iterator<EntryIndex> contentRows(long maxInitTime) throws 
kelondroException {
         return new contentRowIterator(maxInitTime);
     }
-    
-    /*
-    public final class contentRowIterator implements Iterator<EntryIndex> {
-        // iterator that iterates all kelondroRow.Entry-objects in the file
-        // all records that are marked as deleted are omitted
-        
-        private Iterator<kelondroNode> nodeIterator;
-        
-        public contentRowIterator(long maxInitTime) {
-            nodeIterator = contentNodes(maxInitTime);
-        }
 
-        public boolean hasNext() {
-            return nodeIterator.hasNext();
-        }
-
-        public EntryIndex next0() {
-            try {
-                kelondroNode n = (kelondroNode) nodeIterator.next();
-                return row().newEntryIndex(n.getValueRow(), n.handle().index);
-            } catch (IOException e) {
-                throw new kelondroException(filename, e.getMessage());
-            }
-        }
-        
-        public EntryIndex next() {
-            try {
-                kelondroNode n = (kelondroNode) nodeIterator.next();
-                return row().newEntryIndex(n.getValueRow(), n.handle().index);
-            } catch (IOException e) {
-                throw new kelondroException(filename, e.getMessage());
-            }
-        }
-
-        public void remove() {
-            throw new UnsupportedOperationException();
-        }
-        
-    }
-     */
-
      public final class contentRowIterator implements Iterator<EntryIndex> {
         // iterator that iterates all kelondroRow.Entry-objects in the file
         // all records that are marked as deleted are omitted

Modified: trunk/source/de/anomic/yacy/yacySeed.java
===================================================================
--- trunk/source/de/anomic/yacy/yacySeed.java   2008-03-10 23:23:04 UTC (rev 
4547)
+++ trunk/source/de/anomic/yacy/yacySeed.java   2008-03-10 23:28:05 UTC (rev 
4548)
@@ -69,6 +69,7 @@
 import java.util.Iterator;
 import java.util.Random;
 import java.util.Set;
+import java.util.TreeMap;
 
 import de.anomic.kelondro.kelondroBase64Order;
 import de.anomic.net.natLib;
@@ -691,9 +692,9 @@
         if (from == null) return dhtPosition(to);
         final double fromPos = dhtPosition(from);
         final double toPos = dhtPosition(to);
-        return (fromPos < toPos) ? (toPos - fromPos) : (1.0 - fromPos + toPos);
+        return (fromPos <= toPos) ? (toPos - fromPos) : (1.0 - fromPos + 
toPos);
     }
-    
+    /*
     private static String bestNewHash(yacySeedDB seedDB) {
         if ((seedDB == null) || (seedDB.sizeConnected() <= 8)) {
             // use random hash
@@ -734,7 +735,63 @@
         System.out.println("BESTHASH  finally is " + bestHash);
         return bestHash;
     }
+    */
+    private static String bestGap(yacySeedDB seedDB) {
+        if ((seedDB == null) || (seedDB.sizeConnected() <= 2)) {
+            // use random hash
+            return randomHash();
+        }
+        // find gaps
+        TreeMap<Double, String> gaps = hashGaps(seedDB);
+        
+        // take one gap; prefer biggest but take also another smaller by chance
+        String interval = null;
+        Random r = new Random();
+        while (gaps.size() > 0) {
+            interval = gaps.remove(gaps.lastKey());
+            if (r.nextBoolean()) break;
+        }
+        if (interval == null) return randomHash();
+        
+        // find dht position and size of gap
+        double gapsize = dhtDistance(interval.substring(0, 12), 
interval.substring(12));
+        assert gapsize >= 0.0;
+        double gappos = dhtPosition(interval.substring(0, 12)) + (gapsize / 2);
+        if (gappos >= 1.0) gappos = gappos - 1.0; // fix overflow; can only 
occur for gap at end
+        return positionToHash(gappos);
+    }
     
+    private static TreeMap<Double, String> hashGaps(yacySeedDB seedDB) {
+        TreeMap<Double, String>gaps = new TreeMap<Double, String>();
+        if (seedDB == null) return gaps;
+        
+        Iterator<yacySeed> i = seedDB.seedsConnected(true, false, null, 
(float) 0.0);
+        double d;
+        yacySeed s0 = null, s1, first = null;
+        while (i.hasNext()) {
+            s1 = i.next();
+            if (s0 == null) {
+                s0 = s1;
+                first = s0;
+                continue;
+            }
+            if (s0.hash.equals("fF99P8dMio7M")) {
+                System.out.print(0);
+            }
+            d = dhtDistance(s0.hash, s1.hash);
+            assert d >= 0.0;
+            gaps.put(d, s0.hash + s1.hash);
+            s0 = s1;
+        }
+        // compute also the last gap
+        if ((first != null) && (s0 != null)) {
+            d = dhtDistance(s0.hash, first.hash);
+            assert d >= 0.0;
+            gaps.put(d, s0.hash + first.hash);
+        }
+        return gaps;
+    }
+    
     private static String positionToHash(double t) {
         // transform the position of a peer position into a close peer hash
         assert t >= 0.0 : "t = " + t;
@@ -759,7 +816,7 @@
         // genera a seed for the local peer
         // this is the birthplace of a seed, that then will start to travel to 
other peers
 
-        final String hash = bestNewHash(yacyCore.seedDB);
+        final String hash = bestGap(yacyCore.seedDB);
         yacyCore.log.logInfo("init: OWN SEED = " + hash);
 
         final yacySeed newSeed = new yacySeed(hash);

_______________________________________________
YaCy-svn mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/yacy-svn

Antwort per Email an