Author: marrs
Date: Mon Apr 16 18:05:27 2012
New Revision: 1326714

URL: http://svn.apache.org/viewvc?rev=1326714&view=rev
Log:
Added a LRU cache for the version ranges. They are queried a lot, and even 
though the performance of fetching them from the repository has been improved a 
lot, this cache makes a big difference.

Added:
    
ace/trunk/ace-deployment-provider-repositorybased/src/main/java/org/apache/ace/deployment/provider/repositorybased/LRUMap.java
   (with props)
    
ace/trunk/ace-deployment-provider-repositorybased/src/test/java/org/apache/ace/deployment/provider/repositorybased/CacheTest.java
   (with props)
Modified:
    ace/trunk/ace-deployment-provider-repositorybased/   (props changed)
    
ace/trunk/ace-deployment-provider-repositorybased/src/main/java/org/apache/ace/deployment/provider/repositorybased/RepositoryBasedProvider.java

Propchange: ace/trunk/ace-deployment-provider-repositorybased/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Mon Apr 16 18:05:27 2012
@@ -6,3 +6,5 @@ target
 *.ipr
 *.iws
 *.iml
+
+test-output

Added: 
ace/trunk/ace-deployment-provider-repositorybased/src/main/java/org/apache/ace/deployment/provider/repositorybased/LRUMap.java
URL: 
http://svn.apache.org/viewvc/ace/trunk/ace-deployment-provider-repositorybased/src/main/java/org/apache/ace/deployment/provider/repositorybased/LRUMap.java?rev=1326714&view=auto
==============================================================================
--- 
ace/trunk/ace-deployment-provider-repositorybased/src/main/java/org/apache/ace/deployment/provider/repositorybased/LRUMap.java
 (added)
+++ 
ace/trunk/ace-deployment-provider-repositorybased/src/main/java/org/apache/ace/deployment/provider/repositorybased/LRUMap.java
 Mon Apr 16 18:05:27 2012
@@ -0,0 +1,36 @@
+/*
+ * 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.ace.deployment.provider.repositorybased;
+
+import java.util.LinkedHashMap;
+
+public class LRUMap<K, V> extends LinkedHashMap<K, V> {
+       private static final int INITIAL = 64;
+       private static final int MAX = 1024;
+       private static final float LOADFACTOR = 0.75f;
+
+       public LRUMap() {
+               super(INITIAL, LOADFACTOR, true);
+       }
+       
+       @Override
+       protected boolean removeEldestEntry(java.util.Map.Entry<K, V> eldest) {
+               return size() > MAX;
+       }
+}

Propchange: 
ace/trunk/ace-deployment-provider-repositorybased/src/main/java/org/apache/ace/deployment/provider/repositorybased/LRUMap.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
ace/trunk/ace-deployment-provider-repositorybased/src/main/java/org/apache/ace/deployment/provider/repositorybased/RepositoryBasedProvider.java
URL: 
http://svn.apache.org/viewvc/ace/trunk/ace-deployment-provider-repositorybased/src/main/java/org/apache/ace/deployment/provider/repositorybased/RepositoryBasedProvider.java?rev=1326714&r1=1326713&r2=1326714&view=diff
==============================================================================
--- 
ace/trunk/ace-deployment-provider-repositorybased/src/main/java/org/apache/ace/deployment/provider/repositorybased/RepositoryBasedProvider.java
 (original)
+++ 
ace/trunk/ace-deployment-provider-repositorybased/src/main/java/org/apache/ace/deployment/provider/repositorybased/RepositoryBasedProvider.java
 Mon Apr 16 18:05:27 2012
@@ -70,6 +70,9 @@ public class RepositoryBasedProvider imp
      */
     private volatile Repository m_directRepository;
     private final SAXParserFactory m_saxParserFactory;
+    
+    
+    private Map<String,List<String>> m_cachedVersionLists = new LRUMap<String, 
List<String>>();
 
     public RepositoryBasedProvider() {
         m_saxParserFactory = SAXParserFactory.newInstance();
@@ -147,6 +150,22 @@ public class RepositoryBasedProvider imp
 
     @SuppressWarnings("unchecked")
     public List<String> getVersions(String targetId) throws 
IllegalArgumentException, IOException {
+       // check if cache is up to date
+       if (isCacheUpToDate()) {
+               List<String> result = m_cachedVersionLists.get(targetId);
+               if (result != null) {
+                       System.out.println("Cache hit!");
+                       return result;
+               }
+               System.out.println("Cache miss!");
+       }
+       else {
+               m_cachedVersionLists.clear();
+               System.out.println("Cache cleared!");
+       }
+
+       
+       
         List<String> stringVersionList = new ArrayList<String>();
         InputStream input = null;
 
@@ -186,6 +205,8 @@ public class RepositoryBasedProvider imp
             }
         }
 
+        System.out.println("Cache added: " + targetId);
+        m_cachedVersionLists.put(targetId, stringVersionList);
         return stringVersionList;
     }
 
@@ -346,6 +367,17 @@ public class RepositoryBasedProvider imp
 
         return result;
     }
+    
+    private boolean isCacheUpToDate() {
+        CachedRepository cachedRepository = m_cachedRepository;
+        try {
+                       return (cachedRepository != null && 
cachedRepository.isCurrent());
+               }
+        catch (IOException ioe) {
+               m_log.log(LogService.LOG_WARNING, "Failed to check if cache is 
current. Assuming it's not.", ioe);
+               return false;
+               }
+    }
 
     public void updated(Dictionary settings) throws ConfigurationException {
         if (settings != null) {

Added: 
ace/trunk/ace-deployment-provider-repositorybased/src/test/java/org/apache/ace/deployment/provider/repositorybased/CacheTest.java
URL: 
http://svn.apache.org/viewvc/ace/trunk/ace-deployment-provider-repositorybased/src/test/java/org/apache/ace/deployment/provider/repositorybased/CacheTest.java?rev=1326714&view=auto
==============================================================================
--- 
ace/trunk/ace-deployment-provider-repositorybased/src/test/java/org/apache/ace/deployment/provider/repositorybased/CacheTest.java
 (added)
+++ 
ace/trunk/ace-deployment-provider-repositorybased/src/test/java/org/apache/ace/deployment/provider/repositorybased/CacheTest.java
 Mon Apr 16 18:05:27 2012
@@ -0,0 +1,66 @@
+/*
+ * 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.ace.deployment.provider.repositorybased;
+
+import static org.apache.ace.test.utils.TestUtils.UNIT;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class CacheTest {
+       @Test(groups = { UNIT })
+       public void testFillCacheToLimitAndCheckIfEverythingFits() {
+               LRUMap<String, String> map = new LRUMap<String, String>();
+               for (int i = 0; i < 1024; i++) {
+                       String key = "" + i;
+                       map.put(key, key);
+               }
+               for (int i = 0; i < 1024; i++) {
+                       String key = "" + i;
+                       Assert.assertEquals(map.get(key), key);
+               }
+       }
+
+       @Test(groups = { UNIT })
+       public void testOverflowCacheAndValidateOldestElementDisappears() {
+               LRUMap<String, String> map = new LRUMap<String, String>();
+               // add one too many
+               for (int i = 0; i < 1025; i++) {
+                       String key = "" + i;
+                       map.put(key, key);
+               }
+               // retrieve in same order (first one should be gone)
+               for (int i = 0; i < 1025; i++) {
+                       String key = "" + i;
+                       if (i == 0) {
+                               Assert.assertNull(map.get(key));
+                       }
+                       else {
+                               Assert.assertEquals(map.get(key), key);
+                       }
+               }
+               // access the second one
+               map.get("1");
+               // add another one
+               String key = "1025";
+               map.put(key, key);
+               // make sure the third is gone now
+               Assert.assertNull(map.get("2"));
+       }
+}

Propchange: 
ace/trunk/ace-deployment-provider-repositorybased/src/test/java/org/apache/ace/deployment/provider/repositorybased/CacheTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to