Return-Path: X-Original-To: apmail-ace-commits-archive@www.apache.org Delivered-To: apmail-ace-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 269CC96D3 for ; Mon, 16 Apr 2012 18:05:50 +0000 (UTC) Received: (qmail 84155 invoked by uid 500); 16 Apr 2012 18:05:50 -0000 Delivered-To: apmail-ace-commits-archive@ace.apache.org Received: (qmail 84127 invoked by uid 500); 16 Apr 2012 18:05:50 -0000 Mailing-List: contact commits-help@ace.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ace.apache.org Delivered-To: mailing list commits@ace.apache.org Received: (qmail 84119 invoked by uid 99); 16 Apr 2012 18:05:50 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 16 Apr 2012 18:05:50 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 16 Apr 2012 18:05:48 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 6165D2388BF1 for ; Mon, 16 Apr 2012 18:05:28 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1326714 - in /ace/trunk/ace-deployment-provider-repositorybased: ./ src/main/java/org/apache/ace/deployment/provider/repositorybased/ src/test/java/org/apache/ace/deployment/provider/repositorybased/ Date: Mon, 16 Apr 2012 18:05:28 -0000 To: commits@ace.apache.org From: marrs@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120416180528.6165D2388BF1@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 extends LinkedHashMap { + 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 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> m_cachedVersionLists = new LRUMap>(); public RepositoryBasedProvider() { m_saxParserFactory = SAXParserFactory.newInstance(); @@ -147,6 +150,22 @@ public class RepositoryBasedProvider imp @SuppressWarnings("unchecked") public List getVersions(String targetId) throws IllegalArgumentException, IOException { + // check if cache is up to date + if (isCacheUpToDate()) { + List 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 stringVersionList = new ArrayList(); 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 map = new LRUMap(); + 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 map = new LRUMap(); + // 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