Mercurial > dive4elements > river
annotate flys-backend/src/main/java/de/intevation/flys/importer/ImporterSession.java @ 502:c6889097f81f
Importer: Fix silly bug and set Hibernate session flushing back to auto.
flys-backend/trunk@1856 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 09 May 2011 00:38:13 +0000 |
parents | 04d449f7f0c9 |
children | bcc18293a547 |
rev | line source |
---|---|
497
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.flys.importer; |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 import java.util.Iterator; |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 import java.util.Map; |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 import java.util.HashMap; |
501
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
6 import java.util.LinkedHashMap; |
497
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 import java.math.BigDecimal; |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 import org.hibernate.SessionFactory; |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 import org.hibernate.Session; |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 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
|
13 import org.hibernate.FlushMode; |
497
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 import de.intevation.flys.backend.SessionFactoryProvider; |
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 de.intevation.flys.model.WstColumnValue; |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 import de.intevation.flys.model.WstColumn; |
498
8ab04de0b879
Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
497
diff
changeset
|
19 import de.intevation.flys.model.DischargeTableValue; |
8ab04de0b879
Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
497
diff
changeset
|
20 import de.intevation.flys.model.DischargeTable; |
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
|
21 import de.intevation.flys.model.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
|
22 import de.intevation.flys.model.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
|
23 |
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
|
24 import org.apache.log4j.Logger; |
497
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 public class ImporterSession |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 { |
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
|
28 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
|
29 |
501
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
30 public static final int MAX_WST_CACHE_SIZE = 20; |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
31 public static final int MAX_AT_CACHE_SIZE = 20; |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
32 |
497
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 private static final ThreadLocal<ImporterSession> SESSION = |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 new ThreadLocal<ImporterSession>() { |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 @Override |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 protected ImporterSession initialValue() { |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 return new ImporterSession(); |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 } |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 }; |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 protected Session databaseSession; |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 |
501
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
43 protected Map<Integer, Map<ValueKey, WstColumnValue>> wstColumnValues; |
498
8ab04de0b879
Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
497
diff
changeset
|
44 |
501
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
45 protected Map<Integer, Map<ValueKey, DischargeTableValue>> |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
46 dischargeTableValues; |
497
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 |
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
|
48 protected Map<IdValueKey, Range> ranges; |
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
|
49 |
497
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 public static ImporterSession getInstance() { |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 return SESSION.get(); |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 } |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 public ImporterSession() { |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 SessionFactory sessionFactory = |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 SessionFactoryProvider.createSessionFactory(); |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 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
|
58 //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
|
59 |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
60 wstColumnValues = |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
61 new LinkedHashMap<Integer, Map<ValueKey, WstColumnValue>>() { |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
62 @Override |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
63 protected boolean removeEldestEntry( |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
64 Map.Entry<Integer, Map<ValueKey, WstColumnValue>> eldest |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
65 ) { |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
66 return size() > MAX_WST_CACHE_SIZE; |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
67 } |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
68 }; |
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 = |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
71 new LinkedHashMap<Integer, Map<ValueKey, DischargeTableValue>>() { |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
72 @Override |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
73 protected boolean removeEldestEntry( |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
74 Map.Entry<Integer, Map<ValueKey, DischargeTableValue>> eldest |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
75 ) { |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
76 return size() > MAX_AT_CACHE_SIZE; |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
77 } |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
78 }; |
497
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 |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 public Session getDatabaseSession() { |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 return databaseSession; |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 } |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
84 |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
85 public WstColumnValue getWstColumnValue( |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
86 WstColumn column, |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
87 BigDecimal position, |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
88 BigDecimal w |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
89 ) { |
501
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
90 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
|
91 |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
92 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
|
93 |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
94 if (map == null) { |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
95 map = new HashMap<ValueKey, WstColumnValue>(); |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
96 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
|
97 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
|
98 "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
|
99 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
|
100 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
|
101 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
|
102 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
|
103 } |
497
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
104 } |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
105 |
501
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
106 ValueKey key = new ValueKey(position, w); |
497
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
107 |
501
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
108 WstColumnValue wcv = map.get(key); |
497
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 if (wcv != null) { |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
111 return wcv; |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
112 } |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
113 |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
114 wcv = new WstColumnValue(column, position, w); |
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 databaseSession.save(wcv); |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
117 |
501
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
118 map.put(key, wcv); |
497
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
119 |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
120 return wcv; |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
121 } |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
122 |
498
8ab04de0b879
Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
497
diff
changeset
|
123 public DischargeTableValue getDischargeTableValue( |
8ab04de0b879
Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
497
diff
changeset
|
124 DischargeTable table, |
8ab04de0b879
Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
497
diff
changeset
|
125 BigDecimal q, |
8ab04de0b879
Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
497
diff
changeset
|
126 BigDecimal w |
8ab04de0b879
Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
497
diff
changeset
|
127 ) { |
501
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
128 Integer t = table.getId(); |
498
8ab04de0b879
Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
497
diff
changeset
|
129 |
501
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
130 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
|
131 dischargeTableValues.get(t); |
498
8ab04de0b879
Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
497
diff
changeset
|
132 |
501
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
133 if (map == null) { |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
134 map = new HashMap<ValueKey, DischargeTableValue>(); |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
135 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
|
136 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
|
137 "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
|
138 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
|
139 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
|
140 DischargeTableValue dctv = (DischargeTableValue)iter.next(); |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
141 map.put(new ValueKey(q, w), dctv); |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
142 } |
498
8ab04de0b879
Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
497
diff
changeset
|
143 } |
8ab04de0b879
Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
497
diff
changeset
|
144 |
501
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
145 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
|
146 |
501
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
147 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
|
148 |
501
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
149 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
|
150 return dctv; |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
151 } |
498
8ab04de0b879
Importer: Cache the discharge table values, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
497
diff
changeset
|
152 |
501
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
153 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
|
154 |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
155 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
|
156 |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
157 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
|
158 |
04d449f7f0c9
Importer: Change caching strategy not to cause OOM any more.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
500
diff
changeset
|
159 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
|
160 } |
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
|
161 |
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
|
162 public Range getRange(River river, BigDecimal a, BigDecimal b) { |
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
|
163 |
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
|
164 if (ranges == null) { |
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
|
165 loadRanges(); |
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
|
166 } |
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
|
167 |
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
|
168 IdValueKey key = new IdValueKey(river.getId(), a, b); |
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
|
169 |
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
|
170 Range range = ranges.get(key); |
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 |
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
|
172 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
|
173 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
|
174 } |
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
|
175 |
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
|
176 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
|
177 |
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
|
178 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
|
179 |
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
|
180 ranges.put(key, 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
|
181 |
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
|
182 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
|
183 } |
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
|
184 |
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
|
185 protected void loadRanges() { |
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
|
186 log.info("load ranges"); |
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 ranges = new HashMap<IdValueKey, 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
|
188 |
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
|
189 Query query = databaseSession.createQuery("from 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
|
190 |
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
|
191 for (Iterator iter = query.iterate(); iter.hasNext();) { |
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 Range range = (Range)iter.next(); |
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
|
193 ranges.put(new IdValueKey(range), 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
|
194 } |
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 log.info(ranges.size() + " values loaded"); |
497
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
196 } |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
197 } |
67fd63e4ef66
Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
198 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |