jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r329242 - /incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/
Date Fri, 28 Oct 2005 16:38:03 GMT
Author: stefan
Date: Fri Oct 28 09:37:58 2005
New Revision: 329242

URL: http://svn.apache.org/viewcvs?rev=329242&view=rev
Log:
simplified and adapted bdb-persistence to latest modifications in core (r329224)

Removed:
    incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/BLOBStore.java
Modified:
    incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/BerkeleyDBPersistenceManager.java
    incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/NodeReferencesTupleBinding.java
    incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/NodeStateTupleBinding.java
    incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/PropertyStateTupleBinding.java

Modified: incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/BerkeleyDBPersistenceManager.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/BerkeleyDBPersistenceManager.java?rev=329242&r1=329241&r2=329242&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/BerkeleyDBPersistenceManager.java
(original)
+++ incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/BerkeleyDBPersistenceManager.java
Fri Oct 28 09:37:58 2005
@@ -16,18 +16,19 @@
  */
 package org.apache.jackrabbit.core.state.bdb;
 
-import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.InputStream;
-import java.io.OutputStream;
-
+import com.sleepycat.je.Database;
+import com.sleepycat.je.DatabaseConfig;
+import com.sleepycat.je.DatabaseEntry;
+import com.sleepycat.je.Environment;
+import com.sleepycat.je.EnvironmentConfig;
+import com.sleepycat.je.LockMode;
+import com.sleepycat.je.OperationStatus;
+import com.sleepycat.je.Transaction;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.jackrabbit.core.NodeId;
 import org.apache.jackrabbit.core.PropertyId;
 import org.apache.jackrabbit.core.fs.FileSystem;
-import org.apache.jackrabbit.core.fs.FileSystemPathUtil;
-import org.apache.jackrabbit.core.fs.FileSystemResource;
 import org.apache.jackrabbit.core.fs.local.LocalFileSystem;
 import org.apache.jackrabbit.core.state.AbstractPersistenceManager;
 import org.apache.jackrabbit.core.state.ChangeLog;
@@ -38,18 +39,12 @@
 import org.apache.jackrabbit.core.state.NodeState;
 import org.apache.jackrabbit.core.state.PMContext;
 import org.apache.jackrabbit.core.state.PropertyState;
-import org.apache.jackrabbit.name.QName;
+import org.apache.jackrabbit.core.state.util.BLOBStore;
+import org.apache.jackrabbit.core.state.util.FileSystemBLOBStore;
 
-import com.sleepycat.je.Database;
-import com.sleepycat.je.DatabaseConfig;
-import com.sleepycat.je.DatabaseEntry;
-import com.sleepycat.je.Environment;
-import com.sleepycat.je.EnvironmentConfig;
-import com.sleepycat.je.LockMode;
-import com.sleepycat.je.OperationStatus;
-import com.sleepycat.je.Transaction;
+import java.io.File;
 
-public class BerkeleyDBPersistenceManager extends AbstractPersistenceManager implements BLOBStore
{
+public class BerkeleyDBPersistenceManager extends AbstractPersistenceManager {
 
     private static Log log = LogFactory.getLog(BerkeleyDBPersistenceManager.class);
 
@@ -58,7 +53,11 @@
     private boolean initialized = false;
     private Environment environment;
     private Database database;
+    // file system where BLOB data is stored
     private FileSystem blobFS;
+    // BLOBStore that manages BLOB data in the file system
+    private BLOBStore blobStore;
+
     private ThreadLocal localTransaction = new ThreadLocal(); // ?? are persistence managers
thread-safes ???
 
     //
@@ -70,8 +69,9 @@
 
         // prepare the db directory
         File envDir = new File(context.getHomeDir(), "db");
-        if (!envDir.exists())
+        if (!envDir.exists()) {
             envDir.mkdirs();
+        }
 
         log.debug("init berkeleyDb environment at " + envDir.getAbsolutePath());
 
@@ -89,13 +89,14 @@
         database = environment.openDatabase(null, "jcrStore", dbConfig);
 
         /**
-         * store blob's in local file system in a sub directory
+         * store BLOB data in local file system in a sub directory
          * of the workspace home directory
          */
         LocalFileSystem blobFS = new LocalFileSystem();
         blobFS.setRoot(new File(context.getHomeDir(), "blobs"));
         blobFS.init();
         this.blobFS = blobFS;
+        blobStore = new FileSystemBLOBStore(blobFS);
 
         initialized = true;
     }
@@ -113,9 +114,10 @@
         // close environnement
         environment.close();
 
-        // close blob store
+        // close BLOB file system
         blobFS.close();
         blobFS = null;
+        blobStore = null;
 
         initialized = false;
     }
@@ -127,10 +129,11 @@
             DatabaseEntry key = new DatabaseEntry();
             DatabaseEntry value = new DatabaseEntry();
             NodeStateTupleBinding tupleBinding = new NodeStateTupleBinding(id);
-            key.setData(id.toString().getBytes("UTF-8"));
+            key.setData(id.toString().getBytes(ENCODING));
             OperationStatus operationStatus = database.get(null, key, value, LockMode.DEFAULT);
-            if (operationStatus.equals(OperationStatus.NOTFOUND))
+            if (operationStatus.equals(OperationStatus.NOTFOUND)) {
                 throw new NoSuchItemStateException();
+            }
             return (NodeState) tupleBinding.entryToObject(value);
         } catch (NoSuchItemStateException e) {
             throw e;
@@ -144,11 +147,12 @@
         try {
             DatabaseEntry key = new DatabaseEntry();
             DatabaseEntry value = new DatabaseEntry();
-            PropertyStateTupleBinding tupleBinding = new PropertyStateTupleBinding(id, this);
-            key.setData(id.toString().getBytes("UTF-8"));
+            PropertyStateTupleBinding tupleBinding = new PropertyStateTupleBinding(id, blobStore);
+            key.setData(id.toString().getBytes(ENCODING));
             OperationStatus operationStatus = database.get(null, key, value, LockMode.DEFAULT);
-            if (operationStatus.equals(OperationStatus.NOTFOUND))
+            if (operationStatus.equals(OperationStatus.NOTFOUND)) {
                 throw new NoSuchItemStateException();
+            }
             return (PropertyState) tupleBinding.entryToObject(value);
         } catch (NoSuchItemStateException e) {
             throw e;
@@ -163,10 +167,11 @@
             DatabaseEntry key = new DatabaseEntry();
             DatabaseEntry value = new DatabaseEntry();
             NodeReferencesTupleBinding tupleBinding = new NodeReferencesTupleBinding(id);
-            key.setData((id.toString() + ".references").getBytes("UTF-8"));
+            key.setData((id.toString() + ".references").getBytes(ENCODING));
             OperationStatus operationStatus = database.get(null, key, value, LockMode.DEFAULT);
-            if (operationStatus.equals(OperationStatus.NOTFOUND))
+            if (operationStatus.equals(OperationStatus.NOTFOUND)) {
                 throw new NoSuchItemStateException();
+            }
             return (NodeReferences) tupleBinding.entryToObject(value);
         } catch (NoSuchItemStateException e) {
             throw e;
@@ -180,7 +185,7 @@
         try {
             DatabaseEntry key = new DatabaseEntry();
             DatabaseEntry value = new DatabaseEntry();
-            key.setData(id.toString().getBytes("UTF-8"));
+            key.setData(id.toString().getBytes(ENCODING));
             OperationStatus operationStatus = database.get(null, key, value, LockMode.DEFAULT);
             return operationStatus.equals(OperationStatus.SUCCESS);
         } catch (Exception e) {
@@ -193,7 +198,7 @@
         try {
             DatabaseEntry key = new DatabaseEntry();
             DatabaseEntry value = new DatabaseEntry();
-            key.setData(id.toString().getBytes("UTF-8"));
+            key.setData(id.toString().getBytes(ENCODING));
             OperationStatus operationStatus = database.get(null, key, value, LockMode.DEFAULT);
             return operationStatus.equals(OperationStatus.SUCCESS);
         } catch (Exception e) {
@@ -206,7 +211,7 @@
         try {
             DatabaseEntry key = new DatabaseEntry();
             DatabaseEntry value = new DatabaseEntry();
-            key.setData((targetId.toString() + ".references").getBytes("UTF-8"));
+            key.setData((targetId.toString() + ".references").getBytes(ENCODING));
             OperationStatus operationStatus = database.get(null, key, value, LockMode.DEFAULT);
             return operationStatus.equals(OperationStatus.SUCCESS);
         } catch (Exception e) {
@@ -244,11 +249,12 @@
             DatabaseEntry key = new DatabaseEntry();
             DatabaseEntry value = new DatabaseEntry();
             NodeStateTupleBinding tupleBinding = new NodeStateTupleBinding();
-            key.setData(state.getId().toString().getBytes("UTF-8"));
+            key.setData(state.getId().toString().getBytes(ENCODING));
             tupleBinding.objectToEntry(state, value);
             OperationStatus operationStatus = database.put(transaction, key, value);
-            if (!operationStatus.equals(OperationStatus.SUCCESS))
+            if (!operationStatus.equals(OperationStatus.SUCCESS)) {
                 throw new ItemStateException(operationStatus.toString());
+            }
         } catch (Exception e) {
             log.error(e);
             throw new ItemStateException(e);
@@ -260,12 +266,13 @@
             Transaction transaction = (Transaction) localTransaction.get();
             DatabaseEntry key = new DatabaseEntry();
             DatabaseEntry value = new DatabaseEntry();
-            PropertyStateTupleBinding tupleBinding = new PropertyStateTupleBinding(this);
-            key.setData(state.getId().toString().getBytes("UTF-8"));
+            PropertyStateTupleBinding tupleBinding = new PropertyStateTupleBinding(blobStore);
+            key.setData(state.getId().toString().getBytes(ENCODING));
             tupleBinding.objectToEntry(state, value);
             OperationStatus operationStatus = database.put(transaction, key, value);
-            if (!operationStatus.equals(OperationStatus.SUCCESS))
+            if (!operationStatus.equals(OperationStatus.SUCCESS)) {
                 throw new ItemStateException(operationStatus.toString());
+            }
         } catch (Exception e) {
             log.error(e);
             throw new ItemStateException(e);
@@ -278,11 +285,12 @@
             DatabaseEntry key = new DatabaseEntry();
             DatabaseEntry value = new DatabaseEntry();
             NodeReferencesTupleBinding tupleBinding = new NodeReferencesTupleBinding();
-            key.setData((refs.getTargetId().toString() + ".references").getBytes("UTF-8"));
+            key.setData((refs.getTargetId().toString() + ".references").getBytes(ENCODING));
             tupleBinding.objectToEntry(refs, value);
             OperationStatus operationStatus = database.put(transaction, key, value);
-            if (!operationStatus.equals(OperationStatus.SUCCESS))
+            if (!operationStatus.equals(OperationStatus.SUCCESS)) {
                 throw new ItemStateException(operationStatus.toString());
+            }
         } catch (Exception e) {
             log.error(e);
             throw new ItemStateException(e);
@@ -293,10 +301,11 @@
         try {
             Transaction transaction = (Transaction) localTransaction.get();
             DatabaseEntry key = new DatabaseEntry();
-            key.setData(state.getId().toString().getBytes("UTF-8"));
+            key.setData(state.getId().toString().getBytes(ENCODING));
             OperationStatus operationStatus = database.delete(transaction, key);
-            if (!operationStatus.equals(OperationStatus.SUCCESS))
+            if (!operationStatus.equals(OperationStatus.SUCCESS)) {
                 throw new ItemStateException(operationStatus.toString());
+            }
         } catch (Exception e) {
             log.error(e);
             throw new ItemStateException(e);
@@ -307,10 +316,11 @@
         try {
             Transaction transaction = (Transaction) localTransaction.get();
             DatabaseEntry key = new DatabaseEntry();
-            key.setData(state.getId().toString().getBytes("UTF-8"));
+            key.setData(state.getId().toString().getBytes(ENCODING));
             OperationStatus operationStatus = database.delete(transaction, key);
-            if (!operationStatus.equals(OperationStatus.SUCCESS))
+            if (!operationStatus.equals(OperationStatus.SUCCESS)) {
                 throw new ItemStateException(operationStatus.toString());
+            }
         } catch (Exception e) {
             log.error(e);
             throw new ItemStateException(e);
@@ -321,65 +331,14 @@
         try {
             Transaction transaction = (Transaction) localTransaction.get();
             DatabaseEntry key = new DatabaseEntry();
-            key.setData((refs.getTargetId().toString() + ".references").getBytes("UTF-8"));
+            key.setData((refs.getTargetId().toString() + ".references").getBytes(ENCODING));
             OperationStatus operationStatus = database.delete(transaction, key);
-            if (!operationStatus.equals(OperationStatus.SUCCESS))
+            if (!operationStatus.equals(OperationStatus.SUCCESS)) {
                 throw new ItemStateException(operationStatus.toString());
+            }
         } catch (Exception e) {
             log.error(e);
             throw new ItemStateException(e);
         }
     }
-
-    // blobs
-
-    public FileSystemResource get(String blobId) throws Exception {
-        return new FileSystemResource(blobFS, blobId);
-    }
-
-    public String put(PropertyId id, int index, InputStream in, long size) throws Exception
{
-        String path = buildBlobFilePath(id.getParentUUID(), id.getName(), index);
-        OutputStream out = null;
-        FileSystemResource internalBlobFile = new FileSystemResource(blobFS, path);
-        internalBlobFile.makeParentDirs();
-        try {
-            out = new BufferedOutputStream(internalBlobFile.getOutputStream());
-            byte[] buffer = new byte[8192];
-            int read;
-            while ((read = in.read(buffer)) > 0) {
-                out.write(buffer, 0, read);
-            }
-        } finally {
-            out.close();
-        }
-        return path;
-    }
-
-    public boolean remove(String blobId) throws Exception {
-        FileSystemResource res = new FileSystemResource(blobFS, blobId);
-        if (!res.exists()) {
-            return false;
-        }
-        // delete resource and prune empty parent folders
-        res.delete(true);
-        return true;
-    }
-
-    private static String buildBlobFilePath(String parentUUID, QName propName, int it) {
-        StringBuffer sb = new StringBuffer();
-        char[] chars = parentUUID.toCharArray();
-        int cnt = 0;
-        for (int i = 0; i < chars.length; i++) {
-            if (chars[i] == '-') {
-                continue;
-            }
-            if (cnt == 2 || cnt == 4) {
-                sb.append(FileSystem.SEPARATOR_CHAR);
-            }
-            sb.append(chars[i]);
-            cnt++;
-        }
-        return sb.toString() + FileSystem.SEPARATOR + FileSystemPathUtil.escapeName(propName.toString())
+ "." + it + ".bin";
-    }
-
 }

Modified: incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/NodeReferencesTupleBinding.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/NodeReferencesTupleBinding.java?rev=329242&r1=329241&r2=329242&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/NodeReferencesTupleBinding.java
(original)
+++ incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/NodeReferencesTupleBinding.java
Fri Oct 28 09:37:58 2005
@@ -16,19 +16,19 @@
  */
 package org.apache.jackrabbit.core.state.bdb;
 
-import java.util.Collection;
-import java.util.Iterator;
-
-import org.apache.jackrabbit.core.PropertyId;
-import org.apache.jackrabbit.core.state.NodeReferences;
-import org.apache.jackrabbit.core.state.NodeReferencesId;
-
 import com.sleepycat.bind.tuple.TupleBinding;
 import com.sleepycat.bind.tuple.TupleInput;
 import com.sleepycat.bind.tuple.TupleOutput;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.jackrabbit.core.state.NodeReferences;
+import org.apache.jackrabbit.core.state.NodeReferencesId;
+import org.apache.jackrabbit.core.state.util.Serializer;
 
 public class NodeReferencesTupleBinding extends TupleBinding {
 
+    private Log log = LogFactory.getLog(NodeReferencesTupleBinding.class);
+
     private NodeReferencesId id;
 
     public NodeReferencesTupleBinding(NodeReferencesId id) {
@@ -41,29 +41,29 @@
     public Object entryToObject(TupleInput in) {
 
         NodeReferences refs = new NodeReferences(id);
-        refs.clearAllReferences();
 
-        // references
-        int count = in.readInt(); // count
-        for (int i = 0; i < count; i++) {
-            refs.addReference(PropertyId.valueOf(in.readString())); // propertyId
+        try {
+            Serializer.deserialize(refs, in);
+        } catch (Exception e) {
+            // since the TupleInput methods do not throw any
+            // exceptions the above call should neither...
+            String msg = "error while deserializing node references";
+            log.debug(msg);
+            throw new RuntimeException(msg, e);
         }
 
         return refs;
     }
 
     public void objectToEntry(Object o, TupleOutput out) {
-
-        NodeReferences refs = (NodeReferences) o;
-
-        // references
-        Collection c = refs.getReferences();
-        out.writeInt(c.size()); // count
-        for (Iterator iter = c.iterator(); iter.hasNext();) {
-            PropertyId propId = (PropertyId) iter.next();
-            out.writeString(propId.toString()); // propertyId
+        try {
+            Serializer.serialize((NodeReferences) o, out);
+        } catch (Exception e) {
+            // since the TupleOutput methods do not throw any
+            // exceptions the above call should neither...
+            String msg = "error while serializing node references";
+            log.debug(msg);
+            throw new RuntimeException(msg, e);
         }
-
     }
-
 }

Modified: incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/NodeStateTupleBinding.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/NodeStateTupleBinding.java?rev=329242&r1=329241&r2=329242&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/NodeStateTupleBinding.java
(original)
+++ incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/NodeStateTupleBinding.java
Fri Oct 28 09:37:58 2005
@@ -16,21 +16,14 @@
  */
 package org.apache.jackrabbit.core.state.bdb;
 
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Set;
-
+import com.sleepycat.bind.tuple.TupleBinding;
+import com.sleepycat.bind.tuple.TupleInput;
+import com.sleepycat.bind.tuple.TupleOutput;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.jackrabbit.core.NodeId;
-import org.apache.jackrabbit.core.nodetype.NodeDefId;
 import org.apache.jackrabbit.core.state.NodeState;
-import org.apache.jackrabbit.name.QName;
-
-import com.sleepycat.bind.tuple.TupleBinding;
-import com.sleepycat.bind.tuple.TupleInput;
-import com.sleepycat.bind.tuple.TupleOutput;
+import org.apache.jackrabbit.core.state.util.Serializer;
 
 public class NodeStateTupleBinding extends TupleBinding {
 
@@ -49,86 +42,29 @@
 
         NodeState state = new NodeState(id.getUUID(), null, null, NodeState.STATUS_NEW, false);
 
-        // check uuid
-        String s = in.readString();
-        if (!state.getUUID().equals(s)) {
-            String msg = "invalid serialized state: uuid mismatch";
+        try {
+            Serializer.deserialize(state, in);
+        } catch (Exception e) {
+            // since the TupleInput methods do not throw any
+            // exceptions the above call should neither...
+            String msg = "error while deserializing node state";
             log.debug(msg);
-            throw new RuntimeException(msg);
-        }
-
-        // deserialize node state
-
-        // primaryType
-        s = in.readString();
-        state.setNodeTypeName(QName.valueOf(s));
-        // parentUUID
-        s = in.readString();
-        if (s.length() > 0) {
-            state.setParentUUID(s);
-        }
-        // definitionId
-        s = in.readString();
-        state.setDefinitionId(NodeDefId.valueOf(s));
-        // mixin types
-        int count = in.readInt(); // count
-        Set set = new HashSet(count);
-        for (int i = 0; i < count; i++) {
-            set.add(QName.valueOf(in.readString())); // name
-        }
-        if (set.size() > 0) {
-            state.setMixinTypeNames(set);
-        }
-        // properties (names)
-        count = in.readInt(); // count
-        for (int i = 0; i < count; i++) {
-            state.addPropertyName(QName.valueOf(in.readString())); // name
-        }
-        // child nodes (list of name/uuid pairs)
-        count = in.readInt(); // count
-        for (int i = 0; i < count; i++) {
-            QName name = QName.valueOf(in.readString()); // name
-            String s1 = in.readString(); // uuid
-            state.addChildNodeEntry(name, s1);
+            throw new RuntimeException(msg, e);
         }
 
         return state;
     }
 
     public void objectToEntry(Object o, TupleOutput out) {
-
-        NodeState state = (NodeState) o;
-
-        // uuid
-        out.writeString(state.getUUID());
-        // primaryType
-        out.writeString(state.getNodeTypeName().toString());
-        // parentUUID
-        out.writeString(state.getParentUUID() == null ? "" : state.getParentUUID());
-        // definitionId
-        out.writeString(state.getDefinitionId().toString());
-        // mixin types
-        Collection c = state.getMixinTypeNames();
-        out.writeInt(c.size()); // count
-        for (Iterator iter = c.iterator(); iter.hasNext();) {
-            out.writeString(iter.next().toString()); // name
-        }
-        // properties (names)
-        c = state.getPropertyNames();
-        out.writeInt(c.size()); // count
-        for (Iterator iter = c.iterator(); iter.hasNext();) {
-            QName propName = (QName) iter.next();
-            out.writeString(propName.toString()); // name
-        }
-        // child nodes (list of name/uuid pairs)
-        c = state.getChildNodeEntries();
-        out.writeInt(c.size()); // count
-        for (Iterator iter = c.iterator(); iter.hasNext();) {
-            NodeState.ChildNodeEntry entry = (NodeState.ChildNodeEntry) iter.next();
-            out.writeString(entry.getName().toString()); // name
-            out.writeString(entry.getUUID()); // uuid
+        try {
+            Serializer.serialize((NodeState) o, out);
+        } catch (Exception e) {
+            // since the TupleOutput methods do not throw any
+            // exceptions the above call should neither...
+            String msg = "error while serializing node state";
+            log.debug(msg);
+            throw new RuntimeException(msg, e);
         }
-
     }
 
 }

Modified: incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/PropertyStateTupleBinding.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/PropertyStateTupleBinding.java?rev=329242&r1=329241&r2=329242&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/PropertyStateTupleBinding.java
(original)
+++ incubator/jackrabbit/trunk/contrib/bdb-persistence/src/java/org/apache/jackrabbit/core/state/bdb/PropertyStateTupleBinding.java
Fri Oct 28 09:37:58 2005
@@ -16,23 +16,20 @@
  */
 package org.apache.jackrabbit.core.state.bdb;
 
-import java.io.IOException;
-import java.io.InputStream;
-
-import javax.jcr.PropertyType;
-
-import org.apache.jackrabbit.core.PropertyId;
-import org.apache.jackrabbit.core.nodetype.PropDefId;
-import org.apache.jackrabbit.core.state.PropertyState;
-import org.apache.jackrabbit.core.value.BLOBFileValue;
-import org.apache.jackrabbit.core.value.InternalValue;
-
 import com.sleepycat.bind.tuple.TupleBinding;
 import com.sleepycat.bind.tuple.TupleInput;
 import com.sleepycat.bind.tuple.TupleOutput;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.jackrabbit.core.PropertyId;
+import org.apache.jackrabbit.core.state.PropertyState;
+import org.apache.jackrabbit.core.state.util.BLOBStore;
+import org.apache.jackrabbit.core.state.util.Serializer;
 
 public class PropertyStateTupleBinding extends TupleBinding {
 
+    private Log log = LogFactory.getLog(PropertyStateTupleBinding.class);
+
     private BLOBStore blobStore;
     private PropertyId id;
 
@@ -46,91 +43,31 @@
     }
 
     public Object entryToObject(TupleInput in) {
-        try {
-            PropertyState state = new PropertyState(id.getName(), id.getParentUUID(), PropertyState.STATUS_NEW,
false);
 
-            // type
-            int type = in.readInt();
-            state.setType(type);
-            // multiValued
-            boolean multiValued = in.readBoolean();
-            state.setMultiValued(multiValued);
-            // definitionId
-            String s = in.readString();
-            state.setDefinitionId(PropDefId.valueOf(s));
-            // values
-            int count = in.readInt(); // count
-            InternalValue[] values = new InternalValue[count];
-            for (int i = 0; i < count; i++) {
-                InternalValue val;
-                if (type == PropertyType.BINARY) {
-                    s = in.readString(); // value (i.e. blobId)
-                    // special handling required for binary value:
-                    // the value stores the id of the blob resource in the blob store
-                    val = InternalValue.create(blobStore.get(s));
-                } else {
-                    int len = in.readInt(); // lenght of byte[]
-                    byte[] bytes = new byte[len];
-                    in.read(bytes); // byte[]
-                    s = new String(bytes, BerkeleyDBPersistenceManager.ENCODING);
-                    val = InternalValue.valueOf(s, type);
-                }
-                values[i] = val;
-            }
-            state.setValues(values);
+        PropertyState state = new PropertyState(id.getName(), id.getParentUUID(), PropertyState.STATUS_NEW,
false);
 
-            return state;
+        try {
+            Serializer.deserialize(state, in, blobStore);
         } catch (Exception e) {
-            throw new RuntimeException(e);
+            // since the TupleInput methods do not throw any
+            // exceptions the above call should neither...
+            String msg = "error while deserializing property state";
+            log.debug(msg);
+            throw new RuntimeException(msg, e);
         }
+
+        return state;
     }
 
     public void objectToEntry(Object o, TupleOutput out) {
         try {
-            PropertyState state = (PropertyState) o;
-
-            // type
-            out.writeInt(state.getType());
-            // multiValued
-            out.writeBoolean(state.isMultiValued());
-            // definitionId
-            out.writeString(state.getDefinitionId().toString());
-            // values
-            InternalValue[] values = state.getValues();
-            out.writeInt(values.length); // count
-            for (int i = 0; i < values.length; i++) {
-                InternalValue val = values[i];
-                if (state.getType() == PropertyType.BINARY) {
-                    // special handling required for binary value:
-                    // spool binary value to file in blob store
-                    BLOBFileValue blobVal = (BLOBFileValue) val.internalValue();
-                    InputStream in = blobVal.getStream();
-                    String blobId;
-                    try {
-                        blobId = blobStore.put((PropertyId) state.getId(), i, in, blobVal.getLength());
-                    } finally {
-                        try {
-                            in.close();
-                        } catch (IOException e) {
-                            // ignore
-                        }
-                    }
-                    // store id of blob as property value
-                    out.writeString(blobId); // value
-                    // replace value instance with value
-                    // backed by resource in blob store and delete temp file
-                    values[i] = InternalValue.create(blobStore.get(blobId));
-                    blobVal.discard();
-                    blobVal = null; // gc hint
-                } else {
-                    byte[] bytes = val.toString().getBytes(BerkeleyDBPersistenceManager.ENCODING);
-                    out.writeInt(bytes.length); // lenght of byte[]
-                    out.write(bytes); // byte[]
-                }
-            }
+            Serializer.serialize((PropertyState) o, out, blobStore);
         } catch (Exception e) {
-            throw new RuntimeException(e);
+            // since the TupleOutput methods do not throw any
+            // exceptions the above call should neither...
+            String msg = "error while serializing property state";
+            log.debug(msg);
+            throw new RuntimeException(msg, e);
         }
     }
-
 }



Mime
View raw message