Author: toad
Date: 2008-10-02 22:15:15 +0000 (Thu, 02 Oct 2008)
New Revision: 22921

Modified:
   branches/db4o/freenet/src/freenet/support/RandomGrabArray.java
   branches/db4o/freenet/src/freenet/support/RandomGrabArrayItem.java
   branches/db4o/freenet/src/freenet/support/SectoredRandomGrabArray.java
Log:
Get rid of canRemove(). Explicitly remove from queue *ONLY*, since selection 
*must not modify the queue* as selections are transient.
Add tools to enumerate the RGAs/SRGAs for debugging purposes.
Paranoia.
Logging.


Modified: branches/db4o/freenet/src/freenet/support/RandomGrabArray.java
===================================================================
--- branches/db4o/freenet/src/freenet/support/RandomGrabArray.java      
2008-10-02 22:10:14 UTC (rev 22920)
+++ branches/db4o/freenet/src/freenet/support/RandomGrabArray.java      
2008-10-02 22:15:15 UTC (rev 22921)
@@ -3,6 +3,7 @@
 import com.db4o.ObjectContainer;

 import freenet.client.async.ClientContext;
+import freenet.node.SendableRequest;

 /**
  * An array which supports very fast remove-and-return-a-random-element.
@@ -59,12 +60,15 @@
        public RandomGrabArrayItem 
removeRandom(RandomGrabArrayItemExclusionList excluding, ObjectContainer 
container, ClientContext context) {
                RandomGrabArrayItem ret, oret;
                boolean logMINOR = Logger.shouldLog(Logger.MINOR, this);
+               if(logMINOR) Logger.minor(this, "removeRandom() on "+this+" 
index="+index);
                synchronized(this) {
                        final int MAX_EXCLUDED = 10;
                        int excluded = 0;
                        boolean changedMe = false;
                        while(true) {
                                if(index == 0) {
+                                       if(reqs == null)
+                                               throw new 
NullPointerException();
                                        if(logMINOR) Logger.minor(this, "All 
null on "+this);
                                        return null;
                                }
@@ -137,14 +141,7 @@
                                                        changedMe = true;
                                                        ret = chosenItem;
                                                        assert(ret == 
reqs[chosenIndex]);
-                                                       
if(ret.canRemove(container)) {
-                                                               if(chosenIndex 
!= index-1) {
-                                                                       
reqs[chosenIndex] = reqs[index-1];
-                                                               }
-                                                               index--;
-                                                               
ret.setParentGrabArray(null, container);
-                                                       }
-                                                       if(logMINOR) 
Logger.minor(this, "Chosen random item "+ret+" out of "+valid);
+                                                       if(logMINOR) 
Logger.minor(this, "Chosen random item "+ret+" out of "+valid+" total "+index);
                                                        if(persistent && 
changedMe)
                                                                
container.store(this);
                                                        return ret;
@@ -153,27 +150,17 @@
                                                        index = 0;
                                                        if(persistent)
                                                                
container.store(this);
-                                                       if(logMINOR) 
Logger.minor(this, "No valid or excluded items");
+                                                       if(logMINOR) 
Logger.minor(this, "No valid or excluded items total "+index);
                                                        return null;
                                                } else if(valid == 0) {
                                                        if(persistent && 
changedMe)
                                                                
container.store(this);
-                                                       if(logMINOR) 
Logger.minor(this, "No valid items, "+exclude+" excluded items");
+                                                       if(logMINOR) 
Logger.minor(this, "No valid items, "+exclude+" excluded items total "+index);
                                                        return null;
                                                } else if(valid == 1) {
                                                        ret = validItem;
                                                        assert(ret == 
reqs[validIndex]);
-                                                       
if(ret.canRemove(container)) {
-                                                               changedMe = 
true;
-                                                               if(validIndex 
!= index-1) {
-                                                                       
reqs[validIndex] = reqs[index-1];
-                                                               }
-                                                               index--;
-                                                               if(logMINOR) 
Logger.minor(this, "No valid or excluded items after removing "+ret);
-                                                               
ret.setParentGrabArray(null, container);
-                                                       } else {
-                                                               if(logMINOR) 
Logger.minor(this, "No valid or excluded items apart from "+ret);
-                                                       }
+                                                       if(logMINOR) 
Logger.minor(this, "No valid or excluded items apart from "+ret+" total 
"+index);
                                                        if(persistent && 
changedMe)
                                                                
container.store(this);
                                                        return ret;
@@ -216,7 +203,7 @@
                                        }
                                        continue;
                                }
-                               if(ret != null && !ret.canRemove(container)) {
+                               if(ret != null) {
                                        if(logMINOR) Logger.minor(this, 
"Returning (cannot remove): "+ret+" of "+index);
                                        if(persistent && changedMe)
                                                container.store(this);
@@ -252,6 +239,8 @@
        }

        public void remove(RandomGrabArrayItem it, ObjectContainer container) {
+               if(Logger.shouldLog(Logger.MINOR, this))
+                       Logger.minor(this, "Removing "+it+" from "+this);
                synchronized(this) {
                        for(int i=0;i<index;i++) {
                                if(reqs[i] == null) continue;
@@ -282,4 +271,12 @@
                }
                return false;
        }
+       
+       public synchronized int size() {
+               return index;
+       }
+
+       public synchronized RandomGrabArrayItem get(int idx) {
+               return reqs[idx];
+       }
 }

Modified: branches/db4o/freenet/src/freenet/support/RandomGrabArrayItem.java
===================================================================
--- branches/db4o/freenet/src/freenet/support/RandomGrabArrayItem.java  
2008-10-02 22:10:14 UTC (rev 22920)
+++ branches/db4o/freenet/src/freenet/support/RandomGrabArrayItem.java  
2008-10-02 22:15:15 UTC (rev 22921)
@@ -14,13 +14,6 @@
         * holding the RGA lock(s). */
        public boolean isEmpty(ObjectContainer container);

-       /** Can this item be removed from the queue after it has been handled?
-        * Called immediately after finding a request to remove.
-        * If returns false, the item will remain in the queue and may be 
chosen again.
-        * Note that in the case of SendableGet's, this is called before 
chooseKey(), so
-        * it needs to return true if there are less than two requests on this 
object. */
-       public boolean canRemove(ObjectContainer container);
-       
        /** Does this RandomGrabArrayItem support remembering where it is 
registered? */
        public boolean knowsParentGrabArray();


Modified: branches/db4o/freenet/src/freenet/support/SectoredRandomGrabArray.java
===================================================================
--- branches/db4o/freenet/src/freenet/support/SectoredRandomGrabArray.java      
2008-10-02 22:10:14 UTC (rev 22920)
+++ branches/db4o/freenet/src/freenet/support/SectoredRandomGrabArray.java      
2008-10-02 22:15:15 UTC (rev 22921)
@@ -100,6 +100,10 @@
                if(idx == -1) return null;
                else return grabArrays[idx];
        }
+       
+       public synchronized Object getClient(int x) {
+               return grabClients[x];
+       }

        /**
         * Put a grabber. This lets us use things other than 
RandomGrabArrayWithClient's, so don't mix calls


Reply via email to