Author: chetanm
Date: Fri Sep 15 04:24:02 2017
New Revision: 1808408

URL: http://svn.apache.org/viewvc?rev=1808408&view=rev
Log:
OAK-6669 - Blob serializer to serialize BlobStoreBlob

Added:
    
jackrabbit/oak/trunk/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/serializer/
    
jackrabbit/oak/trunk/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/serializer/BlobIdSerializer.java
   (with props)
    
jackrabbit/oak/trunk/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/serializer/
    
jackrabbit/oak/trunk/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/serializer/BlobIdSerializerTest.java
   (with props)

Added: 
jackrabbit/oak/trunk/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/serializer/BlobIdSerializer.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/serializer/BlobIdSerializer.java?rev=1808408&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/serializer/BlobIdSerializer.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/serializer/BlobIdSerializer.java
 Fri Sep 15 04:24:02 2017
@@ -0,0 +1,65 @@
+/*
+ * 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.serializer;
+
+import org.apache.jackrabbit.oak.api.Blob;
+import org.apache.jackrabbit.oak.json.Base64BlobSerializer;
+import org.apache.jackrabbit.oak.json.BlobDeserializer;
+import org.apache.jackrabbit.oak.json.BlobSerializer;
+import org.apache.jackrabbit.oak.plugins.blob.BlobStoreBlob;
+import org.apache.jackrabbit.oak.spi.blob.BlobStore;
+
+/**
+ * Blob serializer which serializes blobs depending on type
+ *
+ * In memory blobs (having contentIdentity as null) would be serialized as 
base64
+ * encoded string. For normal blobs there contentIdentity would be the 
serialized
+ * value
+ */
+public class BlobIdSerializer extends BlobSerializer implements 
BlobDeserializer {
+    private static final String PREFIX = "0y";
+    private final Base64BlobSerializer base64 = new Base64BlobSerializer();
+    private final BlobStore blobStore;
+
+    public BlobIdSerializer(BlobStore blobStore) {
+        this.blobStore = blobStore;
+    }
+
+    @Override
+    public String serialize(Blob blob) {
+        String blobId = blob.getContentIdentity();
+        if (blobId != null) {
+            return blobId;
+        }
+        return encode(blob);
+    }
+
+    @Override
+    public Blob deserialize(String value) {
+        if (value.startsWith(PREFIX)) {
+            return base64.deserialize(value.substring(PREFIX.length()));
+        }
+        return new BlobStoreBlob(blobStore, value);
+    }
+
+    private String encode(Blob blob) {
+        return PREFIX + base64.serialize(blob);
+    }
+}

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

Added: 
jackrabbit/oak/trunk/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/serializer/BlobIdSerializerTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/serializer/BlobIdSerializerTest.java?rev=1808408&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/serializer/BlobIdSerializerTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/serializer/BlobIdSerializerTest.java
 Fri Sep 15 04:24:02 2017
@@ -0,0 +1,59 @@
+/*
+ * 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.serializer;
+
+import org.apache.jackrabbit.oak.api.Blob;
+import org.apache.jackrabbit.oak.plugins.blob.BlobStoreBlob;
+import org.apache.jackrabbit.oak.plugins.memory.AbstractBlob;
+import org.apache.jackrabbit.oak.plugins.memory.ArrayBasedBlob;
+import org.apache.jackrabbit.oak.spi.blob.BlobStore;
+import org.apache.jackrabbit.oak.spi.blob.MemoryBlobStore;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class BlobIdSerializerTest {
+    private BlobStore blobStore = new MemoryBlobStore();
+
+    private BlobIdSerializer serializer = new BlobIdSerializer(blobStore);
+
+    @Test
+    public void inMemoryBlob() throws Exception{
+        Blob b = new ArrayBasedBlob("hello world".getBytes());
+
+        String value = serializer.serialize(b);
+        Blob b2 = serializer.deserialize(value);
+
+        assertTrue(AbstractBlob.equal(b, b2));
+    }
+
+    @Test
+    public void blobStoreBlob() throws Exception{
+        Blob b = new ArrayBasedBlob("hello world".getBytes());
+
+        String value = blobStore.writeBlob(b.getNewStream());
+
+        String serValue = serializer.serialize(new BlobStoreBlob(blobStore, 
value));
+        Blob b2 = serializer.deserialize(serValue);
+
+        assertTrue(AbstractBlob.equal(b, b2));
+    }
+
+}
\ No newline at end of file

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


Reply via email to