This is an automated email from the ASF dual-hosted git repository.

andy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jena.git


The following commit(s) were added to refs/heads/master by this push:
     new 267b967  Allow direct loading of tuples without temporary tables
     new f3b271e  Merge pull request #528 from grahamtriggs/feature/sdbdirect
267b967 is described below

commit 267b967e84671da17465ee04862fb6c2423c4ce4
Author: Graham Triggs <[email protected]>
AuthorDate: Thu Jan 31 15:20:52 2019 +0000

    Allow direct loading of tuples without temporary tables
---
 .../apache/jena/sdb/layout2/TupleLoaderBase.java   | 75 ++++++++++++++++------
 .../apache/jena/sdb/layout2/TupleLoaderDirect.java | 24 +++++++
 .../sdb/layout2/hash/TupleLoaderHashMySQL.java     | 31 ++++++++-
 3 files changed, 108 insertions(+), 22 deletions(-)

diff --git 
a/jena-sdb/src/main/java/org/apache/jena/sdb/layout2/TupleLoaderBase.java 
b/jena-sdb/src/main/java/org/apache/jena/sdb/layout2/TupleLoaderBase.java
index 2c9a604..ad007bf 100644
--- a/jena-sdb/src/main/java/org/apache/jena/sdb/layout2/TupleLoaderBase.java
+++ b/jena-sdb/src/main/java/org/apache/jena/sdb/layout2/TupleLoaderBase.java
@@ -30,6 +30,9 @@ import org.apache.jena.sdb.sql.TableUtils ;
 import org.apache.jena.sdb.store.TableDesc ;
 import org.apache.jena.sparql.util.NodeUtils ;
 
+import static java.sql.Connection.TRANSACTION_NONE;
+import static java.sql.Connection.TRANSACTION_READ_UNCOMMITTED;
+
 public abstract class TupleLoaderBase extends 
org.apache.jena.sdb.store.TupleLoaderBase implements TupleLoaderBasics {
        
        PreparedStatement insertTupleLoader;
@@ -62,16 +65,42 @@ public abstract class TupleLoaderBase extends 
org.apache.jena.sdb.store.TupleLoa
        }
        
        protected void init() throws SQLException {
-           ensureTempTables() ;
-               // Prepare those statements
-               insertNodeLoader = 
connection().prepareStatement(getInsertTempNodes());
-               insertTupleLoader = 
connection().prepareStatement(getInsertTempTuples());
-               insertNodes = getLoadNodes();
-               insertTuples = getLoadTuples();
-               deleteTuples = connection().prepareStatement(getDeleteTuples());
-               deleteAllTuples = 
connection().prepareStatement(getDeleteAllTuples());
-               clearNodeLoader = 
connection().prepareStatement(getClearTempNodes());
-               clearTupleLoader = 
connection().prepareStatement(getClearTempTuples());
+               TupleLoaderDirect directLoader = null;
+
+               switch 
(connection().getSqlConnection().getTransactionIsolation()) {
+                       case TRANSACTION_NONE:
+                       case TRANSACTION_READ_UNCOMMITTED:
+                               break;
+
+                       default:
+                               if (this instanceof TupleLoaderDirect) {
+                                       directLoader = (TupleLoaderDirect) this;
+                               }
+               }
+
+               if (directLoader != null) {
+                       // Prepare those statements
+                       insertNodeLoader = 
connection().prepareStatement(directLoader.getDirectInsertNodes());
+                       insertTupleLoader = 
connection().prepareStatement(directLoader.getDirectInsertTuples());
+                       deleteTuples = 
connection().prepareStatement(getDeleteTuples());
+                       deleteAllTuples = 
connection().prepareStatement(getDeleteAllTuples());
+
+                       insertNodes = null;
+                       insertTuples = null;
+                       clearNodeLoader = null;
+                       clearTupleLoader = null;
+               } else {
+                       ensureTempTables();
+                       // Prepare those statements
+                       insertNodeLoader = 
connection().prepareStatement(getInsertTempNodes());
+                       insertTupleLoader = 
connection().prepareStatement(getInsertTempTuples());
+                       insertNodes = getLoadNodes();
+                       insertTuples = getLoadTuples();
+                       deleteTuples = 
connection().prepareStatement(getDeleteTuples());
+                       deleteAllTuples = 
connection().prepareStatement(getDeleteAllTuples());
+                       clearNodeLoader = 
connection().prepareStatement(getClearTempNodes());
+                       clearTupleLoader = 
connection().prepareStatement(getClearTempTuples());
+               }
        }
        
        public int getArity() {
@@ -181,8 +210,10 @@ public abstract class TupleLoaderBase extends 
org.apache.jena.sdb.store.TupleLoa
                connection().closePreparedStatement(insertNodeLoader);
                connection().closePreparedStatement(deleteTuples);
                connection().closePreparedStatement(deleteAllTuples);
-               connection().closePreparedStatement(clearTupleLoader);
-               connection().closePreparedStatement(clearNodeLoader);
+               if (clearTupleLoader != null)
+                       connection().closePreparedStatement(clearTupleLoader);
+               if (clearNodeLoader != null)
+                       connection().closePreparedStatement(clearNodeLoader);
            } catch (SQLException ex) {}
        }
 
@@ -207,23 +238,25 @@ public abstract class TupleLoaderBase extends 
org.apache.jena.sdb.store.TupleLoa
        
        protected void flush() {
                if (tupleNum == 0) return;
-               
+
                boolean handleTransaction = startTransaction(connection());
-               
+
                try {
                        if (amLoading) {
                                insertNodeLoader.executeBatch();
                                insertTupleLoader.executeBatch();
-                               connection().execUpdate(insertNodes);
-                               connection().execUpdate(insertTuples);
-                               if (!handleTransaction || !clearsOnCommit()) {
-                                       clearNodeLoader.execute();
-                                       clearTupleLoader.execute();
+                               if (insertNodes != null && insertTuples != 
null) {
+                                       connection().execUpdate(insertNodes);
+                                       connection().execUpdate(insertTuples);
+                                       if (!handleTransaction || 
!clearsOnCommit()) {
+                                               clearNodeLoader.execute();
+                                               clearTupleLoader.execute();
+                                       }
                                }
                        } else {
                                deleteTuples.executeBatch();
                        }
-                       
+
                        endTransaction(connection(), handleTransaction);
                } catch (SQLException e) {
                        if (handleTransaction)
@@ -363,7 +396,7 @@ public abstract class TupleLoaderBase extends 
org.apache.jena.sdb.store.TupleLoa
         } catch (SQLException ex)
         {
             // Some problem - due to the differences in databases we didn't 
check whether tables existed first.
-            // So attempt to cleanup, then tryagain to create the temporary 
tables.  
+            // So attempt to cleanup, then tryagain to create the temporary 
tables.
             TableUtils.dropTableSilent(connection(), getNodeLoader()) ;
             TableUtils.dropTableSilent(connection(), getTupleLoader()) ;
             createTempTables() ;    // Allow this to throw the SQLException
diff --git 
a/jena-sdb/src/main/java/org/apache/jena/sdb/layout2/TupleLoaderDirect.java 
b/jena-sdb/src/main/java/org/apache/jena/sdb/layout2/TupleLoaderDirect.java
new file mode 100644
index 0000000..8226520
--- /dev/null
+++ b/jena-sdb/src/main/java/org/apache/jena/sdb/layout2/TupleLoaderDirect.java
@@ -0,0 +1,24 @@
+/*
+ * 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.jena.sdb.layout2;
+
+public interface TupleLoaderDirect {
+    String getDirectInsertNodes();
+    String getDirectInsertTuples();
+}
diff --git 
a/jena-sdb/src/main/java/org/apache/jena/sdb/layout2/hash/TupleLoaderHashMySQL.java
 
b/jena-sdb/src/main/java/org/apache/jena/sdb/layout2/hash/TupleLoaderHashMySQL.java
index cbf5552..876e50e 100644
--- 
a/jena-sdb/src/main/java/org/apache/jena/sdb/layout2/hash/TupleLoaderHashMySQL.java
+++ 
b/jena-sdb/src/main/java/org/apache/jena/sdb/layout2/hash/TupleLoaderHashMySQL.java
@@ -19,10 +19,11 @@
 package org.apache.jena.sdb.layout2.hash;
 
 import org.apache.jena.sdb.layout2.TableDescNodes ;
+import org.apache.jena.sdb.layout2.TupleLoaderDirect;
 import org.apache.jena.sdb.sql.SDBConnection ;
 import org.apache.jena.sdb.store.TableDesc ;
 
-public class TupleLoaderHashMySQL extends TupleLoaderHashBase {
+public class TupleLoaderHashMySQL extends TupleLoaderHashBase implements 
TupleLoaderDirect {
 
        public TupleLoaderHashMySQL(SDBConnection connection, TableDesc 
tableDesc,
                        int chunkSize) {
@@ -71,4 +72,32 @@ public class TupleLoaderHashMySQL extends 
TupleLoaderHashBase {
                
                return stmt.toString();
        }
+
+       @Override
+       public String getDirectInsertNodes() {
+               StringBuilder stmt = new StringBuilder();
+
+               stmt.append("INSERT IGNORE INTO Nodes VALUES (");
+               for (int i = 0; i < getNodeColTypes().length; i++) {
+                       if (i != 0) stmt.append(" , ");
+                       stmt.append("?");
+               }
+               stmt.append(" )");
+
+               return stmt.toString();
+       }
+
+       @Override
+       public String getDirectInsertTuples() {
+               StringBuilder stmt = new StringBuilder();
+
+               stmt.append("INSERT IGNORE INTO 
").append(this.getTableName()).append(" VALUES (");
+               for (int i = 0; i < this.getTableWidth(); i++) {
+                       if (i != 0) stmt.append(" , ");
+                       stmt.append("?");
+               }
+               stmt.append(" )");
+
+               return stmt.toString();
+       }
 }

Reply via email to