Author: amitj
Date: Mon Oct 24 04:52:31 2016
New Revision: 1766343

URL: http://svn.apache.org/viewvc?rev=1766343&view=rev
Log:
OAK-4979: Caching sub-system implementation for DataStore

- Recursively deleting empty parent directories on invalidate
- Moved the commong recursiveDelete & getFile methods to DataStoreCacheUtils

Added:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/DataStoreCacheUtils.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/FileCache.java
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/UploadStagingCache.java

Added: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/DataStoreCacheUtils.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/DataStoreCacheUtils.java?rev=1766343&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/DataStoreCacheUtils.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/DataStoreCacheUtils.java
 Mon Oct 24 04:52:31 2016
@@ -0,0 +1,71 @@
+/*
+ * 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.jackrabbit.oak.plugins.blob;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.commons.io.FileUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Common utility methods used for DataStore caches.
+ */
+public class DataStoreCacheUtils {
+    private static final Logger LOG = 
LoggerFactory.getLogger(UploadStagingCache.class);
+
+    /**
+     * Delete the file from the staged cache and all its empty 
parent-directories.
+     *
+     * @param f the file to be deleted
+     * @throws IOException
+     */
+    public static void recursiveDelete(File f, File root) throws IOException {
+        if (f.exists()) {
+            FileUtils.forceDelete(f);
+            LOG.info("Deleted staged upload file [{}]", f);
+        }
+
+        // delete empty parent folders (except the main directory)
+        while (true) {
+            f = f.getParentFile();
+            if ((f == null) || f.equals(root)
+                || (f.list() == null)
+                || (f.list().length > 0)) {
+                break;
+            }
+            LOG.debug("Deleted directory [{}], [{}]", f, f.delete());
+        }
+    }
+
+    /**
+     * Obtain a placeholder in the file system folder for the given identifier.
+     *
+     * @param id of the file
+     * @return file handle
+     */
+    public static File getFile(String id, File root) {
+        File file = root;
+        file = new File(file, id.substring(0, 2));
+        file = new File(file, id.substring(2, 4));
+        file = new File(file, id.substring(4, 6));
+        return new File(file, id);
+    }
+}

Propchange: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/DataStoreCacheUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/FileCache.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/FileCache.java?rev=1766343&r1=1766342&r2=1766343&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/FileCache.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/FileCache.java
 Mon Oct 24 04:52:31 2016
@@ -107,7 +107,7 @@ public class FileCache extends AbstractC
                     @Nonnull RemovalCause cause) {
                     try {
                         if (cachedFile != null && cachedFile.exists()) {
-                            forceDelete(cachedFile);
+                            DataStoreCacheUtils.recursiveDelete(cachedFile, 
cacheRoot);
                             LOG.info("File [{}] evicted with reason [{}]", 
cachedFile, cause
                                 .toString());
                         }
@@ -119,7 +119,7 @@ public class FileCache extends AbstractC
                 @Override
                 public File load(String key) throws Exception {
                     // Fetch from local cache directory and if not found load 
from backend
-                    File cachedFile = getCacheFile(key);
+                    File cachedFile = DataStoreCacheUtils.getFile(key, 
cacheRoot);
                     if (cachedFile.exists()) {
                         return cachedFile;
                     } else {
@@ -194,7 +194,7 @@ public class FileCache extends AbstractC
     @Override
     public void put(String key, File file) {
         try {
-            File cached = getCacheFile(key);
+            File cached = DataStoreCacheUtils.getFile(key, cacheRoot);
             if (!cached.exists()) {
                 FileUtils.moveFile(file, cached);
             }
@@ -297,20 +297,6 @@ public class FileCache extends AbstractC
         LOG.trace("[{}] files put in im-memory cache", count);
         return count;
     }
-
-    /**
-     * Create a placeholder in the file system cache folder for the given 
identifier.
-     *
-     * @param key for the file
-     * @return File handle for the id
-     */
-    private File getCacheFile(String key) {
-        File file = cacheRoot;
-        file = new File(file, key.substring(0, 2));
-        file = new File(file, key.substring(2, 4));
-        file = new File(file, key.substring(4, 6));
-        return new File(file, key);
-    }
 }
 
 class FileCacheStats extends CacheStats implements DataStoreCacheStatsMBean {

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/UploadStagingCache.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/UploadStagingCache.java?rev=1766343&r1=1766342&r2=1766343&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/UploadStagingCache.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/UploadStagingCache.java
 Mon Oct 24 04:52:31 2016
@@ -234,7 +234,7 @@ public class UploadStagingCache implemen
         cacheStats.markRequest();
 
         long length = input.length();
-        File uploadFile = getFile(id);
+        File uploadFile = DataStoreCacheUtils.getFile(id, uploadCacheSpace);
 
         // if size permits and not upload complete or already scheduled for 
upload
         if (currentSize.addAndGet(length) <= size
@@ -412,7 +412,7 @@ public class UploadStagingCache implemen
         LOG.debug("Trying to delete file [{}]", toBeDeleted);
         long length = toBeDeleted.length();
 
-        delete(toBeDeleted);
+        DataStoreCacheUtils.recursiveDelete(toBeDeleted, uploadCacheSpace);
         LOG.debug("deleted file [{}]", toBeDeleted);
 
         currentSize.addAndGet(-length);
@@ -423,30 +423,6 @@ public class UploadStagingCache implemen
     }
 
     /**
-     * Delete the file from the staged cache and all its empty sub-directories.
-     *
-     * @param f the file to be deleted
-     * @throws IOException
-     */
-    private void delete(File f) throws IOException {
-        if (f.exists()) {
-            FileUtils.forceDelete(f);
-            LOG.info("Deleted staged upload file [{}]", f);
-        }
-
-        // delete empty parent folders (except the main directory)
-        while (true) {
-            f = f.getParentFile();
-            if ((f == null) || f.equals(uploadCacheSpace)
-                || (f.list() == null)
-                || (f.list().length > 0)) {
-                break;
-            }
-            LOG.debug("Deleted directory [{}], [{}]", f, f.delete());
-        }
-    }
-
-    /**
      * Returns the File if present or null otherwise.
      * Any usage of the returned file should assert for its existence as the 
file
      * could be purged from the file system once uploaded using
@@ -466,20 +442,6 @@ public class UploadStagingCache implemen
     }
 
     /**
-     * Create a placeholder in the file system cache folder for the given 
identifier.
-     *
-     * @param id of the file
-     * @return file handle
-     */
-    private File getFile(String id) {
-        File file = uploadCacheSpace;
-        file = new File(file, id.substring(0, 2));
-        file = new File(file, id.substring(2, 4));
-        file = new File(file, id.substring(4, 6));
-        return new File(file, id);
-    }
-
-    /**
      * Cache related stats
      *
      * @return an instance of the {@link DataStoreCacheStatsMBean}.


Reply via email to