harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From regi...@apache.org
Subject svn commit: r794945 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/ main/java/org/apache/harmony/luni/internal/io/ test/impl/common/org/apache/harmony/luni/tests/java/io/
Date Fri, 17 Jul 2009 02:23:00 GMT
Author: regisxu
Date: Fri Jul 17 02:23:00 2009
New Revision: 794945

URL: http://svn.apache.org/viewvc?rev=794945&view=rev
Log:
Apply patch for HARMONY-6200: [classlib][luni] - add cache for file's canonical path

Added:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/io/
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/io/FileCanonPathCache.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/impl/common/org/apache/harmony/luni/tests/java/io/
    harmony/enhanced/classlib/trunk/modules/luni/src/test/impl/common/org/apache/harmony/luni/tests/java/io/FileCanonPathCacheTest.java
Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/File.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/File.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/File.java?rev=794945&r1=794944&r2=794945&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/File.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/File.java Fri Jul 17
02:23:00 2009
@@ -25,6 +25,7 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.harmony.luni.internal.io.FileCanonPathCache;
 import org.apache.harmony.luni.util.DeleteOnExit;
 import org.apache.harmony.luni.util.Msg;
 import org.apache.harmony.luni.util.PriviAction;
@@ -487,6 +488,12 @@
      */
     public String getCanonicalPath() throws IOException {
         byte[] result = properPath(false);
+        String absPath = Util.toUTF8String(result);
+        String canonPath = FileCanonPathCache.get(absPath);
+
+        if (canonPath != null) {
+            return canonPath;
+        }
         if(separatorChar == '/') {
             // resolve the full path first
             result = resolveLink(result, result.length, false);
@@ -558,7 +565,9 @@
         newResult[newLength] = 0;
         newResult = getCanonImpl(newResult);
         newLength = newResult.length;
-        return Util.toUTF8String(newResult, 0, newLength);
+        canonPath = Util.toUTF8String(newResult, 0, newLength);
+        FileCanonPathCache.put(absPath, canonPath);
+        return canonPath;
     }
     
     /*

Added: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/io/FileCanonPathCache.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/io/FileCanonPathCache.java?rev=794945&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/io/FileCanonPathCache.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/io/FileCanonPathCache.java
Fri Jul 17 02:23:00 2009
@@ -0,0 +1,136 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You 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.harmony.luni.internal.io;
+
+import java.util.HashMap;
+import java.util.LinkedList;
+
+/**
+ * A simple cache implementation for file's canonical path. The cache has fixed
+ * size <code> CACHE_SIZE </code> and cached elements would be expired. If
+ * <code>put<code> method is invoked when cache is full, the oldest element will
be removed.
+ *
+ */
+public class FileCanonPathCache {
+
+    static private class CacheElement {
+        String canonicalPath;
+
+        long timestamp;
+
+        public CacheElement(String path) {
+            this.canonicalPath = path;
+            this.timestamp = System.currentTimeMillis();
+        }
+    }
+
+    /**
+     * Max elemnts could be hold in the cache.
+     */
+    public static final int CACHE_SIZE = 256;
+
+    private static HashMap<String, CacheElement> cache = new HashMap<String, CacheElement>(
+            CACHE_SIZE);
+
+    /**
+     * FIFO queue for tracking age of elements.
+     */
+    private static LinkedList<String> list = new LinkedList<String>();
+
+    private static Object lock = new Object();
+
+    /**
+     * Expired time.
+     */
+    private static long timeout = 600000;
+
+    /**
+     * Retrieve element from cache.
+     * 
+     * @param path
+     *            absolute path.
+     * @return canonical path of <code>path</code> if it's in cache.
+     * 
+     */
+    public static String get(String path) {
+        CacheElement element = null;
+        synchronized (lock) {
+            element = cache.get(path);
+        }
+
+        if (element == null) {
+            return null;
+        }
+
+        long time = System.currentTimeMillis();
+        if (time - element.timestamp > timeout) {
+            // remove all elements older than this one
+            synchronized (lock) {
+                if (cache.get(path) != null) {
+                    String oldest = null;
+                    do {
+                        oldest = list.removeFirst();
+                        cache.remove(path);
+                    } while (!path.equals(oldest));
+                }
+            }
+            return null;
+        }
+
+        return element.canonicalPath;
+    }
+
+    /**
+     * Put element to cache.
+     * 
+     * @param path
+     *            absolute path.
+     * @param canonicalPath
+     *            the canonical path of <code>path</code>.
+     */
+    public static void put(String path, String canonicalPath) {
+        CacheElement element = new CacheElement(canonicalPath);
+        synchronized (lock) {
+            if (cache.size() >= CACHE_SIZE) {
+                // cache is full
+                String oldest = list.removeFirst();
+                cache.remove(oldest);
+            }
+            cache.put(path, element);
+            list.addLast(path);
+        }
+    }
+
+    /**
+     * Remove all elements from cache.
+     */
+    public static void clear() {
+        synchronized (lock) {
+            cache.clear();
+            list.clear();
+        }
+    }
+
+    public static long getTimeout() {
+        return timeout;
+    }
+
+    public static void setTimeout(long timeout) {
+        FileCanonPathCache.timeout = timeout;
+    }
+}

Added: harmony/enhanced/classlib/trunk/modules/luni/src/test/impl/common/org/apache/harmony/luni/tests/java/io/FileCanonPathCacheTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/impl/common/org/apache/harmony/luni/tests/java/io/FileCanonPathCacheTest.java?rev=794945&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/impl/common/org/apache/harmony/luni/tests/java/io/FileCanonPathCacheTest.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/impl/common/org/apache/harmony/luni/tests/java/io/FileCanonPathCacheTest.java
Fri Jul 17 02:23:00 2009
@@ -0,0 +1,120 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You 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.harmony.luni.tests.java.io;
+
+import java.io.File;
+
+import org.apache.harmony.luni.internal.io.FileCanonPathCache;
+
+import junit.framework.TestCase;
+
+public class FileCanonPathCacheTest extends TestCase {
+
+    private static int DEFAULT_TIMEOUT = 600000;
+
+    @Override
+    public void setUp() throws Exception {
+        FileCanonPathCache.clear();
+        FileCanonPathCache.setTimeout(DEFAULT_TIMEOUT);
+    }
+
+    public void testGetSet() throws Exception {
+        File file1 = new File("test/hello~1");
+        assertNull(FileCanonPathCache.get(file1.getAbsolutePath()));
+        FileCanonPathCache.put(file1.getAbsolutePath(), file1
+                .getCanonicalPath());
+        assertEquals(file1.getCanonicalPath(), FileCanonPathCache.get(file1
+                .getAbsolutePath()));
+
+        File file2 = new File("test/world~1");
+        assertNull(FileCanonPathCache.get(file2.getAbsolutePath()));
+        FileCanonPathCache.put(file2.getAbsolutePath(), file2
+                .getCanonicalPath());
+        assertEquals(file2.getCanonicalPath(), FileCanonPathCache.get(file2
+                .getAbsolutePath()));
+
+        assertNull(FileCanonPathCache.get("notexist"));
+    }
+
+    public void testGetTimeout01() throws Exception {
+        FileCanonPathCache.setTimeout(10);
+
+        File file1 = new File("test/hello~1");
+        assertNull(FileCanonPathCache.get(file1.getAbsolutePath()));
+        FileCanonPathCache.put(file1.getAbsolutePath(), file1
+                .getCanonicalPath());
+        Thread.sleep(50);
+        assertNull(FileCanonPathCache.get(file1.getAbsolutePath()));
+    }
+
+    public void testGetTimeout02() throws Exception {
+        FileCanonPathCache.setTimeout(10);
+
+        File file1 = new File("test/hello~1");
+        assertNull(FileCanonPathCache.get(file1.getAbsolutePath()));
+        FileCanonPathCache.put(file1.getAbsolutePath(), file1
+                .getCanonicalPath());
+        File file2 = new File("test/hello~2");
+        assertNull(FileCanonPathCache.get(file2.getAbsolutePath()));
+        FileCanonPathCache.put(file2.getAbsolutePath(), file2
+                .getCanonicalPath());
+        File file3 = new File("test/hello~3");
+        assertNull(FileCanonPathCache.get(file3.getAbsolutePath()));
+        FileCanonPathCache.put(file3.getAbsolutePath(), file3
+                .getCanonicalPath());
+        File file4 = new File("test/hello~4");
+        assertNull(FileCanonPathCache.get(file4.getAbsolutePath()));
+        FileCanonPathCache.put(file4.getAbsolutePath(), file4
+                .getCanonicalPath());
+        File file5 = new File("test/hello~5");
+        assertNull(FileCanonPathCache.get(file5.getAbsolutePath()));
+        FileCanonPathCache.put(file5.getAbsolutePath(), file5
+                .getCanonicalPath());
+
+        Thread.sleep(50);
+
+        assertNull(FileCanonPathCache.get(file1.getAbsolutePath()));
+        assertNull(FileCanonPathCache.get(file2.getAbsolutePath()));
+        assertNull(FileCanonPathCache.get(file3.getAbsolutePath()));
+        assertNull(FileCanonPathCache.get(file4.getAbsolutePath()));
+        assertNull(FileCanonPathCache.get(file5.getAbsolutePath()));
+    }
+
+    public void testCacheFull() throws Exception {
+        int cacheSize = FileCanonPathCache.CACHE_SIZE;
+        File[] files = new File[cacheSize];
+        for (int i = 0; i < cacheSize; ++i) {
+            files[i] = new File("test/world" + i);
+            FileCanonPathCache.put(files[i].getAbsolutePath(), files[i]
+                    .getCanonicalPath());
+        }
+
+        for (int i = cacheSize; i < files.length; ++i) {
+            assertEquals(files[i - cacheSize].getCanonicalPath(),
+                    FileCanonPathCache.get(files[i - cacheSize]
+                            .getAbsolutePath()));
+            files[i] = new File("test/world" + i);
+            FileCanonPathCache.put(files[i].getAbsolutePath(), files[i]
+                    .getCanonicalPath());
+            assertEquals(files[i].getCanonicalPath(), FileCanonPathCache
+                    .get(files[i].getAbsolutePath()));
+            assertNull(FileCanonPathCache.get(files[i - cacheSize]
+                    .getAbsolutePath()));
+        }
+    }
+}



Mime
View raw message