comparison backend/src/main/java/org/dive4elements/river/importer/ImportHYKFlowZone.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 4c3ccf2b0304
children
comparison
equal deleted inserted replaced
8985:27851cfda84a 8986:392bbcd8a88b
6 * documentation coming with Dive4Elements River for details. 6 * documentation coming with Dive4Elements River for details.
7 */ 7 */
8 8
9 package org.dive4elements.river.importer; 9 package org.dive4elements.river.importer;
10 10
11 import org.dive4elements.river.model.HYKFormation; 11 import java.math.BigDecimal;
12 import java.util.List;
13
14 import org.dive4elements.river.importer.common.StoreMode;
12 import org.dive4elements.river.model.HYKFlowZone; 15 import org.dive4elements.river.model.HYKFlowZone;
13 import org.dive4elements.river.model.HYKFlowZoneType; 16 import org.dive4elements.river.model.HYKFlowZoneType;
14 17 import org.dive4elements.river.model.HYKFormation;
18 import org.hibernate.Query;
15 import org.hibernate.Session; 19 import org.hibernate.Session;
16 import org.hibernate.Query;
17
18 import java.util.List;
19
20 import java.math.BigDecimal;
21 20
22 public class ImportHYKFlowZone 21 public class ImportHYKFlowZone
23 { 22 {
24 protected ImportHYKFormation formation; 23 protected ImportHYKFormation formation;
25 protected ImportHYKFlowZoneType type; 24 protected ImportHYKFlowZoneType type;
30 29
31 public ImportHYKFlowZone() { 30 public ImportHYKFlowZone() {
32 } 31 }
33 32
34 public ImportHYKFlowZone( 33 public ImportHYKFlowZone(
35 ImportHYKFormation formation, 34 final ImportHYKFormation formation,
36 ImportHYKFlowZoneType type, 35 final ImportHYKFlowZoneType type,
37 BigDecimal a, 36 final BigDecimal a,
38 BigDecimal b 37 final BigDecimal b
39 ) { 38 ) {
40 this.formation = formation; 39 this.formation = formation;
41 this.type = type; 40 this.type = type;
42 this.a = a; 41 this.a = a;
43 this.b = b; 42 this.b = b;
44 } 43 }
45 44
46 public ImportHYKFormation getFormation() { 45 public ImportHYKFormation getFormation() {
47 return formation; 46 return this.formation;
48 } 47 }
49 48
50 public void setFormation(ImportHYKFormation formation) { 49 public void setFormation(final ImportHYKFormation formation) {
51 this.formation = formation; 50 this.formation = formation;
52 } 51 }
53 52
54 public void storeDependencies() { 53 public void storeDependencies() {
55 getPeer(); 54 getPeer();
56 } 55 }
57 56
58 public HYKFlowZone getPeer() { 57 public HYKFlowZone getPeer() {
59 if (peer == null) { 58 if (this.peer == null) {
60 HYKFormation f = formation.getPeer(); 59 final HYKFormation f = this.formation.getPeer();
61 HYKFlowZoneType t = type.getPeer(); 60 final HYKFlowZoneType t = this.type.getPeer();
62 Session session = ImporterSession.getInstance() 61 final Session session = ImporterSession.getInstance()
63 .getDatabaseSession(); 62 .getDatabaseSession();
64 Query query = session.createQuery( 63 List<HYKFlowZone> zones;
65 "from HYKFlowZone where formation=:formation " + 64 if (this.formation.storeMode == StoreMode.INSERT)
66 "and type=:type and a=:a and b=:b"); 65 zones = null;
67 query.setParameter("formation", f); 66 {
68 query.setParameter("type", t); 67 final Query query = session.createQuery(
69 query.setParameter("a", a); 68 "from HYKFlowZone where formation=:formation " +
70 query.setParameter("b", b); 69 "and type=:type and a=:a and b=:b");
71 List<HYKFlowZone> zones = query.list(); 70 query.setParameter("formation", f);
72 if (zones.isEmpty()) { 71 query.setParameter("type", t);
73 peer = new HYKFlowZone(f, t, a, b); 72 query.setParameter("a", this.a);
74 session.save(peer); 73 query.setParameter("b", this.b);
74 zones = query.list();
75 }
76 if ((zones == null) || zones.isEmpty()) {
77 this.peer = new HYKFlowZone(f, t, this.a, this.b);
78 session.save(this.peer);
75 } 79 }
76 else { 80 else {
77 peer = zones.get(0); 81 this.peer = zones.get(0);
78 } 82 }
79 83
80 } 84 }
81 return peer; 85 return this.peer;
82 } 86 }
83 } 87 }
84 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 88 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org