# HG changeset patch # User Sascha L. Teichmann # Date 1304877349 0 # Node ID 8ab04de0b879b97554c76c346a4d70e0c7fd18b1 # Parent 67fd63e4ef669f68eda47019f4a78155aac72dec Importer: Cache the discharge table values, too. flys-backend/trunk@1852 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 67fd63e4ef66 -r 8ab04de0b879 flys-backend/ChangeLog --- 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 + + * 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 * src/main/java/de/intevation/flys/importer/ImporterSession.java: diff -r 67fd63e4ef66 -r 8ab04de0b879 flys-backend/src/main/java/de/intevation/flys/importer/IdValueKey.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 : diff -r 67fd63e4ef66 -r 8ab04de0b879 flys-backend/src/main/java/de/intevation/flys/importer/ImportDischargeTableValue.java --- 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 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; diff -r 67fd63e4ef66 -r 8ab04de0b879 flys-backend/src/main/java/de/intevation/flys/importer/ImporterSession.java --- 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 wstColumnValues; + protected Map wstColumnValues; + + protected Map 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(); + wstColumnValues = new HashMap(); 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(); + + Query query = databaseSession.createQuery("from DischargeTableValue"); + + for (Iterator iter = query.iterate(); iter.hasNext();) { + DischargeTableValue dtv = (DischargeTableValue)iter.next(); + dischargeTableValues.put(new IdValueKey(dtv), dtv); } } } diff -r 67fd63e4ef66 -r 8ab04de0b879 flys-backend/src/main/java/de/intevation/flys/importer/WstColumnValueKey.java --- 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 :