# HG changeset patch # User Sascha L. Teichmann # Date 1326980638 0 # Node ID a4bdf7d8527efa3e1468320910c57ee0f68a29f1 # Parent c5d24e0587ce7bd3b26980b6c22f22750c4d318d Use FastAnnotations for LocationProvider. flys-artifacts/trunk@3723 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r c5d24e0587ce -r a4bdf7d8527e flys-artifacts/ChangeLog --- 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 + + * 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 * src/main/java/de/intevation/flys/artifacts/model/CrossSectionFactory.java, diff -r c5d24e0587ce -r a4bdf7d8527e flys-artifacts/doc/conf/cache.xml --- 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" /> - - - 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) old - : new ArrayList(); - } - - - /** - * Gets Annotations from Session/Database. - * - * @return List of Annotations fetched fresh from session/database. - * @see DistanceInfoService to access cached documents. - */ - protected List getAnnotationsUncached(String river) { - logger.info("Fetch annotations from database."); - - List annotations = new ArrayList(); - annotations = AnnotationsFactory.getPointAnnotations(river); - - return annotations; - } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r c5d24e0587ce -r a4bdf7d8527e flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/AnnotationFacet.java --- 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 as = annotationArtifact.getAnnotations(); - List xy = new ArrayList(); + String riverName = FLYSUtils.getRivername((FLYSArtifact)artifact); - for (Annotation a: as) { + FastAnnotations fas = LocationProvider.getAnnotations(riverName); + + List xy = + new ArrayList(fas.size()); + + for (Iterator 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)); } diff -r c5d24e0587ce -r a4bdf7d8527e flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/LocationProvider.java --- 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 - { - @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 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)element.getValue(); + return (FastAnnotations)element.getValue(); } - Map 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 uncachedKmMap( + protected static FastAnnotations uncachedAnnotations( String river, - Double queryKm + double km ) { - Map map = - new TreeMap(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 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 : diff -r c5d24e0587ce -r a4bdf7d8527e flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/StaticHYKState.java --- 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. */ diff -r c5d24e0587ce -r a4bdf7d8527e flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java --- 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."); + } }