Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/ImportFlowVelocityModelValue.java @ 8986:392bbcd8a88b
Database inserts accelerated by suppressing unnecessary database queries for new data series
author | mschaefer |
---|---|
date | Sun, 08 Apr 2018 18:07:06 +0200 |
parents | 5e38e2924c07 |
children |
comparison
equal
deleted
inserted
replaced
8985:27851cfda84a | 8986:392bbcd8a88b |
---|---|
7 */ | 7 */ |
8 | 8 |
9 package org.dive4elements.river.importer; | 9 package org.dive4elements.river.importer; |
10 | 10 |
11 import java.math.BigDecimal; | 11 import java.math.BigDecimal; |
12 | |
13 import java.util.List; | 12 import java.util.List; |
14 | 13 |
15 import org.hibernate.Session; | 14 import org.dive4elements.river.importer.common.StoreMode; |
16 import org.hibernate.Query; | |
17 | |
18 import org.dive4elements.river.model.FlowVelocityModel; | 15 import org.dive4elements.river.model.FlowVelocityModel; |
19 import org.dive4elements.river.model.FlowVelocityModelValue; | 16 import org.dive4elements.river.model.FlowVelocityModelValue; |
17 import org.hibernate.Query; | |
18 import org.hibernate.Session; | |
20 | 19 |
21 | 20 |
22 public class ImportFlowVelocityModelValue { | 21 public class ImportFlowVelocityModelValue { |
23 | 22 |
24 private BigDecimal station; | 23 private final BigDecimal station; |
25 private BigDecimal q; | 24 private final BigDecimal q; |
26 private BigDecimal totalChannel; | 25 private final BigDecimal totalChannel; |
27 private BigDecimal mainChannel; | 26 private final BigDecimal mainChannel; |
28 private BigDecimal shearStress; | 27 private final BigDecimal shearStress; |
29 | 28 |
30 private FlowVelocityModelValue peer; | 29 private FlowVelocityModelValue peer; |
31 | 30 |
32 | 31 |
33 public ImportFlowVelocityModelValue( | 32 public ImportFlowVelocityModelValue( |
34 BigDecimal station, | 33 final BigDecimal station, |
35 BigDecimal q, | 34 final BigDecimal q, |
36 BigDecimal totalChannel, | 35 final BigDecimal totalChannel, |
37 BigDecimal mainChannel, | 36 final BigDecimal mainChannel, |
38 BigDecimal shearStress | 37 final BigDecimal shearStress |
39 ) { | 38 ) { |
40 this.station = station; | 39 this.station = station; |
41 this.q = q; | 40 this.q = q; |
42 this.totalChannel = totalChannel; | 41 this.totalChannel = totalChannel; |
43 this.mainChannel = mainChannel; | 42 this.mainChannel = mainChannel; |
44 this.shearStress = shearStress; | 43 this.shearStress = shearStress; |
45 } | 44 } |
46 | 45 |
47 | 46 |
48 public void storeDependencies(FlowVelocityModel model) { | 47 public void storeDependencies(final FlowVelocityModel model, final StoreMode parentStoreMode) { |
49 getPeer(model); | 48 getPeer(model, parentStoreMode); |
50 } | 49 } |
51 | 50 |
52 | 51 |
53 public FlowVelocityModelValue getPeer(FlowVelocityModel model) { | 52 public FlowVelocityModelValue getPeer(final FlowVelocityModel model, final StoreMode parentStoreMode) { |
54 if (peer == null) { | 53 if (this.peer == null) { |
55 Session session = ImporterSession.getInstance() | 54 List<FlowVelocityModelValue> values; |
56 .getDatabaseSession(); | 55 final Session session = ImporterSession.getInstance().getDatabaseSession(); |
56 if (parentStoreMode == StoreMode.INSERT) | |
57 values = null; | |
58 else { | |
59 final Query query = session.createQuery( | |
60 "from FlowVelocityModelValue where " + | |
61 " flowVelocity=:model and " + | |
62 " station between :station - 0.00001 and :station + 0.00001" | |
63 ); | |
57 | 64 |
58 Query query = session.createQuery( | 65 query.setParameter("model", model); |
59 "from FlowVelocityModelValue where " + | 66 query.setParameter("station", this.station.doubleValue()); |
60 " flowVelocity=:model and " + | |
61 " station between :station - 0.00001 and :station + 0.00001" | |
62 ); | |
63 | 67 |
64 query.setParameter("model", model); | 68 values = query.list(); |
65 query.setParameter("station", station.doubleValue()); | 69 } |
70 if ((values == null) || values.isEmpty()) { | |
71 this.peer = new FlowVelocityModelValue( | |
72 model, this.station, this.q, this.totalChannel, this.mainChannel, this.shearStress); | |
66 | 73 |
67 List<FlowVelocityModelValue> values = query.list(); | 74 session.save(this.peer); |
68 | |
69 if (values.isEmpty()) { | |
70 peer = new FlowVelocityModelValue( | |
71 model, station, q, totalChannel, mainChannel, shearStress); | |
72 | |
73 session.save(peer); | |
74 } | 75 } |
75 else { | 76 else { |
76 peer = values.get(0); | 77 this.peer = values.get(0); |
77 } | 78 } |
78 } | 79 } |
79 | 80 |
80 return peer; | 81 return this.peer; |
81 } | 82 } |
82 } | 83 } |
83 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 84 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |