You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marmotta.apache.org by ss...@apache.org on 2013/06/10 12:28:45 UTC

[2/3] git commit: further improvements for duplicate inserts of triples

further improvements for duplicate inserts of triples


Project: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/commit/c8feb75d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/tree/c8feb75d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/diff/c8feb75d

Branch: refs/heads/develop
Commit: c8feb75da7914163d60b07ff76a81063d3f0d010
Parents: 5dad67e
Author: Sebastian Schaffert <ss...@apache.org>
Authored: Mon Jun 10 12:02:18 2013 +0200
Committer: Sebastian Schaffert <ss...@apache.org>
Committed: Mon Jun 10 12:02:18 2013 +0200

----------------------------------------------------------------------
 .../kiwi/persistence/KiWiConnection.java        | 39 ++++++++++++--------
 .../marmotta/kiwi/sail/KiWiSailConnection.java  |  7 ++--
 .../kiwi/persistence/h2/statements.properties   |  2 +-
 3 files changed, 29 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/c8feb75d/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiConnection.java
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiConnection.java b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiConnection.java
index 0232dab..2b99ac2 100644
--- a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiConnection.java
+++ b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/persistence/KiWiConnection.java
@@ -922,8 +922,9 @@ public class KiWiConnection {
      * @param triple     the triple to store
      * @throws SQLException
      * @throws NullPointerException in case the subject, predicate, object or context have not been persisted
+     * @return true in case the update added a new triple to the database, false in case the triple already existed
      */
-    public synchronized void storeTriple(KiWiTriple triple) throws SQLException {
+    public synchronized boolean storeTriple(KiWiTriple triple) throws SQLException {
 
         Preconditions.checkNotNull(triple.getSubject().getId());
         Preconditions.checkNotNull(triple.getPredicate().getId());
@@ -933,22 +934,30 @@ public class KiWiConnection {
 
         requireJDBCConnection();
 
-        // retrieve a new triple ID and set it in the object
-        if(triple.getId() == null) {
-            triple.setId(getNextSequence("seq.triples"));
-        }
+        try {
+            // retrieve a new triple ID and set it in the object
+            if(triple.getId() == null) {
+                triple.setId(getNextSequence("seq.triples"));
+            }
 
-        PreparedStatement insertTriple = getPreparedStatement("store.triple");
-        insertTriple.setLong(1,triple.getId());
-        insertTriple.setLong(2,triple.getSubject().getId());
-        insertTriple.setLong(3,triple.getPredicate().getId());
-        insertTriple.setLong(4,triple.getObject().getId());
-        insertTriple.setLong(5,triple.getContext().getId());
-        insertTriple.setBoolean(6,triple.isInferred());
-        insertTriple.setTimestamp(7, new Timestamp(triple.getCreated().getTime()));
-        insertTriple.executeUpdate();
+            PreparedStatement insertTriple = getPreparedStatement("store.triple");
+            insertTriple.setLong(1,triple.getId());
+            insertTriple.setLong(2,triple.getSubject().getId());
+            insertTriple.setLong(3,triple.getPredicate().getId());
+            insertTriple.setLong(4,triple.getObject().getId());
+            insertTriple.setLong(5,triple.getContext().getId());
+            insertTriple.setBoolean(6,triple.isInferred());
+            insertTriple.setTimestamp(7, new Timestamp(triple.getCreated().getTime()));
+            int count = insertTriple.executeUpdate();
 
-        cacheTriple(triple);
+            cacheTriple(triple);
+
+            return count > 0;
+        } catch(SQLException ex) {
+            // this is an ugly hack to catch duplicate key errors in some databases (H2)
+            // better option could be http://stackoverflow.com/questions/6736518/h2-java-insert-ignore-allow-exception
+            return false;
+        }
     }
 
 

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/c8feb75d/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiSailConnection.java
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiSailConnection.java b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiSailConnection.java
index 51ac2f1..ac68b23 100644
--- a/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiSailConnection.java
+++ b/libraries/kiwi/kiwi-triplestore/src/main/java/org/apache/marmotta/kiwi/sail/KiWiSailConnection.java
@@ -176,9 +176,10 @@ public class KiWiSailConnection extends NotifyingSailConnectionBase implements I
                     triplesAdded = true;
                     notifyStatementAdded(triple);
                 } else {
-                    databaseConnection.storeTriple(triple);
-                    triplesAdded = true;
-                    notifyStatementAdded(triple);
+                    if(databaseConnection.storeTriple(triple)) {
+                        triplesAdded = true;
+                        notifyStatementAdded(triple);
+                    }
                 }
 
                 added.add(triple);

http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/c8feb75d/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/h2/statements.properties
----------------------------------------------------------------------
diff --git a/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/h2/statements.properties b/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/h2/statements.properties
index ffb21ab..2fc9afc 100644
--- a/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/h2/statements.properties
+++ b/libraries/kiwi/kiwi-triplestore/src/main/resources/org/apache/marmotta/kiwi/persistence/h2/statements.properties
@@ -60,7 +60,7 @@ store.tliteral       = INSERT INTO nodes (id,ntype,svalue,tvalue,ltype,createdAt
 
 store.namespace      = INSERT INTO namespaces (id,prefix,uri,createdAt) VALUES (?,?,?,?)
 
-store.triple         = MERGE INTO triples (id,subject,predicate,object,context,inferred,createdAt) KEY(id) VALUES (?,?,?,?,?,?,?)
+store.triple         = INSERT INTO triples (id,subject,predicate,object,context,inferred,createdAt) VALUES (?,?,?,?,?,?,?)
 
 
 query.size           = SELECT count(*) FROM triples WHERE deleted = false