changeset 2144:a4bdf7d8527e

Use FastAnnotations for LocationProvider. flys-artifacts/trunk@3723 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 19 Jan 2012 13:43:58 +0000
parents c5d24e0587ce
children e92bc9b0ca1d
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/cache.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/AnnotationFacet.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LocationProvider.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/StaticHYKState.java flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java
diffstat 7 files changed, 92 insertions(+), 132 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Jan 19 13:09:00 2012 +0000
+++ b/flys-artifacts/ChangeLog	Thu Jan 19 13:43:58 2012 +0000
@@ -1,3 +1,22 @@
+2012-01-19	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* doc/conf/cache.xml: Removed "annotations" cache.
+
+	* src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java:
+	  Removed annotation caching.
+
+	* src/main/java/de/intevation/flys/artifacts/model/LocationProvider.java:
+	  Changed internal data structure to FastAnnotations which are also cached.
+
+	* src/main/java/de/intevation/flys/artifacts/model/AnnotationFacet.java:
+	  Use FastAnnotations now which are fetched via the LocationProvider.
+
+	* src/main/java/de/intevation/flys/exports/WaterlevelExporter.java: Added
+	  time measurement for writing the CVS output.
+
+	* src/main/java/de/intevation/flys/artifacts/states/StaticHYKState.java:
+	  Removed superfluous import.
+
 2012-01-19	Felix Wolfsteller	<felix.wolfsteller@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/CrossSectionFactory.java,
--- a/flys-artifacts/doc/conf/cache.xml	Thu Jan 19 13:09:00 2012 +0000
+++ b/flys-artifacts/doc/conf/cache.xml	Thu Jan 19 13:43:58 2012 +0000
@@ -47,15 +47,6 @@
            memoryStoreEvictionPolicy="LFU"
            />
 
-    <!-- This one is used to cache the distance infos per river as Lists -->
-    <cache name="annotations"
-           maxElementsInMemory="2000"
-           eternal="false"
-           timeToIdleSeconds="360"
-           timeToLiveSeconds="86400"
-           memoryStoreEvictionPolicy="LFU"
-           />
-
     <!-- This one is used to cache the computed values.-->
     <cache name="computed.values"
            maxElementsInMemory="200"
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java	Thu Jan 19 13:09:00 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/AnnotationArtifact.java	Thu Jan 19 13:43:58 2012 +0000
@@ -8,8 +8,6 @@
 
 import org.apache.log4j.Logger;
 
-import net.sf.ehcache.Cache;
-
 import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.ArtifactNamespaceContext;
 import de.intevation.artifacts.CallContext;
@@ -24,11 +22,7 @@
 import de.intevation.artifacts.common.utils.XMLUtils;
 
 import de.intevation.flys.artifacts.states.DefaultState;
-import de.intevation.flys.artifacts.cache.CacheFactory;
 import de.intevation.flys.artifacts.context.FLYSContext;
-import de.intevation.flys.artifacts.model.AnnotationsFactory;
-
-import de.intevation.flys.model.Annotation;
 
 import de.intevation.flys.utils.FLYSUtils;
 
@@ -43,9 +37,6 @@
     /** The name of the artifact. */
     public static final String ARTIFACT_NAME = "annotation";
 
-    /** Name of cache. */
-    public static final String CACHE_NAME = "annotations";
-
     /** Get river, setup Facets. */
     @Override
     protected void initialize(Artifact artifact, Object context,
@@ -218,58 +209,5 @@
             // state is not valid, so we do not append its outputs.
         }
     } 
-
-
-    /**
-     * Get Annotations for Points (opposed to segments) in river in range.
-     *
-     * @return list of Annotations.
-     */
-    public List<Annotation> getAnnotations() {
-        String river = FLYSUtils.getRiver(this).getName();
-        logger.debug("Search annotations for river: " + river);
-
-        Cache cache = CacheFactory.getCache(CACHE_NAME);
-        String  key = river;
-        Object  old = null;
-
-        if (cache != null) {
-            logger.debug("We are using a cache for annotations.");
-
-            net.sf.ehcache.Element element = cache.get(key);
-            if (element != null) {
-                logger.info("Fetched annotations from cache.");
-                old = element.getValue();
-            }
-        }
-
-        if (old == null) {
-            old = getAnnotationsUncached(river);
-        }
-
-        if (cache != null && old != null) {
-            cache.put(new net.sf.ehcache.Element(key, old));
-        }
-
-        return old != null
-            ? (List<Annotation>) old
-            : new ArrayList<Annotation>();
-    }
-
-
-    /**
-     * Gets Annotations from Session/Database.
-     *
-     * @return List of Annotations fetched fresh from session/database.
-     * @see DistanceInfoService to access cached documents.
-     */
-    protected List<Annotation> getAnnotationsUncached(String river) {
-        logger.info("Fetch annotations from database.");
-
-        List<Annotation> annotations = new ArrayList<Annotation>();
-        annotations = AnnotationsFactory.getPointAnnotations(river);
-
-        return annotations;
-    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/AnnotationFacet.java	Thu Jan 19 13:09:00 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/AnnotationFacet.java	Thu Jan 19 13:43:58 2012 +0000
@@ -1,6 +1,7 @@
 package de.intevation.flys.artifacts.model;
 
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 
 import org.apache.log4j.Logger;
@@ -11,14 +12,21 @@
 import de.intevation.artifacts.CallContext;
 
 import de.intevation.flys.artifacts.AnnotationArtifact;
+
 import de.intevation.flys.jfree.FLYSAnnotation;
 import de.intevation.flys.jfree.StickyAxisAnnotation;
 
 import de.intevation.flys.model.Annotation;
+import de.intevation.flys.model.FastAnnotations;
 
 import de.intevation.artifactdatabase.state.DefaultFacet;
 import de.intevation.artifactdatabase.state.Facet;
 
+import de.intevation.flys.utils.FLYSUtils;
+
+import de.intevation.flys.artifacts.FLYSArtifact;
+
+
 /**
  * Facet to access Annotations (landmarks, POIs) of a river.
  */
@@ -58,13 +66,20 @@
     public Object getData(Artifact artifact, CallContext context) {
         AnnotationArtifact annotationArtifact = (AnnotationArtifact) artifact;
 
-        List<Annotation>       as = annotationArtifact.getAnnotations();
-        List<XYTextAnnotation> xy = new ArrayList<XYTextAnnotation>();
+        String riverName = FLYSUtils.getRivername((FLYSArtifact)artifact);
 
-        for (Annotation a: as) {
+        FastAnnotations fas = LocationProvider.getAnnotations(riverName);
+
+        List<XYTextAnnotation> xy =
+            new ArrayList<XYTextAnnotation>(fas.size());
+
+        for (Iterator<FastAnnotations.Annotation> iter =
+                fas.filter(FastAnnotations.IS_POINT); iter.hasNext();) {
+            FastAnnotations.Annotation fa = iter.next();
+
             xy.add(new StickyAxisAnnotation(
-                a.getPosition().getValue(),
-                (float) a.getRange().getA().doubleValue(),
+                fa.getPosition(),
+                (float)fa.getA(),
                 StickyAxisAnnotation.SimpleAxis.X_AXIS));
         }
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LocationProvider.java	Thu Jan 19 13:09:00 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LocationProvider.java	Thu Jan 19 13:43:58 2012 +0000
@@ -6,61 +6,45 @@
 import org.apache.log4j.Logger;
 
 import de.intevation.flys.model.Annotation;
-import de.intevation.flys.model.Position;
-import de.intevation.flys.model.Range;
+import de.intevation.flys.model.FastAnnotations;
 
 import de.intevation.flys.artifacts.cache.CacheFactory;
-import de.intevation.flys.artifacts.model.AnnotationsFactory;
 
-import java.util.Map;
-import java.util.TreeMap;
-import java.util.Comparator;
-import java.util.Iterator;
-
-import java.io.Serializable;
-
+import de.intevation.flys.artifacts.model.AnnotationsFactory;
 
 public class LocationProvider {
 
-    public static final double EPSILON = 1e-5;
+    private static final Logger log =
+        Logger.getLogger(LocationProvider.class);
 
-    public static class KmComparator
-    implements          Serializable, Comparator<Double>
-    {
-        @Override
-        public int compare(Double a, Double b) {
-            double diff = a - b;
-            if (diff < -EPSILON) return -1;
-            if (diff > +EPSILON) return +1;
-            return 0;
-        }
-    } // class KmComparator
 
     public static final String CACHE_KEY = "location-provider";
 
     public static final String PREFIX = "lp-";
 
 
-    private static final Logger logger =
-        Logger.getLogger(LocationProvider.class);
-
-
     private LocationProvider() {
     }
 
     public static String getLocation(String river, double km) {
 
-        Double KM = Double.valueOf(km);
+        FastAnnotations fas = getAnnotations(river, km);
 
-        return getKmMap(river, KM).get(KM);
+        FastAnnotations.Annotation an = fas.findByKm(km);
+
+        return an != null ? an.getPosition() : null;
     }
 
-    protected static Map<Double, String> getKmMap(String river, double km) {
+    public static FastAnnotations getAnnotations(String river) {
+        return getAnnotations(river, Double.NaN);
+    }
+
+    protected static FastAnnotations getAnnotations(String river, double km) {
 
         Cache cache = CacheFactory.getCache(CACHE_KEY);
 
         if (cache == null) {
-            return uncachedKmMap(river, km);
+            return uncachedAnnotations(river, km);
         }
 
         String key = PREFIX + river;
@@ -68,44 +52,51 @@
         Element element = cache.get(key);
 
         if (element != null) {
-            return (Map<Double, String>)element.getValue();
+            return (FastAnnotations)element.getValue();
         }
 
-        Map<Double, String> map = uncachedKmMap(river, null);
+        FastAnnotations fas = uncachedAnnotations(river, Double.MAX_VALUE);
 
-        cache.put(new Element(key, map));
+        cache.put(new Element(key, fas));
 
-        return map;
+        return fas;
     }
 
-    protected static Map<Double, String> uncachedKmMap(
+    protected static FastAnnotations uncachedAnnotations(
         String river,
-        Double queryKm
+        double km
     ) {
-        Map<Double, String> map =
-            new TreeMap<Double, String>(new KmComparator());
-        
-        if (queryKm != null) {
+        if (km != Double.MAX_VALUE) {
+            // XXX Fake it by using a standard Annotation.
+
             Annotation annotation =
-                AnnotationsFactory.getAnnotation(river, queryKm);
+                AnnotationsFactory.getAnnotation(river, km);
+
             if (annotation != null) {
-                map.put(queryKm, annotation.getPosition().getValue());
+                FastAnnotations.Annotation fa =
+                    new FastAnnotations.Annotation(
+                        km, Double.NaN,
+                        annotation.getPosition().getValue(), null, null,
+                        Double.NaN, Double.NaN);
+                return new FastAnnotations(
+                    new FastAnnotations.Annotation [] { fa });
             }
-            return map;
+
+            return new FastAnnotations(new FastAnnotations.Annotation[0]);
         }
 
-        for (Iterator<Annotation> iter =
-            AnnotationsFactory.getAnnotationsIterator(river);
-            iter.hasNext();
-        ) {
-            Annotation annotation = iter.next();
-            Position pos   = annotation.getPosition();
-            Range    range = annotation.getRange();
-            Double   km    = range.getA().doubleValue();
-            map.put(km, pos.getValue());
+        long startTime = System.currentTimeMillis();
+
+        FastAnnotations fas = new FastAnnotations(river);
+
+        long stopTime = System.currentTimeMillis();
+
+        if (log.isDebugEnabled()) {
+            log.debug("Loading locations took " +
+                (stopTime-startTime)/1000f + " secs.");
         }
 
-        return map;
+        return fas;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/StaticHYKState.java	Thu Jan 19 13:09:00 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/StaticHYKState.java	Thu Jan 19 13:43:58 2012 +0000
@@ -16,9 +16,6 @@
 import de.intevation.flys.artifacts.model.HYKFacet;
 import de.intevation.flys.artifacts.model.HYKFactory;
 
-import de.intevation.flys.utils.FLYSUtils;
-
-
 /**
  * Only state of a HYKArtifact.
  */
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java	Thu Jan 19 13:09:00 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java	Thu Jan 19 13:43:58 2012 +0000
@@ -438,6 +438,8 @@
                 ? Formatter.getWaterlevelW(context).format(value) : null;
         }
 
+        long startTime = System.currentTimeMillis();
+
         for (int i = 0; i < size; i ++) {
             result = wqkms.get(i, result);
 
@@ -462,6 +464,13 @@
                 });
             }
         }
+
+        long stopTime = System.currentTimeMillis();
+
+        if (logger.isDebugEnabled()) {
+            logger.debug("Writing CSV took " +
+                (float)(stopTime-startTime)/1000f + " secs.");
+        }
     }
 
 

http://dive4elements.wald.intevation.org