changeset 230:fbd57d2eeaef

Changed semantics of locked artifact ids. artifacts/trunk@1634 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 31 Mar 2011 14:48:28 +0000
parents 328ef982d768
children 1ea35226a6de
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/App.java artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java artifact-database/src/main/java/de/intevation/artifactdatabase/DatabaseCleaner.java artifact-database/src/main/java/de/intevation/artifactdatabase/Id.java artifact-database/src/main/java/de/intevation/artifactdatabase/state/DefaultOutput.java
diffstat 7 files changed, 64 insertions(+), 103 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Thu Mar 31 08:58:42 2011 +0000
+++ b/ChangeLog	Thu Mar 31 14:48:28 2011 +0000
@@ -1,3 +1,23 @@
+2011-03-31	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/DatabaseCleaner.java:
+	  The cleaner now fetches the explicit set of locked artifact ids from
+	  the artifact database. This is needed for the next step todo: Prevent
+	  locked artifact from beeing considered as outdated on database level.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/Id.java:
+	  Removed. Not needed any more.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java:
+	  Now provides a copy of the locked artifact ids.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/App.java,
+	  artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java:
+	  Adjusted code to follow the new id locking semantics.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/state/DefaultOutput.java:
+	  Removed needless import.
+
 2011-03-31	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* artifact-database/doc/schema-h2.sql, artifact-database/doc/schema-pg.sql:
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/App.java	Thu Mar 31 08:58:42 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/App.java	Thu Mar 31 14:48:28 2011 +0000
@@ -76,7 +76,7 @@
 
         backend.setCleaner(cleaner);
 
-        cleaner.setFilter(db);
+        cleaner.setLockedIdsProvider(db);
 
         cleaner.start();
 
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Thu Mar 31 08:58:42 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Thu Mar 31 14:48:28 2011 +0000
@@ -34,12 +34,11 @@
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.List;
+import java.util.Set;
 
 import javax.xml.xpath.XPathConstants;
 
@@ -60,7 +59,9 @@
  * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
  */
 public class ArtifactDatabaseImpl
-implements   ArtifactDatabase, Id.Filter, Backend.FactoryLookup
+implements   ArtifactDatabase, 
+             DatabaseCleaner.LockedIdsProvider,
+             Backend.FactoryLookup
 {
     private static Logger logger =
         Logger.getLogger(ArtifactDatabaseImpl.class);
@@ -396,7 +397,7 @@
      * This artifacts should not be removed from the database by the
      * database cleaner.
      */
-    protected HashSet     backgroundIds;
+    protected HashSet<Integer>  backgroundIds;
 
     /**
      * Default constructor.
@@ -423,7 +424,7 @@
      */
     public ArtifactDatabaseImpl(FactoryBootstrap bootstrap, Backend backend) {
 
-        backgroundIds = new HashSet();
+        backgroundIds = new HashSet<Integer>();
 
         setupArtifactCollectionFactory(bootstrap);
         setupArtifactFactories(bootstrap);
@@ -555,7 +556,7 @@
      */
     protected void removeIdFromBackground(int id) {
         synchronized (backgroundIds) {
-            backgroundIds.remove(Integer.valueOf(id));
+            backgroundIds.remove(id);
         }
     }
 
@@ -572,19 +573,10 @@
         }
     }
 
-    public List filterIds(List ids) {
-        int N = ids.size();
-        ArrayList out = new ArrayList(N);
+    public Set<Integer> getLockedIds() {
         synchronized (backgroundIds) {
-            for (int i = 0; i < N; ++i) {
-                Id id = (Id)ids.get(i);
-                // only delete artifact if its not in background.
-                if (!backgroundIds.contains(Integer.valueOf(id.getId()))) {
-                    out.add(id);
-                }
-            }
+            return new HashSet<Integer>(backgroundIds);
         }
-        return out;
     }
 
     public String [][] artifactFactoryNamesAndDescriptions() {
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Thu Mar 31 08:58:42 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Thu Mar 31 14:48:28 2011 +0000
@@ -234,8 +234,8 @@
      * back into the database.
      */
     public final class PersistentArtifact
-    extends            Id
     {
+        private int                id;
         private Artifact           artifact;
         private ArtifactSerializer serializer;
         private Long               ttl;
@@ -254,12 +254,16 @@
             Long               ttl,
             int                id
         ) {
-            super(id);
+            this.id         = id;
             this.artifact   = artifact;
             this.serializer = serializer;
             this.ttl        = ttl;
         }
 
+        public int getId() {
+            return id;
+        }
+
         /**
          * Returns the wrapped living artifact.
          * @return the living artifact.
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/DatabaseCleaner.java	Thu Mar 31 08:58:42 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/DatabaseCleaner.java	Thu Mar 31 14:48:28 2011 +0000
@@ -19,6 +19,8 @@
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
+import java.util.Collections;
 
 import javax.sql.DataSource;
 
@@ -57,6 +59,10 @@
 
     } // interface ArtifactReviver
 
+    public interface LockedIdsProvider {
+        Set<Integer> getLockedIds();
+    } // interface LockedIdsProvider
+
     private static Logger logger = Logger.getLogger(DatabaseCleaner.class);
 
     /**
@@ -66,6 +72,8 @@
      */
     public static final int MAX_ROWS = 50;
 
+    public static final Set<Integer> EMPTY_IDS = Collections.emptySet();
+
     /**
      * The SQL statement to select the outdated artifacts.
      */
@@ -110,7 +118,7 @@
      * A specialized Id filter which only delete some artifacts.
      * This is used to prevent deletion of living artifacts.
      */
-    protected Id.Filter filter;
+    protected LockedIdsProvider lockedIdsProvider;
 
     /**
      * The reviver used to bring the dead artifact on last
@@ -144,8 +152,8 @@
      * would create severe internal problems.
      * @param filter
      */
-    public void setFilter(Id.Filter filter) {
-        this.filter = filter;
+    public void setLockedIdsProvider(LockedIdsProvider lockedIdsProvider) {
+        this.lockedIdsProvider = lockedIdsProvider;
     }
 
     /**
@@ -179,14 +187,14 @@
         return SLEEP_DEFAULT;
     }
 
-    private static final class IdData
-    extends                    Id
-    {
+    private static final class IdData {
+
+        int     id;
         byte [] data;
         String  factoryName;
 
         public IdData(int id, String factoryName, byte [] data) {
-            super(id);
+            this.id          = id;
             this.factoryName = factoryName;
             this.data        = data;
         }
@@ -224,15 +232,22 @@
             fetchIds.setMaxRows(MAX_ROWS);
 
             for (;;) {
-                List ids = new ArrayList();
+                List<IdData> ids = new ArrayList<IdData>();
+
+                Set<Integer> filter = lockedIdsProvider != null
+                    ? lockedIdsProvider.getLockedIds()
+                    : EMPTY_IDS;
 
                 result = fetchIds.executeQuery();
 
                 while (result.next()) {
-                    ids.add(new IdData(
-                        result.getInt(1),
-                        result.getString(2),
-                        result.getBytes(3)));
+                    int id = result.getInt(1);
+                    if (!filter.contains(id)) {
+                        ids.add(new IdData(
+                            id,
+                            result.getString(2),
+                            result.getBytes(3)));
+                    }
                 }
 
                 result.close(); result = null;
@@ -241,12 +256,8 @@
                     break;
                 }
 
-                if (filter != null) {
-                    ids = filter.filterIds(ids);
-                }
-
                 for (int i = ids.size()-1; i >= 0; --i) {
-                    IdData idData = (IdData)ids.get(i);
+                    IdData idData = ids.get(i);
                     Artifact artifact = reviver.reviveArtifact(
                         idData.factoryName, idData.data);
                     idData.data = null;
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Id.java	Thu Mar 31 08:58:42 2011 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,64 +0,0 @@
-/*
- * Copyright (c) 2010 by Intevation GmbH
- *
- * This program is free software under the LGPL (>=v2.1)
- * Read the file LGPL.txt coming with the software for details
- * or visit http://www.gnu.org/licenses/ if it does not exist.
- */
-
-package de.intevation.artifactdatabase;
-
-import java.util.List;
-
-/**
- * Class to model the concept of an 'id' in terms of unique integer as
- * used in databases. Subclasses of this class are able to be processed
- * by this id.
- *
- * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
- */
-public class Id
-{
-    /**
-     * Interface to filter a list of ids.
-     */
-    public interface Filter {
-        /**
-         * A list of ids is processed in a functional way to be
-         * free from some ids by the implementing code. The resulting
-         * list maybe short or equal sized to the original one. The
-         * original list is not modified.
-         * @param ids The list of input ids.
-         * @return A list of processed ids.
-         */
-        List filterIds(List ids);
-    }
-
-    /**
-     * The backing int of the id.
-     */
-    protected int id;
-
-    /**
-     * Default constructor: id = 0
-     */
-    public Id() {
-    }
-
-    /**
-     * Constructor to create an id with a given value.
-     * @param id The id value
-     */
-    public Id(int id) {
-        this.id = id;
-    }
-
-    /**
-     * Returns the id value.
-     * @return The value of the id.
-     */
-    public int getId() {
-        return id;
-    }
-}
-// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/state/DefaultOutput.java	Thu Mar 31 08:58:42 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/state/DefaultOutput.java	Thu Mar 31 14:48:28 2011 +0000
@@ -1,9 +1,7 @@
 package de.intevation.artifactdatabase.state;
 
-import java.util.ArrayList;
 import java.util.List;
 
-
 /**
  * The default implementation of an Output.
  *

http://dive4elements.wald.intevation.org