[PATCH] Ugly: Filter collection items in user land

Wald Commits scm-commit at wald.intevation.org
Thu Sep 3 17:00:36 CEST 2015


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1441292430 -7200
# Node ID 9497f58484a0c85d5e98c36f7d0602ed7fe3a68d
# Parent  3b1e48d22ce00ede83592d95e647f436592bab97
Ugly: Filter collection items in user land.

diff -r 3b1e48d22ce0 -r 9497f58484a0 artifact-database/src/main/java/org/dive4elements/artifactdatabase/DatabaseCleaner.java
--- a/artifact-database/src/main/java/org/dive4elements/artifactdatabase/DatabaseCleaner.java	Thu Sep 03 15:34:07 2015 +0200
+++ b/artifact-database/src/main/java/org/dive4elements/artifactdatabase/DatabaseCleaner.java	Thu Sep 03 17:00:30 2015 +0200
@@ -25,6 +25,7 @@
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
+import java.util.HashSet;
 import java.util.Collections;
 
 import javax.sql.DataSource;
@@ -90,6 +91,7 @@
     public String SQL_OUTDATED_COLLECTIONS;
     public String SQL_DELETE_COLLECTION_ITEMS;
     public String SQL_DELETE_COLLECTION;
+    public String SQL_COLLECTION_ITEMS_ARTIFACT_IDS;
 
     /**
      * The SQL statement to delete some artifacts from the database.
@@ -165,11 +167,12 @@
     }
 
     protected void setupSQL(SQL sql) {
-        SQL_OUTDATED                = sql.get("artifacts.outdated");
-        SQL_OUTDATED_COLLECTIONS    = sql.get("collections.outdated");
-        SQL_DELETE_COLLECTION_ITEMS = sql.get("delete.collection.items");
-        SQL_DELETE_COLLECTION       = sql.get("delete.collection");
-        SQL_DELETE_ARTIFACT         = sql.get("artifacts.delete");
+        SQL_OUTDATED                      = sql.get("artifacts.outdated");
+        SQL_OUTDATED_COLLECTIONS          = sql.get("collections.outdated");
+        SQL_DELETE_COLLECTION_ITEMS       = sql.get("delete.collection.items");
+        SQL_DELETE_COLLECTION             = sql.get("delete.collection");
+        SQL_DELETE_ARTIFACT               = sql.get("artifacts.delete");
+        SQL_COLLECTION_ITEMS_ARTIFACT_IDS = sql.get("collection.items.artifact.id");
     }
 
     /**
@@ -271,11 +274,25 @@
             @Override
             public boolean doIt() throws SQLException {
 
-                PreparedStatement fetchIds   = null;
-                PreparedStatement stmnt      = null;
-                ResultSet         result     = null;
+                PreparedStatement collectionItems = null;
+                PreparedStatement fetchIds        = null;
+                PreparedStatement stmnt           = null;
+                ResultSet         result          = null;
+
+                HashSet<Integer> collectionItemsIds =
+                    new HashSet<Integer>();
 
                 try {
+                    collectionItems = conn.prepareStatement(
+                        SQL_COLLECTION_ITEMS_ARTIFACT_IDS);
+
+                    result = collectionItems.executeQuery();
+
+                    while (result.next()) {
+                        collectionItemsIds.add(result.getInt(1));
+                    }
+                    result.close(); result = null;
+
                     fetchIds = conn.prepareStatement(
                         SQL_OUTDATED.replace("$LOCKED_IDS$", questionMarks));
 
@@ -340,18 +357,28 @@
 
                         result = fetchIds.executeQuery();
 
+                        int total = 0;
+
                         while (result.next()) {
-                            ids.add(new IdData(
-                                result.getInt(1),
-                                result.getString(2),
-                                result.getBytes(3),
-                                result.getString(4)));
+                            total++;
+                            int id = result.getInt(1);
+                            if (!collectionItemsIds.contains(id)) {
+                                ids.add(new IdData(
+                                    id,
+                                    result.getString(2),
+                                    result.getBytes(3),
+                                    result.getString(4)));
+                            }
                         }
 
                         result.close(); result = null;
 
+                        if (total == 0) {
+                            break;
+                        }
+
                         if (ids.isEmpty()) {
-                            break;
+                            continue;
                         }
 
                         for (int i = ids.size()-1; i >= 0; --i) {
@@ -396,6 +423,10 @@
                         try { fetchIds.close(); }
                         catch (SQLException sqle) {}
                     }
+                    if (collectionItems != null) {
+                        try { collectionItems.close(); }
+                        catch (SQLException sqle) {}
+                    }
                 }
                 return true;
             }
diff -r 3b1e48d22ce0 -r 9497f58484a0 artifact-database/src/main/resources/sql/org-h2-driver.properties
--- a/artifact-database/src/main/resources/sql/org-h2-driver.properties	Thu Sep 03 15:34:07 2015 +0200
+++ b/artifact-database/src/main/resources/sql/org-h2-driver.properties	Thu Sep 03 17:00:30 2015 +0200
@@ -9,9 +9,10 @@
 
 artifacts.touch=UPDATE artifacts SET last_access = CURRENT_TIMESTAMP WHERE id = ?
 
+collection.items.artifact.id=SELECT artifact_id FROM collection_items
+
 artifacts.outdated=SELECT id, factory, data, gid FROM artifacts WHERE ttl IS NOT NULL \
     AND DATEDIFF('MILLISECOND', last_access, CURRENT_TIMESTAMP) > ttl \
-    AND id NOT IN (SELECT DISTINCT artifact_id FROM collection_items) \
     AND id NOT IN ($LOCKED_IDS$) \
     LIMIT 50
 
diff -r 3b1e48d22ce0 -r 9497f58484a0 artifact-database/src/main/resources/sql/org-postgresql-driver.properties
--- a/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Thu Sep 03 15:34:07 2015 +0200
+++ b/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Thu Sep 03 17:00:30 2015 +0200
@@ -9,9 +9,10 @@
 
 artifacts.touch=UPDATE artifacts SET last_access = CURRENT_TIMESTAMP WHERE id = ?
 
+collection.items.artifact.id=SELECT artifact_id FROM collection_items
+
 artifacts.outdated=SELECT id, factory, data, gid FROM artifacts WHERE ttl IS NOT NULL \
     AND CURRENT_TIMESTAMP - last_access > (ttl || ' milliseconds')::interval \
-    AND id NOT IN (SELECT DISTINCT artifact_id FROM collection_items) \
     AND id NOT IN ($LOCKED_IDS$) \
     LIMIT 50
 


More information about the Dive4Elements-commits mailing list