Author: toad
Date: 2006-08-09 12:46:15 +0000 (Wed, 09 Aug 2006)
New Revision: 9994

Modified:
   trunk/freenet/src/freenet/node/InsertHandler.java
   trunk/freenet/src/freenet/node/Node.java
   trunk/freenet/src/freenet/node/PeerManager.java
   trunk/freenet/src/freenet/node/RequestSender.java
   trunk/freenet/src/freenet/node/SSKInsertHandler.java
   trunk/freenet/src/freenet/node/Version.java
Log:
938: Decision on when to cache in the long term store was wrong.
What we should actually check is "Is this node closer to the key than any 
connected node?"
What we were checking was "Did we reset the HTL? (i.e. are we closer than all 
hops so far?)".

Modified: trunk/freenet/src/freenet/node/InsertHandler.java
===================================================================
--- trunk/freenet/src/freenet/node/InsertHandler.java   2006-08-09 12:25:31 UTC 
(rev 9993)
+++ trunk/freenet/src/freenet/node/InsertHandler.java   2006-08-09 12:46:15 UTC 
(rev 9994)
@@ -349,7 +349,7 @@
                 if(!canCommit) return;
                 if(!prb.allReceived()) return;
                 CHKBlock block = new CHKBlock(prb.getBlock(), headers, key);
-                node.store(block, resetNearestLoc);
+                node.store(block);
                 Logger.minor(this, "Committed");
             } catch (CHKVerifyException e) {
                 Logger.error(this, "Verify failed in InsertHandler: "+e+" - 
headers: "+HexUtil.bytesToHex(headers), e);

Modified: trunk/freenet/src/freenet/node/Node.java
===================================================================
--- trunk/freenet/src/freenet/node/Node.java    2006-08-09 12:25:31 UTC (rev 
9993)
+++ trunk/freenet/src/freenet/node/Node.java    2006-08-09 12:46:15 UTC (rev 
9994)
@@ -2168,7 +2168,7 @@
                }
                long startTime = System.currentTimeMillis();
                if(cache) {
-                       store(block, false);
+                       store(block);
                }
                is = makeInsertSender((NodeCHK)block.getKey(), 
                                MAX_HTL, uid, null, headers, prb, false, 
lm.getLocation().getValue(), cache);
@@ -2276,7 +2276,7 @@
                if(cache) {
                        try {
                                if(cache)
-                                       store(block, false);
+                                       storeInsert(block);
                        } catch (KeyCollisionException e) {
                                throw new 
LowLevelPutException(LowLevelPutException.COLLISION);
                        }
@@ -2838,13 +2838,26 @@
                                "\nSSK Datacache: 
"+sskDatacache.hits()+"/"+(sskDatacache.hits()+sskDatacache.misses())+"/"+sskDatacache.keyCount());
        }

+       public void store(CHKBlock block) {
+               store(block, block.getKey().toNormalizedDouble());
+       }
+       
        /**
         * Store a datum.
         * @param deep If true, insert to the store as well as the cache. Do 
not set
         * this to true unless the store results from an insert, and this node 
is the
         * closest node to the target; see the description of chkDatastore.
         */
-       public void store(CHKBlock block, boolean deep) {
+       public void store(CHKBlock block, double loc) {
+               boolean deep = !peers.isCloserLocation(loc);
+               store(block, deep);
+       }
+
+       public void storeShallow(CHKBlock block) {
+               store(block, false);
+       }
+       
+       private void store(CHKBlock block, boolean deep) {
                try {
                        if(deep) {
                                chkDatastore.put(block);
@@ -2861,14 +2874,19 @@
                        Logger.error(this, "Cannot store data: "+e, e);
                }
        }
+       
+       /** Store the block if this is a sink. Call for inserts. */
+       public void storeInsert(SSKBlock block) throws KeyCollisionException {
+               store(block, block.getKey().toNormalizedDouble());
+       }

-       /**
-        * Store a datum.
-        * @param deep If true, insert to the store as well as the cache. Do 
not set
-        * this to true unless the store results from an insert, and this node 
is the
-        * closest node to the target; see the description of chkDatastore.
-        */
-       public void store(SSKBlock block, boolean deep) throws 
KeyCollisionException {
+       /** Store only to the cache, and not the store. Called by requests,
+        * as only inserts cause data to be added to the store. */
+       public void storeShallow(SSKBlock block) throws KeyCollisionException {
+               store(block, false);
+       }
+       
+       private void store(SSKBlock block, boolean deep) throws 
KeyCollisionException {
                try {
                        if(deep) {
                                sskDatastore.put(block, false);
@@ -2888,6 +2906,17 @@
        }

        /**
+        * Store a datum.
+        * @param deep If true, insert to the store as well as the cache. Do 
not set
+        * this to true unless the store results from an insert, and this node 
is the
+        * closest node to the target; see the description of chkDatastore.
+        */
+       public void store(SSKBlock block, double loc) throws 
KeyCollisionException {
+               boolean deep = !peers.isCloserLocation(loc);
+               store(block, deep);
+       }
+       
+       /**
         * Remove a sender from the set of currently transferring senders.
         */
        public void removeTransferringSender(NodeCHK key, RequestSender sender) 
{
@@ -3000,7 +3029,7 @@
                        throw new IllegalArgumentException("No pub key when 
inserting");
                }
                if(cache)
-                       cacheKey(key.getPubKeyHash(), key.getPubKey(), 
resetClosestLoc);
+                       cacheKey(key.getPubKeyHash(), key.getPubKey(), 
!peers.isCloserLocation(block.getKey().toNormalizedDouble()));
                Logger.minor(this, 
"makeInsertSender("+key+","+htl+","+uid+","+source+",...,"+fromStore);
                KeyHTLPair kh = new KeyHTLPair(key, htl);
                SSKInsertSender is = null;

Modified: trunk/freenet/src/freenet/node/PeerManager.java
===================================================================
--- trunk/freenet/src/freenet/node/PeerManager.java     2006-08-09 12:25:31 UTC 
(rev 9993)
+++ trunk/freenet/src/freenet/node/PeerManager.java     2006-08-09 12:46:15 UTC 
(rev 9994)
@@ -373,6 +373,36 @@
         return getRandomPeer(null);
     }

+    public double closestPeerLocation(double loc, double ignoreLoc) {
+        PeerNode[] peers;
+        synchronized (this) {
+               peers = connectedPeers;
+               }
+        double bestDiff = 1.0;
+        double bestLoc = Double.MAX_VALUE;
+        for(int i=0;i<peers.length;i++) {
+            PeerNode p = peers[i];
+            if(!p.isRoutable()) continue;
+            double peerloc = p.getLocation().getValue();
+            if(Math.abs(peerloc - ignoreLoc) < Double.MIN_VALUE*2)
+               continue;
+            double diff = distance(peerloc, loc);
+            if(diff < bestDiff) {
+                bestDiff = diff;
+                bestLoc = peerloc;
+            }
+        }
+        return bestLoc;
+    }
+
+    public boolean isCloserLocation(double loc) {
+       double nodeLoc = node.lm.getLocation().getValue();
+       double nodeDist = distance(nodeLoc, loc);
+       double closest = closestPeerLocation(loc, nodeLoc);
+       double closestDist = distance(closest, loc);
+       return closestDist < nodeDist;
+    }
+    
     /**
      * Find the peer which is closest to the target location
      */

Modified: trunk/freenet/src/freenet/node/RequestSender.java
===================================================================
--- trunk/freenet/src/freenet/node/RequestSender.java   2006-08-09 12:25:31 UTC 
(rev 9993)
+++ trunk/freenet/src/freenet/node/RequestSender.java   2006-08-09 12:46:15 UTC 
(rev 9994)
@@ -399,7 +399,7 @@
     private void finishSSK(PeerNode next) {
        try {
                        block = new SSKBlock(sskData, headers, (NodeSSK)key, 
false);
-                       node.store(block, false);
+                       node.storeShallow(block);
                        if(node.random.nextInt(RANDOM_REINSERT_INTERVAL) == 0)
                                node.queueRandomReinsert(block);
                        finish(SUCCESS, next);
@@ -440,12 +440,12 @@
                // requests don't go to the full distance, and therefore 
pollute the 
                // store; simulations it is best to only include data from 
requests
                // which go all the way i.e. inserts.
-               node.store(block, false);
+               node.storeShallow(block);
                        if(node.random.nextInt(RANDOM_REINSERT_INTERVAL) == 0)
                                node.queueRandomReinsert(block);
        } else if (key instanceof NodeSSK) {
                try {
-                               node.store(new SSKBlock(data, headers, 
(NodeSSK)key, false), false);
+                               node.storeShallow(new SSKBlock(data, headers, 
(NodeSSK)key, false));
                        } catch (KeyCollisionException e) {
                                Logger.normal(this, "Collision on "+this);
                        }

Modified: trunk/freenet/src/freenet/node/SSKInsertHandler.java
===================================================================
--- trunk/freenet/src/freenet/node/SSKInsertHandler.java        2006-08-09 
12:25:31 UTC (rev 9993)
+++ trunk/freenet/src/freenet/node/SSKInsertHandler.java        2006-08-09 
12:46:15 UTC (rev 9994)
@@ -287,7 +287,7 @@

        if(canCommit) {
                try {
-                               node.store(block, resetClosestLoc);
+                               node.store(block, 
block.getKey().toNormalizedDouble());
                        } catch (KeyCollisionException e) {
                                Logger.normal(this, "Collision on "+this);
                        }

Modified: trunk/freenet/src/freenet/node/Version.java
===================================================================
--- trunk/freenet/src/freenet/node/Version.java 2006-08-09 12:25:31 UTC (rev 
9993)
+++ trunk/freenet/src/freenet/node/Version.java 2006-08-09 12:46:15 UTC (rev 
9994)
@@ -18,7 +18,7 @@
        public static final String protocolVersion = "1.0";

        /** The build number of the current revision */
-       private static final int buildNumber = 937;
+       private static final int buildNumber = 938;

        /** Oldest build of Fred we will talk to */
        private static final int oldLastGoodBuild = 874;


Reply via email to