changeset 3143:29022c93027d

FixA: Create a cached GaugeFinderFactory to access the gauge along a river flys-artifacts/trunk@4751 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 21 Jun 2012 16:46:05 +0000
parents 9aed2e4de3ca
children 05a7298c4f20
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/cache.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsOverview.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugeFinder.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugeFinderFactory.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java
diffstat 6 files changed, 170 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Jun 21 15:50:58 2012 +0000
+++ b/flys-artifacts/ChangeLog	Thu Jun 21 16:46:05 2012 +0000
@@ -1,3 +1,22 @@
+2012-06-21	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/GaugeFinderFactory.java:
+	  New. Cache access to per river gauge finders. This very useful
+	  if you want to draw the discharge sectors of river at a given
+	  km into a diagram.
+
+	* doc/conf/cache.xml: Added cache 'gauge-finders'.
+
+	* src/main/java/de/intevation/flys/artifacts/model/GaugeFinder.java:
+	  Added find(double km) method to find GaugeRange by km.
+
+	* src/main/java/de/intevation/flys/artifacts/model/FixingsOverview.java:
+	  Uses the instance of the GaugeFinderFactory to access the
+	  gauge ranges.
+
+	* src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java:
+	  Simplified code.
+
 2012-06-21	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/GaugeFinder.java:
--- a/flys-artifacts/doc/conf/cache.xml	Thu Jun 21 15:50:58 2012 +0000
+++ b/flys-artifacts/doc/conf/cache.xml	Thu Jun 21 16:46:05 2012 +0000
@@ -124,6 +124,16 @@
            diskPersistent="true"
            />
 
+    <!-- This one is used to load gauge finders -->
+    <cache name="gauge-finders"
+           maxElementsInMemory="15"
+           eternal="false"
+           timeToLiveSeconds="14400"
+           memoryStoreEvictionPolicy="LRU"
+           overflowToDisk="true"
+           diskPersistent="true"
+           />
+
     <!-- This one is used for the cross section lookup 
          Because of lazy fetching and relatively big amount of data, disabled
          cache for now.
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsOverview.java	Thu Jun 21 15:50:58 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsOverview.java	Thu Jun 21 16:46:05 2012 +0000
@@ -37,17 +37,6 @@
         "WHERE" +
         "    name = :name";
 
-    public static final String SQL_GAUGES =
-        "SELECT" +
-        "    g.id AS gauge_id," +
-        "    r.a  AS a," +
-        "    r.b  AS b " +
-        "FROM gauges g" +
-        "    JOIN ranges r ON g.range_id = r.id " +
-        "WHERE" +
-        "    g.river_id = :river_id " +
-        "ORDER BY r.a";
-
     public static final String SQL_FIXINGS =
         "SELECT" +
         "    id AS wst_id," +
@@ -382,35 +371,6 @@
         return true;
     }
 
-    protected GaugeFinder loadGauges(Session session) {
-        SQLQuery query = session.createSQLQuery(SQL_GAUGES)
-            .addScalar("gauge_id", StandardBasicTypes.INTEGER)
-            .addScalar("a",        StandardBasicTypes.DOUBLE)
-            .addScalar("b",        StandardBasicTypes.DOUBLE);
-
-        query.setInteger("river_id", riverId);
-
-        List<Object []> list = query.list();
-
-        if (list.isEmpty()) {
-            log.warn("River " + riverId + " has no gauges.");
-            return null;
-        }
-
-        List<GaugeRange> gauges = new ArrayList<GaugeRange>();
-
-        for (Object [] row: list) {
-            int    gaugeId = (Integer)row[0];
-            double start   = (Double) row[1];
-            double end     = (Double) row[2];
-            GaugeRange gauge = new GaugeRange(start, end, gaugeId);
-            gauges.add(gauge);
-        }
-
-        return new GaugeFinder(gauges, isKmUp);
-    }
-
-
     protected void loadFixings(Session session) {
         SQLQuery query = session.createSQLQuery(SQL_FIXINGS)
             .addScalar("wst_id",      StandardBasicTypes.INTEGER)
@@ -480,10 +440,11 @@
             return false;
         }
 
-        GaugeFinder gaugeFinder = loadGauges(session);
+        GaugeFinderFactory gff = GaugeFinderFactory.getInstance();
 
-        if (gaugeFinder == null
-        || !gaugeFinder.loadDischargeSectors(session, riverId)) {
+        GaugeFinder gaugeFinder = gff.getGaugeFinder(riverId, isKmUp);
+
+        if (gaugeFinder == null) {
             return false;
         }
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugeFinder.java	Thu Jun 21 15:50:58 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugeFinder.java	Thu Jun 21 16:46:05 2012 +0000
@@ -62,8 +62,7 @@
         this.isKmUp = isKmUp;
     }
 
-    public GaugeRange find(Range range) {
-        double km = isKmUp ? range.start : range.end;
+    public GaugeRange find(double km) {
         for (GaugeRange gauge: gauges) {
             if (gauge.inside(km)) {
                 return gauge;
@@ -72,6 +71,10 @@
         return null;
     }
 
+    public GaugeRange find(Range range) {
+        return find(isKmUp ? range.start : range.end);
+    }
+
     public GaugeRange find(int gaugeId) {
         for (GaugeRange gauge: gauges) {
             if (gauge.gaugeId == gaugeId) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugeFinderFactory.java	Thu Jun 21 16:46:05 2012 +0000
@@ -0,0 +1,130 @@
+package de.intevation.flys.artifacts.model;
+
+import de.intevation.flys.artifacts.cache.CacheFactory;
+
+import de.intevation.flys.backend.SessionHolder;
+
+import de.intevation.flys.model.River;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.Element;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.SQLQuery;
+import org.hibernate.Session;
+
+import org.hibernate.type.StandardBasicTypes;
+
+public class GaugeFinderFactory
+{
+    private static Logger log = Logger.getLogger(GaugeFinderFactory.class);
+
+    public static final String CACHE_NAME = "gauge-finders";
+
+    public static final String SQL_GAUGES =
+        "SELECT" +
+        "    g.id AS gauge_id," +
+        "    r.a  AS a," +
+        "    r.b  AS b " +
+        "FROM gauges g" +
+        "    JOIN ranges r ON g.range_id = r.id " +
+        "WHERE" +
+        "    g.river_id = :river_id " +
+        "ORDER BY r.a";
+
+    private static GaugeFinderFactory INSTANCE;
+
+    protected GaugeFinderFactory() {
+    }
+
+    public static synchronized GaugeFinderFactory getInstance() {
+        if (INSTANCE == null) {
+            INSTANCE = new GaugeFinderFactory();
+        }
+
+        return INSTANCE;
+    }
+
+    public GaugeFinder getGaugeFinder(String riverName) {
+        River river = RiverFactory.getRiver(riverName);
+        return river != null
+            ? getGaugeFinder(river.getId(), river.getKmUp())
+            : null;
+    }
+
+    public synchronized GaugeFinder getGaugeFinder(
+        int     riverId,
+        boolean isKmUp
+    ) {
+        Cache cache = CacheFactory.getCache(CACHE_NAME);
+
+        if (cache == null) {
+            return getUncached(riverId, isKmUp);
+        }
+
+        String cacheKey = riverId + "-" + isKmUp;
+        Element element = cache.get(cacheKey);
+
+        if (element != null) {
+            return (GaugeFinder)element.getValue();
+        }
+
+        GaugeFinder finder = getUncached(riverId, isKmUp);
+
+        if (finder != null) {
+            cache.put(new Element(cacheKey, finder));
+        }
+
+        return finder;
+    }
+
+    protected GaugeFinder loadGauges(
+        Session session,
+        int     riverId,
+        boolean isKmUp
+    ) {
+        SQLQuery query = session.createSQLQuery(SQL_GAUGES)
+            .addScalar("gauge_id", StandardBasicTypes.INTEGER)
+            .addScalar("a",        StandardBasicTypes.DOUBLE)
+            .addScalar("b",        StandardBasicTypes.DOUBLE);
+
+        query.setInteger("river_id", riverId);
+
+        List<Object []> list = query.list();
+
+        if (list.isEmpty()) {
+            log.warn("River " + riverId + " has no gauges.");
+            return null;
+        }
+
+        List<GaugeRange> gauges = new ArrayList<GaugeRange>();
+
+        for (Object [] row: list) {
+            int    gaugeId = (Integer)row[0];
+            double start   = (Double) row[1];
+            double end     = (Double) row[2];
+            GaugeRange gauge = new GaugeRange(start, end, gaugeId);
+            gauges.add(gauge);
+        }
+
+        return new GaugeFinder(gauges, isKmUp);
+    }
+
+    protected GaugeFinder getUncached(int riverId, boolean isKmUp) {
+        Session session = SessionHolder.HOLDER.get();
+
+        GaugeFinder finder = loadGauges(session, riverId, isKmUp);
+
+        if (finder == null
+        || !finder.loadDischargeSectors(session, riverId)) {
+            return null;
+        }
+        
+        return finder;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java	Thu Jun 21 15:50:58 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RiverFactory.java	Thu Jun 21 16:46:05 2012 +0000
@@ -45,7 +45,7 @@
 
         List<River> rivers = query.list();
 
-        return (rivers != null && rivers.size() > 0) ? rivers.get(0) : null;
+        return rivers.isEmpty() ? null : rivers.get(0);
     }
 
 
@@ -65,7 +65,7 @@
 
         List<River> rivers = query.list();
 
-        return (rivers != null && rivers.size() > 0) ? rivers.get(0) : null;
+        return rivers.isEmpty() ? null : rivers.get(0);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org