Index: C:/sourceCode/jackrabbit-svn/jackrabbit-svn/jackrabbit-data/src/main/java/org/apache/jackrabbit/core/data/LocalCache.java =================================================================== --- C:/sourceCode/jackrabbit-svn/jackrabbit-svn/jackrabbit-data/src/main/java/org/apache/jackrabbit/core/data/LocalCache.java (revision 1617888) +++ C:/sourceCode/jackrabbit-svn/jackrabbit-svn/jackrabbit-data/src/main/java/org/apache/jackrabbit/core/data/LocalCache.java (working copy) @@ -29,6 +29,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Set; @@ -74,17 +75,13 @@ private final File tmp; /** - * The maximum size of cache in bytes. - */ - private long maxSize; - - /** * If true cache is in purgeMode and not available. All operation would be * no-op. */ private volatile boolean purgeMode; private AsyncUploadCache asyncUploadCache; + /** * Build LRU cache of files located at 'path'. It uses lastModified property @@ -93,29 +90,46 @@ * * @param path file system path * @param tmpPath temporary directory used by cache. - * @param maxSize maximum size of cache. + * @param maxSizeInBytes maximum size of cache. * @param cachePurgeTrigFactor factor which triggers cache to purge mode. - * That is if current size exceed (cachePurgeTrigFactor * maxSize), the + * That is if current size exceed (cachePurgeTrigFactor * maxSizeInBytes), the * cache will go in auto-purge mode. * @param cachePurgeResizeFactor after cache purge size of cache will be - * just less (cachePurgeResizeFactor * maxSize). + * just less (cachePurgeResizeFactor * maxSizeInBytes). * @param asyncUploadCache {@link AsyncUploadCache} * @throws RepositoryException */ - public LocalCache(String path, String tmpPath, long size, double cachePurgeTrigFactor, + public LocalCache(String path, String tmpPath, long maxSizeInBytes, double cachePurgeTrigFactor, double cachePurgeResizeFactor, AsyncUploadCache asyncUploadCache) throws IOException, ClassNotFoundException { - this.maxSize = size; directory = new File(path); tmp = new File(tmpPath); LOG.info( "cachePurgeTrigFactor =[{}], cachePurgeResizeFactor =[{}], cachePurgeTrigFactorSize =[{}], cachePurgeResizeFactorSize =[{}]", new Object[] { cachePurgeTrigFactor, cachePurgeResizeFactor, - (cachePurgeTrigFactor * size), (cachePurgeResizeFactor * size) }); - cache = new LRUCache(size, cachePurgeTrigFactor, cachePurgeResizeFactor); + (cachePurgeTrigFactor * maxSizeInBytes), (cachePurgeResizeFactor * maxSizeInBytes) }); + cache = new LRUCache(maxSizeInBytes, cachePurgeTrigFactor, cachePurgeResizeFactor); this.asyncUploadCache = asyncUploadCache; - - new Thread(new CacheBuildJob()).start(); + long startTime = System.currentTimeMillis(); + ArrayList allFiles = new ArrayList(); + Iterator it = FileUtils.iterateFiles(directory, null, true); + while (it.hasNext()) { + File f = it.next(); + allFiles.add(f); + } + long t1 = System.currentTimeMillis(); + LOG.debug("Time taken to recursive [{}] took [{}] sec", + allFiles.size(), ((t1 - startTime) / 1000)); + Collections.sort(allFiles, new Comparator() { + public int compare(File o1, File o2) { + long l1 = o1.lastModified(), l2 = o2.lastModified(); + return l1 < l2 ? -1 : l1 > l2 ? 1 : 0; + } + }); + long t2 = System.currentTimeMillis(); + LOG.debug("Time taken to sort [{}] took [{}] sec", + allFiles.size(), ((t2 - t1) / 1000)); + new Thread(new CacheBuildJob(allFiles)).start(); } /** @@ -134,39 +148,38 @@ fileName = fileName.replace("\\", "/"); File f = getFile(fileName); long length = 0; - synchronized (this) { - if (!f.exists() || isInPurgeMode()) { - OutputStream out = null; - File transFile = null; - try { - TransientFileFactory tff = TransientFileFactory.getInstance(); - transFile = tff.createTransientFile("s3-", "tmp", tmp); - out = new BufferedOutputStream(new FileOutputStream(transFile)); - length = IOUtils.copyLarge(in, out); - } finally { - IOUtils.closeQuietly(out); + if (!f.exists() || isInPurgeMode()) { + OutputStream out = null; + File transFile = null; + try { + TransientFileFactory tff = TransientFileFactory.getInstance(); + transFile = tff.createTransientFile("s3-", "tmp", tmp); + out = new BufferedOutputStream(new FileOutputStream(transFile)); + length = IOUtils.copyLarge(in, out); + } finally { + IOUtils.closeQuietly(out); + } + // rename the file to local fs cache + if (canAdmitFile(length) + && (f.getParentFile().exists() || f.getParentFile().mkdirs()) + && transFile.renameTo(f) && f.exists()) { + if (transFile.exists() && transFile.delete()) { + LOG.info("tmp file [{}] not deleted successfully", + transFile.getAbsolutePath()); } - // rename the file to local fs cache - if (canAdmitFile(length) - && (f.getParentFile().exists() || f.getParentFile().mkdirs()) - && transFile.renameTo(f) && f.exists()) { - if (transFile.exists() && transFile.delete()) { - LOG.info("tmp file [{}] not deleted successfully", transFile.getAbsolutePath()); - } - transFile = null; - LOG.debug("file [{}] added to local cache.", fileName); - cache.put(fileName, f.length()); - } else { - f = transFile; - } + transFile = null; + LOG.debug("file [{}] added to local cache.", fileName); + cache.put(fileName, f.length()); } else { - // f.exists and not in purge mode - f.setLastModified(System.currentTimeMillis()); - cache.put(fileName, f.length()); + f = transFile; } - cache.tryPurge(); - return new LazyFileInputStream(f); + } else { + // f.exists and not in purge mode + f.setLastModified(System.currentTimeMillis()); + cache.put(fileName, f.length()); } + cache.tryPurge(); + return new LazyFileInputStream(f); } /** @@ -178,7 +191,7 @@ * @param src file to be added to cache. * @throws IOException */ - public synchronized File store(String fileName, final File src) { + public File store(String fileName, final File src) { try { return store(fileName, src, false).getFile(); } catch (IOException ioe) { @@ -205,7 +218,7 @@ * file, if it is added to {@link LocalCache} or original file. * @throws IOException */ - public synchronized AsyncUploadCacheResult store(String fileName, File src, boolean tryForAsyncUpload) throws IOException { + public AsyncUploadCacheResult store(String fileName, File src, boolean tryForAsyncUpload) throws IOException { fileName = fileName.replace("\\", "/"); File dest = getFile(fileName); File parent = dest.getParentFile(); @@ -240,7 +253,7 @@ return file == null ? null : new LazyFileInputStream(file); } - public synchronized File getFileIfStored(String fileName) throws IOException { + public File getFileIfStored(String fileName) throws IOException { fileName = fileName.replace("\\", "/"); File f = getFile(fileName); // return file in purge mode = true and file present in asyncUploadCache @@ -264,7 +277,7 @@ * * @param fileName file name that need to be removed from cache. */ - public synchronized void delete(String fileName) { + public void delete(String fileName) { if (isInPurgeMode()) { LOG.debug("purgeMode true :delete returned"); return; @@ -277,7 +290,7 @@ * Returns length of file if exists in cache else returns null. * @param fileName name of the file. */ - public synchronized Long getFileLength(String fileName) { + public Long getFileLength(String fileName) { Long length = null; try { length = cache.get(fileName); @@ -308,7 +321,7 @@ * @param length of the file. * @return true if yes else return false. */ - private synchronized boolean canAdmitFile(final long length) { + private boolean canAdmitFile(final long length) { //order is important here boolean value = !isInPurgeMode() && (cache.canAdmitFile(length)); if (!value) { @@ -551,27 +564,17 @@ * asynchronously. */ private class CacheBuildJob implements Runnable { + List allFiles ; + + private CacheBuildJob(List allFiles) { + this.allFiles = allFiles; + } public void run() { long startTime = System.currentTimeMillis(); - ArrayList allFiles = new ArrayList(); - Iterator it = FileUtils.iterateFiles(directory, null, true); - while (it.hasNext()) { - File f = it.next(); - allFiles.add(f); - } - long t1 = System.currentTimeMillis(); - LOG.debug("Time taken to recursive [{}] took [{}] sec", - allFiles.size(), ((t1 - startTime) / 1000)); - Collections.sort(allFiles, new Comparator() { - public int compare(File o1, File o2) { - long l1 = o1.lastModified(), l2 = o2.lastModified(); - return l1 < l2 ? -1 : l1 > l2 ? 1 : 0; - } - }); - long t2 = System.currentTimeMillis(); - LOG.debug("Time taken to sort [{}] took [{}] sec", - allFiles.size(), ((t2 - t1) / 1000)); + String dataStorePath = directory.getAbsolutePath(); + LOG.info("directoryPath = " + dataStorePath); + dataStorePath = dataStorePath.replace("\\", "/"); String tmpPath = tmp.getAbsolutePath(); LOG.debug("tmp path [{}]", tmpPath); long time = System.currentTimeMillis();