[PATCH 1 of 2] (issue1838) Get min and max directly from database

Wald Commits scm-commit at wald.intevation.org
Tue Aug 18 08:19:17 CEST 2015


# HG changeset patch
# User Tom Gottfried <tom at intevation.de>
# Date 1439799485 -7200
# Node ID d5917ff74d8a22cdf90bb04cc5d6eecb2501645a
# Parent  8179cca1796a493bca731358fb53e02e976bc688
(issue1838) Get min and max directly from database.

   The former implementation led to a connection leak.

diff -r 8179cca1796a -r d5917ff74d8a backend/src/main/java/org/dive4elements/river/model/River.java
--- a/backend/src/main/java/org/dive4elements/river/model/River.java	Fri Aug 14 18:24:04 2015 +0200
+++ b/backend/src/main/java/org/dive4elements/river/model/River.java	Mon Aug 17 10:18:05 2015 +0200
@@ -379,42 +379,23 @@
      * @return the min and max distance of this river.
      */
     public double[] determineMinMaxDistance() {
-        List<Gauge> gauges = getGauges();
+        Session session = SessionHolder.HOLDER.get();
 
-        if (gauges == null || gauges.isEmpty()) {
-            return null;
+        Query query = session.createQuery(
+            "select min(range.a), max(range.b) from Gauge "
+            + "where river=:river "
+            + "and range is not null");
+        query.setParameter("river", this);
+
+        List<Object[]> result = query.list();
+
+        if (!result.isEmpty()) {
+            Object[] minMax = result.get(0);
+            return new double[] { ((BigDecimal)minMax[0]).doubleValue(),
+                ((BigDecimal)minMax[1]).doubleValue() };
         }
 
-        double minmax[] = new double[] { Double.MAX_VALUE, -Double.MAX_VALUE };
-
-        for (Gauge g: gauges) {
-            Range r = g.getRange();
-
-            if (r == null) {
-                continue;
-            }
-
-            double a = r.getA().doubleValue();
-            if (a < minmax[0]) {
-                minmax[0] = a;
-            }
-            if (a > minmax[1]) {
-                minmax[1] = a;
-            }
-
-            BigDecimal bigB = r.getB();
-            if (bigB != null) {
-                double b = bigB.doubleValue();
-                if (b < minmax[0]) {
-                    minmax[0] = b;
-                }
-                if (b > minmax[1]) {
-                    minmax[1] = b;
-                }
-            }
-        }
-
-        return minmax;
+        return null;
     }
 
     public Map<Double, Double> queryGaugeDatumsKMs() {


More information about the Dive4Elements-commits mailing list