Skip to content

Commit

Permalink
Merge pull request #44 from DSL-UMD/distributed
Browse files Browse the repository at this point in the history
voltdb stored procedure: remove child
  • Loading branch information
gangliao authored Apr 1, 2019
2 parents a5e5aa1 + 9b6728e commit 8bc38e8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
Expand Down Expand Up @@ -230,25 +231,39 @@ public static long getChild(final long parentId, final String childName) {
}

public static void removeChild(final long childId) {
try {
String env = System.getenv("DATABASE");
if (env.equals("VOLT")) {
// call a stored procedure
Connection conn = DatabaseConnection.getInstance().getConnection();
// delete file/directory recusively
String sql =
"DELETE FROM inodes WHERE id IN ("
+ " 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"
+ ");";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setLong(1, childId);
pst.executeUpdate();
pst.close();
} catch (SQLException ex) {
System.err.println(ex.getMessage());
CallableStatement proc = conn.prepareCall("{call RemoveChild(?)}");
proc.setLong(1, childId);
rs = proc.executeUpdate();
while (rs.next()) {
LOG.info("removeChild Return: " + rs.getLong(1));
}
rs.close();
proc.close();
} else {
try {
Connection conn = DatabaseConnection.getInstance().getConnection();
// delete file/directory recusively
String sql =
"DELETE FROM inodes WHERE id IN ("
+ " 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"
+ ");";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setLong(1, childId);
pst.executeUpdate();
pst.close();
} catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
LOG.info("removeChild: " + childId);
}
Expand Down
31 changes: 31 additions & 0 deletions voltdb/RemoveChild.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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;");
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;
}
}

0 comments on commit 8bc38e8

Please sign in to comment.