comparison backend/src/main/java/org/dive4elements/river/importer/ImportCrossSection.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 9344aa0fb021
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.River;
12 import org.dive4elements.river.model.CrossSection;
13 import org.dive4elements.river.model.TimeInterval;
14
15 import org.hibernate.Session;
16 import org.hibernate.Query;
17
18 import java.util.List; 11 import java.util.List;
19 12
20 import org.apache.log4j.Logger; 13 import org.apache.log4j.Logger;
14 import org.dive4elements.river.importer.common.StoreMode;
15 import org.dive4elements.river.model.CrossSection;
16 import org.dive4elements.river.model.River;
17 import org.dive4elements.river.model.TimeInterval;
18 import org.hibernate.Query;
19 import org.hibernate.Session;
21 20
22 /** CrossSection to be imported, holds list of ImportCrossSectionLines. */ 21 /** CrossSection to be imported, holds list of ImportCrossSectionLines. */
23 public class ImportCrossSection 22 public class ImportCrossSection
24 { 23 {
25 private static Logger log = Logger.getLogger(ImportCrossSection.class); 24 private static Logger log = Logger.getLogger(ImportCrossSection.class);
26 25
27 protected ImportRiver river; 26 protected ImportRiver river;
28 protected String description; 27 protected String description;
29 protected ImportTimeInterval timeInterval; 28 protected ImportTimeInterval timeInterval;
30 protected List<ImportCrossSectionLine> lines; 29 protected List<ImportCrossSectionLine> lines;
30 protected StoreMode storeMode;
31 31
32 protected CrossSection peer; 32 protected CrossSection peer;
33 33
34 public ImportCrossSection() { 34 public ImportCrossSection() {
35 } 35 }
36 36
37 public ImportCrossSection( 37 public ImportCrossSection(
38 ImportRiver river, 38 final ImportRiver river,
39 String description, 39 final String description,
40 ImportTimeInterval timeInterval, 40 final ImportTimeInterval timeInterval,
41 List<ImportCrossSectionLine> lines 41 final List<ImportCrossSectionLine> lines
42 ) { 42 ) {
43 this.river = river; 43 this.river = river;
44 this.description = description; 44 this.description = description;
45 this.timeInterval = timeInterval; 45 this.timeInterval = timeInterval;
46 this.lines = lines; 46 this.lines = lines;
47 this.storeMode = StoreMode.NONE;
47 wireWithLines(); 48 wireWithLines();
48 } 49 }
49 50
50 public void wireWithLines() { 51 public void wireWithLines() {
51 for (ImportCrossSectionLine line: lines) { 52 for (final ImportCrossSectionLine line: this.lines) {
52 line.setCrossSection(this); 53 line.setCrossSection(this);
53 } 54 }
54 } 55 }
55 56
56 public ImportRiver getRiver() { 57 public ImportRiver getRiver() {
57 return river; 58 return this.river;
58 } 59 }
59 60
60 public void setRiver(ImportRiver river) { 61 public void setRiver(final ImportRiver river) {
61 this.river = river; 62 this.river = river;
62 } 63 }
63 64
64 public String getDescription() { 65 public String getDescription() {
65 return description; 66 return this.description;
66 } 67 }
67 68
68 public void setDescription(String description) { 69 public void setDescription(final String description) {
69 this.description = description; 70 this.description = description;
70 } 71 }
71 72
72 public ImportTimeInterval getTimeInterval() { 73 public ImportTimeInterval getTimeInterval() {
73 return timeInterval; 74 return this.timeInterval;
74 } 75 }
75 76
76 public void setTimeInterval(ImportTimeInterval timeInterval) { 77 public void setTimeInterval(final ImportTimeInterval timeInterval) {
77 this.timeInterval = timeInterval; 78 this.timeInterval = timeInterval;
78 } 79 }
79 80
80 public void storeDependencies() { 81 public void storeDependencies() {
81 82
82 log.info("store cross section '" + description + "'"); 83 log.info("store cross section '" + this.description + "'");
83 84
84 getPeer(); 85 getPeer();
85 86
86 int i = 1, N = lines.size(); 87 // int i = 1;
88 // final int N = this.lines.size();
87 89
88 for (ImportCrossSectionLine line: lines) { 90 for (final ImportCrossSectionLine line: this.lines) {
89 line.storeDependencies(); 91 line.storeDependencies();
90 log.info(" stored " + i + " lines. remaining: " + (N-i)); 92 // log.info(" stored " + i + " lines. remaining: " + (N-i));
91 ++i; 93 // ++i;
92 } 94 }
95 log.info(" lines stored: " + this.lines.size());
93 } 96 }
94 97
95 public CrossSection getPeer() { 98 public CrossSection getPeer() {
96 99
97 if (peer == null) { 100 if (this.peer == null) {
98 River r = river.getPeer(); 101 final River r = this.river.getPeer();
99 TimeInterval t = timeInterval != null 102 final TimeInterval t = (this.timeInterval != null) ? this.timeInterval.getPeer() : null;
100 ? timeInterval.getPeer()
101 : null;
102 103
103 Session session = 104 final Session session =
104 ImporterSession.getInstance().getDatabaseSession(); 105 ImporterSession.getInstance().getDatabaseSession();
105 106
106 Query query = session.createQuery( 107 final Query query = session.createQuery(
107 "from CrossSection where " + 108 "from CrossSection where " +
108 "river=:r and " + 109 "river=:r and " +
109 "timeInterval=:t and " + 110 "timeInterval=:t and " +
110 "description=:d"); 111 "description=:d");
111 112
112 query.setParameter("r", r); 113 query.setParameter("r", r);
113 query.setParameter("t", t); 114 query.setParameter("t", t);
114 query.setParameter("d", description); 115 query.setParameter("d", this.description);
115 116
116 List<CrossSection> crossSections = query.list(); 117 final List<CrossSection> crossSections = query.list();
117 if (crossSections.isEmpty()) { 118 if (crossSections.isEmpty()) {
118 peer = new CrossSection(r, t, description); 119 this.peer = new CrossSection(r, t, this.description);
119 session.save(peer); 120 session.save(this.peer);
121 this.storeMode = StoreMode.INSERT;
120 } 122 }
121 else { 123 else {
122 peer = crossSections.get(0); 124 this.peer = crossSections.get(0);
125 this.storeMode = StoreMode.UPDATE;
123 } 126 }
124 } 127 }
125 return peer; 128 return this.peer;
126 } 129 }
127 } 130 }
128 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 131 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org