jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1039408 - in /jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence: bundle/BundleFsPersistenceManager.java pool/BundleDbPersistenceManager.java pool/PostgreSQLPersistenceManager.java util/BundleReader.java
Date Fri, 26 Nov 2010 15:19:33 GMT
Author: jukka
Date: Fri Nov 26 15:19:33 2010
New Revision: 1039408

URL: http://svn.apache.org/viewvc?rev=1039408&view=rev
Log:
JCR-2821: Consistency check/fix doesn't work with PSQL persistence manager

Unify bundle loading to automatically choose .getBlob().getBinaryStream() or .getBinaryStream()
depending on the type of the result set.
Move bundle size tracking inside BundleReader.

Fix based on a patch by Omid Milani

Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/BundleFsPersistenceManager.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/BundleDbPersistenceManager.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/PostgreSQLPersistenceManager.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleReader.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/BundleFsPersistenceManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/BundleFsPersistenceManager.java?rev=1039408&r1=1039407&r2=1039408&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/BundleFsPersistenceManager.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/BundleFsPersistenceManager.java
Fri Nov 26 15:19:33 2010
@@ -30,7 +30,6 @@ import org.apache.jackrabbit.core.persis
 import org.apache.jackrabbit.core.persistence.util.FileSystemBLOBStore;
 import org.apache.jackrabbit.core.persistence.util.NodePropBundle;
 import org.apache.jackrabbit.core.persistence.util.Serializer;
-import org.apache.jackrabbit.core.util.TrackingInputStream;
 import org.apache.jackrabbit.core.id.NodeId;
 import org.apache.jackrabbit.core.id.PropertyId;
 import org.apache.jackrabbit.core.state.ItemStateException;
@@ -239,10 +238,7 @@ public class BundleFsPersistenceManager 
             }
             InputStream in = itemFs.getInputStream(path);
             try {
-                TrackingInputStream cin = new TrackingInputStream(in);
-                NodePropBundle bundle = binding.readBundle(cin, id);
-                bundle.setSize(cin.getPosition());
-                return bundle;
+                return binding.readBundle(in, id);
             } finally {
                 IOUtils.closeQuietly(in);
             }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/BundleDbPersistenceManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/BundleDbPersistenceManager.java?rev=1039408&r1=1039407&r2=1039408&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/BundleDbPersistenceManager.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/BundleDbPersistenceManager.java
Fri Nov 26 15:19:33 2010
@@ -25,8 +25,8 @@ import java.io.InputStream;
 import java.sql.Blob;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.sql.Types;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 
@@ -41,11 +41,11 @@ import org.apache.jackrabbit.core.id.Nod
 import org.apache.jackrabbit.core.id.PropertyId;
 import org.apache.jackrabbit.core.persistence.PMContext;
 import org.apache.jackrabbit.core.persistence.bundle.AbstractBundlePersistenceManager;
+import org.apache.jackrabbit.core.persistence.util.BLOBStore;
 import org.apache.jackrabbit.core.persistence.util.BundleBinding;
 import org.apache.jackrabbit.core.persistence.util.ErrorHandling;
-import org.apache.jackrabbit.core.persistence.util.NodePropBundle;
-import org.apache.jackrabbit.core.persistence.util.BLOBStore;
 import org.apache.jackrabbit.core.persistence.util.FileSystemBLOBStore;
+import org.apache.jackrabbit.core.persistence.util.NodePropBundle;
 import org.apache.jackrabbit.core.persistence.util.Serializer;
 import org.apache.jackrabbit.core.state.ChangeLog;
 import org.apache.jackrabbit.core.state.ItemStateException;
@@ -796,27 +796,20 @@ public class BundleDbPersistenceManager
 
                     // issuing 2nd statement to circumvent issue JCR-1474
                     ResultSet bRs = null;
-                    byte[] data = null;
                     try {
                         bRs = conHelper.exec(bundleSelectSQL, getKey(id), false, 0);
                         if (!bRs.next()) {
                             throw new SQLException("bundle cannot be retrieved?");
                         }
-                        Blob blob = bRs.getBlob(1);
-                        data = getBytes(blob);
+                        // parse and check bundle
+                        NodePropBundle bundle = readBundle(id, bRs, 1);
+                        checkBundleConsistency(id, bundle, fix, modifications);
+                    } catch (SQLException e) {
+                        log.error("Unable to parse bundle " + id, e);
                     } finally {
                         DbUtility.close(bRs);
                     }
 
-                    try {
-                        // parse and check bundle
-                        NodePropBundle bundle = binding.readBundle(
-                                new ByteArrayInputStream(data), id);
-                        checkBundleConsistency(id, bundle, fix, modifications);
-                    } catch (IOException e) {
-                        log.error("Unable to parse bundle " + id
-                                + ": " + Arrays.toString(data), e);
-                    }
                     count++;
                     if (count % 1000 == 0) {
                         log.info(name + ": checked " + count + "/" + total + " bundles...");
@@ -1021,55 +1014,57 @@ public class BundleDbPersistenceManager
     /**
      * {@inheritDoc}
      */
+    @Override
     protected NodePropBundle loadBundle(NodeId id) throws ItemStateException {
-        ResultSet rs = null;
         try {
-            rs = conHelper.exec(bundleSelectSQL, getKey(id), false, 0);
-            if (!rs.next()) {
-                return null;
-            }
-            byte[] bytes = getBytes(rs.getBlob(1));
-
+            ResultSet rs =
+                conHelper.exec(bundleSelectSQL, getKey(id), false, 0);
             try {
-                NodePropBundle bundle =
-                    binding.readBundle(new ByteArrayInputStream(bytes), id);
-                bundle.setSize(bytes.length);
-                return bundle;
-            } catch (IOException e) {
-                log.error("Unable to parse serialization of bundle " + id
-                        + ": " + Arrays.toString(bytes), e);
-                throw e;
+                if (rs.next()) {
+                    return readBundle(id, rs, 1);
+                } else {
+                    return null;
+                }
+            } finally {
+                rs.close();
             }
-        } catch (Exception e) {
+        } catch (SQLException e) {
             String msg = "failed to read bundle: " + id + ": " + e;
             log.error(msg);
             throw new ItemStateException(msg, e);
-        } finally {
-            DbUtility.close(rs);
         }
     }
 
     /**
-     * Reads the blob's bytes and returns it. this is a helper method to
-     * circumvent issue JCR-1039 and JCR-1474
-     * @param blob blob to read
-     * @return bytes of the blob
-     * @throws SQLException if an SQL error occurs
-     * @throws IOException if an I/O error occurs
+     * Reads and parses a bundle from the BLOB in the given column of the
+     * current row of the given result set. This is a helper method to
+     * circumvent issues JCR-1039 and JCR-1474.
+     *
+     * @param id bundle identifier
+     * @param rs result set
+     * @param column BLOB column
+     * @return parsed bundle
+     * @throws SQLException if the bundle can not be read or parsed
      */
-    private byte[] getBytes(Blob blob) throws SQLException, IOException {
-        InputStream in = null;
+    private NodePropBundle readBundle(NodeId id, ResultSet rs, int column)
+            throws SQLException {
         try {
-            long length = blob.length();
-            byte[] bytes = new byte[(int) length];
-            in = blob.getBinaryStream();
-            int read, pos = 0;
-            while ((read = in.read(bytes, pos, bytes.length - pos)) > 0) {
-                pos += read;
+            InputStream in;
+            if (rs.getMetaData().getColumnType(column) == Types.BLOB) {
+                in = rs.getBlob(column).getBinaryStream();
+            } else {
+                in = rs.getBinaryStream(column);
             }
-            return bytes;
-        } finally {
-            IOUtils.closeQuietly(in);
+            try {
+                return binding.readBundle(in, id);
+            } finally {
+                in.close();
+            }
+        } catch (IOException e) {
+            SQLException exception =
+                new SQLException("Failed to parse bundle " + id);
+            exception.initCause(e);
+            throw exception;
         }
     }
 

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/PostgreSQLPersistenceManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/PostgreSQLPersistenceManager.java?rev=1039408&r1=1039407&r2=1039408&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/PostgreSQLPersistenceManager.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/pool/PostgreSQLPersistenceManager.java
Fri Nov 26 15:19:33 2010
@@ -16,18 +16,9 @@
  */
 package org.apache.jackrabbit.core.persistence.pool;
 
-import java.io.InputStream;
-import java.sql.ResultSet;
 import java.sql.SQLException;
 
-import org.apache.jackrabbit.core.id.NodeId;
 import org.apache.jackrabbit.core.persistence.PMContext;
-import org.apache.jackrabbit.core.persistence.util.NodePropBundle;
-import org.apache.jackrabbit.core.state.ItemStateException;
-import org.apache.jackrabbit.core.util.TrackingInputStream;
-import org.apache.jackrabbit.core.util.db.DbUtility;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Extends the {@link BundleDbPersistenceManager} by PostgreSQL specific code.
@@ -49,12 +40,6 @@ import org.slf4j.LoggerFactory;
 public class PostgreSQLPersistenceManager extends BundleDbPersistenceManager {
 
     /**
-     * Logger instance.
-     */
-    private static Logger log =
-        LoggerFactory.getLogger(PostgreSQLPersistenceManager.class);
-
-    /**
      * {@inheritDoc}
      */
     public void init(PMContext context) throws Exception {
@@ -85,36 +70,4 @@ public class PostgreSQLPersistenceManage
         return SM_LONGLONG_KEYS;
     }
 
-    /**
-     * PostgreSQL needs slightly different handling of the binary value that is received:
-     * rs.getBinaryStream vs rs.getBlob in the super class.
-     * 
-     * {@inheritDoc}
-     */
-    @Override
-    protected NodePropBundle loadBundle(NodeId id) throws ItemStateException {
-        ResultSet rs = null;
-        try {
-            rs = conHelper.exec(bundleSelectSQL, getKey(id), false, 0);
-            if (rs.next()) {
-                InputStream input = rs.getBinaryStream(1);
-                try {
-                    TrackingInputStream cin = new TrackingInputStream(input);
-                    NodePropBundle bundle = binding.readBundle(cin, id);
-                    bundle.setSize(cin.getPosition());
-                    return bundle;
-                } finally {
-                    input.close();
-                }
-            } else {
-                return null;
-            }
-        } catch (Exception e) {
-            String msg = "failed to read bundle: " + id + ": " + e;
-            log.error(msg);
-            throw new ItemStateException(msg, e);
-        } finally {
-           DbUtility.close(rs);
-        }
-    }
 }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleReader.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleReader.java?rev=1039408&r1=1039407&r2=1039408&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleReader.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleReader.java
Fri Nov 26 15:19:33 2010
@@ -18,6 +18,7 @@ package org.apache.jackrabbit.core.persi
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.apache.commons.io.input.CountingInputStream;
 import org.apache.jackrabbit.core.id.NodeId;
 import org.apache.jackrabbit.core.id.PropertyId;
 import org.apache.jackrabbit.core.value.InternalValue;
@@ -89,6 +90,14 @@ class BundleReader {
 
     private final BundleBinding binding;
 
+    /**
+     * Counter for the number of bytes read from the input stream.
+     */
+    private final CountingInputStream cin;
+
+    /**
+     * Wrapper for reading structured data from the input stream.
+     */
     private final DataInputStream in;
 
     private final int version;
@@ -112,7 +121,8 @@ class BundleReader {
     public BundleReader(BundleBinding binding, InputStream stream)
             throws IOException {
         this.binding = binding;
-        this.in = new DataInputStream(stream);
+        this.cin = new CountingInputStream(stream);
+        this.in = new DataInputStream(cin);
         this.version = in.readUnsignedByte();
     }
 
@@ -124,12 +134,14 @@ class BundleReader {
      * @throws IOException if an I/O error occurs.
      */
     public NodePropBundle readBundle(NodeId id) throws IOException {
+        long start = cin.getByteCount();
         NodePropBundle bundle = new NodePropBundle(id);
         if (version >= BundleBinding.VERSION_3) {
             readBundleNew(bundle);
         } else {
             readBundleOld(bundle);
         }
+        bundle.setSize(cin.getByteCount() - start);
         return bundle;
     }
 



Mime
View raw message