changeset 2119:dc28ea60b53d

Added cached/chunked access to cross section lines. TODO: Use the FastCrossSectionLines. flys-artifacts/trunk@3689 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 16 Jan 2012 15:51:46 +0000
parents 4d57d456e261
children f021080cb409
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/cache.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FastCrossSectionChunk.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FastCrossSectionLine.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FastCrossSectionLineFactory.java
diffstat 5 files changed, 215 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Mon Jan 16 13:40:32 2012 +0000
+++ b/flys-artifacts/ChangeLog	Mon Jan 16 15:51:46 2012 +0000
@@ -1,3 +1,18 @@
+2012-01-16	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* doc/conf/cache.xml: Added cache for fast section lines chunks.
+
+	* src/main/java/de/intevation/flys/artifacts/model/FastCrossSectionChunk.java:
+	  New. Chunks of FastCrossSectionLines. Stores cross section lines for ranges
+	  of one KM.
+
+	* src/main/java/de/intevation/flys/artifacts/model/FastCrossSectionLineFactory.java:
+	  New. Access fast cross section lines. It uses the cache 'fast-cross-section-lines'
+	  configured in cache.xml to store chunks of cross section lines.
+
+	* src/main/java/de/intevation/flys/artifacts/model/FastCrossSectionLine.java:
+	  New. Stores the important data (points) of a cross section line.
+
 2012-01-16  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/states/CalculationSelect.java:
--- a/flys-artifacts/doc/conf/cache.xml	Mon Jan 16 13:40:32 2012 +0000
+++ b/flys-artifacts/doc/conf/cache.xml	Mon Jan 16 15:51:46 2012 +0000
@@ -92,6 +92,14 @@
            memoryStoreEvictionPolicy="LRU"
            />
 
+    <!-- This one is used to load chunks of the cross section lines  -->
+    <cache name="fast-cross-section-lines"
+           maxElementsInMemory="100"
+           eternal="false"
+           timeToLiveSeconds="7200"
+           memoryStoreEvictionPolicy="LRU"
+           />
+
     <!-- This one is used for the cross section lookup 
          Because of lazy fetching and relatively big amount of data, disabled
          cache for now.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FastCrossSectionChunk.java	Mon Jan 16 15:51:46 2012 +0000
@@ -0,0 +1,77 @@
+package de.intevation.flys.artifacts.model;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Collections;
+
+import java.io.Serializable;
+
+import de.intevation.flys.model.CrossSectionLine;
+import de.intevation.flys.model.CrossSection;
+
+public class FastCrossSectionChunk
+implements   Serializable
+{
+    public static final String PREFIX = "FCSC:";
+    public static final double KM_RANGE = 1.0;
+
+    protected double startKm;
+    protected int    crossSectionId;
+
+    protected List<FastCrossSectionLine> crossSectionLines;
+
+    public FastCrossSectionChunk() {
+    }
+
+    public FastCrossSectionChunk(CrossSection cs, double km) {
+
+        crossSectionId = cs.getId();
+        startKm = Math.floor(km);
+        double stopKm = startKm + KM_RANGE;
+
+        List<CrossSectionLine> csls = cs.getLines(startKm, stopKm);
+
+        crossSectionLines = new ArrayList
+            <FastCrossSectionLine>(csls.size());
+
+        for (CrossSectionLine csl: csls) {
+            crossSectionLines.add(new FastCrossSectionLine(csl));
+        }
+    }
+
+    public FastCrossSectionLine getCrossSectionLine(double km) {
+        FastCrossSectionLine key = new FastCrossSectionLine(km);
+        int pos = Collections.binarySearch(
+            crossSectionLines, key, FastCrossSectionLine.KM_CMP);
+        return pos < 0 ? null : crossSectionLines.get(pos);
+    }
+
+    public static String createHashKey(CrossSection cs, double km) {
+        return PREFIX + cs.getId() + ":" + (int)Math.floor(km);
+    }
+
+    public String getHashKey() {
+        return PREFIX + crossSectionId + ":" + (int)Math.floor(startKm);
+    }
+
+    public double getStartKm() {
+        return startKm;
+    }
+
+    public void setStartKm(double startKm) {
+        this.startKm = startKm;
+    }
+
+    public double getStopKm() {
+        return startKm + KM_RANGE;
+    }
+
+    public int getCrossSectionId() {
+        return crossSectionId;
+    }
+
+    public void setCrossSectionId(int crossSectionId) {
+        this.crossSectionId = crossSectionId;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FastCrossSectionLine.java	Mon Jan 16 15:51:46 2012 +0000
@@ -0,0 +1,63 @@
+package de.intevation.flys.artifacts.model;
+
+import java.util.List;
+import java.util.Comparator;
+
+import java.io.Serializable;
+
+import java.math.BigDecimal;
+
+import de.intevation.flys.model.CrossSectionLine;
+
+import java.awt.geom.Point2D;
+
+public class FastCrossSectionLine
+implements   Serializable
+{
+    public static final double EPSILON = 1e-5;
+
+    public static final Comparator<FastCrossSectionLine> KM_CMP =
+        new Comparator<FastCrossSectionLine>() {
+            public int compare(
+                FastCrossSectionLine a,
+                FastCrossSectionLine b
+            ) {
+                double diff = a.km - b.km;
+                if (diff < -EPSILON) return -1;
+                return diff > +EPSILON ? +1 : 0;
+            }
+        };
+
+    protected double km;
+    protected List<Point2D> points;
+
+    public FastCrossSectionLine() {
+    }
+
+    public FastCrossSectionLine(double km) {
+        this.km = km;
+    }
+
+    public FastCrossSectionLine(CrossSectionLine csl) {
+        BigDecimal kmBD = csl.getKm();
+        km = kmBD != null ? kmBD.doubleValue() : 0d;
+        points = csl.fetchCrossSectionLinesPoints();
+    }
+
+    public double getKm() {
+        return km;
+    }
+
+    public void setKm(double km) {
+        this.km = km;
+    }
+
+    public List<Point2D> getPoints() {
+        return points;
+    }
+
+    public void setPoints(List<Point2D> points) {
+        this.points = points;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FastCrossSectionLineFactory.java	Mon Jan 16 15:51:46 2012 +0000
@@ -0,0 +1,52 @@
+package de.intevation.flys.artifacts.model;
+
+import de.intevation.flys.artifacts.cache.CacheFactory;
+
+import de.intevation.flys.model.CrossSection;
+import de.intevation.flys.model.CrossSectionLine;
+
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.Element;
+
+import java.util.List;
+
+
+public class FastCrossSectionLineFactory
+{
+    public static final String CACHE_NAME = "fast-cross-section-lines";
+
+    private FastCrossSectionLineFactory() {
+    }
+
+    public static FastCrossSectionLine getCrossSectionLine(
+        CrossSection cs,
+        double       km
+    ) {
+        Cache cache = CacheFactory.getCache(CACHE_NAME);
+
+        if (cache == null) {
+            List<CrossSectionLine> lines = cs.getLines(km, km);
+            return lines.isEmpty()
+                ? null
+                : new FastCrossSectionLine(lines.get(0));
+        }
+
+        String cacheKey = FastCrossSectionChunk.createHashKey(cs, km);
+
+        Element element = cache.get(cacheKey);
+
+        FastCrossSectionChunk fcsc;
+
+        if (element != null) {
+            fcsc = (FastCrossSectionChunk)element.getValue();
+        }
+        else {
+            fcsc = new FastCrossSectionChunk(cs, km);
+            element = new Element(cacheKey, fcsc);
+            cache.put(element);
+        }
+
+        return fcsc.getCrossSectionLine(km);
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org