annotate backend/src/main/java/org/dive4elements/river/importer/ImporterSession.java @ 8027:1e327d86c898

Load grain fractions from database only. Do not store new ones.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 10 Jul 2014 16:11:55 +0200
parents 4c3ccf2b0304
children cdb9f6d97f6a
rev   line source
5844
4dd33b86dc61 Added header to river backend.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
4dd33b86dc61 Added header to river backend.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
2 * Software engineering by Intevation GmbH
4dd33b86dc61 Added header to river backend.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
3 *
5992
4c3ccf2b0304 Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5844
diff changeset
4 * This file is Free Software under the GNU AGPL (>=v3)
5844
4dd33b86dc61 Added header to river backend.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
5992
4c3ccf2b0304 Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5844
diff changeset
6 * documentation coming with Dive4Elements River for details.
5844
4dd33b86dc61 Added header to river backend.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
7 */
4dd33b86dc61 Added header to river backend.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
8
5829
18619c1e7c2a Repaired internal references.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5828
diff changeset
9 package org.dive4elements.river.importer;
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10
8027
1e327d86c898 Load grain fractions from database only. Do not store new ones.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5992
diff changeset
11 import java.util.HashMap;
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 import java.util.Iterator;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 import java.util.Map;
503
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
14 import java.util.TreeMap;
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 import java.math.BigDecimal;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 import org.hibernate.SessionFactory;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 import org.hibernate.Session;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 import org.hibernate.Query;
499
cce054f27dac Importer: Only accept main value types 'Q', 'W', 'D' and 'T' by default.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 498
diff changeset
20 import org.hibernate.FlushMode;
5829
18619c1e7c2a Repaired internal references.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5828
diff changeset
21 import org.dive4elements.river.backend.SessionFactoryProvider;
8027
1e327d86c898 Load grain fractions from database only. Do not store new ones.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5992
diff changeset
22 import org.dive4elements.river.model.GrainFraction;
5829
18619c1e7c2a Repaired internal references.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5828
diff changeset
23 import org.dive4elements.river.model.WstColumnValue;
18619c1e7c2a Repaired internal references.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5828
diff changeset
24 import org.dive4elements.river.model.WstColumn;
18619c1e7c2a Repaired internal references.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5828
diff changeset
25 import org.dive4elements.river.model.DischargeTableValue;
18619c1e7c2a Repaired internal references.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5828
diff changeset
26 import org.dive4elements.river.model.DischargeTable;
18619c1e7c2a Repaired internal references.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5828
diff changeset
27 import org.dive4elements.river.model.Range;
18619c1e7c2a Repaired internal references.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5828
diff changeset
28 import org.dive4elements.river.model.River;
500
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
29 import org.apache.log4j.Logger;
5829
18619c1e7c2a Repaired internal references.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5828
diff changeset
30 import org.dive4elements.artifacts.common.utils.LRUCache;
1227
cdb35d96ca09 Importer session: Make use of the LRU cache from artifacts common.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 503
diff changeset
31
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 public class ImporterSession
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 {
500
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
34 private static Logger log = Logger.getLogger(ImporterSession.class);
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
35
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 private static final ThreadLocal<ImporterSession> SESSION =
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 new ThreadLocal<ImporterSession>() {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 @Override
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 protected ImporterSession initialValue() {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 return new ImporterSession();
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 };
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 protected Session databaseSession;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45
1227
cdb35d96ca09 Importer session: Make use of the LRU cache from artifacts common.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 503
diff changeset
46 protected LRUCache<Integer, Map<ValueKey, WstColumnValue>>
503
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
47 wstColumnValues;
498
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
48
1227
cdb35d96ca09 Importer session: Make use of the LRU cache from artifacts common.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 503
diff changeset
49 protected LRUCache<Integer, Map<ValueKey, DischargeTableValue>>
501
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
50 dischargeTableValues;
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51
1227
cdb35d96ca09 Importer session: Make use of the LRU cache from artifacts common.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 503
diff changeset
52 protected LRUCache<Integer, Map<ValueKey, Range>>
503
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
53 ranges;
500
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
54
8027
1e327d86c898 Load grain fractions from database only. Do not store new ones.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5992
diff changeset
55 private Map<String, GrainFraction> grainFractions;
1e327d86c898 Load grain fractions from database only. Do not store new ones.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5992
diff changeset
56
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 public static ImporterSession getInstance() {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 return SESSION.get();
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 public ImporterSession() {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 SessionFactory sessionFactory =
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 SessionFactoryProvider.createSessionFactory();
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 databaseSession = sessionFactory.openSession();
502
c6889097f81f Importer: Fix silly bug and set Hibernate session flushing back to auto.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 501
diff changeset
65 //databaseSession.setFlushMode(FlushMode.MANUAL);
501
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
66
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
67 wstColumnValues =
1227
cdb35d96ca09 Importer session: Make use of the LRU cache from artifacts common.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 503
diff changeset
68 new LRUCache<Integer, Map<ValueKey, WstColumnValue>>();
501
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
69
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
70 dischargeTableValues =
1227
cdb35d96ca09 Importer session: Make use of the LRU cache from artifacts common.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 503
diff changeset
71 new LRUCache<Integer, Map<ValueKey, DischargeTableValue>>();
503
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
72
1227
cdb35d96ca09 Importer session: Make use of the LRU cache from artifacts common.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 503
diff changeset
73 ranges = new LRUCache<Integer, Map<ValueKey, Range>>();
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 public Session getDatabaseSession() {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77 return databaseSession;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 public WstColumnValue getWstColumnValue(
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 WstColumn column,
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 BigDecimal position,
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 BigDecimal w
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 ) {
501
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
85 Integer c = column.getId();
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
86
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
87 Map<ValueKey, WstColumnValue> map = wstColumnValues.get(c);
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
88
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
89 if (map == null) {
503
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
90 map = new TreeMap<ValueKey, WstColumnValue>(
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
91 ValueKey.EPSILON_COMPARATOR);
501
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
92 wstColumnValues.put(c, map);
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
93 Query query = databaseSession.createQuery(
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
94 "from WstColumnValue where wstColumn.id=:cid");
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
95 query.setParameter("cid", c);
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
96 for (Iterator iter = query.iterate(); iter.hasNext();) {
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
97 WstColumnValue wcv = (WstColumnValue)iter.next();
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
98 map.put(new ValueKey(wcv.getPosition(), wcv.getW()), wcv);
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
99 }
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101
501
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
102 ValueKey key = new ValueKey(position, w);
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103
501
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
104 WstColumnValue wcv = map.get(key);
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 if (wcv != null) {
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 return wcv;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 wcv = new WstColumnValue(column, position, w);
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112 databaseSession.save(wcv);
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
113
501
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
114 map.put(key, wcv);
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
116 return wcv;
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
117 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118
498
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
119 public DischargeTableValue getDischargeTableValue(
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
120 DischargeTable table,
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
121 BigDecimal q,
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
122 BigDecimal w
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
123 ) {
501
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
124 Integer t = table.getId();
498
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
125
501
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
126 Map<ValueKey, DischargeTableValue> map =
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
127 dischargeTableValues.get(t);
498
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
128
501
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
129 if (map == null) {
503
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
130 map = new TreeMap<ValueKey, DischargeTableValue>(
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
131 ValueKey.EPSILON_COMPARATOR);
501
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
132 dischargeTableValues.put(t, map);
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
133 Query query = databaseSession.createQuery(
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
134 "from DischargeTableValue where dischargeTable.id=:tid");
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
135 query.setParameter("tid", t);
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
136 for (Iterator iter = query.iterate(); iter.hasNext();) {
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
137 DischargeTableValue dctv = (DischargeTableValue)iter.next();
503
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
138 map.put(new ValueKey(dctv.getQ(), dctv.getW()), dctv);
501
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
139 }
498
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
140 }
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
141
501
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
142 ValueKey key = new ValueKey(q, w);
498
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
143
501
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
144 DischargeTableValue dctv = map.get(key);
498
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
145
501
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
146 if (dctv != null) {
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
147 return dctv;
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
148 }
498
8ab04de0b879 Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
149
501
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
150 dctv = new DischargeTableValue(table, q, w);
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
151
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
152 databaseSession.save(dctv);
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
153
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
154 map.put(key, dctv);
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
155
04d449f7f0c9 Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 500
diff changeset
156 return dctv;
500
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
157 }
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
158
8027
1e327d86c898 Load grain fractions from database only. Do not store new ones.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5992
diff changeset
159 public GrainFraction getGrainFraction(String name) {
1e327d86c898 Load grain fractions from database only. Do not store new ones.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5992
diff changeset
160 if (grainFractions == null) {
1e327d86c898 Load grain fractions from database only. Do not store new ones.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5992
diff changeset
161 grainFractions = new HashMap<String, GrainFraction>();
1e327d86c898 Load grain fractions from database only. Do not store new ones.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5992
diff changeset
162 Query query = databaseSession.createQuery("from GrainFraction");
1e327d86c898 Load grain fractions from database only. Do not store new ones.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5992
diff changeset
163 for (Iterator iter = query.iterate(); iter.hasNext();) {
1e327d86c898 Load grain fractions from database only. Do not store new ones.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5992
diff changeset
164 GrainFraction gf = (GrainFraction)iter.next();
1e327d86c898 Load grain fractions from database only. Do not store new ones.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5992
diff changeset
165 grainFractions.put(gf.getName(), gf);
1e327d86c898 Load grain fractions from database only. Do not store new ones.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5992
diff changeset
166 }
1e327d86c898 Load grain fractions from database only. Do not store new ones.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5992
diff changeset
167 }
1e327d86c898 Load grain fractions from database only. Do not store new ones.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5992
diff changeset
168 return grainFractions.get(name);
1e327d86c898 Load grain fractions from database only. Do not store new ones.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5992
diff changeset
169 }
1e327d86c898 Load grain fractions from database only. Do not store new ones.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5992
diff changeset
170
500
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
171 public Range getRange(River river, BigDecimal a, BigDecimal b) {
503
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
172 Integer r = river.getId();
500
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
173
503
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
174 Map<ValueKey, Range> map = ranges.get(r);
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
175
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
176 if (map == null) {
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
177 map = new TreeMap<ValueKey, Range>(
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
178 ValueKey.EPSILON_COMPARATOR);
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
179 ranges.put(r, map);
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
180 Query query = databaseSession.createQuery(
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
181 "from Range where river.id=:rid");
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
182 query.setParameter("rid", r);
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
183 for (Iterator iter = query.iterate(); iter.hasNext();) {
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
184 Range range = (Range)iter.next();
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
185 map.put(new ValueKey(range.getA(), range.getB()), range);
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
186 }
500
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
187 }
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
188
503
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
189 ValueKey key = new ValueKey(a, b);
500
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
190
503
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
191 Range range = map.get(key);
500
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
192
502
c6889097f81f Importer: Fix silly bug and set Hibernate session flushing back to auto.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 501
diff changeset
193 if (range != null) {
500
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
194 return range;
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
195 }
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
196
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
197 range = new Range(a, b, river);
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
198
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
199 databaseSession.save(range);
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
200
503
bcc18293a547 Importer: Added unsharp lookup to avoid numerical problems.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 502
diff changeset
201 map.put(key, range);
500
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
202
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
203 return range;
d50cd3a632e0 Importer: Use BigDecimals in hashing to prevent numerical problems. Cache ranges globally, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 499
diff changeset
204 }
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
205 }
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
206 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org