You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by re...@apache.org on 2014/04/04 13:53:15 UTC

svn commit: r1584616 - /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java

Author: reschke
Date: Fri Apr  4 11:53:15 2014
New Revision: 1584616

URL: http://svn.apache.org/r1584616
Log:
OAK-1266 - implement batch remove

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

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java?rev=1584616&r1=1584615&r2=1584616&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java Fri Apr  4 11:53:15 2014
@@ -61,6 +61,7 @@ import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Objects;
 import com.google.common.cache.Cache;
+import com.google.common.collect.Lists;
 import com.google.common.util.concurrent.Striped;
 
 public class RDBDocumentStore implements CachingDocumentStore {
@@ -154,11 +155,11 @@ public class RDBDocumentStore implements
     }
 
     @Override
-    public <T extends Document> void remove(Collection<T> collection, List<String> keys) {
-        // TODO Use batch delete
-        for (String key : keys) {
-            remove(collection, key);
+    public <T extends Document> void remove(Collection<T> collection, List<String> ids) {
+        for (String id : ids) {
+            invalidateCache(collection, id);
         }
+        delete(collection, ids);
     }
 
     @Override
@@ -519,8 +520,11 @@ public class RDBDocumentStore implements
         String tableName = getTable(collection);
         try {
             connection = getConnection();
-            dbDelete(connection, tableName, id);
+            boolean success = dbDelete(connection, tableName, id);
             connection.commit();
+            if (!success) {
+                throw new MicroKernelException("not deleted: " + id);
+            }
         } catch (Exception ex) {
             throw new MicroKernelException(ex);
         } finally {
@@ -528,6 +532,25 @@ public class RDBDocumentStore implements
         }
     }
 
+    private <T extends Document> void delete(Collection<T> collection, List<String> ids) {
+        for (List<String> sublist : Lists.partition(ids, 64)) {
+            Connection connection = null;
+            String tableName = getTable(collection);
+            try {
+                connection = getConnection();
+                boolean success = dbDelete(connection, tableName, sublist);
+                connection.commit();
+                if (!success) {
+                    throw new MicroKernelException("not deleted: " + ids);
+                }
+            } catch (Exception ex) {
+                throw new MicroKernelException(ex);
+            } finally {
+                closeConnection(connection);
+            }
+        }
+    }
+
     private <T extends Document> boolean updateDocument(@Nonnull Collection<T> collection, @Nonnull T document, Long oldmodcount) {
         Connection connection = null;
         String tableName = getTable(collection);
@@ -736,6 +759,31 @@ public class RDBDocumentStore implements
         }
     }
 
+    private boolean dbDelete(Connection connection, String tableName, List<String> ids) throws SQLException {
+        StringBuilder inClause = new StringBuilder();
+        int cnt = ids.size();
+        for (int i = 0; i < cnt; i++) {
+            inClause.append('?');
+            if (i != cnt - 1) {
+                inClause.append(',');
+            }
+        }
+        PreparedStatement stmt = connection.prepareStatement("delete from " + tableName + " where ID in (" + inClause.toString()
+                + ")");
+        try {
+            for (int i = 0; i < cnt; i++) {
+                stmt.setString(i + 1, ids.get(i));
+            }
+            int result = stmt.executeUpdate();
+            if (result != cnt) {
+                LOG.debug("DB delete failed for " + tableName + "/" + ids);
+            }
+            return result == cnt;
+        } finally {
+            stmt.close();
+        }
+    }
+
     @Override
     public void setReadWriteMode(String readWriteMode) {
         // ignored