jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r708598 - in /jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db: DbDataStore.java DbInputStream.java DbResources.java
Date Tue, 28 Oct 2008 15:42:51 GMT
Author: thomasm
Date: Tue Oct 28 08:42:51 2008
New Revision: 708598

URL: http://svn.apache.org/viewvc?rev=708598&view=rev
Log:
JCR-1825 DbDataStore doesn't support concurrent reads

Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbDataStore.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbInputStream.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbResources.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbDataStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbDataStore.java?rev=708598&r1=708597&r2=708598&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbDataStore.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbDataStore.java
Tue Oct 28 08:42:51 2008
@@ -62,7 +62,7 @@
  *     <param name="{@link #setDriver(String) driver}" value="org.postgresql.Driver"/>
  *     <param name="{@link #setMinRecordLength(int) minRecordLength}" value="1024"/>
  *     <param name="{@link #setMaxConnections(int) maxConnections}" value="2"/>
- *     <param name="{@link #setCopyWhenReading(int) copyWhenReading}" value="true"/>
+ *     <param name="{@link #setCopyWhenReading(boolean) copyWhenReading}" value="true"/>
  *     <param name="{@link #setTablePrefix(int) tablePrefix}" value=""/>
  * &lt/DataStore>
  * </pre>
@@ -93,24 +93,45 @@
 public class DbDataStore implements DataStore {
 
     /**
-     * The digest algorithm used to uniquely identify records.
+     * The default value for the minimum object size.
      */
-    protected static final String DIGEST = "SHA-1";
+    public static final int DEFAULT_MIN_RECORD_LENGTH = 100;
 
     /**
-     * Logger instance
+     * The default value for the maximum connections.
      */
-    private static Logger log = LoggerFactory.getLogger(DbDataStore.class);
+    public static final int DEFAULT_MAX_CONNECTIONS = 3;
+    
+    /**
+     * Write to a temporary file to get the length (slow, but always works).
+     * This is the default setting.
+     */
+    public static final String STORE_TEMP_FILE = "tempFile";
 
     /**
-     * The default value for the minimum object size.
+     * Call PreparedStatement.setBinaryStream(..., -1)
      */
-    public static final int DEFAULT_MIN_RECORD_LENGTH = 100;
+    public static final String STORE_SIZE_MINUS_ONE = "-1";
 
     /**
-     * The default value for the maximum connections.
+     * Call PreparedStatement.setBinaryStream(..., Integer.MAX_VALUE)
      */
-    public static final int DEFAULT_MAX_CONNECTIONS = 3;
+    public static final String STORE_SIZE_MAX = "max";
+    
+    /**
+     * The digest algorithm used to uniquely identify records.
+     */
+    protected static final String DIGEST = "SHA-1";
+    
+    /**
+     * The prefix used for temporary objects.
+     */
+    protected static final String TEMP_PREFIX = "TEMP_";
+
+    /**
+     * Logger instance
+     */
+    private static Logger log = LoggerFactory.getLogger(DbDataStore.class);
 
     /**
      * The minimum modified date. If a file is accessed (read or write) with a modified date
@@ -159,11 +180,6 @@
     protected Pool connectionPool;
 
     /**
-     * The prefix used for temporary objects.
-     */
-    protected static final String TEMP_PREFIX = "TEMP_";
-
-    /**
      * The prefix for the datastore table, empty by default.
      */
     protected String tablePrefix = "";
@@ -251,26 +267,10 @@
     protected String storeStream = STORE_TEMP_FILE;
 
     /**
-     * Write to a temporary file to get the length (slow, but always works).
-     * This is the default setting.
-     */
-    public static final String STORE_TEMP_FILE = "tempFile";
-
-    /**
-     * Call PreparedStatement.setBinaryStream(..., -1)
-     */
-    public static final String STORE_SIZE_MINUS_ONE = "-1";
-
-    /**
-     * Call PreparedStatement.setBinaryStream(..., Integer.MAX_VALUE)
-     */
-    public static final String STORE_SIZE_MAX = "max";
-
-    /**
      * Copy the stream to a temp file before returning it.
      * Enabled by default to support concurrent reads.
      */
-    private boolean copyWhenReading = true;
+    protected boolean copyWhenReading = true;
 
     /**
      * All data identifiers that are currently in use are in this set until they are garbage
collected.
@@ -366,7 +366,7 @@
         } catch (Exception e) {
             throw convert("Can not insert new record", e);
         } finally {
-            conn.closeSilently(rs);
+            DatabaseHelper.closeSilently(rs);
             putBack(conn);
             if (fileInput != null) {
                 try {
@@ -437,7 +437,7 @@
         } catch (Exception e) {
             throw convert("Can not read records", e);
         } finally {
-            conn.closeSilently(rs);
+            DatabaseHelper.closeSilently(rs);
             putBack(conn);
         }
     }
@@ -481,7 +481,7 @@
         } catch (Exception e) {
             throw convert("Can not read identifier " + identifier, e);
         } finally {
-            conn.closeSilently(rs);
+            DatabaseHelper.closeSilently(rs);
             putBack(conn);
         }
     }
@@ -507,23 +507,26 @@
             if (!rs.next()) {
                 throw new DataStoreException("Record not found: " + identifier);
             }
-            InputStream result = null;
             InputStream stream = rs.getBinaryStream(2);
+            DbResources dbResource = null;
             if (stream == null) {
                 // If the stream is null, go ahead and close resources
-                result = new ByteArrayInputStream(new byte[0]);
+                stream = new ByteArrayInputStream(new byte[0]);
+                dbResource = new DbResources(stream);
+                DatabaseHelper.closeSilently(rs);
+                putBack(conn);
+            } else if (copyWhenReading) {
+                // If we copy while reading, create a temp file and close the stream
+                File temp = moveToTempFile(stream);
+                stream = new TempFileInputStream(temp);
+                dbResource = new DbResources(stream);
                 DatabaseHelper.closeSilently(rs);
                 putBack(conn);
             } else {
-                result = new BufferedInputStream(stream);
-                if (copyWhenReading) {
-                    File temp = moveToTempFile(result);
-                    result = new TempFileInputStream(temp);
-                }
+                stream = new BufferedInputStream(stream);
+                dbResource = new DbResources(conn, rs, stream, this);
             }
-
-            DbResources dbResources = new DbResources(conn, rs, prep, result, this);
-            return dbResources;
+            return dbResource;
         } catch (Exception e) {
             DatabaseHelper.closeSilently(rs);
             putBack(conn);

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbInputStream.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbInputStream.java?rev=708598&r1=708597&r2=708598&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbInputStream.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbInputStream.java
Tue Oct 28 08:42:51 2008
@@ -39,10 +39,11 @@
     protected DataIdentifier identifier;
 
     /**
-     * @param in the stream obtained by a call to ResultSet.getBinaryStream().
-     * @param con the connection to the DB. It must not be closed.
-     * @param rs the result set from wich the stream is obtained. It must not be closed.
-     * @param stmt the statemen that produced the result set. It must not be closed.
+     * Create a database input stream for the given identifier.
+     * Database access is delayed until the first byte is read from the stream.
+     * 
+     * @param store the database data store
+     * @param identifier the data identifier
      */
     protected DbInputStream(DbDataStore store, DataIdentifier identifier) {
         super(null);
@@ -88,15 +89,7 @@
      * When the stream is consumed, the database resources held by the instance are closed.
      */
     public int read(byte[] b) throws IOException {
-        if (streamFinished) {
-            return -1;
-        }
-        int c = read(b, 0, b.length);
-        if (c == -1) {
-            streamFinished = true;
-            close();
-        }
-        return c;
+        return read(b, 0, b.length);
     }
 
     /**
@@ -127,6 +120,7 @@
             streamClosed = true;
             // It may be null (see constructor)
             if (in != null) {
+                in.close();
                 super.close();
             }
             // resources may be null (if getStream() was not called)

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbResources.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbResources.java?rev=708598&r1=708597&r2=708598&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbResources.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/db/DbResources.java
Tue Oct 28 08:42:51 2008
@@ -18,7 +18,6 @@
 
 import java.io.InputStream;
 import java.sql.ResultSet;
-import java.sql.Statement;
 
 import org.apache.jackrabbit.core.persistence.bundle.util.ConnectionRecoveryManager;
 import org.slf4j.Logger;
@@ -33,36 +32,52 @@
 
     protected final ConnectionRecoveryManager conn;
     protected final ResultSet rs;
-    protected final Statement stmt;
     protected final InputStream in;
     protected final DbDataStore store;
     protected boolean closed;
-
-    public DbResources(ConnectionRecoveryManager conn, ResultSet rs, Statement stmt, InputStream
in, DbDataStore store) {
+    
+    /**
+     * Construct a db resource using the specified input stream.
+     * 
+     * @param in the input stream
+     */
+    public DbResources(InputStream in) {
+        this(null, null, in, null);
+    }
+
+    /**
+     * Construct a db resource using the specified connection. The connection
+     * will be returned to the data store once the resource is fully read. If
+     * the connection is null, then this class is just a container for the input
+     * stream. This is to support other kinds of input streams as well.
+     * 
+     * @param conn the connection (may be null)
+     * @param rs the result set (may be null)
+     * @param in the input stream
+     * @param store the data store
+     */
+    public DbResources(ConnectionRecoveryManager conn, ResultSet rs, InputStream in, DbDataStore
store) {
         this.conn = conn;
         this.rs = rs;
-        this.stmt = stmt;
         this.in = in;
         this.store = store;
-        this.closed = false;
-    }
-
-    public ConnectionRecoveryManager getConnection() {
-        return conn;
+        if (conn == null) {
+            closed = true;
+        }
     }
 
+    /**
+     * Get the input stream.
+     * 
+     * @return the input stream
+     */
     public InputStream getInputStream() {
         return in;
     }
 
-    public ResultSet getResultSet() {
-        return rs;
-    }
-
-    public Statement getStatement() {
-        return stmt;
-    }
-
+    /**
+     * Close the stream, and return the connection to the data store.
+     */
     public void close() {
         if (!closed) {
             closed = true;



Mime
View raw message