changeset 542:9497f58484a0

Ugly: Filter collection items in user land.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 03 Sep 2015 17:00:30 +0200
parents 3b1e48d22ce0
children e9abf747f6ce
files artifact-database/src/main/java/org/dive4elements/artifactdatabase/DatabaseCleaner.java artifact-database/src/main/resources/sql/org-h2-driver.properties artifact-database/src/main/resources/sql/org-postgresql-driver.properties
diffstat 3 files changed, 49 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- 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;
             }
--- 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
 
--- 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
 

http://dive4elements.wald.intevation.org