Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan D. Brooks2017-08-14 15:24:55 -0400
committerRyan Brooks2017-08-15 14:36:06 -0400
commit5e7fe61d8b641a0c2868af0b1292bd43f11bc367 (patch)
tree76fd03c6ba5a04c170aa43080bed0874386d79fa
parent6cc615741615ed6fbd475dc2b4aa38802dcbf3f6 (diff)
downloadorg.eclipse.osee-5e7fe61d8b641a0c2868af0b1292bd43f11bc367.tar.gz
org.eclipse.osee-5e7fe61d8b641a0c2868af0b1292bd43f11bc367.tar.xz
org.eclipse.osee-5e7fe61d8b641a0c2868af0b1292bd43f11bc367.zip
refinement: Improve performance of join cleaner
-rw-r--r--plugins/org.eclipse.osee.orcs.db/src/org/eclipse/osee/orcs/db/internal/sql/join/JoinCleanerCallable.java64
1 files changed, 34 insertions, 30 deletions
diff --git a/plugins/org.eclipse.osee.orcs.db/src/org/eclipse/osee/orcs/db/internal/sql/join/JoinCleanerCallable.java b/plugins/org.eclipse.osee.orcs.db/src/org/eclipse/osee/orcs/db/internal/sql/join/JoinCleanerCallable.java
index ed3eef39438..ce08672b44a 100644
--- a/plugins/org.eclipse.osee.orcs.db/src/org/eclipse/osee/orcs/db/internal/sql/join/JoinCleanerCallable.java
+++ b/plugins/org.eclipse.osee.orcs.db/src/org/eclipse/osee/orcs/db/internal/sql/join/JoinCleanerCallable.java
@@ -10,8 +10,6 @@
*******************************************************************************/
package org.eclipse.osee.orcs.db.internal.sql.join;
-import java.util.HashMap;
-import java.util.function.Consumer;
import org.eclipse.osee.executor.admin.CancellableCallable;
import org.eclipse.osee.jdbc.JdbcClient;
import org.eclipse.osee.jdbc.JdbcStatement;
@@ -24,54 +22,60 @@ import org.eclipse.osee.logger.Log;
* @author Roberto E. Escobar
*/
public class JoinCleanerCallable extends CancellableCallable<Void> {
-
private final static String DELETE_JOIN_CLEANUP = "DELETE FROM osee_join_cleanup WHERE query_id = ?";
- private final static String SELECT_FROM_JOIN_CLEANUP = "SELECT * from osee_join_cleanup";
+ private final static String SELECT_FROM_JOIN_CLEANUP = "SELECT * from osee_join_cleanup order by table_name";
private final Log logger;
private final JdbcClient jdbcClient;
+ private String previousTableName = "";
+ private long currentTime;
+ private OseePreparedStatement joinDelete;
+ private OseePreparedStatement cleanupDelete;
public JoinCleanerCallable(Log logger, JdbcClient jdbcClient) {
this.logger = logger;
this.jdbcClient = jdbcClient;
}
- private boolean isExpired(Long issuedAt, Long lifetime) {
- return lifetime != -1 && issuedAt + lifetime < System.currentTimeMillis() / 1000;
+ private boolean isExpired(long issuedAt, long lifetime, long currentTime) {
+ return lifetime != -1 && issuedAt + lifetime < currentTime;
}
@Override
public Void call() throws Exception {
try {
- OseePreparedStatement cleanupDelete = jdbcClient.getBatchStatement(DELETE_JOIN_CLEANUP);
- HashMap<String, OseePreparedStatement> joinDeletes = new HashMap<>();
-
- Consumer<JdbcStatement> consumer = stmt -> {
- Long issuedAt = stmt.getLong("issued_at");
- Long expiresIn = stmt.getLong("expires_in");
- if (isExpired(issuedAt, expiresIn)) {
- String tableName = stmt.getString("table_name");
- Integer queryId = stmt.getInt("query_id");
-
- OseePreparedStatement joinDelete = joinDeletes.get(tableName);
- if (joinDelete == null) {
- joinDelete = jdbcClient.getBatchStatement("DELETE FROM " + tableName + " WHERE query_id = ?");
- joinDeletes.put(tableName, joinDelete);
- }
+ currentTime = System.currentTimeMillis() / 1000;
- joinDelete.addToBatch(queryId);
- cleanupDelete.addToBatch(queryId);
- }
- };
- jdbcClient.runQuery(consumer, SELECT_FROM_JOIN_CLEANUP);
-
- cleanupDelete.execute();
- for (OseePreparedStatement joinDelete : joinDeletes.values()) {
+ cleanupDelete = jdbcClient.getBatchStatement(DELETE_JOIN_CLEANUP);
+ try {
+ jdbcClient.runQuery(this::processRow, SELECT_FROM_JOIN_CLEANUP);
+ } finally {
joinDelete.execute();
+ cleanupDelete.execute();
}
} catch (Exception ex) {
- logger.error(ex, "Error cleaning join");
+ logger.error(ex, "Error cleaning join tables");
}
return null;
}
+
+ private void processRow(JdbcStatement stmt) {
+ long issuedAt = stmt.getLong("issued_at");
+ long expiresIn = stmt.getLong("expires_in");
+
+ if (isExpired(issuedAt, expiresIn, currentTime)) {
+ String tableName = stmt.getString("table_name");
+ if (!tableName.equals(previousTableName)) {
+ if (joinDelete != null) {
+ joinDelete.execute();
+ }
+ joinDelete = jdbcClient.getBatchStatement("DELETE FROM " + tableName + " WHERE query_id = ?");
+ previousTableName = tableName;
+ }
+
+ Integer queryId = stmt.getInt("query_id");
+ joinDelete.addToBatch(queryId);
+ cleanupDelete.addToBatch(queryId);
+ }
+ }
} \ No newline at end of file

Back to the top