changeset 705:853dceead0f4

Only generate facets when needed. flys-artifacts/trunk@2157 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sat, 18 Jun 2011 17:03:17 +0000
parents eab5e5089d77
children ddd8b37d5cd3
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/cache.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DataFacet.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DurationCurveFacet.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WaterlevelFacet.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/ComputedDischargeCurveState.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DischargeLongitudinalSection.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DurationCurveState.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WaterlevelState.java flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java flys-artifacts/src/main/java/de/intevation/flys/collections/OutputParser.java
diffstat 14 files changed, 122 insertions(+), 88 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Jun 17 16:17:03 2011 +0000
+++ b/flys-artifacts/ChangeLog	Sat Jun 18 17:03:17 2011 +0000
@@ -1,3 +1,26 @@
+2011-06-18  Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/DataFacet.java
+	  src/main/java/de/intevation/flys/artifacts/model/WaterlevelFacet.java,
+	  src/main/java/de/intevation/flys/artifacts/model/DurationCurveFacet.java,
+	  src/main/java/de/intevation/flys/artifacts/states/ComputedDischargeCurveState.java,
+	  src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java,
+	  src/main/java/de/intevation/flys/artifacts/states/DischargeLongitudinalSection.java,
+	  src/main/java/de/intevation/flys/artifacts/states/DurationCurveState.java,
+	  src/main/java/de/intevation/flys/artifacts/states/WaterlevelState.java,
+	  src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java:
+	  Only generate facets when needed.
+
+	* src/main/java/de/intevation/flys/collections/OutputParser.java:
+	  Removed more XPath for simply accessing attributes of an element.
+
+	* doc/conf/cache.xml: 200 elements in memory for "computed.values" are enough,
+	  LRU as eviction strategy is sufficent.
+
+	* src/main/java/de/intevation/flys/collections/AttributeWriter.java,
+	  src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java: Use
+	  more 'standard' Java naming conventions.
+
 2011-06-17  Sascha L. Teichmann <sascha.teichmann@intevation.de>
 
 	Merged in the facet-slt branch to bring in the 'facet' feature.
--- a/flys-artifacts/doc/conf/cache.xml	Fri Jun 17 16:17:03 2011 +0000
+++ b/flys-artifacts/doc/conf/cache.xml	Sat Jun 18 17:03:17 2011 +0000
@@ -39,11 +39,11 @@
 
     <!-- This one is used to cache the computed values.-->
     <cache name="computed.values"
-           maxElementsInMemory="1000"
+           maxElementsInMemory="200"
            eternal="false"
            timeToLiveSeconds="172800"
            overflowToDisk="true"
            diskPersistent="true"
-           memoryStoreEvictionPolicy="LFU"
+           memoryStoreEvictionPolicy="LRU"
            />
 </ehcache>
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Fri Jun 17 16:17:03 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Sat Jun 18 17:03:17 2011 +0000
@@ -196,7 +196,7 @@
         try {
             saveData(target, XPATH_FEED_INPUT, context);
 
-            compute(context, ComputeType.FEED);
+            compute(context, ComputeType.FEED, true);
 
             return describe(target, context);
         }
@@ -245,7 +245,7 @@
             setCurrentStateId(targetState);
 
             logger.debug("Compute data for state: " + targetState);
-            compute(context, ComputeType.ADVANCE);
+            compute(context, ComputeType.ADVANCE, true);
 
             return describe(target, context);
         }
@@ -1076,14 +1076,15 @@
     public String hash() {
         Set<Map.Entry<String, StateData>> entries = data.entrySet();
 
-        int hash  = 0;
-        int shift = 3;
+        long hash  = 0L;
+        int  shift = 3;
 
         for (Map.Entry<String, StateData> entry: entries) {
             String key   = entry.getKey();
             Object value = entry.getValue().getValue();
 
-            hash ^= (key.hashCode() << shift) | (value.hashCode() << 2 * shift);
+            hash ^= ((long)key.hashCode() << shift) 
+                 |  ((long)value.hashCode() << (shift + shift));
             shift += 2;
         }
 
@@ -1098,8 +1099,12 @@
      *
      * @param context The CallContext.
      */
-    public Object compute(CallContext context, ComputeType type) {
-        return compute(context, hash(), type);
+    public Object compute(
+        CallContext context, 
+        ComputeType type, 
+        boolean     generateFacets
+    ) {
+        return compute(context, hash(), type, generateFacets);
     }
 
 
@@ -1114,12 +1119,17 @@
      *
      * @return the computed data.
      */
-    public Object compute(CallContext context, String hash, ComputeType type) {
+    public Object compute(
+        CallContext context,
+        String      hash,
+        ComputeType type, 
+        boolean     generateFacets
+    ) {
         DefaultState current = (DefaultState) getCurrentState(context);
 
         logger.debug("Create ComputeCallback for state: " + current.getID());
 
-        return compute(context, hash, current, type);
+        return compute(context, hash, current, type, generateFacets);
     }
 
 
@@ -1127,11 +1137,12 @@
         CallContext   context,
         String        key,
         DefaultState  state,
-        ComputeType   type
+        ComputeType   type,
+        boolean       generateFacets
     ) {
         String stateID = state.getID();
 
-        List<Facet> fs = new ArrayList<Facet>();
+        List<Facet> fs = generateFacets ? new ArrayList<Facet>() : null;
 
         try {
             Cache cache = CacheFactory.getCache(COMPUTING_CACHE);
@@ -1168,11 +1179,13 @@
             return res;
         }
         finally {
-            if (fs.isEmpty()) {
-                facets.remove(stateID);
-            }
-            else {
-                facets.put(stateID, fs);
+            if (generateFacets) {
+                if (fs.isEmpty()) {
+                    facets.remove(stateID);
+                }
+                else {
+                    facets.put(stateID, fs);
+                }
             }
         }
     }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Fri Jun 17 16:17:03 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Sat Jun 18 17:03:17 2011 +0000
@@ -244,16 +244,16 @@
                 out.getDescription(),
                 out.getMimeType());
 
-            Set<String> outtype = new HashSet<String>();
+            Set<String> outTypes = new HashSet<String>();
 
             for (Facet f: out.getFacets()) {
-                outtype.add(f.getName());
+                outTypes.add(f.getName());
             }
 
             for (Facet f: fs) {
                 String type = f.getName();
 
-                if(outtype.contains(type)) {
+                if (outTypes.contains(type)) {
                     o.addFacet(f);
                 }
             }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DataFacet.java	Fri Jun 17 16:17:03 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DataFacet.java	Sat Jun 18 17:03:17 2011 +0000
@@ -37,7 +37,7 @@
     public Object getData(Artifact artifact, CallContext context) {
         FLYSArtifact flys = (FLYSArtifact)artifact;
         return flys.compute(
-            context, hash != null ? hash : flys.hash(), type);
+            context, hash != null ? hash : flys.hash(), type, false);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DurationCurveFacet.java	Fri Jun 17 16:17:03 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DurationCurveFacet.java	Sat Jun 18 17:03:17 2011 +0000
@@ -27,7 +27,8 @@
 
         WINFOArtifact winfo = (WINFOArtifact) artifact;
 
-        WQDay[] wqday = (WQDay[]) winfo.compute(context, ComputeType.ADVANCE);
+        WQDay[] wqday =
+            (WQDay[])winfo.compute(context, ComputeType.ADVANCE, false);
 
         return wqday[index];
     }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WaterlevelFacet.java	Fri Jun 17 16:17:03 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/WaterlevelFacet.java	Sat Jun 18 17:03:17 2011 +0000
@@ -26,7 +26,8 @@
 
         WINFOArtifact winfo = (WINFOArtifact) artifact;
 
-        WQKms[] wqkms = (WQKms[]) winfo.compute(context, ComputeType.ADVANCE);
+        WQKms[] wqkms =
+            (WQKms[])winfo.compute(context, ComputeType.ADVANCE, false);
 
         return wqkms[index];
     }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/ComputedDischargeCurveState.java	Fri Jun 17 16:17:03 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/ComputedDischargeCurveState.java	Sat Jun 18 17:03:17 2011 +0000
@@ -61,19 +61,21 @@
             }
         }
 
-        Object[] args = new Object[] {
-            winfo.getRiver().getName(),
-            res.getName()
-        };
+        if (facets != null) {
+            Object[] args = new Object[] {
+                winfo.getRiver().getName(),
+                res.getName()
+            };
 
-        String name = Resources.getMsg(
-            context.getMeta(),
-            "chart.computed.discharge.curve.curve.label",
-            "",
-            args);
+            String name = Resources.getMsg(
+                context.getMeta(),
+                "chart.computed.discharge.curve.curve.label",
+                "",
+                args);
 
-        facets.add(new WaterlevelFacet(0, COMPUTED_DISCHARGE_Q, name));
-        facets.add(new DataFacet(CSV, "CSV data"));
+            facets.add(new WaterlevelFacet(0, COMPUTED_DISCHARGE_Q, name));
+            facets.add(new DataFacet(CSV, "CSV data"));
+        }
 
         return new WQKms[] { res };
     }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DischargeLongitudinalSection.java	Fri Jun 17 16:17:03 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DischargeLongitudinalSection.java	Sat Jun 18 17:03:17 2011 +0000
@@ -49,6 +49,10 @@
             }
         }
 
+        if (facets == null) {
+            return res;
+        }
+
         for (int i = 0; i < res.length; i++) {
             String nameW = null;
             String nameQ = null;
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DurationCurveState.java	Fri Jun 17 16:17:03 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DurationCurveState.java	Sat Jun 18 17:03:17 2011 +0000
@@ -58,29 +58,31 @@
             }
         }
 
-        Object[] args = new Object[] {
-            winfo.getRiver().getName()
-        };
-
-        String nameW = Resources.getMsg(
-            context.getMeta(),
-            "chart.duration.curve.curve.w",
-            "",
-            args);
+        if (facets != null) {
+            Object[] args = new Object[] {
+                winfo.getRiver().getName()
+            };
 
-        String nameQ = Resources.getMsg(
-            context.getMeta(),
-            "chart.duration.curve.curve.q",
-            "",
-            args);
+            String nameW = Resources.getMsg(
+                context.getMeta(),
+                "chart.duration.curve.curve.w",
+                "",
+                args);
 
-        Facet w = new DurationCurveFacet(0, DURATION_W, nameW);
-        Facet q = new DurationCurveFacet(0, DURATION_Q, nameQ);
+            String nameQ = Resources.getMsg(
+                context.getMeta(),
+                "chart.duration.curve.curve.q",
+                "",
+                args);
 
-        facets.add(w);
-        facets.add(q);
+            Facet w = new DurationCurveFacet(0, DURATION_W, nameW);
+            Facet q = new DurationCurveFacet(0, DURATION_Q, nameQ);
 
-        facets.add(new DataFacet(CSV, "CSV data"));
+            facets.add(w);
+            facets.add(q);
+
+            facets.add(new DataFacet(CSV, "CSV data"));
+        }
 
         return new WQDay[] { res };
     }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java	Fri Jun 17 16:17:03 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java	Sat Jun 18 17:03:17 2011 +0000
@@ -318,7 +318,9 @@
             res = winfo.getDischargeCurveData();
         }
 
-        // TODO: Generate facets!
+        if (facets != null) {
+            // TODO: Generate facets!
+        }
 
 
         return res;
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WaterlevelState.java	Fri Jun 17 16:17:03 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WaterlevelState.java	Sat Jun 18 17:03:17 2011 +0000
@@ -4,13 +4,8 @@
 
 import org.apache.log4j.Logger;
 
-import org.w3c.dom.Element;
-
-import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.CallContext;
 
-import de.intevation.artifacts.common.utils.XMLUtils;
-
 import de.intevation.artifactdatabase.state.Facet;
 
 import de.intevation.flys.artifacts.FLYSArtifact;
@@ -30,17 +25,6 @@
     private static Logger logger = Logger.getLogger(WaterlevelState.class);
 
 
-    protected Element[] createItems(
-        XMLUtils.ElementCreator cr,
-        Artifact    artifact,
-        String      name,
-        CallContext context)
-    {
-        logger.debug("WaterlevelState.createItems");
-        return null;
-    }
-
-
     @Override
     public Object computeAdvance(
         FLYSArtifact artifact,
@@ -64,6 +48,10 @@
             }
         }
 
+        if (facets == null) {
+            return res;
+        }
+
         for (int i = 0; i < res.length; i++) {
             String nameW = null;
             String nameQ = null;
--- a/flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java	Fri Jun 17 16:17:03 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java	Sat Jun 18 17:03:17 2011 +0000
@@ -1,6 +1,5 @@
 package de.intevation.flys.collections;
 
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
@@ -47,19 +46,16 @@
             ArtifactNamespaceContext.NAMESPACE_PREFIX);
 
         Element attribute = cr.create("attribute");
-        Element outs       = cr.create("outputs");
+        Element outs      = cr.create("outputs");
 
         attribute.appendChild(outs);
         doc.appendChild(attribute);
 
-        Iterator<String> iter = newAttr.keySet().iterator();
-
-        while (iter.hasNext()) {
-            String outName = iter.next();
+        for (String outName: newAttr.keySet()) {
 
             Output a = newAttr.get(outName);
             Output b = oldAttr.get(outName);
-
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
             writeOutput(doc, outs, cr, a, b);
         }
 
@@ -151,4 +147,4 @@
         output.appendChild(theme);
     }
 }
-
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/collections/OutputParser.java	Fri Jun 17 16:17:03 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/collections/OutputParser.java	Sat Jun 18 17:03:17 2011 +0000
@@ -67,7 +67,7 @@
         logger.debug("Artifact has " + num + " outputs.");
 
         for (int i = 0; i < num; i++) {
-            Node out = outs.item(i);
+            Element out = (Element)outs.item(i);
 
             parseOutput(uuid, out);
         }
@@ -91,9 +91,11 @@
     }
 
 
-    protected void parseOutput(String uuid, Node out) {
-        String name = XMLUtils.xpathString(
-            out, "@art:name", ArtifactNamespaceContext.INSTANCE);
+    protected void parseOutput(String uuid, Element out) {
+
+        String uri = ArtifactNamespaceContext.NAMESPACE_URI;
+
+        String name = out.getAttributeNS(uri, "name");
 
         if (outs.get(name) == null) {
             logger.debug("Create new output: " + name);
@@ -104,12 +106,12 @@
     }
 
 
-    protected void newOutput(Node out, String name) {
-        String desc = XMLUtils.xpathString(
-            out, "@art:description", ArtifactNamespaceContext.INSTANCE);
+    protected void newOutput(Element out, String name) {
 
-        String mimetype = XMLUtils.xpathString(
-            out, "@art:mime-type", ArtifactNamespaceContext.INSTANCE);
+        String uri = ArtifactNamespaceContext.NAMESPACE_URI;
+
+        String desc     = out.getAttributeNS(uri, "description");
+        String mimetype = out.getAttributeNS(uri, "mime-type");
 
         Output o = new DefaultOutput(name, desc, mimetype);
 

http://dive4elements.wald.intevation.org