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 E6A3C105E0 for ; Wed, 1 May 2013 13:17:49 +0000 (UTC) Received: (qmail 70517 invoked by uid 500); 1 May 2013 13:17:49 -0000 Delivered-To: apmail-ace-commits-archive@ace.apache.org Received: (qmail 70180 invoked by uid 500); 1 May 2013 13:17:47 -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 68363 invoked by uid 99); 1 May 2013 13:17:41 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 01 May 2013 13:17:41 +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; Wed, 01 May 2013 13:17:39 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id AEAA42388993; Wed, 1 May 2013 13:17:19 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1477982 - /ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/file/BundleFileStore.java Date: Wed, 01 May 2013 13:17:19 -0000 To: commits@ace.apache.org From: bramk@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130501131719.AEAA42388993@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: bramk Date: Wed May 1 13:17:19 2013 New Revision: 1477982 URL: http://svn.apache.org/r1477982 Log: ACE-346 Optimized metadata synchronization No longer use a map of all files. Just check the highest lastmodified. Modified: ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/file/BundleFileStore.java Modified: ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/file/BundleFileStore.java URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/file/BundleFileStore.java?rev=1477982&r1=1477981&r2=1477982&view=diff ============================================================================== --- ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/file/BundleFileStore.java (original) +++ ace/trunk/org.apache.ace.obr/src/org/apache/ace/obr/storage/file/BundleFileStore.java Wed May 1 13:17:19 2013 @@ -26,13 +26,8 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.channels.FileChannel; -import java.util.ArrayList; import java.util.Dictionary; -import java.util.HashMap; -import java.util.List; -import java.util.Map; import java.util.Stack; -import java.util.concurrent.ConcurrentHashMap; import java.util.jar.Attributes; import java.util.jar.JarInputStream; import java.util.jar.Manifest; @@ -59,25 +54,32 @@ public class BundleFileStore implements private static int BUFFER_SIZE = 8 * 1024; private static final String REPOSITORY_XML = "repository.xml"; - private final Object m_dirLock = new Object(); + // injected by dependencymanager + private volatile MetadataGenerator m_metadata; + private volatile LogService m_log; - private volatile MetadataGenerator m_metadata; /* will be injected by dependencymanager */ - private volatile LogService m_log; /* will be injected by dependencymanager */ + private volatile long m_dirLastModified; + private volatile File m_dir; - private volatile Map m_foundFiles;; - - /** protected by m_dirLock. */ - private File m_dir; - - public void generateMetadata() throws IOException { - File dir = getWorkingDir(); - m_metadata.generateMetadata(dir); - m_foundFiles = mapDirectory(dir); + /** + * Checks if the the directory was modified since we last checked. If so, the meta-data generator is called. + * + * @throws IOException If there is a problem synchronizing the meta-data. + */ + public void synchronizeMetadata() throws IOException { + File dir = m_dir; + synchronized (REPOSITORY_XML) { + long dirLastmodified = getDirLastModified(dir); + if (dirLastmodified > m_dirLastModified) { + m_metadata.generateMetadata(dir); + m_dirLastModified = getDirLastModified(dir); + } + } } public InputStream get(String fileName) throws IOException { - if (REPOSITORY_XML.equals(fileName) && directoryChanged(getWorkingDir(), m_foundFiles)) { - generateMetadata(); // might be called too often + if (REPOSITORY_XML.equals(fileName)) { + synchronizeMetadata(); } FileInputStream result = null; try { @@ -150,15 +152,13 @@ public class BundleFileStore implements throw new ConfigurationException(OBRFileStoreConstants.FILE_LOCATION_KEY, "Is not a directory: " + newDir); } - synchronized (m_dirLock) { - m_dir = newDir; - } - m_foundFiles = null; + m_dir = newDir; + m_dirLastModified = 0l; } } else { // clean up after getting a null as dictionary, as the service is going to be pulled afterwards - m_foundFiles = null; + m_dirLastModified = 0l; } } @@ -167,55 +167,42 @@ public class BundleFileStore implements */ protected void start() { try { - generateMetadata(); + synchronizeMetadata(); } catch (IOException e) { m_log.log(LogService.LOG_ERROR, "Could not generate initial meta data for bundle repository"); } } - private Map mapDirectory(File dir) { - Map foundFiles = new HashMap(); + /** + * Returns the highest last-modified for the directory by recursively looking at all directories and files. + * + * @param dir + * The directory + * @return the Last-modified + */ + private long getDirLastModified(File dir) { + long highest = 0l; Stack dirs = new Stack(); dirs.push(dir); - while(!dirs.isEmpty()){ + while (!dirs.isEmpty()) { File pwd = dirs.pop(); - for(File file : pwd.listFiles()){ - if(file.isDirectory()){ - dirs.push(file); - } else { - foundFiles.put(file.getAbsolutePath(), file.lastModified() ^ file.length()); - } + long modified = pwd.lastModified(); + if (modified > highest) { + highest = modified; } - } - return foundFiles; - } - - private boolean directoryChanged(File dir, Map foundFiles) { - if(foundFiles == null){ - return true; - } - Stack dirs = new Stack(); - dirs.push(dir); - int fileCount = 0; - while(!dirs.isEmpty()){ - File pwd = dirs.pop(); - for(File file : pwd.listFiles()){ - if(file.isDirectory()){ + for (File file : pwd.listFiles()) { + if (file.isDirectory()) { dirs.push(file); - } else { - fileCount++; - Long modifiedDateAndLengthXOR = foundFiles.get(file.getAbsolutePath()); - if(modifiedDateAndLengthXOR == null || (file.lastModified() ^ file.length()) != modifiedDateAndLengthXOR){ - return true; - } + continue; + } + modified = file.lastModified(); + if (modified > highest) { + highest = modified; } } } - if(fileCount != foundFiles.size()){ - return true; - } - return false; + return highest; } /** @@ -362,11 +349,7 @@ public class BundleFileStore implements * @return the working directory of this file store. */ private File getWorkingDir() { - final File dir; - synchronized (m_dirLock) { - dir = m_dir; - } - return dir; + return m_dir; } /**