jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r571094 - in /jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core: ./ persistence/bundle/util/ value/
Date Thu, 30 Aug 2007 09:35:40 GMT
Author: thomasm
Date: Thu Aug 30 02:35:39 2007
New Revision: 571094

URL: http://svn.apache.org/viewvc?rev=571094&view=rev
Log:
JCR-926: global data store: new in-memory, data store, and temp file BLOB

Added:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInDataStore.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInMemory.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInTempFile.java
  (with props)
Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/NodeImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/PropertyImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/BundleBinding.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ItemStateBinding.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/InternalValue.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/NodeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/NodeImpl.java?rev=571094&r1=571093&r2=571094&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/NodeImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/NodeImpl.java
Thu Aug 30 02:35:39 2007
@@ -4098,11 +4098,11 @@
             Value[] values = prop.getValues();
             InternalValue[] ivalues = new InternalValue[values.length];
             for (int i = 0; i < values.length; i++) {
-                ivalues[i] = InternalValue.create(values[i], session.getNamespaceResolver());
+                ivalues[i] = InternalValue.create(values[i], session.getNamespaceResolver(),
rep.getDataStore());
             }
             internalSetProperty(prop.getQName(), ivalues);
         } else {
-            InternalValue value = InternalValue.create(prop.getValue(), session.getNamespaceResolver());
+            InternalValue value = InternalValue.create(prop.getValue(), session.getNamespaceResolver(),
rep.getDataStore());
             internalSetProperty(prop.getQName(), value);
         }
     }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/PropertyImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/PropertyImpl.java?rev=571094&r1=571093&r2=571094&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/PropertyImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/PropertyImpl.java
Thu Aug 30 02:35:39 2007
@@ -740,7 +740,7 @@
 
         InternalValue value;
         try {
-            value = InternalValue.createTemporary(stream);
+            value = InternalValue.createTemporary(stream, rep.getDataStore());
             if (reqType != PropertyType.BINARY) {
                 // type conversion required
                 Value jcrValue = value.toJCRValue(session.getNamespaceResolver());

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/BundleBinding.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/BundleBinding.java?rev=571094&r1=571093&r2=571094&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/BundleBinding.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/BundleBinding.java
Thu Aug 30 02:35:39 2007
@@ -321,7 +321,9 @@
             switch (type) {
                 case PropertyType.BINARY:
                     int size = in.readInt();
-                    if (size == -1) {
+                    if (InternalValue.USE_DATA_STORE && size == -2) {
+                        val = InternalValue.create(dataStore, in.readUTF());
+                    } else if (size == -1) {
                         blobIds[i] = in.readUTF();
                         try {
                             if (blobStore instanceof ResourceBasedBLOBStore) {
@@ -433,7 +435,15 @@
                         log.error("Error while reading size of binary: " + e);
                         return false;
                     }
-                    if (size == -1) {
+                    if (InternalValue.USE_DATA_STORE && size == -2) {
+                        try {
+                            String s = in.readUTF();
+                            log.info("  global data store id: " + s);
+                        } catch (IOException e) {
+                            log.error("Error while reading blob id: " + e);
+                            return false;
+                        }
+                    } else if (size == -1) {
                         try {
                             String s = in.readUTF();
                             log.info("  blobid: " + s);
@@ -548,6 +558,11 @@
             InternalValue val = values[i];
             switch (state.getType()) {
                 case PropertyType.BINARY:
+                    if(InternalValue.USE_DATA_STORE) {
+                        out.writeInt(-2);
+                        out.writeUTF(val.toString());
+                        break;
+                    }
                     // special handling required for binary value:
                     // spool binary value to file in blob store
                     BLOBFileValue blobVal = val.getBLOBFileValue();

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ItemStateBinding.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ItemStateBinding.java?rev=571094&r1=571093&r2=571094&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ItemStateBinding.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ItemStateBinding.java
Thu Aug 30 02:35:39 2007
@@ -302,7 +302,9 @@
             switch (type) {
                 case PropertyType.BINARY:
                     int size = in.readInt();
-                    if (size == -1) {
+                    if (InternalValue.USE_DATA_STORE && size == -2) {
+                        val = InternalValue.create(dataStore, in.readUTF());
+                    } else if (size == -1) {
                         String s = in.readUTF();
                         try {
                             if (blobStore instanceof ResourceBasedBLOBStore) {
@@ -382,6 +384,11 @@
             switch (state.getType()) {
                 case PropertyType.BINARY:
                     try {
+                        if(InternalValue.USE_DATA_STORE) {
+                            out.writeInt(-2);
+                            out.writeUTF(val.toString());
+                            break;
+                        }
                         // special handling required for binary value:
                         // spool binary value to file in blob store
                         BLOBFileValue blobVal = val.getBLOBFileValue();

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInDataStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInDataStore.java?rev=571094&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInDataStore.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInDataStore.java
Thu Aug 30 02:35:39 2007
@@ -0,0 +1,130 @@
+/*
+ * 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.core.value;
+
+import org.apache.jackrabbit.core.data.DataIdentifier;
+import org.apache.jackrabbit.core.data.DataRecord;
+import org.apache.jackrabbit.core.data.DataStore;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.jcr.RepositoryException;
+
+/**
+ * Represents binary data which is stored in the data store.
+ */
+public class BLOBInDataStore extends BLOBFileValue {
+    
+    private final DataStore store;
+    private final DataIdentifier identifier;
+    private DataRecord dataRecord;
+    
+    /**
+     * the prefix of the string representation of this value
+     */    
+    private static final String PREFIX = "dataStore:";
+    
+    /**
+     * The default logger
+     */
+    private static Logger log = LoggerFactory.getLogger(BLOBInDataStore.class);
+    
+
+    private BLOBInDataStore(DataStore store, DataIdentifier identifier) {
+        assert store != null;
+        assert identifier != null;
+        this.store = store;
+        this.identifier = identifier;
+    }
+
+    public void delete(boolean pruneEmptyParentDirs) {
+        // do nothing
+        // TODO need to implement garbage collection
+    }
+
+    public void discard() {
+        // do nothing
+        // TODO need to implement garbage collection
+    }
+
+    public boolean equals(Object obj) {
+        if(!(obj instanceof BLOBInDataStore) || obj == null) {
+            return false;
+        }
+        BLOBInDataStore other = (BLOBInDataStore) obj;
+        return store == other.store && identifier.equals(other.identifier);
+    }
+
+    public long getLength() {
+        try {
+            return getDataRecord().getLength();
+        } catch (IOException e) {
+            log.warn("getLength for " + identifier + " failed", e);
+            return -1;
+        }
+    }
+
+    public InputStream getStream() throws IllegalStateException, RepositoryException {
+        try {
+            return getDataRecord().getStream();
+        } catch (IOException e) {
+            throw new RepositoryException(e);
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public String toString() {
+        StringBuffer buff = new StringBuffer(20);
+        buff.append(PREFIX);
+        buff.append(identifier.toString());
+        return buff.toString();
+    }   
+    
+    static BLOBInDataStore getInstance(DataStore store, String s) {
+        String id = s.substring(PREFIX.length());
+        DataIdentifier identifier = new DataIdentifier(id);
+        return new BLOBInDataStore(store, identifier);
+    }
+    
+    static BLOBInDataStore getInstance(DataStore store, InputStream in) throws IOException
{
+        DataRecord rec = store.addRecord(in);
+        DataIdentifier identifier = rec.getIdentifier();
+        return new BLOBInDataStore(store, identifier);
+    }    
+    
+    /**
+     * Checks if String can be converted to an instance of this class.
+     * @param s
+     * @return true if it can be converted
+     */
+    static boolean isInstance(String s) {
+        return s.startsWith(PREFIX);
+    }    
+    
+    private DataRecord getDataRecord() throws IOException {
+        if(dataRecord == null) {
+            dataRecord = store.getRecord(identifier);
+        }
+        return dataRecord;
+    }
+
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInDataStore.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInMemory.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInMemory.java?rev=571094&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInMemory.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInMemory.java
Thu Aug 30 02:35:39 2007
@@ -0,0 +1,179 @@
+/*
+ * 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.core.value;
+
+import org.apache.jackrabbit.uuid.Constants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.util.Arrays;
+
+import javax.jcr.RepositoryException;
+
+/**
+ * Represents binary data which is backed by a byte[] (in memory).
+ */
+public class BLOBInMemory extends BLOBFileValue {
+    
+    /**
+     * Logger instance for this class
+     */
+    private static Logger log = LoggerFactory.getLogger(BLOBInMemory.class);
+    
+    /**
+     * the prefix of the string representation of this value
+     */    
+    private static final String PREFIX = "0x";
+    
+    /**
+     * the data
+     */
+    private byte[] data;
+    
+    /**
+     * empty array
+     */    
+    private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
+    
+    /**
+     * empty instance
+     */
+    private static final BLOBInMemory EMPTY = new BLOBInMemory(EMPTY_BYTE_ARRAY);
+    
+    /**
+     * Creates a new instance from a
+     * <code>byte[]</code> array.
+     *
+     * @param data the byte array
+     */    
+    private BLOBInMemory(byte[] data) {
+        this.data = data;
+    }
+    
+    /**
+     * Creates a new instance from a
+     * <code>byte[]</code> array.
+     *
+     * @param data the byte array
+     */    
+    static BLOBInMemory getInstance(byte[] data) {
+        if(data.length == 0) {
+            return EMPTY;
+        } else {
+            return new BLOBInMemory(data);
+        }
+    }
+    
+    /**
+     * Checks if String can be converted to an instance of this class.
+     * @param s
+     * @return true if it can be converted
+     */
+    static boolean isInstance(String s) {
+        return s.startsWith(PREFIX);
+    }    
+    
+    /**
+     * Convert a String to an instance of this class.
+     * @param s
+     * @return the instance
+     */
+    static BLOBInMemory getInstance(String s) throws IllegalArgumentException {
+        assert s.startsWith(PREFIX);
+        s = s.substring(PREFIX.length());
+        int len = s.length();
+        if (len % 2 != 0) {
+            String msg = "unable to deserialize byte array " + s + " , length=" + s.length();
+            log.debug(msg);            
+            throw new IllegalArgumentException(msg);
+        }
+        len /= 2;
+        byte[] data = new byte[len];
+        try {
+            for (int i = 0; i < len; i++) {
+                data[i] = (byte) ((Character.digit(s.charAt(i+i), 16) << 4) | (Character.digit(s.charAt(i+i+1),
16)));
+            }
+        } catch (NumberFormatException e) {
+            String msg = "unable to deserialize byte array " + s;
+            log.debug(msg);            
+            throw new IllegalArgumentException(msg);
+        }
+        return BLOBInMemory.getInstance(data);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void delete(boolean pruneEmptyParentDirs) {
+        // TODO avoid: this makes the value mutable
+        data = EMPTY_BYTE_ARRAY;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void discard() {
+        // TODO avoid: this makes the value mutable
+        data = EMPTY_BYTE_ARRAY;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public long getLength() {
+        return data.length;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public InputStream getStream() throws IllegalStateException, RepositoryException {
+        return new ByteArrayInputStream(data);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public String toString() {
+        StringBuffer buff = new StringBuffer(PREFIX.length() + 2 * data.length);
+        buff.append(PREFIX);
+        char[] hex = Constants.hexDigits;
+        for (int i = 0; i < data.length; i++) {
+            int c = data[i] & 0xff;
+            buff.append(hex[c >> 4]);
+            buff.append(hex[c & 0xf]);
+        }
+        return buff.toString();
+    }    
+    
+    /**
+     * {@inheritDoc}
+     */
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof BLOBInMemory) {
+            BLOBInMemory other = (BLOBInMemory) obj;
+            return Arrays.equals(data, other.data);
+        }
+        return false;
+    }
+
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInMemory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInTempFile.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInTempFile.java?rev=571094&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInTempFile.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInTempFile.java
Thu Aug 30 02:35:39 2007
@@ -0,0 +1,142 @@
+/*
+ * 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.core.value;
+
+import org.apache.jackrabbit.util.TransientFileFactory;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import javax.jcr.RepositoryException;
+
+/**
+ * Represents binary data which is stored in a temporary file.
+ */
+public class BLOBInTempFile extends BLOBFileValue {
+    
+    /**
+     * the prefix of the string representation of this value
+     */    
+    private static final String PREFIX = "tempFile:";
+    
+    private File file;
+    private long length;
+    
+    /**
+     * Creates a new instance from a
+     * <code>byte[]</code> array.
+     *
+     * @param data the byte array
+     * @throws IOException 
+     * @throws IOException 
+     */    
+    private BLOBInTempFile(InputStream in) throws IOException {
+        OutputStream out = null;
+        try {
+            TransientFileFactory fileFactory = TransientFileFactory.getInstance();
+            file = fileFactory.createTransientFile("bin", null, null);
+            out = new FileOutputStream(file);
+            byte[] buffer = new byte[4 * 1024];
+            while(true) {
+                int len = in.read(buffer);
+                if(len < 0) {
+                    break;
+                }
+                out.write(buffer, 0, len);
+                length += len;                
+            }
+        } finally {
+            if(out != null) {
+                out.close();
+            }
+            in.close();
+        }
+    }
+    
+    /**
+     * Creates a new instance from a
+     * <code>in[]</code> stream.
+     *
+     * @param in the stream
+     */    
+    static BLOBInTempFile getInstance(InputStream in) throws IOException {
+        return new BLOBInTempFile(in);
+    }
+    
+    /**
+     * {@inheritDoc}
+     */
+    public void delete(boolean pruneEmptyParentDirs) {
+        file.delete();
+        length = -1;
+        file = null;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void discard() {
+        file.delete();
+        length = -1;
+        file = null;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public long getLength() {
+        return length;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public InputStream getStream() throws IllegalStateException, RepositoryException {
+        try {
+            return new FileInputStream(file);
+        } catch (FileNotFoundException fnfe) {
+            throw new RepositoryException("file backing binary value not found", fnfe);
+        }        
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public String toString() {
+        return PREFIX + file.toString();
+    }    
+    
+    /**
+     * {@inheritDoc}
+     */
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof BLOBInTempFile) {
+            BLOBInTempFile other = (BLOBInTempFile) obj;
+            return (file == other.file) || (length == other.length && file != null
&& file.equals(other.file));
+        }
+        return false;
+    }
+    
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/BLOBInTempFile.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/InternalValue.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/InternalValue.java?rev=571094&r1=571093&r2=571094&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/InternalValue.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/InternalValue.java
Thu Aug 30 02:35:39 2007
@@ -42,9 +42,12 @@
 import javax.jcr.RepositoryException;
 import javax.jcr.Value;
 import javax.jcr.ValueFormatException;
+
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.SequenceInputStream;
 import java.util.Calendar;
 
 /**
@@ -79,38 +82,52 @@
      */
     public static final boolean USE_DATA_STORE = Boolean.valueOf(System.getProperty("org.jackrabbit.useDataStore",
"false")).booleanValue();
 
+    /**
+     * Byte arrays smaller or equal this size are always kept in memory
+     */
+    private final static int MIN_BLOB_FILE_SIZE = Integer.parseInt(System.getProperty("org.jackrabbit.minBlobFileSize",
"100"));
+
     private final Object val;
     private final int type;
 
     //------------------------------------------------------< factory methods >
     /**
-     * @param value
-     * @param nsResolver
-     * @return
-     * @throws ValueFormatException
-     * @throws RepositoryException
+     * Create a new internal value from the given JCR value.
+     * Large binary values are stored in a temporary file.
+     * 
+     * @param value the JCR value
+     * @param nsResolver the namespace resolver
+     * @return the created internal value
      */
-    public static InternalValue create(Value value, NamespaceResolver nsResolver, DataStore
store)
+    public static InternalValue create(Value value, NamespaceResolver nsResolver)
             throws ValueFormatException, RepositoryException {
-        return create(value, nsResolver);
+        return create(value, nsResolver, null);
     }
 
     /**
-     * @param value
-     * @param nsResolver
-     * @return
-     * @throws ValueFormatException
-     * @throws RepositoryException
+     * Create a new internal value from the given JCR value.
+     * If the data store is enabled, large binary values are stored in the data store.
+     * 
+     * @param value the JCR value
+     * @param nsResolver the namespace resolver
+     * @param store the data store
+     * @return the created internal value
      */
-    public static InternalValue create(Value value, NamespaceResolver nsResolver)
+    public static InternalValue create(Value value, NamespaceResolver nsResolver, DataStore
store)
             throws ValueFormatException, RepositoryException {
         if (value == null) {
             throw new IllegalArgumentException("null value");
         }
-
         switch (value.getType()) {
             case PropertyType.BINARY:
                 try {
+                    if(USE_DATA_STORE) {
+                        if(store == null) {
+                            return new InternalValue(BLOBInTempFile.getInstance(value.getStream()));
+                        } else {
+                            return new InternalValue(getBLOBFileValue(store, value.getStream()));
+                        }
+                    }
                     if (value instanceof BLOBFileValue) {
                         return new InternalValue((BLOBFileValue) value);
                     } else {
@@ -203,17 +220,40 @@
      * @return the created value
      */
     public static InternalValue create(byte[] value) {
+        if(USE_DATA_STORE) {        
+            return new InternalValue(BLOBInMemory.getInstance(value));
+        }
         return new InternalValue(new BLOBValue(value));
     }
 
     /**
-     * @param value
-     * @return
-     * @throws IOException
+     * Create an internal value that is backed by a temporary file.
+     * 
+     * @param value the stream
+     * @param store the data store
+     * @return the internal value
      */
     public static InternalValue createTemporary(InputStream value) throws IOException {
+        if(USE_DATA_STORE) {        
+            return new InternalValue(BLOBInTempFile.getInstance(value));
+        }
         return new InternalValue(new BLOBValue(value, true));
-    }    
+    }
+    
+    /**
+     * Create an internal value that is backed by a temporary file (if data store usage is
disabled)
+     * or (if it is enabled) in the data store.
+     * 
+     * @param value the stream
+     * @param store the data store
+     * @return the internal value
+     */
+    public static InternalValue createTemporary(InputStream value, DataStore store) throws
IOException {
+        if(USE_DATA_STORE) {
+            return new InternalValue(getBLOBFileValue(store, value));
+        }
+        return new InternalValue(new BLOBValue(value, true));
+    }
 
     /**
      * @param value
@@ -243,6 +283,18 @@
     public static InternalValue create(File value) throws IOException {
         return new InternalValue(new BLOBValue(value));
     }
+    
+    /**
+     * Create a binary object with the given identifier.
+     * 
+     * @param store the data store
+     * @param id the identifier
+     * @return the value
+     */    
+    public static InternalValue create(DataStore store, String id) {
+        assert USE_DATA_STORE;
+        return new InternalValue(getBLOBFileValue(store, id));
+    }
 
     /**
      * @param value
@@ -394,6 +446,9 @@
      * @throws RepositoryException
      */
     public InternalValue createCopy() throws RepositoryException {
+        if(USE_DATA_STORE) {
+            return this;
+        }
         if (type == PropertyType.BINARY) {
             // return a copy since the wrapped BLOBFileValue instance is mutable
             try {
@@ -538,4 +593,38 @@
         val = value;
         type = PropertyType.REFERENCE;
     }
+    
+    private static BLOBFileValue getBLOBFileValue(DataStore store, InputStream in) throws
IOException {
+        byte[] buffer = new byte[MIN_BLOB_FILE_SIZE];
+        int pos = 0, len = MIN_BLOB_FILE_SIZE;
+        while(pos < MIN_BLOB_FILE_SIZE) {
+            int l = in.read(buffer, pos, len);
+            if(l < 0) {
+                break;
+            }
+            pos += l;
+            len -= l;
+        }
+        if(pos < MIN_BLOB_FILE_SIZE) {
+            // shrink the buffer
+            byte[] data = new byte[pos];
+            System.arraycopy(buffer, 0, data, 0, pos);
+            return BLOBInMemory.getInstance(data);
+        } else {
+            // a few bytes are already read, need to re-build the input stream
+            in = new SequenceInputStream(new ByteArrayInputStream(buffer, 0, pos), in);
+            return BLOBInDataStore.getInstance(store, in);
+        }
+    }
+
+    private static BLOBFileValue getBLOBFileValue(DataStore store, String id) {
+        if(BLOBInMemory.isInstance(id)) {
+            return BLOBInMemory.getInstance(id);
+        } else if(BLOBInDataStore.isInstance(id)) {
+            return BLOBInDataStore.getInstance(store, id);
+        } else {
+            throw new IllegalArgumentException("illegal binary id: " + id);
+        }
+    }
+
 }



Mime
View raw message