You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by mr...@apache.org on 2014/04/24 13:47:11 UTC

svn commit: r1589680 - in /jackrabbit/oak/branches/1.0: ./ oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BlobTest.java

Author: mreutegg
Date: Thu Apr 24 11:47:11 2014
New Revision: 1589680

URL: http://svn.apache.org/r1589680
Log:
OAK-1761: DocumentNodeStore does not make use of References while serializing Blob

Modified:
    jackrabbit/oak/branches/1.0/   (props changed)
    jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
    jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BlobTest.java

Propchange: jackrabbit/oak/branches/1.0/
------------------------------------------------------------------------------
  Merged /jackrabbit/oak/trunk:r1589661

Modified: jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java?rev=1589680&r1=1589679&r2=1589680&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java (original)
+++ jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java Thu Apr 24 11:47:11 2014
@@ -280,7 +280,17 @@ public final class DocumentNodeStore
             if (blob instanceof BlobStoreBlob) {
                 return ((BlobStoreBlob) blob).getBlobId();
             }
+
             String id;
+
+            String reference = blob.getReference();
+            if(reference != null){
+                id = blobStore.getBlobId(reference);
+                if(id != null){
+                    return id;
+                }
+            }
+
             try {
                 id = createBlob(blob.getNewStream()).getBlobId();
             } catch (IOException e) {

Modified: jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BlobTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BlobTest.java?rev=1589680&r1=1589679&r2=1589680&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BlobTest.java (original)
+++ jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/BlobTest.java Thu Apr 24 11:47:11 2014
@@ -18,9 +18,17 @@ package org.apache.jackrabbit.oak.plugin
 
 import static org.junit.Assert.assertEquals;
 
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Random;
 
+import org.apache.jackrabbit.oak.api.Blob;
+import org.apache.jackrabbit.oak.kernel.BlobSerializer;
+import org.apache.jackrabbit.oak.plugins.blob.BlobStoreBlob;
+import org.apache.jackrabbit.oak.plugins.memory.ArrayBasedBlob;
+import org.apache.jackrabbit.oak.spi.blob.MemoryBlobStore;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -71,8 +79,58 @@ public class BlobTest {
         mk.dispose();
     }
 
+    @Test
+    public void testBlobSerialization() throws Exception{
+        TestBlobStore blobStore = new TestBlobStore();
+        DocumentMK mk = new DocumentMK.Builder().setBlobStore(blobStore).open();
+        BlobSerializer blobSerializer = mk.getNodeStore().getBlobSerializer();
+
+        Blob blob = new BlobStoreBlob(blobStore, "foo");
+        assertEquals("foo", blobSerializer.serialize(blob));
+        assertEquals(0, blobStore.writeCount);
+
+        blob = new ArrayBasedBlob("foo".getBytes());
+        blobSerializer.serialize(blob);
+        assertEquals(1, blobStore.writeCount);
+
+        byte[] bytes = "foo".getBytes();
+        String blobId = blobStore.writeBlob(new ByteArrayInputStream(bytes));
+        String reference = blobStore.getReference(blobId);
+        blob = new ReferencedBlob("foo".getBytes(), reference);
+
+        blobStore.writeCount = 0;
+        blobSerializer.serialize(blob);
+
+        //Using reference so no reference should be written
+        assertEquals(0, blobStore.writeCount);
+    }
+
     private static void log(String s) {
         LOG.info(s);
     }
 
+
+    private static class TestBlobStore extends MemoryBlobStore {
+        int writeCount;
+
+        @Override
+        public String writeBlob(InputStream in) throws IOException {
+            writeCount++;
+            return super.writeBlob(in);
+        }
+    }
+
+    private static class ReferencedBlob extends ArrayBasedBlob {
+        private final String reference;
+
+        public ReferencedBlob(byte[] value, String reference) {
+            super(value);
+            this.reference = reference;
+        }
+
+        @Override
+        public String getReference() {
+            return reference;
+        }
+    }
 }