You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by st...@apache.org on 2005/11/04 16:49:17 UTC

svn commit: r330828 - in /incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state: ChangeLog.java ItemState.java NodeState.java PropertyState.java db/SimpleDbPersistenceManager.java

Author: stefan
Date: Fri Nov  4 07:48:51 2005
New Revision: 330828

URL: http://svn.apache.org/viewcvs?rev=330828&view=rev
Log:
- fixed/tweaked ItemState Serializable support
- fixed minor issue in ChangeLog.merge
- tweaked SimpleDbPersistenceManager.store(PropertyState) and .store(NodeState) methods

Modified:
    incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ChangeLog.java
    incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ItemState.java
    incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/NodeState.java
    incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/PropertyState.java
    incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/db/SimpleDbPersistenceManager.java

Modified: incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ChangeLog.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ChangeLog.java?rev=330828&r1=330827&r2=330828&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ChangeLog.java (original)
+++ incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ChangeLog.java Fri Nov  4 07:48:51 2005
@@ -181,19 +181,19 @@
     }
 
     /**
-     * Merge another change log to this change log
+     * Merge another change log with this change log
      *
      * @param other other change log
      */
     public void merge(ChangeLog other) {
-        // Remove all states from our added set that have now been deleted
+        // Remove all states from our 'added' set that have now been deleted
         Iterator iter = other.deletedStates();
         while (iter.hasNext()) {
             ItemState state = (ItemState) iter.next();
             if (addedStates.remove(state.getId()) == null) {
                 deletedStates.put(state.getId(), state);
             }
-            // also remove from eventual modified state
+            // also remove from possibly modified state
             modifiedStates.remove(state.getId());
         }
 
@@ -203,6 +203,10 @@
             ItemState state = (ItemState) iter.next();
             if (!addedStates.containsKey(state.getId())) {
                 modifiedStates.put(state.getId(), state);
+            } else {
+                // adapt status and replace 'added'
+                state.setStatus(ItemState.STATUS_NEW);
+                addedStates.put(state.getId(), state);
             }
         }
 
@@ -210,8 +214,18 @@
         iter = other.addedStates();
         while (iter.hasNext()) {
             ItemState state = (ItemState) iter.next();
-            addedStates.put(state.getId(), state);
-            deletedStates.remove(state.getId());
+            ItemState deletedState = (ItemState) deletedStates.remove(state.getId());
+            if (deletedState != null) {
+                // the newly 'added' state had previously been deleted;
+                // merging those two operations results in a modified state
+
+                // adapt status/modCount and add to modified
+                state.setStatus(deletedState.getStatus());
+                state.setModCount(deletedState.getModCount());
+                modifiedStates.put(state.getId(), state);
+            } else {
+                addedStates.put(state.getId(), state);
+            }
         }
 
         // add refs

Modified: incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ItemState.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ItemState.java?rev=330828&r1=330827&r2=330828&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ItemState.java (original)
+++ incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/ItemState.java Fri Nov  4 07:48:51 2005
@@ -34,7 +34,7 @@
 public abstract class ItemState implements ItemStateListener, Serializable {
 
     /** Serialization UID of this class. */
-    static final long serialVersionUID = -337199573967324199L;
+    static final long serialVersionUID = 2017294661624942639L;
 
     private static Logger log = Logger.getLogger(ItemState.class);
 

Modified: incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/NodeState.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/NodeState.java?rev=330828&r1=330827&r2=330828&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/NodeState.java (original)
+++ incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/NodeState.java Fri Nov  4 07:48:51 2005
@@ -44,7 +44,7 @@
 public class NodeState extends ItemState {
 
     /** Serialization UID of this class. */
-    static final long serialVersionUID = -764076390011517389L;
+    static final long serialVersionUID = -4116945555530446652L;
 
     /** the uuid of this node */
     protected String uuid;
@@ -1041,25 +1041,22 @@
         private void writeObject(ObjectOutputStream out) throws IOException {
             // important: fields must be written in same order as they are
             // read in readObject(ObjectInputStream)
-            out.writeInt(size()); // count
+            out.writeShort(size()); // count
             for (Iterator iter = iterator(); iter.hasNext();) {
                 NodeState.ChildNodeEntry entry =
                         (NodeState.ChildNodeEntry) iter.next();
-                //out.writeObject(entry.getName());   // name
                 out.writeUTF(entry.getName().toString());   // name
                 out.writeUTF(entry.getUUID());  // uuid
             }
         }
 
-        private void readObject(ObjectInputStream in)
-                throws IOException, ClassNotFoundException {
+        private void readObject(ObjectInputStream in) throws IOException {
             entries = new LinkedMap();
             nameMap = new HashMap();
             // important: fields must be read in same order as they are
             // written in writeObject(ObjectOutputStream)
-            int count = in.readInt();   // count
+            short count = in.readShort();   // count
             for (int i = 0; i < count; i++) {
-                //QName name = (QName) in.readObject();    // name
                 QName name = QName.valueOf(in.readUTF());    // name
                 String s = in.readUTF();   // uuid
                 add(name, s);

Modified: incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/PropertyState.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/PropertyState.java?rev=330828&r1=330827&r2=330828&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/PropertyState.java (original)
+++ incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/PropertyState.java Fri Nov  4 07:48:51 2005
@@ -21,25 +21,23 @@
 import org.apache.jackrabbit.core.value.BLOBFileValue;
 import org.apache.jackrabbit.core.value.InternalValue;
 import org.apache.jackrabbit.name.QName;
-import org.apache.jackrabbit.util.Base64;
 
 import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
-import java.io.StringWriter;
 
 /**
  * <code>PropertyState</code> represents the state of a <code>Property</code>.
  */
 public class PropertyState extends ItemState {
 
-    /** Serialization UID of this class. */
-    static final long serialVersionUID = 8960688206140247183L;
+    /**
+     * Serialization UID of this class.
+     */
+    static final long serialVersionUID = 4569719974514326906L;
 
     protected QName name;
     protected InternalValue[] values;
@@ -194,14 +192,13 @@
     private void writeObject(ObjectOutputStream out) throws IOException {
         // important: fields must be written in same order as they are
         // read in readObject(ObjectInputStream)
-        //out.writeObject(name);
         out.writeUTF(name.toString());
         out.writeInt(type);
         out.writeBoolean(multiValued);
         if (values == null) {
-            out.writeObject(null);
+            out.writeShort(-1);
         } else {
-            String[] strings = new String[values.length];
+            out.writeShort(values.length);
             for (int i = 0; i < values.length; i++) {
                 InternalValue val = values[i];
                 try {
@@ -209,18 +206,18 @@
                         // special handling required for binary value
                         BLOBFileValue blob = (BLOBFileValue) val.internalValue();
                         InputStream in = blob.getStream();
-                        // use 32k initial buffer size as binary data is
-                        // probably not just a couple of bytes
-                        StringWriter writer = new StringWriter(32768);
+                        out.writeLong(blob.getLength());
+                        byte[] buf = new byte[0x2000];
                         try {
-                            Base64.encode(in, writer);
+                            int read;
+                            while ((read = in.read(buf)) > 0) {
+                                out.write(buf, 0, read);
+                            }
                         } finally {
                             in.close();
-                            writer.close();
                         }
-                        strings[i] = writer.toString();
                     } else {
-                        strings[i] = val.toString();
+                        out.writeUTF(val.toString());
                     }
                 } catch (IllegalStateException ise) {
                     throw new IOException(ise.getMessage());
@@ -228,34 +225,69 @@
                     throw new IOException(re.getMessage());
                 }
             }
-            out.writeObject(strings);
         }
     }
 
-    private void readObject(ObjectInputStream in)
-            throws IOException, ClassNotFoundException {
+    private void readObject(ObjectInputStream in) throws IOException {
         // important: fields must be read in same order as they are
         // written in writeObject(ObjectOutputStream)
-        //name = (QName) in.readObject();
         name = QName.valueOf(in.readUTF());
         type = in.readInt();
         multiValued = in.readBoolean();
-        Object obj = in.readObject();
-        if (obj == null) {
+        short count = in.readShort(); // # of values
+        if (count < 0) {
             values = null;
         } else {
-            String[] strings = (String[]) obj;
-            values = new InternalValue[strings.length];
-            for (int i = 0; i < strings.length; i++) {
-                String str = strings[i];
+            values = new InternalValue[count];
+            for (int i = 0; i < values.length; i++) {
                 if (type == PropertyType.BINARY) {
                     // special handling required for binary value
-                    ByteArrayOutputStream bos = new ByteArrayOutputStream(str.length());
-                    Base64.decode(str, bos);
-                    bos.close();
-                    values[i] = InternalValue.create(new ByteArrayInputStream(bos.toByteArray()));
+                    final long length = in.readLong();
+                    final InputStream stream = in;
+                    // create InputStream wrapper of size 'length'
+                    values[i] = InternalValue.create(new InputStream() {
+
+                        long consumed = 0;
+
+                        public int read() throws IOException {
+                            if (consumed >= length) {
+                                return -1;  // eof
+                            }
+                            int b = stream.read();
+                            consumed++;
+                            return b;
+                        }
+
+                        public int read(byte b[], int off, int len) throws IOException {
+                            if (consumed >= length) {
+                                return -1;  // eof
+                            }
+                            if ((consumed + len) > length) {
+                                len = (int) (length - consumed);
+                            }
+                            int read = super.read(b, off, len);
+                            consumed += read;
+                            return read;
+                        }
+
+                        public long skip(long n) throws IOException {
+                            if (consumed >= length && n > 0) {
+                                return -1;  // eof
+                            }
+                            if ((consumed + n) > length) {
+                                n = length - consumed;
+                            }
+                            long skipped = super.skip(n);
+                            consumed += skipped;
+                            return skipped;
+                        }
+
+                        public void close() {
+                            // nop
+                        }
+                    });
                 } else {
-                    values[i] = InternalValue.valueOf(str, type);
+                    values[i] = InternalValue.valueOf(in.readUTF(), type);
                 }
             }
         }

Modified: incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/db/SimpleDbPersistenceManager.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/db/SimpleDbPersistenceManager.java?rev=330828&r1=330827&r2=330828&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/db/SimpleDbPersistenceManager.java (original)
+++ incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/state/db/SimpleDbPersistenceManager.java Fri Nov  4 07:48:51 2005
@@ -29,6 +29,7 @@
 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.core.state.ItemState;
 import org.apache.jackrabbit.core.state.util.BLOBStore;
 import org.apache.jackrabbit.core.state.util.FileSystemBLOBStore;
 import org.apache.jackrabbit.core.state.util.Serializer;
@@ -526,7 +527,8 @@
         }
 
         // check if insert or update
-        boolean update = exists((NodeId) state.getId());
+        boolean update = state.getStatus() != ItemState.STATUS_NEW;
+        //boolean update = exists((NodeId) state.getId());
         PreparedStatement stmt = (update) ? nodeStateUpdate : nodeStateInsert;
 
         try {
@@ -569,7 +571,8 @@
         }
 
         // check if insert or update
-        boolean update = exists((PropertyId) state.getId());
+        boolean update = state.getStatus() != ItemState.STATUS_NEW;
+        //boolean update = exists((PropertyId) state.getId());
         PreparedStatement stmt = (update) ? propertyStateUpdate : propertyStateInsert;
 
         try {