Author: chetanm
Date: Tue May 19 10:01:52 2015
New Revision: 1680222

URL: http://svn.apache.org/r1680222
Log:
OAK-2627 - Optimize equals in AbstractBlob

Added:
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlobTest.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java?rev=1680222&r1=1680221&r2=1680222&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java
 Tue May 19 10:01:52 2015
@@ -55,6 +55,15 @@ public abstract class AbstractBlob imple
             return false; // blobs not equal, given known and non-equal lengths
         }
 
+        String ai = a.getContentIdentity();
+        String bi = b.getContentIdentity();
+
+        //Check for identity first. If they are same then its
+        //definitely same blob. If not we need to check further.
+        if (ai != null && bi != null && ai.equals(bi)){
+            return true;
+        }
+
         try {
             return ByteStreams.equal(supplier(a), supplier(b));
         } catch (IOException e) {

Added: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlobTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlobTest.java?rev=1680222&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlobTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlobTest.java
 Tue May 19 10:01:52 2015
@@ -0,0 +1,84 @@
+/*
+ * 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.memory;
+
+import java.io.InputStream;
+import java.util.Random;
+
+import javax.annotation.Nonnull;
+
+import org.apache.jackrabbit.oak.api.Blob;
+import org.junit.Test;
+
+import static com.google.common.base.Preconditions.checkState;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class AbstractBlobTest {
+    private Random rnd = new Random();
+
+    @Test
+    public void blobComparisonBasedOnContentIdentity() throws Exception {
+        byte[] data = bytes(100);
+        Blob a = new TestBlob(data, "id1", false);
+        Blob b = new TestBlob(data, "id1", false);
+        assertTrue(AbstractBlob.equal(a, b));
+
+        Blob a2 = new TestBlob(data, "id1", true);
+        Blob b2 = new TestBlob(data, "id2", true);
+        assertTrue("Blobs with different id but same content should match", 
AbstractBlob.equal(a2, b2));
+    }
+
+    @Test
+    public void blobComparisonBasedOnLength() throws Exception {
+        Blob a = new TestBlob(bytes(100), null, false);
+        Blob b = new TestBlob(bytes(50), null, false);
+        assertFalse("Blob comparison should not fallback on content if lengths 
not same", AbstractBlob.equal(a, b));
+    }
+
+    private byte[] bytes(int size) {
+        byte[] data = new byte[size];
+        rnd.nextBytes(data);
+        return data;
+    }
+
+    private static class TestBlob extends ArrayBasedBlob {
+        private final String id;
+        private final boolean allowAccessToContent;
+
+        public TestBlob(byte[] value, String id, boolean allowAccessToContent) 
{
+            super(value);
+            this.id = id;
+            this.allowAccessToContent = allowAccessToContent;
+        }
+
+        @Override
+        public String getContentIdentity() {
+            return id;
+        }
+
+        @Nonnull
+        @Override
+        public InputStream getNewStream() {
+            checkState(allowAccessToContent, "Cannot access the stream");
+            return super.getNewStream();
+        }
+    }
+}

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


Reply via email to