Author: reschke
Date: Fri Feb  7 16:01:36 2014
New Revision: 1565700

URL: http://svn.apache.org/r1565700
Log:
OAK-1266 - fallback for databases not supporting "binary"

Added:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBMeta.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBBlobStore.java
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/RDBBlobStoreTest.java

Modified: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBBlobStore.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBBlobStore.java?rev=1565700&r1=1565699&r2=1565700&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBBlobStore.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBBlobStore.java
 Fri Feb  7 16:01:36 2014
@@ -30,6 +30,8 @@ import javax.sql.DataSource;
 import org.apache.jackrabbit.mk.api.MicroKernelException;
 import org.apache.jackrabbit.mk.blobs.AbstractBlobStore;
 import org.apache.jackrabbit.mk.util.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class RDBBlobStore extends AbstractBlobStore {
 
@@ -81,19 +83,37 @@ public class RDBBlobStore extends Abstra
         }
     }
 
+    private static final Logger LOG = 
LoggerFactory.getLogger(RDBBlobStore.class);
+
     private Connection connection;
 
     private void initialize(Connection con) throws Exception {
         con.setAutoCommit(false);
 
+        try {
+            createTables(con, "binary");
+        } catch (SQLException ex) {
+            con.rollback();
+            LOG.debug("failed to create tables, retrying after getting DB 
metadata", ex);
+            // try to query database meta info for binary type
+            String btype = RDBMeta.findBinaryType(con.getMetaData());
+            if (btype == null) {
+                String message = "Could not determine binary type for " + 
RDBMeta.getDataBaseName(con.getMetaData());
+                LOG.error(message);
+                throw new Exception(message);
+            }
+            createTables(con, btype);
+        }
+
+        this.connection = con;
+    }
+
+    private void createTables(Connection con, String binaryType) throws 
SQLException {
         Statement stmt = con.createStatement();
-        stmt.execute("create table if not exists datastore_meta" + "(id 
varchar primary key, level int, lastMod bigint)");
-        stmt.execute("create table if not exists datastore_data" + "(id 
varchar primary key, data binary)");
+        stmt.execute("create table if not exists datastore_meta (id varchar 
primary key, level int, lastMod bigint)");
+        stmt.execute("create table if not exists datastore_data (id varchar 
primary key, data " + binaryType + ")");
         stmt.close();
-
         con.commit();
-
-        this.connection = con;
     }
 
     private long minLastModified;
@@ -106,7 +126,7 @@ public class RDBBlobStore extends Abstra
             throw new IOException(e);
         }
     }
-    
+
     private void storeBlockInDatabase(byte[] digest, int level, byte[] data) 
throws SQLException {
         try {
             String id = StringUtils.convertBytesToHex(digest);
@@ -222,7 +242,7 @@ public class RDBBlobStore extends Abstra
             throw new IOException(e);
         }
     }
-        
+
     private int sweepFromDatabase() throws SQLException {
         try {
             int count = 0;

Added: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBMeta.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBMeta.java?rev=1565700&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBMeta.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBMeta.java
 Fri Feb  7 16:01:36 2014
@@ -0,0 +1,62 @@
+/*
+ * 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.oak.plugins.document.rdb;
+
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import javax.annotation.CheckForNull;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class RDBMeta {
+
+    private static final Logger LOG = LoggerFactory.getLogger(RDBMeta.class);
+
+    /**
+     * Tries to find a suitable binary type
+     */
+    @CheckForNull
+    public static String findBinaryType(DatabaseMetaData md) throws 
SQLException {
+        String binaryType = null;
+        String dbtype = getDataBaseName(md);
+
+        ResultSet rs = md.getTypeInfo();
+        while (rs.next()) {
+            String name = rs.getString(1);
+            int type = rs.getInt(2);
+            int size = rs.getInt(3);
+            LOG.debug("Type information from " + dbtype + " -> " + name + " " 
+ type + " " + size);
+            if ((type == java.sql.Types.BINARY || type == 
java.sql.Types.VARBINARY) && (size == 0 || size > 65535)) {
+                binaryType = name;
+            }
+        }
+        return binaryType;
+    }
+
+    public static String getDataBaseName(DatabaseMetaData md) {
+        try {
+            return md.getDatabaseProductName() + " " + 
md.getDatabaseMajorVersion() + "." + md.getDatabaseMinorVersion() + " ("
+                    + md.getDriverName() + " " + md.getDriverVersion() + ")";
+        } catch (SQLException e) {
+            return "(Could not determine DB type: " + e.getMessage() + ")";
+        }
+    }
+}

Propchange: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBMeta.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBMeta.java
------------------------------------------------------------------------------
    svn:executable = *

Modified: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/RDBBlobStoreTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/RDBBlobStoreTest.java?rev=1565700&r1=1565699&r2=1565700&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/RDBBlobStoreTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/blob/RDBBlobStoreTest.java
 Fri Feb  7 16:01:36 2014
@@ -34,6 +34,8 @@ public class RDBBlobStoreTest extends Ab
 
     public void tearDown() throws Exception {
         super.tearDown();
-        blobStore.dispose();
+        if (blobStore != null) {
+            blobStore.dispose();
+        }
     }
 }


Reply via email to