jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1394650 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak: api/Blob.java plugins/memory/AbstractBlob.java
Date Fri, 05 Oct 2012 17:08:32 GMT
Author: mduerig
Date: Fri Oct  5 17:08:31 2012
New Revision: 1394650

URL: http://svn.apache.org/viewvc?rev=1394650&view=rev
Log:
OAK-350: Unify PropertyState and CoreValue
- correct hashing for Blob

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/plugins/memory/AbstractBlob.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=1394650&r1=1394649&r2=1394650&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 Fri
Oct  5 17:08:31 2012
@@ -23,4 +23,10 @@ public interface Blob {
      * @return the length of this 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/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=1394650&r1=1394649&r2=1394650&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
Fri Oct  5 17:08:31 2012
@@ -2,8 +2,12 @@ package org.apache.jackrabbit.oak.plugin
 
 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 org.apache.jackrabbit.oak.api.Blob;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -16,7 +20,7 @@ import org.slf4j.LoggerFactory;
 public abstract class AbstractBlob implements Blob {
     private static final Logger log = LoggerFactory.getLogger(AbstractBlob.class);
 
-    private Optional<Integer> hashCode = Optional.absent();
+    private Optional<HashCode> hashCode = Optional.absent();
 
     /**
      * This hash code implementation returns the hash code of the underlying stream
@@ -24,28 +28,56 @@ public abstract class AbstractBlob imple
      */
     @Override
     public int hashCode() {
+        return calculateSha256().asInt();
+    }
+
+    @Override
+    public byte[] sha256() {
+        return calculateSha256().asBytes();
+    }
+
+    private HashCode calculateSha256() {
         // Blobs are immutable so we can safely cache the hash
         if (!hashCode.isPresent()) {
-            InputStream s = getNewStream();
+            InputStream is = getNewStream();
             try {
-                hashCode = Optional.of(s.hashCode());
+                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(s);
+                close(is);
             }
         }
         return hashCode.get();
     }
 
     /**
-     * To {@code Blob} instances are considered equal iff they have the same hash code
+     * To {@code Blob} instances are considered equal iff they have the same SHA-256 hash
code
      * are equal.
      * @param other
      * @return
      */
     @Override
     public boolean equals(Object other) {
-        return other == this || other instanceof Blob && hashCode() == other.hashCode();
+        if (other == this) {
+            return true;
+        }
+        if (!(other instanceof Blob)) {
+            return false;
+        }
+
+        Blob that = (Blob) other;
+        return Arrays.equals(sha256(), that.sha256());
     }
 
     private static void close(InputStream s) {



Mime
View raw message