Author: toad
Date: 2007-02-21 03:58:43 +0000 (Wed, 21 Feb 2007)
New Revision: 11879

Modified:
   trunk/freenet/src/freenet/client/ArchiveManager.java
   trunk/freenet/src/freenet/client/ArchiveStoreContext.java
   trunk/freenet/src/freenet/client/ArchiveStoreItem.java
   trunk/freenet/src/freenet/client/ErrorArchiveStoreItem.java
   trunk/freenet/src/freenet/client/RealArchiveStoreItem.java
Log:
Archive handling:
Synchronization.
Logging.

Modified: trunk/freenet/src/freenet/client/ArchiveManager.java
===================================================================
--- trunk/freenet/src/freenet/client/ArchiveManager.java        2007-02-21 
03:41:54 UTC (rev 11878)
+++ trunk/freenet/src/freenet/client/ArchiveManager.java        2007-02-21 
03:58:43 UTC (rev 11879)
@@ -391,8 +391,13 @@
         * Call synchronized on storedData.
         */
        private void trimStoredData() {
-               while((cachedData > maxCachedData) || (storedData.size() > 
maxCachedElements)) {
+               while(true) {
+                       synchronized(this) {
+                               if(cachedData <= maxCachedData && 
storedData.size() <= maxCachedElements) return;
+                       }
                        ArchiveStoreItem e = (ArchiveStoreItem) 
storedData.popValue();
+                       if(logMINOR)
+                               Logger.minor(this, "Dropping "+e+" : 
cachedData="+cachedData+" of "+maxCachedData);
                        e.close();
                }
        }
@@ -429,4 +434,8 @@
                        return Metadata.ARCHIVE_ZIP;
                else throw new IllegalArgumentException(); 
        }
+
+       public synchronized void decrementSpace(long spaceUsed) {
+               cachedData -= spaceUsed;
+       }
 }

Modified: trunk/freenet/src/freenet/client/ArchiveStoreContext.java
===================================================================
--- trunk/freenet/src/freenet/client/ArchiveStoreContext.java   2007-02-21 
03:41:54 UTC (rev 11878)
+++ trunk/freenet/src/freenet/client/ArchiveStoreContext.java   2007-02-21 
03:58:43 UTC (rev 11879)
@@ -119,9 +119,11 @@

        /** Notify that an archive store item with this key has been expelled 
from the cache. */
        public void removeItem(ArchiveStoreItem item) {
+               long spaceUsed = item.spaceUsed();
                synchronized(myItems) {
-                       myItems.remove(item);
+                       if(myItems.remove(item) == null) return;
                }
+               manager.decrementSpace(spaceUsed);
        }

        public short getArchiveType() {

Modified: trunk/freenet/src/freenet/client/ArchiveStoreItem.java
===================================================================
--- trunk/freenet/src/freenet/client/ArchiveStoreItem.java      2007-02-21 
03:41:54 UTC (rev 11878)
+++ trunk/freenet/src/freenet/client/ArchiveStoreItem.java      2007-02-21 
03:58:43 UTC (rev 11879)
@@ -31,4 +31,9 @@
         * Return cached data as a Bucket, or throw an ArchiveFailureException.
         */
        abstract Bucket getDataOrThrow() throws ArchiveFailureException;
+
+       /**
+        * Return the amount of cache space used by the item.
+        */
+       abstract long spaceUsed();
 }

Modified: trunk/freenet/src/freenet/client/ErrorArchiveStoreItem.java
===================================================================
--- trunk/freenet/src/freenet/client/ErrorArchiveStoreItem.java 2007-02-21 
03:41:54 UTC (rev 11878)
+++ trunk/freenet/src/freenet/client/ErrorArchiveStoreItem.java 2007-02-21 
03:58:43 UTC (rev 11879)
@@ -31,5 +31,9 @@
        Bucket getDataOrThrow() throws ArchiveFailureException {
                throw new ArchiveFailureException(error);
        }
+
+       public long spaceUsed() {
+               return 0;
+       }

 }

Modified: trunk/freenet/src/freenet/client/RealArchiveStoreItem.java
===================================================================
--- trunk/freenet/src/freenet/client/RealArchiveStoreItem.java  2007-02-21 
03:41:54 UTC (rev 11878)
+++ trunk/freenet/src/freenet/client/RealArchiveStoreItem.java  2007-02-21 
03:58:43 UTC (rev 11879)
@@ -59,13 +59,13 @@
                return FileUtil.estimateUsage(myFilename, underBucket.size());
        }

-       synchronized void close() {
+       void close() {
                super.close();
-               if(finalized) return;
-               long sz = spaceUsed();
-               underBucket.finalize();
-               finalized = true;
-               this.manager.cachedData -= sz;
+               synchronized(this) {
+                       if(finalized) return;
+                       underBucket.finalize();
+                       finalized = true;
+               }
        }

        Bucket getDataOrThrow() throws ArchiveFailureException {


Reply via email to