changeset 498:8ab04de0b879

Importer: Cache the discharge table values, too. flys-backend/trunk@1852 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 08 May 2011 17:55:49 +0000
parents 67fd63e4ef66
children cce054f27dac
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/IdValueKey.java flys-backend/src/main/java/de/intevation/flys/importer/ImportDischargeTableValue.java flys-backend/src/main/java/de/intevation/flys/importer/ImporterSession.java flys-backend/src/main/java/de/intevation/flys/importer/WstColumnValueKey.java
diffstat 5 files changed, 121 insertions(+), 77 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Sun May 08 15:29:45 2011 +0000
+++ b/flys-backend/ChangeLog	Sun May 08 17:55:49 2011 +0000
@@ -1,3 +1,15 @@
+2011-05-08	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/importer/WstColumnValueKey.java: Deleted
+	* src/main/java/de/intevation/flys/importer/IdValueKey.java: Reinserted
+	  here in a more generalized form.
+
+	* src/main/java/de/intevation/flys/importer/ImporterSession.java:
+	  Cache the discharge table value, too.
+
+	* src/main/java/de/intevation/flys/importer/ImportDischargeTableValue.java:
+	  Use the global cache.
+
 2011-05-08	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/importer/ImporterSession.java:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/IdValueKey.java	Sun May 08 17:55:49 2011 +0000
@@ -0,0 +1,59 @@
+package de.intevation.flys.importer;
+
+import de.intevation.flys.model.WstColumnValue;
+import de.intevation.flys.model.DischargeTableValue;
+
+
+public class IdValueKey {
+
+    protected int    id;
+    protected double a;
+    protected double b;
+
+
+    public IdValueKey(WstColumnValue value) {
+        this.id = value.getWstColumn().getId();
+        this.a  = value.getPosition().doubleValue();
+        this.b  = value.getW().doubleValue();
+    }
+
+    public IdValueKey(DischargeTableValue value) {
+        this.id = value.getDischargeTable().getId();
+        this.a  = value.getQ().doubleValue();
+        this.b  = value.getW().doubleValue();
+    }
+
+
+    public IdValueKey(int id, double a, double b) {
+        this.id = id;
+        this.a  = a;
+        this.b  = b;
+    }
+
+    private static final int hashDouble(double d) {
+        long v = Double.doubleToLongBits(d);
+        return (int)(v^(v>>>32));
+    }
+
+    public int hashCode() {
+        int a = id;
+        int b = hashDouble(a);
+        int c = hashDouble(b);
+
+        return (a | (b << 10) | (c << 20));
+    }
+
+
+    public boolean equals(Object obj) {
+        if (!(obj instanceof IdValueKey)) {
+            return false;
+        }
+
+        IdValueKey other = (IdValueKey) obj;
+
+        return id == other.id
+            && a  == other.a
+            && b  == other.b;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportDischargeTableValue.java	Sun May 08 15:29:45 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportDischargeTableValue.java	Sun May 08 17:55:49 2011 +0000
@@ -35,23 +35,8 @@
 
     public DischargeTableValue getPeer(DischargeTable dischargeTable) {
         if (peer == null) {
-            Session session = ImporterSession.getInstance().getDatabaseSession();
-
-            Query query = session.createQuery(
-                "from DischargeTableValue where " +
-                "dischargeTable.id=:tableId and q=:q and w=:w");
-            query.setParameter("tableId", dischargeTable.getId());
-            query.setParameter("q", q);
-            query.setParameter("w", w);
-
-            List<DischargeTableValue> dischargeTableValues = query.list();
-            if (dischargeTableValues.isEmpty()) {
-                peer = new DischargeTableValue(dischargeTable, q, w);
-                session.save(peer);
-            }
-            else {
-                peer = dischargeTableValues.get(0);
-            }
+            peer = ImporterSession.getInstance()
+                .getDischargeTableValue(dischargeTable, q, w);
         }
 
         return peer;
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImporterSession.java	Sun May 08 15:29:45 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImporterSession.java	Sun May 08 17:55:49 2011 +0000
@@ -14,6 +14,8 @@
 
 import de.intevation.flys.model.WstColumnValue;
 import de.intevation.flys.model.WstColumn;
+import de.intevation.flys.model.DischargeTableValue;
+import de.intevation.flys.model.DischargeTable;
 
 public class ImporterSession
 {
@@ -27,7 +29,9 @@
 
     protected Session databaseSession;
 
-    protected Map<WstColumnValueKey, WstColumnValue> wstColumnValues;
+    protected Map<IdValueKey, WstColumnValue> wstColumnValues;
+
+    protected Map<IdValueKey, DischargeTableValue> dischargeTableValues;
 
     public static ImporterSession getInstance() {
         return SESSION.get();
@@ -52,7 +56,7 @@
             loadWstColumnValues();
         }
 
-        WstColumnValueKey key = new WstColumnValueKey(
+        IdValueKey key = new IdValueKey(
             column.getId(),
             position.doubleValue(),
             w.doubleValue());
@@ -73,13 +77,53 @@
     }
 
     protected void loadWstColumnValues() {
-        wstColumnValues = new HashMap<WstColumnValueKey, WstColumnValue>();
+        wstColumnValues = new HashMap<IdValueKey, WstColumnValue>();
 
         Query query = databaseSession.createQuery("from WstColumnValue");
 
         for (Iterator iter = query.iterate(); iter.hasNext();) {
             WstColumnValue wcv = (WstColumnValue)iter.next();
-            wstColumnValues.put(new WstColumnValueKey(wcv), wcv);
+            wstColumnValues.put(new IdValueKey(wcv), wcv);
+        }
+    }
+
+    public DischargeTableValue getDischargeTableValue(
+        DischargeTable table,
+        BigDecimal     q,
+        BigDecimal     w
+    ) {
+        if (dischargeTableValues == null) {
+            loadDischargeTableValues();
+        }
+
+        IdValueKey key = new IdValueKey(
+            table.getId(),
+            q.doubleValue(),
+            w.doubleValue());
+
+        DischargeTableValue dtv = dischargeTableValues.get(key);
+
+        if (dtv != null) {
+            return dtv;
+        }
+
+        dtv = new DischargeTableValue(table, q, w);
+
+        databaseSession.save(dtv);
+
+        dischargeTableValues.put(key, dtv);
+
+        return dtv;
+    }
+
+    protected void loadDischargeTableValues() {
+        dischargeTableValues = new HashMap<IdValueKey, DischargeTableValue>();
+
+        Query query = databaseSession.createQuery("from DischargeTableValue");
+
+        for (Iterator iter = query.iterate(); iter.hasNext();) {
+            DischargeTableValue dtv = (DischargeTableValue)iter.next();
+            dischargeTableValues.put(new IdValueKey(dtv), dtv);
         }
     }
 }
--- a/flys-backend/src/main/java/de/intevation/flys/importer/WstColumnValueKey.java	Sun May 08 15:29:45 2011 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,56 +0,0 @@
-package de.intevation.flys.importer;
-
-import de.intevation.flys.model.WstColumnValue;
-
-
-public class WstColumnValueKey {
-
-    protected int    wstColumnId;
-    protected double position;
-    protected double w;
-
-
-    public WstColumnValueKey(WstColumnValue value) {
-        this.wstColumnId = value.getWstColumn().getId();
-        this.position    = value.getPosition().doubleValue();
-        this.w           = value.getW().doubleValue();
-    }
-
-
-    public WstColumnValueKey(int wstColumnId, double position, double w) {
-        this.wstColumnId = wstColumnId;
-        this.position    = position;
-        this.w           = w;
-    }
-
-
-    public int hashCode() {
-        int a = new Integer(wstColumnId).hashCode();
-        int b = new Double(position).hashCode();
-        int c = new Double(w).hashCode();
-
-        return (a ^ (b << 3) ^ (c << 6));
-    }
-
-
-    public boolean equals(Object obj) {
-        if (!(obj instanceof WstColumnValueKey)) {
-            return false;
-        }
-
-        WstColumnValueKey other = (WstColumnValueKey) obj;
-
-        if (this.wstColumnId != other.wstColumnId) {
-            return false;
-        }
-        else if (this.position != other.position) {
-            return false;
-        }
-        else if (this.w != other.w) {
-            return false;
-        }
-
-        return true;
-    }
-}
-// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org