You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ec...@apache.org on 2014/11/16 07:58:25 UTC

svn commit: r1639972 - in /commons/proper/vfs/trunk: core/src/main/java/org/apache/commons/vfs2/cache/ core/src/test/java/org/apache/commons/vfs2/cache/ core/src/test/java/org/apache/commons/vfs2/test/ src/changes/

Author: ecki
Date: Sun Nov 16 06:58:24 2014
New Revision: 1639972

URL: http://svn.apache.org/r1639972
Log:
[VFS-545] Remove and avoid FileSystems in cache. Add/fix Javadoc, added Unittests.

Modified:
    commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/AbstractFilesCache.java
    commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/DefaultFilesCache.java
    commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/LRUFilesCache.java
    commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/NullFilesCache.java
    commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/SoftRefFilesCache.java
    commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/cache/LRUFilesCacheTests.java
    commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/cache/NullFilesCacheTests.java
    commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/CacheTestSuite.java
    commons/proper/vfs/trunk/src/changes/changes.xml

Modified: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/AbstractFilesCache.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/AbstractFilesCache.java?rev=1639972&r1=1639971&r2=1639972&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/AbstractFilesCache.java (original)
+++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/AbstractFilesCache.java Sun Nov 16 06:58:24 2014
@@ -16,10 +16,15 @@
  */
 package org.apache.commons.vfs2.cache;
 
+import org.apache.commons.vfs2.FileObject;
 import org.apache.commons.vfs2.FilesCache;
 import org.apache.commons.vfs2.provider.AbstractVfsComponent;
 
 
 public abstract class AbstractFilesCache extends AbstractVfsComponent implements FilesCache
 {
+    // @Override - commented in FilesCache interface
+    public void touchFile(final FileObject file)
+    {
+    }
 }

Modified: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/DefaultFilesCache.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/DefaultFilesCache.java?rev=1639972&r1=1639971&r2=1639972&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/DefaultFilesCache.java (original)
+++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/DefaultFilesCache.java Sun Nov 16 06:58:24 2014
@@ -25,17 +25,27 @@ import org.apache.commons.vfs2.FileObjec
 import org.apache.commons.vfs2.FileSystem;
 
 /**
- * A {@link org.apache.commons.vfs2.FilesCache} implementation.
+ * A simple {@link org.apache.commons.vfs2.FilesCache FilesCache} implementation.
  * <p>
- * This implementation caches every file for the complete lifetime of the used
- * {@link org.apache.commons.vfs2.FileSystemManager}.
+ * This implementation caches every file with no expire or limit.
+ * All files and filesystems are hard reachable references. This implementation
+ * holds a list of filesystem specific {@linkplain ConcurrentHashMap ConcurrentHashMaps} in
+ * the main cache map.
+ * <p>
+ * Cached {@linkplain FileObject FileObjects} as well as {@linkplain FileSystem FileSystems}
+ * are only removed when {@link #clear(FileSystem)} is called (i.e. on filesystem close).
+ * When the used {@link org.apache.commons.vfs2.FileSystemManager FileSystemManager} is closed,
+ * it will also {@linkplain #close() close} this cache (which frees all entries).
+ * <p>
+ * Despite its name, this is not the fallback implementation used by
+ * {@link org.apache.commons.vfs2.impl.DefaultFileSystemManager#init() DefaultFileSystemManager#init()}
+ * anymore.
  */
 public class DefaultFilesCache extends AbstractFilesCache
 {
-
-    /** The FileSystem cache */
+    /** The FileSystem cache. Keeps one Map for each FileSystem. */
     private final ConcurrentMap<FileSystem, ConcurrentMap<FileName, FileObject>> filesystemCache =
-          new ConcurrentHashMap<FileSystem, ConcurrentMap<FileName, FileObject>>(10);
+                    new ConcurrentHashMap<FileSystem, ConcurrentMap<FileName, FileObject>>(10);
 
     @Override
     public void putFile(final FileObject file)
@@ -54,23 +64,35 @@ public class DefaultFilesCache extends A
     @Override
     public FileObject getFile(final FileSystem filesystem, final FileName name)
     {
-        final Map<FileName, FileObject> files = getOrCreateFilesystemCache(filesystem);
-        return files.get(name);
+        // avoid creating filesystem entry for empty filesystem cache:
+        final Map<FileName, FileObject> files = filesystemCache.get(filesystem);
+        if (files == null)
+        {
+            // cache for filesystem is not known => file is not cached:
+            return null;
+        }
+
+        return files.get(name); // or null
     }
 
     @Override
     public void clear(final FileSystem filesystem)
     {
-        final Map<FileName, FileObject> files = getOrCreateFilesystemCache(filesystem);
-        files.clear();
+        // avoid keeping a reference to the FileSystem (key) object
+        final Map<FileName, FileObject> files = filesystemCache.remove(filesystem);
+        if (files != null)
+        {
+            files.clear(); // help GC
+        }
     }
 
     protected ConcurrentMap<FileName, FileObject> getOrCreateFilesystemCache(final FileSystem filesystem)
     {
         ConcurrentMap<FileName, FileObject> files = filesystemCache.get(filesystem);
-        if (files == null)
+        // we loop to make sure we never return null even when concurrent clean is called
+        while (files == null)
         {
-            filesystemCache.putIfAbsent(filesystem, new ConcurrentHashMap<FileName, FileObject>());
+            filesystemCache.putIfAbsent(filesystem, new ConcurrentHashMap<FileName, FileObject>(200, 0.75f, 8));
             files = filesystemCache.get(filesystem);
         }
 
@@ -88,11 +110,13 @@ public class DefaultFilesCache extends A
     @Override
     public void removeFile(final FileSystem filesystem, final FileName name)
     {
-        final Map<?, ?> files = getOrCreateFilesystemCache(filesystem);
-        files.remove(name);
-    }
-
-    public void touchFile(final FileObject file)
-    {
+        // avoid creating filesystem entry for empty filesystem cache:
+        final Map<FileName, FileObject> files = filesystemCache.get(filesystem);
+        if (files != null)
+        {
+            files.remove(name);
+            // This would be too racey:
+            // if (files.empty()) filesystemCache.remove(filessystem);
+        }
     }
 }

Modified: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/LRUFilesCache.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/LRUFilesCache.java?rev=1639972&r1=1639971&r2=1639972&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/LRUFilesCache.java (original)
+++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/LRUFilesCache.java Sun Nov 16 06:58:24 2014
@@ -254,6 +254,7 @@ public class LRUFilesCache extends Abstr
         }
     }
 
+    @Override
     public void touchFile(final FileObject file)
     {
         // this moves the file back on top

Modified: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/NullFilesCache.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/NullFilesCache.java?rev=1639972&r1=1639971&r2=1639972&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/NullFilesCache.java (original)
+++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/NullFilesCache.java Sun Nov 16 06:58:24 2014
@@ -56,8 +56,4 @@ public class NullFilesCache extends Abst
     public void removeFile(final FileSystem filesystem, final FileName name)
     {
     }
-
-    public void touchFile(final FileObject file)
-    {
-    }
 }

Modified: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/SoftRefFilesCache.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/SoftRefFilesCache.java?rev=1639972&r1=1639971&r2=1639972&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/SoftRefFilesCache.java (original)
+++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/cache/SoftRefFilesCache.java Sun Nov 16 06:58:24 2014
@@ -336,10 +336,6 @@ public class SoftRefFilesCache extends A
         }
     }
 
-    public void touchFile(final FileObject fileObject)
-    {
-    }
-
     private boolean removeFile(final FileSystemAndNameKey key)
     {
         if (log.isDebugEnabled())

Modified: commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/cache/LRUFilesCacheTests.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/cache/LRUFilesCacheTests.java?rev=1639972&r1=1639971&r2=1639972&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/cache/LRUFilesCacheTests.java (original)
+++ commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/cache/LRUFilesCacheTests.java Sun Nov 16 06:58:24 2014
@@ -17,12 +17,11 @@
 package org.apache.commons.vfs2.cache;
 
 import org.apache.commons.vfs2.FileObject;
-import org.apache.commons.vfs2.test.AbstractProviderTestCase;
 
 /**
- * NullFilesCache
+ * Tests for {@link LRUFilesCache} used by {@link LRUFilesCacheTestCase}.
  */
-public class LRUFilesCacheTests extends AbstractProviderTestCase
+public class LRUFilesCacheTests extends FilesCacheTestsBase
 {
     public void testFilesCache() throws Exception
     {
@@ -73,4 +72,9 @@ public class LRUFilesCacheTests extends 
         final FileObject dir1_2 = scratchFolder.resolveFile("dir1");
         assertFalse(dir1 == dir1_2);
     }
+
+    public void testClass()
+    {
+        assertTrue(getManager().getFilesCache() instanceof LRUFilesCache);
+    }
 }

Modified: commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/cache/NullFilesCacheTests.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/cache/NullFilesCacheTests.java?rev=1639972&r1=1639971&r2=1639972&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/cache/NullFilesCacheTests.java (original)
+++ commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/cache/NullFilesCacheTests.java Sun Nov 16 06:58:24 2014
@@ -16,14 +16,15 @@
  */
 package org.apache.commons.vfs2.cache;
 
+import org.apache.commons.vfs2.FileName;
 import org.apache.commons.vfs2.FileObject;
-import org.apache.commons.vfs2.test.AbstractProviderTestCase;
+import org.apache.commons.vfs2.FileSystem;
+import org.apache.commons.vfs2.FilesCache;
 
 /**
- * NullFilesCache
- *
+ * Tests for {@link NullFilesCache} used by {@link NullFilesCacheTestCase}.
  */
-public class NullFilesCacheTests extends AbstractProviderTestCase
+public class NullFilesCacheTests extends FilesCacheTestsBase
 {
     public void testFilesCache() throws Exception
     {
@@ -32,6 +33,35 @@ public class NullFilesCacheTests extends
         final FileObject dir1 = scratchFolder.resolveFile("dir1");
         final FileObject dir1_2 = scratchFolder.resolveFile("dir1");
 
-        assertFalse(dir1 == dir1_2);
+        assertFalse("Should always be new instance with NullCache", dir1 == dir1_2);
+    }
+
+    @Override
+    public void testBasicCacheOps() throws Exception
+    {
+        // the basic test looks different for a null cache:
+
+        final FilesCache cache = getManager().getFilesCache();
+        final FileObject fo = getWriteFolder().resolveFile("dir1");
+        final FileName fn = fo.getName();
+        final FileSystem fs = fo.getFileSystem();
+
+        cache.clear(fs);
+        assertNull(cache.getFile(fs, fn));
+
+        cache.putFile(fo);
+        assertNull(null, cache.getFile(fs, fn));
+
+        assertFalse(cache.putFileIfAbsent(fo)); // hmmm?
+        assertNull(null, cache.getFile(fs, fn));
+
+        cache.removeFile(fs, fn);
+        assertNull(cache.getFile(fs, fn));
+    }
+
+
+    public void testClass()
+    {
+        assertTrue(getManager().getFilesCache() instanceof NullFilesCache);
     }
 }

Modified: commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/CacheTestSuite.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/CacheTestSuite.java?rev=1639972&r1=1639971&r2=1639972&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/CacheTestSuite.java (original)
+++ commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/test/CacheTestSuite.java Sun Nov 16 06:58:24 2014
@@ -18,14 +18,11 @@ package org.apache.commons.vfs2.test;
 
 
 /**
- * The suite of tests for a file system.
+ * The suite of tests for a file cache.
  */
 public class CacheTestSuite
     extends AbstractTestSuite
 {
-    /**
-     * Adds the tests for a file system to this suite.
-     */
     public CacheTestSuite(final ProviderTestConfig providerConfig) throws Exception
     {
         this(providerConfig, "", false);

Modified: commons/proper/vfs/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/src/changes/changes.xml?rev=1639972&r1=1639971&r2=1639972&view=diff
==============================================================================
--- commons/proper/vfs/trunk/src/changes/changes.xml (original)
+++ commons/proper/vfs/trunk/src/changes/changes.xml Sun Nov 16 06:58:24 2014
@@ -24,8 +24,11 @@
   <body>
     <release version="2.1" date="TBD" description="New features and bug fix release.">
 <!--       <action issue="VFS-443" dev="ggregory" type="update" due-to="nickallen"> -->
-<!--     	[Local] Need an easy way to convert from a FileObject to a File. -->
+<!--        [Local] Need an easy way to convert from a FileObject to a File. -->
 <!--       </action> -->
+      <action issue="VFS-545" dev="ecki" type="fix">
+       Make DefaultFilesCache remove reference to filesystem when it is cleared (closed).
+      </action>
       <action issue="VFS-521" dev="ecki" type="fix">
        [Ram][Tests] Make RAM provider test pass on Java 8
        (JDK-8042377, self-suppression not permitted, MonitorOutputStream#close()).