diff gnv-artifacts/src/main/java/de/intevation/gnv/state/StateBase.java @ 1030:c07d9f9a738c

Removed bugs that existed in the caching mechanism (issue264, issue268). gnv-artifacts/trunk@1067 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 06 May 2010 08:32:56 +0000
parents dfd02f8d3602
children 9981452c7e75
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/StateBase.java	Tue May 04 14:24:23 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/StateBase.java	Thu May 06 08:32:56 2010 +0000
@@ -13,6 +13,8 @@
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
 
 import javax.xml.xpath.XPathConstants;
 
@@ -243,8 +245,7 @@
                 InputValue inputValue = this.inputValues.get(tmpItem.getName());
                 if (inputValue != null) {
                     if (this.inputData == null) {
-                        this.inputData = new HashMap<String, InputData>(
-                                inputData.size());
+                        this.inputData = new TreeMap<String, InputData>();
                     }
 
                     boolean valid = InputValidator.isInputValid(tmpItem.getValue(),
@@ -348,8 +349,7 @@
                 InputValue inputValue = this.inputValues.get(tmpItem.getName());
                 if (inputValue != null) {
                     if (this.inputData == null) {
-                        this.inputData = new HashMap<String, InputData>(
-                                inputData.size());
+                        this.inputData = new TreeMap<String, InputData>();
                     }
 
                     boolean valid = InputValidator.isInputValid(tmpItem.getValue(),
@@ -462,6 +462,7 @@
             InputData data = this.inputData.get(value);
             if (data != null
                 && this.inputValues.containsKey(data.getName())) {
+
                 int size = this.inputValues.get(data.getName())
                         .usedInQueries();
                 String type = this.inputValues.get(data.getName())
@@ -695,7 +696,7 @@
         String                  uuid)
     {
         State parent = getParent();
-        if (parent != null && parent instanceof StateBase) {
+        if (parent instanceof StateBase) {
             ((StateBase) parent).describeStatic(
                 artCreator, creator, document, staticNode, context, uuid);
         }
@@ -712,7 +713,7 @@
         Node                    staticNode,
         CallMeta                callMeta
     ) {
-        InputData  data = inputData.get(dataName);
+        InputData  data = dataName!= null ? inputData.get(dataName) : null;
 
         if (data == null) {
             return;
@@ -937,15 +938,25 @@
 
 
     protected void setHash(String uuid) {
-        this.hash = uuid +
-                    HASH_ID_SEPARATOR +
-                    id +
-                    HASH_ID_SEPARATOR +
-                    inputData.hashCode();
+        String newHash = uuid + HASH_ID_SEPARATOR + id + HASH_ID_SEPARATOR;
+        Set    keys    = inputData.keySet();
+
+        int nhash = 0;
+        int shift = 0;
+
+        for (Object o: keys) {
+            nhash ^= inputData.get(o).hashCode() << shift;
+            shift += 2;
+        }
+
+        log.info("### OLD HASH: " + hash);
+        log.info("### NEW HASH: " + (newHash + nhash));
+
+        this.hash = newHash + nhash;
     }
 
 
-    protected String getHash() {
+    protected String getHash(String uuid) {
         return this.hash;
     }
 
@@ -954,9 +965,10 @@
         CacheFactory factory = CacheFactory.getInstance();
         if (factory.isInitialized()) {
             // we use a cache
-            log.debug("Using cache.");
             Cache cache = factory.getCache();
-            String key  = getHash();
+            String key  = getHash(uuid);
+
+            log.debug("Using cache - key: " + key);
 
             net.sf.ehcache.Element value = cache.get(key);
             if (value != null) {
@@ -973,6 +985,7 @@
                     List<Object> data     = queryDatabase(filterValues, uuid);
 
                     cache.put(new net.sf.ehcache.Element(key, data));
+
                     return data;
                 }
                 catch (QueryException qe) {

http://dive4elements.wald.intevation.org