jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1540433 - in /jackrabbit/oak/trunk: oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/value/ oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/ oak-jcr/src/test/...
Date Sun, 10 Nov 2013 02:07:29 GMT
Author: jukka
Date: Sun Nov 10 02:07:29 2013
New Revision: 1540433

URL: http://svn.apache.org/r1540433
Log:
OAK-1164: Duplicate binaries

Drop the ValueBasedBlob class, and replace the functionality with
a mechanism for unwrapping a binary ValueImpl instance. The solution
is a bit ugly, but required until or unless we separate the Value
interface more cleanly from PropertyState.

Also adjust the XML import code to use ValueFactory instead of
Jackrabbit's ValueHelper and related Value classes for creating
Value instances.

Removed:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/ValueBasedBlob.java
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BinaryPropertyState.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/PropertyStates.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/value/ValueImpl.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/BufferedStringValue.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/DocViewImportHandler.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/StringValue.java
    jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/SysViewImportHandler.java
    jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/TestContentLoader.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BinaryPropertyState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BinaryPropertyState.java?rev=1540433&r1=1540432&r2=1540433&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BinaryPropertyState.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/BinaryPropertyState.java
Sun Nov 10 02:07:29 2013
@@ -25,6 +25,7 @@ import org.apache.jackrabbit.oak.api.Blo
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Type;
 import org.apache.jackrabbit.oak.plugins.value.Conversions;
+import org.apache.jackrabbit.oak.plugins.value.ValueImpl;
 import org.apache.jackrabbit.oak.plugins.value.Conversions.Converter;
 
 public class BinaryPropertyState extends SinglePropertyState<Blob> {
@@ -78,8 +79,7 @@ public class BinaryPropertyState extends
      */
     public static PropertyState binaryProperty(
             @Nonnull String name, @Nonnull Value value) {
-        return new BinaryPropertyState(
-                name, new ValueBasedBlob(checkNotNull(value)));
+        return new BinaryPropertyState(name, ValueImpl.getBlob(value));
     }
 
     @Override

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/PropertyStates.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/PropertyStates.java?rev=1540433&r1=1540432&r2=1540433&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/PropertyStates.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/PropertyStates.java
Sun Nov 10 02:07:29 2013
@@ -109,7 +109,7 @@ public final class PropertyStates {
             case PropertyType.BINARY:
                 List<Blob> blobs = Lists.newArrayList();
                 for (Value value : values) {
-                    blobs.add(new ValueBasedBlob(value));
+                    blobs.add(ValueImpl.getBlob(value));
                 }
                 return MultiBinaryPropertyState.binaryPropertyFromBlob(name, blobs);
             case PropertyType.LONG:

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/value/ValueImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/value/ValueImpl.java?rev=1540433&r1=1540432&r2=1540433&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/value/ValueImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/value/ValueImpl.java
Sun Nov 10 02:07:29 2013
@@ -26,6 +26,7 @@ import javax.jcr.RepositoryException;
 import javax.jcr.Value;
 import javax.jcr.ValueFormatException;
 
+import org.apache.jackrabbit.oak.api.Blob;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Type;
 import org.apache.jackrabbit.oak.namepath.NamePathMapper;
@@ -40,6 +41,11 @@ import static com.google.common.base.Pre
  */
 public class ValueImpl implements Value {
 
+    public static Blob getBlob(Value value) {
+        checkState(value instanceof ValueImpl);
+        return ((ValueImpl) value).getBlob();
+    }
+
     private final PropertyState propertyState;
     private final int index;
     private final NamePathMapper namePathMapper;
@@ -72,6 +78,10 @@ public class ValueImpl implements Value 
         this(checkSingleValued(property), 0, namePathMapper);
     }
 
+    private Blob getBlob() {
+        return propertyState.getValue(Type.BINARY, index);
+    }
+
     /**
      * Same as {@link #getString()} unless that names and paths are returned in their
      * Oak representation instead of being mapped to their JCR representation.

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/BufferedStringValue.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/BufferedStringValue.java?rev=1540433&r1=1540432&r2=1540433&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/BufferedStringValue.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/BufferedStringValue.java
Sun Nov 10 02:07:29 2013
@@ -18,6 +18,7 @@ package org.apache.jackrabbit.oak.jcr.xm
 
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
@@ -30,6 +31,7 @@ import java.io.Reader;
 import java.io.StringReader;
 import java.io.StringWriter;
 import java.io.Writer;
+
 import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
 import javax.jcr.Value;
@@ -39,7 +41,6 @@ import org.apache.jackrabbit.oak.namepat
 import org.apache.jackrabbit.oak.spi.xml.TextValue;
 import org.apache.jackrabbit.util.Base64;
 import org.apache.jackrabbit.util.TransientFileFactory;
-import org.apache.jackrabbit.value.ValueHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -89,7 +90,7 @@ class BufferedStringValue implements Tex
     /**
      * Whether the value is base64 encoded.
      */
-    private boolean base64;
+    private final boolean base64;
 
     /**
      * Constructs a new empty {@code BufferedStringValue}.
@@ -97,13 +98,16 @@ class BufferedStringValue implements Tex
      * @param valueFactory The value factory
      * @param namePathMapper the name/path mapper
      */
-    protected BufferedStringValue(ValueFactory valueFactory, NamePathMapper namePathMapper)
{
+    protected BufferedStringValue(
+            ValueFactory valueFactory, NamePathMapper namePathMapper,
+            boolean base64) {
         buffer = new StringWriter();
         length = 0;
         tmpFile = null;
         writer = null;
         this.namePathMapper = namePathMapper;
         this.valueFactory = valueFactory;
+        this.base64 = base64;
     }
 
     /**
@@ -169,8 +173,25 @@ class BufferedStringValue implements Tex
     }
 
     private Reader openReader() throws IOException {
-        return new InputStreamReader(
-                new BufferedInputStream(new FileInputStream(tmpFile)), "UTF-8");
+        return new InputStreamReader(openStream(), "UTF-8");
+    }
+
+    private InputStream openStream() throws IOException {
+        return new BufferedInputStream(new FileInputStream(tmpFile));
+    }
+
+    private InputStream stream() throws IOException {
+        if (base64) {
+            return new Base64ReaderInputStream(reader());
+        } else if (buffer != null) {
+            return new ByteArrayInputStream(retrieve().getBytes("UTF-8"));
+        } else if (tmpFile != null) {
+            // close writer first
+            writer.close();
+            return openStream();
+        } else {
+            throw new IOException("this instance has already been disposed");
+        }
     }
 
     /**
@@ -242,36 +263,23 @@ class BufferedStringValue implements Tex
 
     //--------------------------------------------------------< TextValue >
 
-    @Override
+    @Override @SuppressWarnings("deprecation")
     public Value getValue(int targetType) throws RepositoryException {
         try {
+            if (targetType == PropertyType.BINARY) {
+                return valueFactory.createValue(stream());
+            }
+
+            String jcrValue = retrieveString();
             if (targetType == PropertyType.NAME) {
-                return ValueHelper.deserialize(
-                        namePathMapper.getOakName(retrieveString()), targetType, false, valueFactory);
+                jcrValue = namePathMapper.getOakName(jcrValue);
             } else if (targetType == PropertyType.PATH) {
-                return ValueHelper.deserialize(
-                        namePathMapper.getOakPath(retrieveString()), targetType, false, valueFactory);
-            } else if (targetType == PropertyType.BINARY) {
-                if (length() < 0x10000) {
-                    // < 65kb: deserialize BINARY type using String
-                    return ValueHelper.deserialize(retrieve(), targetType, false, valueFactory);
-                } else {
-                    // >= 65kb: deserialize BINARY type using Reader
-                    Reader reader = reader();
-                    try {
-                        return ValueHelper.deserialize(reader, targetType, false, valueFactory);
-                    } finally {
-                        reader.close();
-                    }
-                }
-            } else {
-                // all other types
-                return ValueHelper.deserialize(retrieveString(), targetType, false, valueFactory);
+                jcrValue = namePathMapper.getOakPath(jcrValue);
             }
+            return valueFactory.createValue(jcrValue, targetType);
         } catch (IOException e) {
-            String msg = "failed to retrieve serialized value";
-            log.debug(msg, e);
-            throw new RepositoryException(msg, e);
+            throw new RepositoryException(
+                    "failed to retrieve serialized value", e);
         }
     }
 
@@ -336,15 +344,12 @@ class BufferedStringValue implements Tex
             remaining--;
             return buffer[pos++] & 0xff;
         }
-    }
 
-    /**
-     * Whether this value is base64 encoded
-     *
-     * @param base64 the flag
-     */
-    public void setBase64(boolean base64) {
-        this.base64 = base64;
+        @Override
+        public void close() throws IOException {
+            reader.close();
+        }
+
     }
 
 }

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/DocViewImportHandler.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/DocViewImportHandler.java?rev=1540433&r1=1540432&r2=1540433&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/DocViewImportHandler.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/DocViewImportHandler.java
Sun Nov 10 02:07:29 2013
@@ -108,7 +108,9 @@ class DocViewImportHandler extends Targe
     private void appendCharacters(char[] ch, int start, int length)
             throws SAXException {
         if (textHandler == null) {
-            textHandler = new BufferedStringValue(sessionContext.getValueFactory(), currentNamePathMapper());
+            textHandler = new BufferedStringValue(
+                    sessionContext.getValueFactory(), currentNamePathMapper(),
+                    false);
         }
         try {
             textHandler.append(ch, start, length);

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/StringValue.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/StringValue.java?rev=1540433&r1=1540432&r2=1540433&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/StringValue.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/StringValue.java
Sun Nov 10 02:07:29 2013
@@ -16,6 +16,9 @@
  */
 package org.apache.jackrabbit.oak.jcr.xml;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 
 import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
@@ -24,7 +27,7 @@ import javax.jcr.ValueFactory;
 
 import org.apache.jackrabbit.oak.namepath.NamePathMapper;
 import org.apache.jackrabbit.oak.spi.xml.TextValue;
-import org.apache.jackrabbit.value.ValueHelper;
+import org.apache.jackrabbit.util.Base64;
 
 /**
  * {@code StringValue} represents an immutable serialized value.
@@ -57,14 +60,32 @@ class StringValue implements TextValue {
         return this.value;
     }
 
-    @Override
+    @Override @SuppressWarnings("deprecation")
     public Value getValue(int type) throws RepositoryException {
-        String inputValue = type == PropertyType.NAME ?
-                namePathMapper.getOakName(value) :
-                type == PropertyType.PATH ?
-                        namePathMapper.getOakPath(value) :
-                        value;
-        return ValueHelper.deserialize(inputValue, type, false, valueFactory);
+        if (type == PropertyType.BINARY) {
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            try {
+                Base64.decode(value, baos);
+                return valueFactory.createValue(
+                        new ByteArrayInputStream(baos.toByteArray()));
+            } catch (IOException e) {
+                throw new RepositoryException(
+                        "Failed to decode binary value: " + value, e);
+            }
+        }
+
+        // The ValueFactory instance takes care of name and path mapping
+        // from JCR to Oak values, but here we need an additional level of
+        // mapping for XML to JCR values.
+        String jcrValue;
+        if (type == PropertyType.NAME) {
+            jcrValue = namePathMapper.getOakName(value);
+        } else if (type == PropertyType.PATH) {
+            jcrValue = namePathMapper.getOakPath(value);
+        } else {
+            jcrValue = value;
+        }
+        return valueFactory.createValue(jcrValue, type);
     }
 
     @Override

Modified: jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/SysViewImportHandler.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/SysViewImportHandler.java?rev=1540433&r1=1540432&r2=1540433&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/SysViewImportHandler.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/xml/SysViewImportHandler.java
Sun Nov 10 02:07:29 2013
@@ -20,6 +20,7 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Stack;
+
 import javax.jcr.InvalidSerializedDataException;
 import javax.jcr.NamespaceRegistry;
 import javax.jcr.PropertyType;
@@ -164,9 +165,12 @@ class SysViewImportHandler extends Targe
             }
         } else if (namespaceURI.equals(NamespaceConstants.NAMESPACE_SV) && "value".equals(localName))
{
             // sv:value element
-            currentPropValue = new BufferedStringValue(sessionContext.getValueFactory(),
currentNamePathMapper());
-            String xsiType = atts.getValue("xsi:type");
-            currentPropValue.setBase64("xs:base64Binary".equals(xsiType));
+            boolean base64 =
+                    currentPropType == PropertyType.BINARY
+                    || "xs:base64Binary".equals(atts.getValue("xsi:type"));
+            currentPropValue = new BufferedStringValue(
+                    sessionContext.getValueFactory(), currentNamePathMapper(),
+                    base64);
         } else {
             throw new SAXException(new InvalidSerializedDataException(
                     "Unexpected element in system view xml document: {" + namespaceURI +
'}' + localName));

Modified: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/TestContentLoader.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/TestContentLoader.java?rev=1540433&r1=1540432&r2=1540433&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/TestContentLoader.java
(original)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/TestContentLoader.java
Sun Nov 10 02:07:29 2013
@@ -147,7 +147,7 @@ public class TestContentLoader {
         resource.addMixin("mix:referenceable");
         resource.setProperty("jcr:encoding", ENCODING);
         resource.setProperty("jcr:mimeType", "text/plain");
-        resource.setProperty("jcr:data", new BinaryValue("Hello w\u00F6rld.".getBytes(ENCODING)));
+        resource.setProperty("jcr:data", "Hello w\u00F6rld.", PropertyType.BINARY);
         resource.setProperty("jcr:lastModified", Calendar.getInstance());
 
         Node resReference = getOrAddNode(node, "reference");
@@ -230,8 +230,7 @@ public class TestContentLoader {
         resource = getOrAddNode(node, "invalidBin");
         resource.setProperty("jcr:encoding", ENCODING);
         resource.setProperty("jcr:mimeType", "text/plain");
-        byte[] bytes = "Hello w\u00F6rld.".getBytes(ENCODING);
-        resource.setProperty(name, new BinaryValue(bytes));
+        resource.setProperty(name, "Hello w\u00F6rld.", PropertyType.BINARY);
         resource.setProperty("jcr:lastModified", Calendar.getInstance());
     }
 }



Mime
View raw message