Author: shashank
Date: Wed Feb  3 10:37:38 2016
New Revision: 1728294

URL: http://svn.apache.org/viewvc?rev=1728294&view=rev
Log:
OAK-3253 Support caching in FileDataStoreService

Added support. if "cacheSize > 0" is configured, CachingFDS is used. Otherwise 
OakFileDataStore() is used. 
2 test cases added.

Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/FileDataStoreService.java
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/datastore/DataStoreServiceTest.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/FileDataStoreService.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/FileDataStoreService.java?rev=1728294&r1=1728293&r2=1728294&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/FileDataStoreService.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/FileDataStoreService.java
 Wed Feb  3 10:37:38 2016
@@ -19,24 +19,57 @@
 
 package org.apache.jackrabbit.oak.plugins.blob.datastore;
 
-import java.util.Map;
-
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.ConfigurationPolicy;
+import org.apache.jackrabbit.core.data.CachingFDS;
 import org.apache.jackrabbit.core.data.DataStore;
+import org.apache.jackrabbit.oak.commons.PropertiesUtil;
 import org.osgi.service.component.ComponentContext;
 
+import javax.jcr.RepositoryException;
+import java.util.Map;
+import java.util.Properties;
+
 @Component(policy = ConfigurationPolicy.REQUIRE, name = 
FileDataStoreService.NAME)
-public class FileDataStoreService extends AbstractDataStoreService{
+public class FileDataStoreService extends AbstractDataStoreService {
     public static final String NAME = 
"org.apache.jackrabbit.oak.plugins.blob.datastore.FileDataStore";
 
+    public static final String CACHE_PATH = "cachePath";
+    public static final String CACHE_SIZE = "cacheSize";
+    public static final String FS_BACKEND_PATH = "fsBackendPath";
+    public static final String PATH = "path";
+
     @Override
-    protected DataStore createDataStore(ComponentContext context, Map<String, 
Object> config) {
-        return new OakFileDataStore();
+    protected DataStore createDataStore(ComponentContext context, Map<String, 
Object> config) throws RepositoryException {
+
+        long cacheSize = PropertiesUtil.toLong(config.get(CACHE_SIZE), 0L);
+        // return CachingFDS when cacheSize > 0
+        if (cacheSize > 0) {
+            String fsBackendPath = PropertiesUtil.toString(config.get(PATH), 
"");
+            if ("".equals(fsBackendPath)) {
+                throw new RepositoryException("Cannot create 
FileDataStoreService with caching. [{path}] property not configured.");
+            }
+            CachingFDS dataStore = new CachingFDS();
+            config.remove(PATH);
+            config.remove(CACHE_SIZE);
+            config.put(FS_BACKEND_PATH, fsBackendPath);
+            config.put("cacheSize", cacheSize);
+            String cachePath = PropertiesUtil.toString(config.get(CACHE_PATH), 
"");
+            if (!"".equals(cachePath)) {
+                config.remove(CACHE_PATH);
+                config.put(PATH, cachePath);
+            }
+            Properties properties = new Properties();
+            properties.putAll(config);
+            dataStore.setProperties(properties);
+            return dataStore;
+        } else {
+            return new OakFileDataStore();
+        }
     }
 
     @Override
     protected String[] getDescription() {
-        return new String[] {"type=filesystem"};
+        return new String[]{"type=filesystem"};
     }
 }

Modified: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/datastore/DataStoreServiceTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/datastore/DataStoreServiceTest.java?rev=1728294&r1=1728293&r2=1728294&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/datastore/DataStoreServiceTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/datastore/DataStoreServiceTest.java
 Wed Feb  3 10:37:38 2016
@@ -19,10 +19,14 @@
 
 package org.apache.jackrabbit.oak.plugins.blob.datastore;
 
-import java.util.Map;
-
 import com.google.common.collect.ImmutableMap;
+import org.apache.jackrabbit.core.data.DataStore;
+import org.apache.jackrabbit.core.data.Backend;
+import org.apache.jackrabbit.core.data.CachingFDS;
+import org.apache.jackrabbit.core.data.FileDataStore;
+import org.apache.jackrabbit.core.data.FSBackend;
 import org.apache.jackrabbit.oak.api.jmx.CacheStatsMBean;
+import org.apache.jackrabbit.oak.commons.PropertiesUtil;
 import org.apache.jackrabbit.oak.spi.blob.stats.BlobStoreStatsMBean;
 import org.apache.jackrabbit.oak.stats.StatisticsProvider;
 import org.apache.sling.testing.mock.osgi.MockOsgi;
@@ -31,7 +35,12 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
 
-import static org.junit.Assert.assertNotNull;
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.junit.Assert.*;
 
 public class DataStoreServiceTest {
     @Rule
@@ -54,4 +63,61 @@ public class DataStoreServiceTest {
         assertNotNull(context.getService(CacheStatsMBean.class));
     }
 
+    /**
+     *
+     * Test @CachingFDS is returned when cacheSize > 0
+     */
+    @Test
+    public void configCachingFDS() throws Exception {
+        String nasPath = folder.getRoot().getAbsolutePath() + "/NASPath";
+        String cachePath = folder.getRoot().getAbsolutePath() + "/cachePath";
+        long cacheSize = 100l;
+        Map<String, Object> config = new HashMap<String, Object>();
+        config.put("repository.home", folder.getRoot().getAbsolutePath());
+        config.put(FileDataStoreService.CACHE_SIZE, cacheSize);
+        config.put(FileDataStoreService.PATH, nasPath);
+        config.put(FileDataStoreService.CACHE_PATH, cachePath);
+        FileDataStoreService fdsSvc = new FileDataStoreService();
+
+        DataStore ds = fdsSvc.createDataStore(context.componentContext(), 
config);
+        PropertiesUtil.populate(ds, config, false);
+        ds.init(folder.getRoot().getAbsolutePath());
+        assertTrue("not instance of CachingFDS", ds instanceof CachingFDS);
+        CachingFDS cds = (CachingFDS) ds;
+        assertEquals("cachesize not equal", cacheSize, cds.getCacheSize());
+        assertEquals("cachepath not equal", cachePath, cds.getPath());
+        Backend backend = cds.getBackend();
+        Properties props = (Properties) getField(backend, "properties");
+        assertEquals("path not equal", nasPath, 
props.getProperty(FSBackend.FS_BACKEND_PATH));
+
+
+    }
+
+    /**
+     *
+     * Test to verify @FileDataStore is returned if cacheSize is not 
configured.
+     */
+    @Test
+    public void configFileDataStore() throws Exception {
+        String nasPath = folder.getRoot().getAbsolutePath() + "/NASPath";
+        String cachePath = folder.getRoot().getAbsolutePath() + "/cachePath";
+        Map<String, Object> config = new HashMap<String, Object>();
+        config.put("repository.home", folder.getRoot().getAbsolutePath());
+        config.put(FileDataStoreService.PATH, nasPath);
+        config.put(FileDataStoreService.CACHE_PATH, cachePath);
+        FileDataStoreService fdsSvc = new FileDataStoreService();
+
+        DataStore ds = fdsSvc.createDataStore(context.componentContext(), 
config);
+        PropertiesUtil.populate(ds, config, false);
+        ds.init(folder.getRoot().getAbsolutePath());
+        assertTrue("not instance of FileDataStore", ds instanceof 
FileDataStore);
+        FileDataStore fds = (FileDataStore) ds;
+        assertEquals("path not equal", nasPath, fds.getPath());
+    }
+
+    private static Object getField(Object obj, String fieldName) throws 
Exception {
+        Field f = obj.getClass().getDeclaredField(fieldName); 
//NoSuchFieldException
+        f.setAccessible(true);
+        return f.get(obj);
+    }
 }


Reply via email to