commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ima...@apache.org
Subject cvs commit: jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/cache LRUFilesCacheTestCase.java LRUFilesCacheTests.java
Date Wed, 16 Jun 2004 20:21:55 GMT
imario      2004/06/16 13:21:55

  Added:       vfs/src/java/org/apache/commons/vfs/cache LRUFilesCache.java
               vfs/src/test/org/apache/commons/vfs/cache
                        LRUFilesCacheTestCase.java LRUFilesCacheTests.java
  Log:
  LRUFilesCache
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/cache/LRUFilesCache.java
  
  Index: LRUFilesCache.java
  ===================================================================
  /*
   * Copyright 2002, 2003,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.vfs.cache;
  
  import org.apache.commons.collections.map.AbstractLinkedMap;
  import org.apache.commons.collections.map.LRUMap;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.apache.commons.vfs.FileName;
  import org.apache.commons.vfs.FileObject;
  import org.apache.commons.vfs.FileSystem;
  import org.apache.commons.vfs.FileSystemException;
  import org.apache.commons.vfs.VfsLog;
  import org.apache.commons.vfs.provider.AbstractFileObject;
  import org.apache.commons.vfs.util.Messages;
  
  import java.util.HashMap;
  import java.util.Map;
  
  /**
   * This implementation caches every file using {@link LRUMap}.<br>
   * The default constructor uses a LRU size of 100 per filesystem.
   *
   * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
   * @version $Revision: 1.1 $ $Date: 2004/06/16 20:21:55 $
   */
  public class LRUFilesCache extends AbstractFilesCache
  {
      /**
       * The logger to use.
       */
      private Log log = LogFactory.getLog(LRUFilesCache.class);
  
      private final Map filesystemCache = new HashMap(10);
      private final int lruSize;
  
      private class MyLRUMap extends LRUMap
      {
          final FileSystem filesystem;
  
          public MyLRUMap(final FileSystem filesystem, int size)
          {
              super(size, true);
              this.filesystem = filesystem;
          }
  
          protected boolean removeLRU(final AbstractLinkedMap.LinkEntry linkEntry)
          {
              synchronized (LRUFilesCache.this)
              {
                  AbstractFileObject file = (AbstractFileObject) linkEntry.getValue();
                  // System.err.println(">>> " + size() + " check removeLRU:" + linkEntry.getKey().toString());
  
                  if (file.isAttached() || file.isContentOpen())
                  {
                      // do not allow open or attached files to be removed
                      // System.err.println(">>> " + size() + " VETO removeLRU:"
+ linkEntry.getKey().toString() + " (" + file.isAttached() + "/" + file.isContentOpen() +
")");
                      return false;
                  }
  
                  // System.err.println(">>> " + size() + " removeLRU:" + linkEntry.getKey().toString());
                  if (super.removeLRU(linkEntry))
                  {
                      try
                      {
                          // force detach
                          file.close();
                      }
                      catch (FileSystemException e)
                      {
                          VfsLog.warn(getLogger(), log, Messages.getString("vfs.impl/LRUFilesCache-remove-ex.warn"),
e);
                      }
  
                      Map files = (Map) filesystemCache.get(filesystem);
                      if (files.size() < 1)
                      {
                          filesystemCache.remove(filesystem);
                      }
  
                      return true;
                  }
  
                  return false;
              }
          }
      }
  
      /**
       * Default constructor. Uses a LRU size of 100 per filesystem.
       */
      public LRUFilesCache()
      {
          this(100);
      }
  
      /**
       * Set the desired LRU size.
       *
       * @param lruSize the LRU size
       */
      public LRUFilesCache(int lruSize)
      {
          this.lruSize = lruSize;
      }
  
      public void putFile(final FileObject file)
      {
          synchronized (this)
          {
              Map files = getOrCreateFilesystemCache(file.getFileSystem());
  
              // System.err.println(">>> " + files.size() + " put:" + file.toString());
  
              files.put(file.getName(), file);
          }
      }
  
      public FileObject getFile(final FileSystem filesystem, final FileName name)
      {
          synchronized (this)
          {
              Map files = getOrCreateFilesystemCache(filesystem);
  
              // FileObject fo = (FileObject) files.get(name);
              // System.err.println(">>> " + files.size() + " get:" + name.toString()
+ " " + fo);
  
              return (FileObject) files.get(name);
          }
      }
  
      public void clear(final FileSystem filesystem)
      {
          synchronized (this)
          {
              // System.err.println(">>> clear fs " + filesystem);
  
              Map files = getOrCreateFilesystemCache(filesystem);
              files.clear();
  
              filesystemCache.remove(filesystem);
          }
      }
  
      protected Map getOrCreateFilesystemCache(final FileSystem filesystem)
      {
          Map files = (Map) filesystemCache.get(filesystem);
          if (files == null)
          {
              // System.err.println(">>> create fs " + filesystem);
  
              files = new MyLRUMap(filesystem, lruSize);
              filesystemCache.put(filesystem, files);
          }
  
          return files;
      }
  
      public void close()
      {
          super.close();
  
          synchronized (this)
          {
              // System.err.println(">>> clear all");
  
              filesystemCache.clear();
          }
      }
  
      public void removeFile(final FileSystem filesystem, final FileName name)
      {
          synchronized (this)
          {
              Map files = getOrCreateFilesystemCache(filesystem);
  
              // System.err.println(">>> " + files.size() + " remove:" + name.toString());
  
              files.remove(name);
  
              if (files.size() < 1)
              {
                  filesystemCache.remove(filesystem);
              }
          }
      }
  
      public void touchFile(final FileObject file)
      {
          // this moves the file back on top
          getFile(file.getFileSystem(), file.getName());
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/cache/LRUFilesCacheTestCase.java
  
  Index: LRUFilesCacheTestCase.java
  ===================================================================
  /*
   * Copyright 2002, 2003,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.vfs.cache;
  
  import junit.framework.Test;
  import org.apache.commons.AbstractVfsTestCase;
  import org.apache.commons.vfs.FileObject;
  import org.apache.commons.vfs.FileSystemManager;
  import org.apache.commons.vfs.FilesCache;
  import org.apache.commons.vfs.test.AbstractProviderTestConfig;
  import org.apache.commons.vfs.test.CacheTestSuite;
  import org.apache.commons.vfs.test.ProviderTestConfig;
  
  import java.io.File;
  
  /**
   * Tests the NullFilesCache
   * 
   * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
   * @version $Revision: 1.1 $ $Date: 2004/06/16 20:21:55 $
   */
  public class LRUFilesCacheTestCase
      extends AbstractProviderTestConfig
      implements ProviderTestConfig
  {
      public static Test suite() throws Exception
      {
          CacheTestSuite suite = new CacheTestSuite(new LRUFilesCacheTestCase());
          suite.addTests(LRUFilesCacheTests.class);
          return suite;
      }
  
      public FilesCache getFilesCache()
      {
          return new LRUFilesCache(5);
      }
  
      public FileObject getBaseTestFolder(final FileSystemManager manager) throws Exception
      {
          final File testDir = AbstractVfsTestCase.getTestDirectoryFile();
          return manager.toFileObject(testDir);
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/cache/LRUFilesCacheTests.java
  
  Index: LRUFilesCacheTests.java
  ===================================================================
  /*
   * Copyright 2002, 2003,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.vfs.cache;
  
  import org.apache.commons.vfs.FileObject;
  import org.apache.commons.vfs.test.AbstractProviderTestCase;
  
  /**
   * NullFilesCache
   * 
   * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
   * @version $Revision: 1.1 $ $Date: 2004/06/16 20:21:55 $
   */
  public class LRUFilesCacheTests extends AbstractProviderTestCase
  {
      public void testFilesCache() throws Exception
      {
          FileObject scratchFolder = getWriteFolder();
  
          // releaseable
          FileObject dir1 = scratchFolder.resolveFile("dir1");
  
          // avoid cache removal
          FileObject dir2 = scratchFolder.resolveFile("dir2");
          dir2.getContent();
  
          // releaseable
          FileObject dir3 = scratchFolder.resolveFile("dir3");
  
          // releaseable
          FileObject dir4 = scratchFolder.resolveFile("dir4");
  
          // releaseable
          FileObject dir5 = scratchFolder.resolveFile("dir5");
  
          // releaseable
          FileObject dir6 = scratchFolder.resolveFile("dir6");
  
          // releaseable
          FileObject dir7 = scratchFolder.resolveFile("dir7");
  
          // releaseable
          FileObject dir8 = scratchFolder.resolveFile("dir8");
  
          // check if the cache still holds the right instance
          FileObject dir2_2 = scratchFolder.resolveFile("dir2");
          assertTrue(dir2 == dir2_2);
  
          // check if the cache still holds the right instance
          FileObject dir1_2 = scratchFolder.resolveFile("dir1");
          assertFalse(dir1 == dir1_2);
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message