You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by th...@apache.org on 2008/11/18 16:19:51 UTC

svn commit: r718632 - in /jackrabbit/trunk/jackrabbit-core/src: main/java/org/apache/jackrabbit/core/data/db/ main/java/org/apache/jackrabbit/core/persistence/bundle/util/ test/java/org/apache/jackrabbit/core/data/

Author: thomasm
Date: Tue Nov 18 07:19:51 2008
New Revision: 718632

URL: http://svn.apache.org/viewvc?rev=718632&view=rev
Log:
JCR-1864 Database Data Store: clean up the code

Added:
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/DataStoreTest.java   (with props)
Removed:
    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
    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/persistence/bundle/util/ConnectionRecoveryManager.java
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/TestAll.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=718632&r1=718631&r2=718632&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 Nov 18 07:19:51 2008
@@ -485,18 +485,17 @@
             putBack(conn);
         }
     }
-
+    
     /**
-     * getDatabaseResources() does NOT close the DB resources on success. It's up to the client of
-     * the stream backed by these resources to close it and therefore close the DB resources.
-     *
+     * Open the input stream. This method sets those fields of the caller
+     * that need to be closed once the input stream is read.
+     * 
+     * @param inputStream the database input stream object
      * @param identifier data identifier
-     * @return database resources that will back the stream corresponding
-     *                     to the passed data identifier
-     * @throws DataStoreException if the data store could not be accessed,
-     *                     or if the given identifier is invalid
-     */
-    public DbResources getDatabaseResources(DataIdentifier identifier) throws DataStoreException {
+     * @throws DataStoreException if the data store could not be accessed, 
+     *          or if the given identifier is invalid
+     */    
+    InputStream openStream(DbInputStream inputStream, DataIdentifier identifier) throws DataStoreException {
         ConnectionRecoveryManager conn = null;
         ResultSet rs = null;
         try {
@@ -508,32 +507,29 @@
                 throw new DataStoreException("Record not found: " + identifier);
             }
             InputStream stream = rs.getBinaryStream(2);
-            DbResources dbResource = null;
             if (stream == null) {
-                // If the stream is null, go ahead and close resources
                 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 {
                 stream = new BufferedInputStream(stream);
-                dbResource = new DbResources(conn, rs, stream, this);
+                inputStream.setConnection(conn);
+                inputStream.setResultSet(rs);
             }
-            return dbResource;
+            return stream;
         } catch (Exception e) {
             DatabaseHelper.closeSilently(rs);
             putBack(conn);
-            throw convert("Retrieving database resources ", e);
+            throw convert("Retrieving database resource ", e);
         }
     }
-
+    
     /**
      * {@inheritDoc}
      */

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=718632&r1=718631&r2=718632&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 Nov 18 07:19:51 2008
@@ -16,27 +16,35 @@
  */
 package org.apache.jackrabbit.core.data.db;
 
-import java.io.FilterInputStream;
+import java.io.EOFException;
 import java.io.IOException;
+import java.io.InputStream;
+import java.sql.ResultSet;
+
 import org.apache.jackrabbit.core.data.DataIdentifier;
 import org.apache.jackrabbit.core.data.DataStoreException;
+import org.apache.jackrabbit.core.persistence.bundle.util.ConnectionRecoveryManager;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * This class represents an input stream backed by a database. It allows the stream to be used by
- * keeping the DB resources open until the stream is closed. When the stream is finished or
- * close()d, then the resources are freed.
+ * This class represents an input stream backed by a database. It allows the
+ * stream to be used by keeping the database objects open until the stream is
+ * closed. When the stream is finished or closed, then the database objects are
+ * freed.
  */
-public class DbInputStream extends FilterInputStream {
+public class DbInputStream extends InputStream {
 
     private static Logger log = LoggerFactory.getLogger(DbInputStream.class);
 
-    protected DbResources resources;
-    protected boolean streamFinished;
-    protected boolean streamClosed;
     protected DbDataStore store;
     protected DataIdentifier identifier;
+    protected boolean endOfStream;
+    protected InputStream in;
+    
+    protected ConnectionRecoveryManager conn;
+    protected ResultSet rs;
+    
 
     /**
      * Create a database input stream for the given identifier.
@@ -46,39 +54,42 @@
      * @param identifier the data identifier
      */
     protected DbInputStream(DbDataStore store, DataIdentifier identifier) {
-        super(null);
-        streamFinished = false;
-        streamClosed = true;
         this.store = store;
         this.identifier = identifier;
     }
 
-    private void getStream() throws IOException {
-        try {
-            resources = store.getDatabaseResources(identifier);
-            in = resources.getInputStream();
-            streamClosed = false;
-        } catch (DataStoreException e) {
-            IOException e2 = new IOException(e.getMessage());
-            e2.initCause(e);
-            throw e2;
+    /**
+     * Open the stream if required.
+     * 
+     * @throws IOException
+     */
+    protected void openStream() throws IOException {
+        if (endOfStream) {
+            throw new EOFException();
+        }
+        if (in == null) {
+            try {
+                in = store.openStream(this, identifier);
+            } catch (DataStoreException e) {
+                IOException e2 = new IOException(e.getMessage());
+                e2.initCause(e);
+                throw e2;
+            }
         }
     }
 
     /**
      * {@inheritDoc}
-     * When the stream is consumed, the database resources held by the instance are closed.
+     * When the stream is consumed, the database objects held by the instance are closed.
      */
     public int read() throws IOException {
-        if (streamFinished) {
+        if (endOfStream) {
             return -1;
         }
-        if (in == null) {
-            getStream();
-        }
+        openStream();
         int c = in.read();
         if (c == -1) {
-            streamFinished = true;
+            endOfStream = true;
             close();
         }
         return c;
@@ -86,7 +97,7 @@
 
     /**
      * {@inheritDoc}
-     * When the stream is consumed, the database resources held by the instance are closed.
+     * When the stream is consumed, the database objects held by the instance are closed.
      */
     public int read(byte[] b) throws IOException {
         return read(b, 0, b.length);
@@ -94,18 +105,16 @@
 
     /**
      * {@inheritDoc}
-     * When the stream is consumed, the database resources held by the instance are closed.
+     * When the stream is consumed, the database objects held by the instance are closed.
      */
     public int read(byte[] b, int off, int len) throws IOException {
-        if (streamFinished) {
+        if (endOfStream) {
             return -1;
         }
-        if (in == null) {
-            getStream();
-        }
+        openStream();
         int c = in.read(b, off, len);
         if (c == -1) {
-            streamFinished = true;
+            endOfStream = true;
             close();
         }
         return c;
@@ -113,19 +122,24 @@
 
     /**
      * {@inheritDoc}
-     * When the stream is consumed, the database resources held by the instance are closed.
+     * When the stream is consumed, the database objects held by the instance are closed.
      */
     public void close() throws IOException {
-        if (!streamClosed) {
-            streamClosed = true;
-            // It may be null (see constructor)
-            if (in != null) {
-                in.close();
-                super.close();
+        if (in != null) {
+            in.close();
+            in = null;
+            // some additional database objects 
+            // may need to be closed
+            if (rs != null) {
+                DatabaseHelper.closeSilently(rs);
+                rs = null;
             }
-            // resources may be null (if getStream() was not called)
-            if (resources != null) {
-                resources.close();
+            if (conn != null) {
+                try {
+                    store.putBack(conn);
+                } catch (DataStoreException e) {
+                    log.info("Error closing DbResource", e);
+                }
             }
         }
     }
@@ -134,9 +148,10 @@
      * {@inheritDoc}
      */
     public long skip(long n) throws IOException {
-        if (in == null) {
-            getStream();
-        }
+        if (endOfStream) {
+            return -1;
+        }        
+        openStream();
         return in.skip(n);
     }
 
@@ -144,9 +159,10 @@
      * {@inheritDoc}
      */
     public int available() throws IOException {
-        if (in == null) {
-            getStream();
-        }
+        if (endOfStream) {
+            return 0;
+        }        
+        openStream();
         return in.available();
     }
 
@@ -154,12 +170,13 @@
      * {@inheritDoc}
      */
     public void mark(int readlimit) {
-        if (in == null) {
-            try {
-                getStream();
-            } catch (IOException e) {
-                log.info("Error getting underlying stream: ", e);
-            }
+        if (endOfStream) {
+            return;
+        } 
+        try {
+            openStream();
+        } catch (IOException e) {
+            log.info("Error getting underlying stream: ", e);
         }
         in.mark(readlimit);
     }
@@ -168,9 +185,10 @@
      * {@inheritDoc}
      */
     public void reset() throws IOException {
-        if (in == null) {
-            getStream();
-        }
+        if (endOfStream) {
+            throw new EOFException();
+        }         
+        openStream();
         in.reset();
     }
 
@@ -178,14 +196,36 @@
      * {@inheritDoc}
      */
     public boolean markSupported() {
-        if (in == null) {
-            try {
-                getStream();
-            } catch (IOException e) {
-                log.info("Error getting underlying stream: ", e);
-                return false;
-            }
+        if (endOfStream) {
+            return false;
+        }      
+        try {
+            openStream();
+        } catch (IOException e) {
+            log.info("Error getting underlying stream: ", e);
+            return false;
         }
         return in.markSupported();
     }
+
+    /**
+     * Set the database connection of this input stream. This object must be
+     * closed once the stream is closed.
+     * 
+     * @param conn the connection
+     */
+    void setConnection(ConnectionRecoveryManager conn) {
+        this.conn = conn;
+    }
+
+    /**
+     * Set the result set of this input stream. This object must be closed once
+     * the stream is closed.
+     * 
+     * @param rs the result set
+     */
+    void setResultSet(ResultSet rs) {
+        this.rs = rs;
+    }
+
 }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionRecoveryManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionRecoveryManager.java?rev=718632&r1=718631&r2=718632&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionRecoveryManager.java (original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/bundle/util/ConnectionRecoveryManager.java Tue Nov 18 07:19:51 2008
@@ -479,13 +479,4 @@
         }
     }
 
-    public void closeSilently(ResultSet rs) {
-        if (rs != null) {
-            try {
-                rs.close();
-            } catch (SQLException e) {
-                // ignore
-            }
-        }
-    }
 }

Added: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/DataStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/DataStoreTest.java?rev=718632&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/DataStoreTest.java (added)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/DataStoreTest.java Tue Nov 18 07:19:51 2008
@@ -0,0 +1,210 @@
+/*
+ * 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.data;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.jackrabbit.core.data.db.DbDataStore;
+import org.apache.jackrabbit.test.JUnitTest;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Random;
+
+/**
+ * Tests for the data store.
+ * Both file data store and database data store are tested,
+ * with single threaded and multi-threaded tests.
+ */
+public class DataStoreTest extends JUnitTest {
+    
+    private static final boolean TEST_DATABASE = false;
+    private File testDir = new File(System.getProperty("java.io.tmpdir"), "dataStore");
+    
+    public void setUp() {
+        testDir.mkdirs();
+    }
+    
+    public void tearDown() throws IOException {
+        FileUtils.deleteDirectory(testDir);
+    }
+    
+    public void test() throws Exception {
+        try {
+            
+            if (TEST_DATABASE) {
+                DbDataStore dds = new DbDataStore();
+                String dbPath = (testDir + "/db").replace('\\', '/');
+                
+                // 3 sec
+                String url = "jdbc:h2:mem:" + dbPath + "/db";
+                
+                // 4 sec
+                // String url = "jdbc:h2:" + dbPath + "/db";
+                
+                // 26 sec
+                // String url = "jdbc:derby:" + dbPath + "/db";
+                
+                new File(dbPath).mkdirs();
+                dds.setUrl(url + ";create=true");
+                dds.setUser("sa");
+                dds.setPassword("sa");
+                dds.setCopyWhenReading(false);
+                dds.init(dbPath);
+                // doTest(dds, 0);
+                doTestMultiThreaded(dds, 4);
+                dds.close();
+                shutdownDatabase(url);
+                
+                FileUtils.deleteDirectory(testDir);
+                new File(dbPath).mkdirs();
+                dds = new DbDataStore();
+                dds.setUrl(url + ";create=true");
+                dds.setUser("sa");
+                dds.setPassword("sa");
+                dds.setCopyWhenReading(true);
+                dds.init(dbPath);
+                // doTest(dds, 0);
+                doTestMultiThreaded(dds, 4);
+                dds.close();
+                shutdownDatabase(url);
+            }
+
+            FileDataStore fds = new FileDataStore();
+            fds.init(testDir + "/file");
+            doTest(fds, 0);
+            // doTestMultiThreaded(fds, 4);
+            fds.close();
+            
+        } catch (Throwable t) {
+            t.printStackTrace();
+            throw new Error(t);
+        }
+    }
+    
+    private void shutdownDatabase(String url) {
+        if (url.startsWith("jdbc:derby:") || url.startsWith("jdbc:hsqldb:")) {
+            try {
+                DriverManager.getConnection(url + ";shutdown=true");
+            } catch (SQLException e) {
+                // ignore
+            }
+        }
+    }
+    
+    private void doTestMultiThreaded(final DataStore ds, int threadCount) throws Exception {
+        final Exception[] exception = new Exception[1];
+        Thread[] threads = new Thread[threadCount];
+        for (int i = 0; i < threadCount; i++) {
+            final int x = i;
+            Thread t = new Thread() {
+                public void run() {
+                    try {
+                        doTest(ds, x);
+                    } catch (Exception e) {
+                        exception[0] = e;
+                    }
+                }
+            };
+            threads[i] = t;
+            t.start();
+        }
+        for (int i = 0; i < threadCount; i++) {
+            threads[i].join();
+        }
+        if (exception[0] != null) {
+            throw exception[0];
+        }
+    }
+    
+    private void doTest(DataStore ds, int offset) throws Exception {
+        ArrayList list = new ArrayList();
+        HashMap map = new HashMap();
+        for (int i = 0; i < 100; i++) {
+            int size = 100 + i * 10;
+            RandomInputStream in = new RandomInputStream(size + offset, size);
+            DataRecord rec = ds.addRecord(in);
+            list.add(rec);
+            map.put(rec, new Integer(size));
+        }
+        Random random = new Random(1);
+        for (int i = 0; i < list.size(); i++) {
+            int pos = random.nextInt(list.size());
+            DataRecord rec = (DataRecord) list.get(pos);
+            int size = ((Integer) map.get(rec)).intValue();
+            rec = ds.getRecord(rec.getIdentifier());
+            assertEquals(size, rec.getLength());
+            InputStream in = rec.getStream();
+            RandomInputStream expected = new RandomInputStream(size + offset, size);
+            if (random.nextBoolean()) {
+                in = readInputStreamRandomly(in, random);
+            }
+            assertEquals(expected, in);
+            in.close();
+        }
+    }
+    
+    InputStream readInputStreamRandomly(InputStream in, Random random) throws IOException {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        byte[] buffer = new byte[8000];
+        while (true) {
+            if (random.nextBoolean()) {
+                int x = in.read();
+                if (x < 0) {
+                    break;
+                }
+                out.write(x);
+            } else {
+                if (random.nextBoolean()) {
+                    int l = in.read(buffer);
+                    if (l < 0) {
+                        break;
+                    }
+                    out.write(buffer, 0, l);
+                } else {
+                    int offset = random.nextInt(buffer.length / 2);
+                    int len = random.nextInt(buffer.length / 2);
+                    int l = in.read(buffer, offset, len);
+                    if (l < 0) {
+                        break;
+                    }
+                    out.write(buffer, offset, l);
+                }
+            }
+        }
+        in.close();
+        return new ByteArrayInputStream(out.toByteArray());
+    }
+    
+    void assertEquals(InputStream a, InputStream b) throws IOException {
+        while (true) {
+            int ai = a.read();
+            int bi = b.read();
+            assertEquals(ai, bi);
+            if (ai < 0) {
+                break;
+            }
+        }
+    }
+
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/DataStoreTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/DataStoreTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/TestAll.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/TestAll.java?rev=718632&r1=718631&r2=718632&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/TestAll.java (original)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/TestAll.java Tue Nov 18 07:19:51 2008
@@ -32,6 +32,7 @@
      */
     public static Test suite() {
         TestSuite suite = new TestSuite("Data tests");
+        suite.addTestSuite(DataStoreTest.class);
         suite.addTestSuite(NodeTypeTest.class);
         suite.addTestSuite(ExportImportTest.class);
         suite.addTestSuite(GarbageCollectorTest.class);