Author: amitj
Date: Wed Oct  5 07:10:07 2016
New Revision: 1763347

URL: http://svn.apache.org/viewvc?rev=1763347&view=rev
Log:
OAK-4879: Proper implementation of getOrCreateReferenceKey in CachingFDS

* New OakCachingFDS class which extends from JR2 CachingFDS to override 
getOrCreateReferenceKey for a proper implementation.

Added:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakCachingFDS.java
   (with props)
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakCachingFDSTest.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/FileDataStoreService.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=1763347&r1=1763346&r2=1763347&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 Oct  5 07:10:07 2016
@@ -53,7 +53,9 @@ public class FileDataStoreService extend
             String fsBackendPath = PropertiesUtil.toString(config.get(PATH), 
null);
             Preconditions.checkNotNull(fsBackendPath, "Cannot create " +
                     "FileDataStoreService with caching. [{path}] property not 
configured.");
-            CachingFDS dataStore = new CachingFDS();
+            OakCachingFDS dataStore = new OakCachingFDS();
+            dataStore.setFsBackendPath(fsBackendPath);
+
             // Disabling asyncUpload by default
             
dataStore.setAsyncUploadLimit(PropertiesUtil.toInteger(config.get("asyncUploadLimit"),
 0));
             config.remove(PATH);

Added: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakCachingFDS.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakCachingFDS.java?rev=1763347&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakCachingFDS.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakCachingFDS.java
 Wed Oct  5 07:10:07 2016
@@ -0,0 +1,58 @@
+/*
+ * 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.datastore;
+
+import java.io.File;
+import java.io.IOException;
+import java.security.SecureRandom;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.jackrabbit.core.data.CachingFDS;
+import org.apache.jackrabbit.core.data.DataStoreException;
+
+/**
+ * Overrides the implementation of
+ * {@link 
org.apache.jackrabbit.core.data.CachingDataStore#getOrCreateReferenceKey}.
+ */
+public class OakCachingFDS extends CachingFDS {
+    /** The path for FS Backend **/
+    private String fsBackendPath;
+
+    public void setFsBackendPath(String fsBackendPath) {
+        this.fsBackendPath = fsBackendPath;
+    }
+
+    @Override
+    protected byte[] getOrCreateReferenceKey() throws DataStoreException {
+        File file = new File(fsBackendPath, "reference.key");
+        try {
+            if (file.exists()) {
+                return FileUtils.readFileToByteArray(file);
+            } else {
+                byte[] key = new byte[256];
+                new SecureRandom().nextBytes(key);
+                FileUtils.writeByteArrayToFile(file, key);
+                return key;
+            }
+        } catch (IOException e) {
+            throw new DataStoreException(
+                "Unable to access reference key file " + file.getPath(), e);
+        }
+    }
+}

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

Added: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakCachingFDSTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakCachingFDSTest.java?rev=1763347&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakCachingFDSTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakCachingFDSTest.java
 Wed Oct  5 07:10:07 2016
@@ -0,0 +1,119 @@
+/*
+ * 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.datastore;
+
+import java.io.File;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.jackrabbit.core.data.DataStore;
+import org.apache.jackrabbit.oak.spi.blob.BlobStore;
+import org.apache.jackrabbit.oak.stats.StatisticsProvider;
+import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import static com.google.common.collect.Maps.newHashMap;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertNotNull;
+
+/**
+ * Tests {@link OakCachingFDS} and OSGi registration using {@link 
FileDataStoreService}.
+ */
+public class OakCachingFDSTest {
+    @Rule
+    public TemporaryFolder folder = new TemporaryFolder(new File("target"));
+
+    @Rule
+    public OsgiContext context = new OsgiContext();
+
+    @Before
+    public void setUp() {
+    }
+
+    private OakCachingFDS dataStore;
+
+    private String fsBackendPath;
+
+    private String path;
+
+    @Before
+    public void setup() throws Exception {
+        fsBackendPath = folder.newFolder().getAbsolutePath();
+        path = folder.newFolder().getAbsolutePath();
+    }
+
+    @Test
+    public void createAndCheckReferenceKey() throws Exception {
+        createCachingFDS();
+        assertReferenceKey();
+    }
+
+    @Test
+    public void registerAndCheckReferenceKey() throws Exception {
+        context.registerService(StatisticsProvider.class, 
StatisticsProvider.NOOP);
+        registerCachingFDS();
+        assertReferenceKey();
+    }
+
+    public void assertReferenceKey() throws Exception {
+        byte[] key = dataStore.getOrCreateReferenceKey();
+
+        // Check bytes retrieved from reference.key file
+        File refFile = new File(fsBackendPath, "reference.key");
+        byte[] keyRet = FileUtils.readFileToByteArray(refFile);
+        assertArrayEquals(key, keyRet);
+
+        assertArrayEquals(key, dataStore.getOrCreateReferenceKey());
+    }
+
+    private void registerCachingFDS() {
+        Map<String, Object> props = newHashMap();
+        props.put("cachePath", path);
+        props.put("path", fsBackendPath);
+        props.put("cacheSize", "10");
+        props.put("repository.home", new 
File(fsBackendPath).getParentFile().getAbsolutePath());
+
+        context.registerInjectActivateService(new FileDataStoreService(), 
props);
+        assertNotNull(context.getService(BlobStore.class));
+        BlobStore blobStore = context.getService(BlobStore.class);
+        assert blobStore instanceof DataStoreBlobStore;
+
+        DataStore ds = ((DataStoreBlobStore) blobStore).getDataStore();
+        assert ds instanceof OakCachingFDS;
+        dataStore = (OakCachingFDS) ds;
+    }
+
+    private void createCachingFDS() throws Exception {
+        Properties props = new Properties();
+        props.put("fsBackendPath", fsBackendPath);
+        props.put("path", path);
+        props.put("cacheSize", "10");
+
+        dataStore = new OakCachingFDS();
+        dataStore.setFsBackendPath(fsBackendPath);
+        dataStore.setAsyncUploadLimit(0);
+        dataStore.setProperties(props);
+        dataStore.init(folder.newFolder().getAbsolutePath());
+    }
+}

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


Reply via email to