jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1526715 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak: api/Blob.java kernel/JsopDiff.java plugins/memory/AbstractBlob.java plugins/memory/ArrayBasedBlob.java
Date Thu, 26 Sep 2013 21:46:38 GMT
Author: jukka
Date: Thu Sep 26 21:46:38 2013
New Revision: 1526715

URL: http://svn.apache.org/r1526715
Log:
OAK-1050: Remove Blob.sha256()

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Blob.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ArrayBasedBlob.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Blob.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Blob.java?rev=1526715&r1=1526714&r2=1526715&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Blob.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Blob.java Thu
Sep 26 21:46:38 2013
@@ -42,10 +42,4 @@ public interface Blob {
      */
     long length();
 
-    /**
-     * The SHA-256 digest of the underlying stream
-     * @return
-     */
-    byte[] sha256();
-
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java?rev=1526715&r1=1526714&r2=1526715&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/JsopDiff.java
Thu Sep 26 21:46:38 2013
@@ -22,7 +22,6 @@ import static org.apache.jackrabbit.oak.
 import static org.apache.jackrabbit.oak.api.Type.STRINGS;
 
 import java.io.IOException;
-import java.util.Arrays;
 
 import javax.jcr.PropertyType;
 
@@ -77,10 +76,8 @@ public class JsopDiff implements NodeSta
      * debugging purposes.
      * <p>
      * This method does not store binaries but returns them inlined
-     * in the format
-     * <pre>
-     *   "Blob{" + Arrays.toString(blob.sha256()) + "}"
-     * </pre>.
+     * in the format <code>Blob{...}</code>, where the <code>...</code>
+     * is implementation-dependent - typically the SHA256 hash of the binary.
      *
      * @param before  before node state
      * @param after  after node state
@@ -98,7 +95,7 @@ public class JsopDiff implements NodeSta
 
             @Override
             protected String writeBlob(Blob blob) {
-                return "Blob{" + Arrays.toString(blob.sha256()) + '}';
+                return "Blob{" + blob + '}';
             }
 
             @Override

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=1526715&r1=1526714&r2=1526715&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
Thu Sep 26 21:46:38 2013
@@ -22,14 +22,12 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.Arrays;
 
-import com.google.common.base.Optional;
 import com.google.common.hash.HashCode;
-import com.google.common.hash.Hasher;
 import com.google.common.hash.Hashing;
+import com.google.common.io.ByteStreams;
+import com.google.common.io.InputSupplier;
 
 import org.apache.jackrabbit.oak.api.Blob;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Abstract base class for {@link Blob} implementations.
@@ -37,9 +35,32 @@ import org.slf4j.LoggerFactory;
  * {@code hashCode} and {@code equals}.
  */
 public abstract class AbstractBlob implements Blob {
-    private static final Logger log = LoggerFactory.getLogger(AbstractBlob.class);
 
-    private Optional<HashCode> hashCode = Optional.absent();
+    private static HashCode calculateSha256(final Blob blob) {
+        try {
+            return ByteStreams.hash(
+                    new InputSupplier<InputStream>() {
+                        @Override
+                        public InputStream getInput() throws IOException {
+                            return blob.getNewStream();
+                        }
+                    },
+                    Hashing.sha256());
+        } catch (IOException e) {
+            throw new RuntimeException(
+                    "Failed to calculate the hash code of a stream", e);
+        }
+    }
+
+    private HashCode hashCode = null;
+
+    private synchronized HashCode calculateSha256() {
+        // Blobs are immutable so we can safely cache the hash
+        if (hashCode == null) {
+            hashCode = calculateSha256(this);
+        }
+        return hashCode;
+    }
 
     /**
      * This hash code implementation returns the hash code of the underlying stream
@@ -50,39 +71,13 @@ public abstract class AbstractBlob imple
         return calculateSha256().asInt();
     }
 
-    @Override
-    public byte[] sha256() {
+    protected byte[] sha256() {
         return calculateSha256().asBytes();
     }
 
-    private HashCode calculateSha256() {
-        // Blobs are immutable so we can safely cache the hash
-        if (!hashCode.isPresent()) {
-            InputStream is = getNewStream();
-            try {
-                try {
-                    Hasher hasher = Hashing.sha256().newHasher();
-                    byte[] buf = new byte[0x1000];
-                    int r;
-                    while((r = is.read(buf)) != -1) {
-                        hasher.putBytes(buf, 0, r);
-                    }
-                    hashCode = Optional.of(hasher.hash());
-                }
-                catch (IOException e) {
-                    log.warn("Error while hashing stream", e);
-                }
-            }
-            finally {
-                close(is);
-            }
-        }
-        return hashCode.get();
-    }
-
     /**
-     * To {@code Blob} instances are considered equal iff they have the same SHA-256 hash
code
-     * are equal.
+     * To {@code Blob} instances are considered equal iff they have the
+     * same SHA-256 hash code  are equal.
      * @param other
      * @return
      */
@@ -90,22 +85,21 @@ public abstract class AbstractBlob imple
     public boolean equals(Object other) {
         if (other == this) {
             return true;
-        }
-        if (!(other instanceof Blob)) {
+        } else if (other instanceof AbstractBlob) {
+            AbstractBlob that = (AbstractBlob) other;
+            return Arrays.equals(sha256(), that.sha256());
+        } else if (other instanceof Blob) {
+            Blob that = (Blob) other;
+            return Arrays.equals(sha256(), calculateSha256(that).asBytes());
+        } else {
             return false;
         }
-
-        Blob that = (Blob) other;
-        return Arrays.equals(sha256(), that.sha256());
     }
 
-    private static void close(InputStream s) {
-        try {
-            s.close();
-        }
-        catch (IOException e) {
-            log.warn("Error while closing stream", e);
-        }
+    @Override
+    public String toString() {
+        return calculateSha256().toString();
     }
 
+
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ArrayBasedBlob.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ArrayBasedBlob.java?rev=1526715&r1=1526714&r2=1526715&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ArrayBasedBlob.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ArrayBasedBlob.java
Thu Sep 26 21:46:38 2013
@@ -23,8 +23,6 @@ import java.io.InputStream;
 
 import javax.annotation.Nonnull;
 
-import com.google.common.base.Charsets;
-
 /**
  * This {@code Blob} implementations is based on an array of bytes.
  */
@@ -35,11 +33,6 @@ public class ArrayBasedBlob extends Abst
         this.value = value;
     }
 
-    @Override
-    public String toString() {
-        return new String(value, Charsets.UTF_8);
-    }
-
     @Nonnull
     @Override
     public InputStream getNewStream() {



Mime
View raw message