Author: toad
Date: 2008-11-29 20:20:39 +0000 (Sat, 29 Nov 2008)
New Revision: 23987

Modified:
   branches/db4o/freenet/src/freenet/support/io/BucketTools.java
   branches/db4o/freenet/src/freenet/support/io/TempBucketFactory.java
Log:
Merge 1183


Modified: branches/db4o/freenet/src/freenet/support/io/BucketTools.java
===================================================================
--- branches/db4o/freenet/src/freenet/support/io/BucketTools.java       
2008-11-29 20:04:11 UTC (rev 23986)
+++ branches/db4o/freenet/src/freenet/support/io/BucketTools.java       
2008-11-29 20:20:39 UTC (rev 23987)
@@ -316,7 +316,10 @@
                                if(bytes <= 0) {
                                        if(truncateLength == Long.MAX_VALUE)
                                                break;
-                                       throw new IOException("Could not move 
required quantity of data in copyFrom: "+bytes+" (moved "+moved+" of 
"+truncateLength+"): unable to read from "+is);
+                                       IOException ioException = new 
IOException("Could not move required quantity of data in copyFrom: "
+                                               + bytes + " (moved " + moved + 
" of " + truncateLength + "): unable to read from " + is);
+                                       ioException.printStackTrace();
+                                       throw ioException;
                                }
                                os.write(buf, 0, bytes);
                                moved += bytes;

Modified: branches/db4o/freenet/src/freenet/support/io/TempBucketFactory.java
===================================================================
--- branches/db4o/freenet/src/freenet/support/io/TempBucketFactory.java 
2008-11-29 20:04:11 UTC (rev 23986)
+++ branches/db4o/freenet/src/freenet/support/io/TempBucketFactory.java 
2008-11-29 20:20:39 UTC (rev 23987)
@@ -16,6 +16,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.lang.ref.WeakReference;
 import java.util.LinkedList;
 import java.util.Queue;
 import java.util.Random;
@@ -88,9 +89,10 @@
                        this.creationTime = now;
                        this.osIndex = 0;
                        this.tbis = new Vector<TempBucketInputStream>();
+                       if(logMINOR) Logger.minor(this, "Created "+this, new 
Exception("debug"));
                }
                
-               private void closeInputStreams(boolean forFree) {
+               private synchronized void closeInputStreams(boolean forFree) {
                        for(TempBucketInputStream is : tbis) {
                                try {
                                        if(forFree)
@@ -152,7 +154,10 @@
                        if(osIndex > 0)
                                throw new IOException("Only one OutputStream 
per bucket!");
                        hasWritten = true;
-                       return new TempBucketOutputStream(++osIndex);
+                       OutputStream os = new TempBucketOutputStream(++osIndex);
+                       if(logMINOR)
+                               Logger.minor(this, "Got "+os+" for "+this);
+                       return os;
                }
 
                private class TempBucketOutputStream extends OutputStream {
@@ -236,7 +241,8 @@
                                throw new IOException("No OutputStream has been 
openned! Why would you want an InputStream then?");
                        TempBucketInputStream is = new 
TempBucketInputStream(osIndex);
                        tbis.add(is);
-                       
+                       if(logMINOR)
+                               Logger.minor(this, "Got "+is+" for "+this);
                        return is;
                }
                
@@ -349,7 +355,7 @@
                        if(isRAMBucket()) {
                                _hasFreed(currentSize);
                                synchronized(ramBucketQueue) {
-                                       ramBucketQueue.remove(this);
+                                       ramBucketQueue.remove(getReference());
                                }
                        }
                }
@@ -367,6 +373,19 @@
                        currentBucket.storeTo(container);
                        container.store(this);
                }
+               
+               private WeakReference<TempBucket> weakRef = new 
WeakReference<TempBucket>(this);
+
+               public WeakReference<TempBucket> getReference() {
+                       return weakRef;
+               }
+               
+               protected void finalize() {
+                       if (!hasBeenFreed) {
+                               Logger.error(this, "TempBucket not freed, 
size=" + size() + ", isRAMBucket=" + isRAMBucket()+" : "+this);
+                               free();
+                       }
+               }
        }
        
        // Storage accounting disabled by default.
@@ -461,7 +480,7 @@
                TempBucket toReturn = new TempBucket(now, realBucket);
                if(useRAMBucket) { // No need to consider them for migration if 
they can't be migrated
                        synchronized(ramBucketQueue) {
-                               ramBucketQueue.add(toReturn);
+                               ramBucketQueue.add(toReturn.getReference());
                        }
                }
                return toReturn;
@@ -474,14 +493,25 @@
                final Queue<TempBucket> toMigrate = new 
LinkedList<TempBucket>();
                do {
                        synchronized(ramBucketQueue) {
-                               final TempBucket tmpBucket = 
ramBucketQueue.peek();
-                               if((tmpBucket == null) || 
(tmpBucket.creationTime + RAMBUCKET_MAX_AGE > now))
+                               final WeakReference<TempBucket> tmpBucketRef = 
ramBucketQueue.peek();
+                               if (tmpBucketRef == null)
                                        shouldContinue = false;
                                else {
-                                       if(logMINOR)
-                                               Logger.minor(this, "The bucket 
is "+TimeUtil.formatTime(now - tmpBucket.creationTime)+" old: we will 
force-migrate it to disk.");
-                                       ramBucketQueue.remove(tmpBucket);
-                                       toMigrate.add(tmpBucket);
+                                       TempBucket tmpBucket = 
tmpBucketRef.get();
+                                       if (tmpBucket == null) {
+                                               
ramBucketQueue.remove(tmpBucketRef);
+                                               continue; // ugh. this is freed
+                                       }
+
+                                       if (tmpBucket.creationTime + 
RAMBUCKET_MAX_AGE > now)
+                                               shouldContinue = false;
+                                       else {
+                                               if (logMINOR)
+                                                       Logger.minor(this, "The 
bucket is " + TimeUtil.formatTime(now - tmpBucket.creationTime)
+                                                               + " old: we 
will force-migrate it to disk.");
+                                               
ramBucketQueue.remove(tmpBucketRef);
+                                               toMigrate.add(tmpBucket);
+                                       }
                                }
                        }
                } while(shouldContinue);
@@ -504,11 +534,11 @@
                }
        }
        
-       private final Queue<TempBucket> ramBucketQueue = new 
LinkedBlockingQueue<TempBucket>();
+       private final Queue<WeakReference<TempBucket>> ramBucketQueue = new 
LinkedBlockingQueue<WeakReference<TempBucket>>();
        
        private Bucket _makeFileBucket() {
                Bucket fileBucket = new 
TempFileBucket(filenameGenerator.makeRandomFilename(), filenameGenerator);
                // Do we want it to be encrypted?
                return (reallyEncrypt ? new 
PaddedEphemerallyEncryptedBucket(fileBucket, 1024, strongPRNG, weakPRNG) : 
fileBucket);
        }
-}
\ No newline at end of file
+}

_______________________________________________
cvs mailing list
[email protected]
http://emu.freenetproject.org/cgi-bin/mailman/listinfo/cvs

Reply via email to