Author: reschke
Date: Wed Apr 10 14:44:16 2019
New Revision: 1857253

URL: http://svn.apache.org/viewvc?rev=1857253&view=rev
Log:
OAK-8201: RDBDocumentStore in ReadOnly mode should never modify persistence

Modified:
    
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
    
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStoreSchemaUpgradeTest.java

Modified: 
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java?rev=1857253&r1=1857252&r2=1857253&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
 Wed Apr 10 14:44:16 2019
@@ -937,9 +937,11 @@ public class RDBDocumentStore implements
 
     private DocumentStoreStatsCollector stats;
 
+    private boolean readOnly;
+
     // VERSION column mapping in queries used by RDBVersionGCSupport
     public static String VERSIONPROP = "__version";
-    
+
     // set of supported indexed properties
     private static final Set<String> INDEXEDPROPERTIES = new 
HashSet<String>(Arrays.asList(new String[] { MODIFIED,
             NodeDocument.HAS_BINARY_FLAG, NodeDocument.DELETED_ONCE, 
NodeDocument.SD_TYPE, NodeDocument.SD_MAX_REV_TIME_IN_SECS, VERSIONPROP }));
@@ -967,6 +969,8 @@ public class RDBDocumentStore implements
 
         this.callStack = LOG.isDebugEnabled() ? new Exception("call stack of 
RDBDocumentStore creation") : null;
 
+        this.readOnly = builder.getReadOnlyMode();
+
         this.ch = new RDBConnectionHandler(ds);
         Connection con = this.ch.getRWConnection();
         String catalog = con.getCatalog();
@@ -1302,16 +1306,21 @@ public class RDBDocumentStore implements
             closeResultSet(checkResultSet);
             boolean dbWasChanged = false;
 
-            if (!hasVersionColumn && upgradeToSchema >= 1) {
-                dbWasChanged |= upgradeTable(con, tableName, 1);
+            if (this.readOnly) {
+                LOG.debug("Skipping table update code because store is 
initialized in readOnly mode");
             }
+            else {
+                if (!hasVersionColumn && upgradeToSchema >= 1) {
+                    dbWasChanged |= upgradeTable(con, tableName, 1);
+                }
 
-            if (!hasSDTypeColumn && upgradeToSchema >= 2) {
-                dbWasChanged |= upgradeTable(con, tableName, 2);
-            }
+                if (!hasSDTypeColumn && upgradeToSchema >= 2) {
+                    dbWasChanged |= upgradeTable(con, tableName, 2);
+                }
 
-            if (!indexOn.contains("MODIFIED") && col == Collection.NODES) {
-                dbWasChanged |= addModifiedIndex(con, tableName);
+                if (!indexOn.contains("MODIFIED") && col == Collection.NODES) {
+                    dbWasChanged |= addModifiedIndex(con, tableName);
+                }
             }
 
             tablesPresent.add(tableName);
@@ -1323,6 +1332,12 @@ public class RDBDocumentStore implements
             // table does not appear to exist
             con.rollback();
 
+            LOG.debug("trying to read from '" + tableName + "'", ex);
+            if (this.readOnly) {
+                throw new SQLException("Would like to create table '" + 
tableName
+                        + "', but RDBDocumentStore has been initialized in 
'readonly' mode");
+            }
+
             try {
                 creatStatement = con.createStatement();
                 
creatStatement.execute(this.dbInfo.getTableCreationStatement(tableName, 
initialSchema));
@@ -1349,7 +1364,7 @@ public class RDBDocumentStore implements
                 getTableMetaData(con, col, tmd);
             }
             catch (SQLException ex2) {
-                LOG.error("Failed to create table " + tableName + " in " + 
dbname, ex2);
+                LOG.error("Failed to create table '" + tableName + "' in '" + 
dbname + "'", ex2);
                 throw ex2;
             }
         }
@@ -1441,6 +1456,10 @@ public class RDBDocumentStore implements
         }
     }
 
+    public boolean isReadOnly() {
+        return readOnly;
+    }
+
     @Override
     protected void finalize() throws Throwable {
         if (!this.ch.isClosed() && this.callStack != null) {

Modified: 
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStoreSchemaUpgradeTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStoreSchemaUpgradeTest.java?rev=1857253&r1=1857252&r2=1857253&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStoreSchemaUpgradeTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStoreSchemaUpgradeTest.java
 Wed Apr 10 14:44:16 2019
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertFal
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.util.ArrayList;
 import java.util.Collections;
@@ -33,6 +34,7 @@ import javax.sql.DataSource;
 import org.apache.jackrabbit.oak.commons.junit.LogCustomizer;
 import org.apache.jackrabbit.oak.plugins.document.Collection;
 import org.apache.jackrabbit.oak.plugins.document.DocumentMK;
+import org.apache.jackrabbit.oak.plugins.document.DocumentStoreException;
 import org.apache.jackrabbit.oak.plugins.document.DocumentStoreFixture;
 import org.apache.jackrabbit.oak.plugins.document.NodeDocument;
 import org.apache.jackrabbit.oak.plugins.document.NodeDocument.SplitDocType;
@@ -84,6 +86,22 @@ public class RDBDocumentStoreSchemaUpgra
         } finally {
             if (rdb != null) {
                 rdb.dispose();
+            }
+        }
+    }
+
+    @Test
+    public void initDefaultRO() {
+        RDBOptions op = new 
RDBOptions().tablePrefix("T00RO").initialSchema(0).upgradeToSchema(0).dropTablesOnClose(true);
+        RDBDocumentStore rdb = null;
+        try {
+            DocumentMK.Builder builder = new 
DocumentMK.Builder().setReadOnlyMode();
+            rdb = new RDBDocumentStore(this.ds, builder, op);
+            fail("should fail");
+        } catch (DocumentStoreException expected) {
+        } finally {
+            if (rdb != null) {
+                rdb.dispose();
             }
         }
     }


Reply via email to