From 0c387a72d952633dac8183bc91291b4e6f7dc715 Mon Sep 17 00:00:00 2001 From: Gang Liao Date: Tue, 1 Oct 2019 03:11:15 -0400 Subject: [PATCH] [ENH] multiple-partition constraint --- voltdb/BatchRemoveINodes.java | 68 +++++++++++++++++++++--------- voltdb/RemoveChild.java | 63 ++++++++++++++++++++------- voltdb/UpdateModificationTime.java | 16 ++++--- 3 files changed, 107 insertions(+), 40 deletions(-) diff --git a/voltdb/BatchRemoveINodes.java b/voltdb/BatchRemoveINodes.java index 9e735fd3bb3..e2a02afc378 100644 --- a/voltdb/BatchRemoveINodes.java +++ b/voltdb/BatchRemoveINodes.java @@ -1,33 +1,63 @@ import org.voltdb.*; +import java.util.List; +import java.util.ArrayList; public class BatchRemoveINodes extends VoltProcedure { - public final SQLStmt sql1 = - new SQLStmt( - "WITH RECURSIVE cte AS (" - + " SELECT id, parent FROM inodes d WHERE id = ?" - + " UNION ALL" - + " SELECT d.id, d.parent FROM cte" - + " JOIN inodes d ON cte.id = d.parent" - + " )" - + " SELECT id FROM cte;"); + // public final SQLStmt sql1 = + // new SQLStmt( + // "WITH RECURSIVE cte AS (" + // + " SELECT id, parent FROM inodes d WHERE id = ?" + // + " UNION ALL" + // + " SELECT d.id, d.parent FROM cte" + // + " JOIN inodes d ON cte.id = d.parent" + // + " )" + // + " SELECT id FROM cte;"); + // public final SQLStmt sql2 = new SQLStmt("DELETE FROM inodes WHERE id = ?;"); + + // public long run(long[] ids) throws VoltAbortException { + // for (int i = 0; i < ids.length; ++i) { + // voltQueueSQL(sql1, ids[i]); + // } + // VoltTable[] results = voltExecuteSQL(); + + // if (results[0].getRowCount() < 1) { + // return -1; + // } + + // for (int j = 0; j < results.length; ++j) { + // for (int i = 0; i < results[j].getRowCount(); ++i) { + // voltQueueSQL(sql2, results[j].fetchRow(i).getLong(0)); + // } + // } + // voltExecuteSQL(); + // return 1; + // } + + public final SQLStmt sql1 = new SQLStmt("SELECT id FROM inodes WHERE parent = ?"); public final SQLStmt sql2 = new SQLStmt("DELETE FROM inodes WHERE id = ?;"); public long run(long[] ids) throws VoltAbortException { - for (int i = 0; i < ids.length; ++i) { - voltQueueSQL(sql1, ids[i]); - } - VoltTable[] results = voltExecuteSQL(); + List set = new ArrayList(Arrays.asList(ids)); - if (results[0].getRowCount() < 1) { - return -1; + int i = 0; + while (i < set.size()) { + long cid = set.get(i); + i++; + voltQueueSQL(sql1, cid); + VoltTable[] results = voltExecuteSQL(); + if (results[0].getRowCount() < 1) { + continue; + } + for (int j = 0; j < results[0].getRowCount(); ++j) { + set.add(results[0].fetchRow(j).getLong(0)); + } } - for (int j = 0; j < results.length; ++j) { - for (int i = 0; i < results[j].getRowCount(); ++i) { - voltQueueSQL(sql2, results[j].fetchRow(i).getLong(0)); - } + for (Long id : set) { + voltQueueSQL(sql2, id); } + voltExecuteSQL(); return 1; } diff --git a/voltdb/RemoveChild.java b/voltdb/RemoveChild.java index 88d229a7d6b..9ed3979e7ae 100644 --- a/voltdb/RemoveChild.java +++ b/voltdb/RemoveChild.java @@ -1,29 +1,62 @@ +import java.util.ArrayList; +import java.util.List; import org.voltdb.*; // https://docs.voltdb.com/tutorial/Part5.php public class RemoveChild extends VoltProcedure { - public final SQLStmt sql1 = - new SQLStmt( - "WITH RECURSIVE cte AS (" - + " SELECT id, parent FROM inodes d WHERE id = ?" - + " UNION ALL" - + " SELECT d.id, d.parent FROM cte" - + " JOIN inodes d ON cte.id = d.parent" - + " )" - + " SELECT id FROM cte;"); + // CTE only support single partition query + // public final SQLStmt sql1 = + // new SQLStmt( + // "WITH RECURSIVE cte AS (" + // + " SELECT id, parent FROM inodes d WHERE id = ?" + // + " UNION ALL" + // + " SELECT d.id, d.parent FROM cte" + // + " JOIN inodes d ON cte.id = d.parent" + // + " )" + // + " SELECT id FROM cte;"); + + // public final SQLStmt sql2 = new SQLStmt("DELETE FROM inodes WHERE id = ?;"); + + // public long run(long id) throws VoltAbortException { + // voltQueueSQL(sql1, id); + // VoltTable[] results = voltExecuteSQL(); + + // if (results[0].getRowCount() < 1) { + // return -1; + // } + // for (int i = 0; i < results[0].getRowCount(); ++i) { + // voltQueueSQL(sql2, results[0].fetchRow(i).getLong(0)); + // } + // voltExecuteSQL(); + // return 1; + // } + + public final SQLStmt sql1 = new SQLStmt("SELECT id FROM inodes WHERE parent = ?"); public final SQLStmt sql2 = new SQLStmt("DELETE FROM inodes WHERE id = ?;"); public long run(long id) throws VoltAbortException { - voltQueueSQL(sql1, id); - VoltTable[] results = voltExecuteSQL(); + List set = new ArrayList<>(); + set.add(id); - if (results[0].getRowCount() < 1) { - return -1; + int i = 0; + while (i < set.size()) { + long cid = set.get(i); + i++; + voltQueueSQL(sql1, cid); + VoltTable[] results = voltExecuteSQL(); + if (results[0].getRowCount() < 1) { + continue; + } + for (int j = 0; j < results[0].getRowCount(); ++j) { + set.add(results[0].fetchRow(j).getLong(0)); + } } - for (int i = 0; i < results[0].getRowCount(); ++i) { - voltQueueSQL(sql2, results[0].fetchRow(i).getLong(0)); + + for (Long id : set) { + voltQueueSQL(sql2, id); } + voltExecuteSQL(); return 1; } diff --git a/voltdb/UpdateModificationTime.java b/voltdb/UpdateModificationTime.java index bfba26a1efe..a2df57043f5 100644 --- a/voltdb/UpdateModificationTime.java +++ b/voltdb/UpdateModificationTime.java @@ -2,14 +2,18 @@ // https://docs.voltdb.com/tutorial/Part5.php public class UpdateModificationTime extends VoltProcedure { - - public final SQLStmt sql = - new SQLStmt( - "UPDATE inodes SET modificationTime = (" - + "SELECT modificationTime FROM inodes WHERE id = ?) WHERE id = ?;"); + public final SQLStmt sql1 = new SQLStmt("SELECT modificationTime FROM inodes WHERE id = ?"); + public final SQLStmt sql2 = new SQLStmt("UPDATE inodes SET modificationTime = ? WHERE id = ?;"); public long run(final long id, final long childId) throws VoltAbortException { - voltQueueSQL(sql, childId, id); + voltQueueSQL(sql1, childId); + VoltTable[] results = voltExecuteSQL(); + if (results[0].getRowCount() < 1) { + return -1; + } + + Long mtime = results[0].fetchRow(0).getLong(0); + voltQueueSQL(sql2, mtime, id); voltExecuteSQL(); return 1; }