comparison backend/src/main/java/org/dive4elements/river/importer/ImportHYKEntry.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.HYKEntry; 11 import java.math.BigDecimal;
12 import org.dive4elements.river.model.HYK; 12 import java.util.ArrayList;
13
14 import java.util.Date; 13 import java.util.Date;
15 import java.util.List; 14 import java.util.List;
16 import java.util.ArrayList;
17 15
18 import java.math.BigDecimal; 16 import org.dive4elements.river.importer.common.StoreMode;
19 17 import org.dive4elements.river.model.HYK;
18 import org.dive4elements.river.model.HYKEntry;
19 import org.hibernate.Query;
20 import org.hibernate.Session; 20 import org.hibernate.Session;
21 import org.hibernate.Query;
22 21
23 public class ImportHYKEntry 22 public class ImportHYKEntry
24 { 23 {
25 protected ImportHYK hyk; 24 protected ImportHYK hyk;
26 protected BigDecimal km; 25 protected BigDecimal km;
27 protected Date measure; 26 protected Date measure;
28 27
29 protected List<ImportHYKFormation> formations; 28 protected List<ImportHYKFormation> formations;
30 29
30 protected StoreMode storeMode;
31
31 protected HYKEntry peer; 32 protected HYKEntry peer;
32 33
33 public ImportHYKEntry() { 34 public ImportHYKEntry() {
34 formations = new ArrayList<ImportHYKFormation>(); 35 this.formations = new ArrayList<>();
36 this.storeMode = StoreMode.NONE;
35 } 37 }
36 38
37 public ImportHYKEntry( 39 public ImportHYKEntry(
38 ImportHYK hyk, 40 final ImportHYK hyk,
39 BigDecimal km, 41 final BigDecimal km,
40 Date measure 42 final Date measure
41 ) { 43 ) {
42 this(); 44 this();
43 this.hyk = hyk; 45 this.hyk = hyk;
44 this.km = km; 46 this.km = km;
45 this.measure = measure; 47 this.measure = measure;
46 } 48 }
47 49
48 public ImportHYK getHYK() { 50 public ImportHYK getHYK() {
49 return hyk; 51 return this.hyk;
50 } 52 }
51 53
52 public void setHYK(ImportHYK hyk) { 54 public void setHYK(final ImportHYK hyk) {
53 this.hyk = hyk; 55 this.hyk = hyk;
54 } 56 }
55 57
56 public BigDecimal getKm() { 58 public BigDecimal getKm() {
57 return km; 59 return this.km;
58 } 60 }
59 61
60 public void setKm(BigDecimal km) { 62 public void setKm(final BigDecimal km) {
61 this.km = km; 63 this.km = km;
62 } 64 }
63 65
64 public void addFormation(ImportHYKFormation formation) { 66 public void addFormation(final ImportHYKFormation formation) {
65 int numFormation = formations.size(); 67 final int numFormation = this.formations.size();
66 formations.add(formation); 68 this.formations.add(formation);
67 formation.setFormationNum(numFormation); 69 formation.setFormationNum(numFormation);
68 formation.setEntry(this); 70 formation.setEntry(this);
69 } 71 }
70 72
71 public void storeDependencies() { 73 public void storeDependencies() {
72 getPeer(); 74 getPeer();
73 for (ImportHYKFormation formation: formations) { 75 for (final ImportHYKFormation formation: this.formations) {
74 formation.storeDependencies(); 76 formation.storeDependencies();
75 } 77 }
76 } 78 }
77 79
78 public HYKEntry getPeer() { 80 public HYKEntry getPeer() {
79 if (peer == null) { 81 if (this.peer == null) {
80 HYK h = hyk.getPeer(); 82 final HYK h = this.hyk.getPeer();
81 Session session = ImporterSession.getInstance() 83 final Session session = ImporterSession.getInstance()
82 .getDatabaseSession(); 84 .getDatabaseSession();
83 Query query = session.createQuery( 85 List<HYKEntry> entries;
84 "from HYKEntry where HYK=:hyk " + 86 if (this.hyk.storeMode == StoreMode.INSERT)
85 "and km=:km and measure=:measure"); 87 entries = null;
86 query.setParameter("hyk", h); 88 else {
87 query.setParameter("km", km); 89 final Query query = session.createQuery(
88 query.setParameter("measure", measure); 90 "from HYKEntry where HYK=:hyk " +
89 List<HYKEntry> entries = query.list(); 91 "and km=:km and measure=:measure");
90 if (entries.isEmpty()) { 92 query.setParameter("hyk", h);
91 peer = new HYKEntry(h, km, measure); 93 query.setParameter("km", this.km);
92 session.save(peer); 94 query.setParameter("measure", this.measure);
95 entries = query.list();
96 }
97 if ((entries == null) || entries.isEmpty()) {
98 this.peer = new HYKEntry(h, this.km, this.measure);
99 session.save(this.peer);
100 this.storeMode = StoreMode.INSERT;
93 } 101 }
94 else { 102 else {
95 peer = entries.get(0); 103 this.peer = entries.get(0);
104 this.storeMode = StoreMode.UPDATE;
96 } 105 }
97 } 106 }
98 return peer; 107 return this.peer;
99 } 108 }
100 } 109 }
101 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 110 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org