Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/ImportWstColumn.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 |
---|---|
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 java.math.BigDecimal; | |
12 import java.util.ArrayList; | |
13 import java.util.List; | |
14 import java.util.Random; | |
15 | |
16 import org.apache.log4j.Logger; | |
17 import org.dive4elements.river.importer.common.StoreMode; | |
18 import org.dive4elements.river.model.River; | |
19 import org.dive4elements.river.model.TimeInterval; | |
11 import org.dive4elements.river.model.Wst; | 20 import org.dive4elements.river.model.Wst; |
12 import org.dive4elements.river.model.WstColumn; | 21 import org.dive4elements.river.model.WstColumn; |
13 import org.dive4elements.river.model.River; | 22 import org.hibernate.Query; |
14 import org.dive4elements.river.model.TimeInterval; | |
15 | |
16 import org.hibernate.Session; | 23 import org.hibernate.Session; |
17 import org.hibernate.Query; | |
18 | |
19 import java.util.List; | |
20 import java.util.ArrayList; | |
21 import java.util.Random; | |
22 | |
23 import java.math.BigDecimal; | |
24 | |
25 import org.apache.log4j.Logger; | |
26 | 24 |
27 | 25 |
28 /** Unmapped column of a WST. */ | 26 /** Unmapped column of a WST. */ |
29 public class ImportWstColumn | 27 public class ImportWstColumn |
30 { | 28 { |
39 protected ImportTimeInterval timeInterval; | 37 protected ImportTimeInterval timeInterval; |
40 | 38 |
41 protected List<ImportWstColumnQRange> columnQRanges; | 39 protected List<ImportWstColumnQRange> columnQRanges; |
42 protected List<ImportWstColumnValue> columnValues; | 40 protected List<ImportWstColumnValue> columnValues; |
43 | 41 |
42 protected StoreMode storeMode; | |
43 | |
44 protected WstColumn peer; | 44 protected WstColumn peer; |
45 | 45 |
46 public ImportWstColumn() { | 46 public ImportWstColumn() { |
47 columnQRanges = new ArrayList<ImportWstColumnQRange>(); | 47 this.columnQRanges = new ArrayList<>(); |
48 columnValues = new ArrayList<ImportWstColumnValue>(); | 48 this.columnValues = new ArrayList<>(); |
49 this.storeMode = StoreMode.NONE; | |
49 } | 50 } |
50 | 51 |
51 public ImportWstColumn( | 52 public ImportWstColumn( |
52 ImportWst wst, | 53 final ImportWst wst, |
53 String name, | 54 final String name, |
54 String description, | 55 final String description, |
55 Integer position, | 56 final Integer position, |
56 String source | 57 final String source |
57 ) { | 58 ) { |
58 this(); | 59 this(); |
59 this.wst = wst; | 60 this.wst = wst; |
60 this.name = name; | 61 this.name = name; |
61 this.description = description; | 62 this.description = description; |
62 this.position = position; | 63 this.position = position; |
63 } | 64 } |
64 | 65 |
65 public ImportWstColumn( | 66 public ImportWstColumn( |
66 ImportWst wst, | 67 final ImportWst wst, |
67 String name, | 68 final String name, |
68 String description, | 69 final String description, |
69 Integer position | 70 final Integer position |
70 ) { | 71 ) { |
71 this(wst, name, description, position, null); | 72 this(wst, name, description, position, null); |
72 } | 73 } |
73 | 74 |
74 public ImportWst getWst() { | 75 public ImportWst getWst() { |
75 return wst; | 76 return this.wst; |
76 } | 77 } |
77 | 78 |
78 public void setWst(ImportWst wst) { | 79 public void setWst(final ImportWst wst) { |
79 this.wst = wst; | 80 this.wst = wst; |
80 } | 81 } |
81 | 82 |
82 public String getName() { | 83 public String getName() { |
83 return name; | 84 return this.name; |
84 } | 85 } |
85 | 86 |
86 public void setName(String name) { | 87 public void setName(final String name) { |
87 this.name = name; | 88 this.name = name; |
88 } | 89 } |
89 | 90 |
90 public String getDescription() { | 91 public String getDescription() { |
91 return description; | 92 return this.description; |
92 } | 93 } |
93 | 94 |
94 public void setDescription(String description) { | 95 public void setDescription(final String description) { |
95 this.description = description; | 96 this.description = description; |
96 } | 97 } |
97 | 98 |
98 public Integer getPosition() { | 99 public Integer getPosition() { |
99 return position; | 100 return this.position; |
100 } | 101 } |
101 | 102 |
102 public void setPosition(Integer position) { | 103 public void setPosition(final Integer position) { |
103 this.position = position; | 104 this.position = position; |
104 } | 105 } |
105 | 106 |
106 public String getSource() { | 107 public String getSource() { |
107 return source; | 108 return this.source; |
108 } | 109 } |
109 | 110 |
110 public void setSource(String source) { | 111 public void setSource(final String source) { |
111 this.source = source; | 112 this.source = source; |
112 } | 113 } |
113 | 114 |
114 public void addColumnValue(BigDecimal position, BigDecimal w) { | 115 public void addColumnValue(final BigDecimal position, final BigDecimal w) { |
115 columnValues.add( | 116 this.columnValues.add( |
116 new ImportWstColumnValue(this, position, w)); | 117 new ImportWstColumnValue(this, position, w)); |
117 } | 118 } |
118 | 119 |
119 public void addColumnQRange(ImportWstQRange columnQRange) { | 120 public void addColumnQRange(final ImportWstQRange columnQRange) { |
120 columnQRanges.add( | 121 this.columnQRanges.add( |
121 new ImportWstColumnQRange(this, columnQRange)); | 122 new ImportWstColumnQRange(this, columnQRange)); |
122 } | 123 } |
123 | 124 |
124 | 125 |
125 /** Get the Column Values stored in this column. */ | 126 /** Get the Column Values stored in this column. */ |
126 public List<ImportWstColumnValue> getColumnValues() { | 127 public List<ImportWstColumnValue> getColumnValues() { |
127 return columnValues; | 128 return this.columnValues; |
128 } | 129 } |
129 | 130 |
130 | 131 |
131 public void storeDependencies(River river) { | 132 public void storeDependencies(final River river) { |
132 log.info("store column '" + name + "'"); | 133 log.info("store column '" + this.name + "'"); |
133 getPeer(river); | 134 getPeer(river); |
134 | 135 |
135 for (ImportWstColumnQRange columnQRange: columnQRanges) { | 136 for (final ImportWstColumnQRange columnQRange: this.columnQRanges) { |
136 columnQRange.getPeer(river); | 137 columnQRange.getPeer(river); |
137 } | 138 } |
138 | 139 |
139 for (ImportWstColumnValue columnValue: columnValues) { | 140 for (final ImportWstColumnValue columnValue: this.columnValues) { |
140 columnValue.getPeer(river); | 141 columnValue.getPeer(river); |
141 } | 142 } |
142 } | 143 } |
143 | 144 |
144 public ImportTimeInterval getTimeInterval() { | 145 public ImportTimeInterval getTimeInterval() { |
145 return timeInterval; | 146 return this.timeInterval; |
146 } | 147 } |
147 | 148 |
148 public void setTimeInterval(ImportTimeInterval timeInterval) { | 149 public void setTimeInterval(final ImportTimeInterval timeInterval) { |
149 this.timeInterval = timeInterval; | 150 this.timeInterval = timeInterval; |
150 } | 151 } |
151 | 152 |
152 public boolean guessWaterLevelIncreasing() { | 153 public boolean guessWaterLevelIncreasing() { |
153 | 154 |
154 int N = columnValues.size(); | 155 final int N = this.columnValues.size(); |
155 | 156 |
156 if (N < 2) { | 157 if (N < 2) { |
157 return true; | 158 return true; |
158 } | 159 } |
159 | 160 |
160 Random r = new Random(); | 161 final Random r = new Random(); |
161 int up = 0; | 162 int up = 0; |
162 | 163 |
163 int S = N < 50 ? N : (int)(0.1f * N)+1; | 164 final int S = N < 50 ? N : (int)(0.1f * N)+1; |
164 for (int s = 0; s < S; ++s) { | 165 for (int s = 0; s < S; ++s) { |
165 int i1, i2; | 166 int i1, i2; |
166 do { | 167 do { |
167 i1 = r.nextInt(N-1); | 168 i1 = r.nextInt(N-1); |
168 i2 = r.nextInt(N-1); | 169 i2 = r.nextInt(N-1); |
169 } while (i1 == i2); | 170 } while (i1 == i2); |
170 ImportWstColumnValue b = columnValues.get(i1); | 171 ImportWstColumnValue b = this.columnValues.get(i1); |
171 ImportWstColumnValue a = columnValues.get(i2); | 172 ImportWstColumnValue a = this.columnValues.get(i2); |
172 if (b.getPosition().compareTo(a.getPosition()) < 0) { | 173 if (b.getPosition().compareTo(a.getPosition()) < 0) { |
173 ImportWstColumnValue t = a; a = b; b = t; | 174 final ImportWstColumnValue t = a; a = b; b = t; |
174 } | 175 } |
175 | 176 |
176 if (a.getW().compareTo(b.getW()) < 0) ++up; | 177 if (a.getW().compareTo(b.getW()) < 0) ++up; |
177 } | 178 } |
178 | 179 |
179 return up > S - up; | 180 return up > S - up; |
180 } | 181 } |
181 | 182 |
182 /** Get corresponding mapped wst-column (from database). */ | 183 /** Get corresponding mapped wst-column (from database). */ |
183 public WstColumn getPeer(River river) { | 184 public WstColumn getPeer(final River river) { |
184 if (peer == null) { | 185 if (this.peer == null) { |
185 Wst w = wst.getPeer(river); | 186 final Wst w = this.wst.getPeer(river); |
186 Session session = ImporterSession.getInstance() | 187 List<WstColumn> columns; |
187 .getDatabaseSession(); | 188 final Session session = ImporterSession.getInstance() |
188 Query query = session.createQuery( | 189 .getDatabaseSession(); |
189 "from WstColumn where" + | 190 if (this.wst.storeMode == StoreMode.INSERT) |
190 " wst=:wst and name=:name" + | 191 columns = null; |
191 " and source=:source" + | |
192 " and position=:position"); | |
193 query.setParameter("wst", w); | |
194 query.setParameter("name", name); | |
195 query.setParameter("position", position); | |
196 query.setParameter("source", source); | |
197 | |
198 TimeInterval ti = timeInterval != null | |
199 ? timeInterval.getPeer() | |
200 : null; | |
201 | |
202 List<WstColumn> columns = query.list(); | |
203 if (columns.isEmpty()) { | |
204 log.debug("source: " + source); | |
205 peer = new WstColumn( | |
206 w, name, description, source, position, ti); | |
207 session.save(peer); | |
208 } | |
209 else { | 192 else { |
210 peer = columns.get(0); | 193 final Query query = session.createQuery( |
211 } | 194 "from WstColumn where" + |
212 } | 195 " wst=:wst and name=:name" + |
213 return peer; | 196 " and source=:source" + |
197 " and position=:position"); | |
198 query.setParameter("wst", w); | |
199 query.setParameter("name", this.name); | |
200 query.setParameter("position", this.position); | |
201 query.setParameter("source", this.source); | |
202 columns = query.list(); | |
203 } | |
204 | |
205 final TimeInterval ti = (this.timeInterval != null) ? this.timeInterval.getPeer() : null; | |
206 | |
207 if ((columns == null) || columns.isEmpty()) { | |
208 log.debug("source: " + this.source); | |
209 this.peer = new WstColumn( | |
210 w, this.name, this.description, this.source, this.position, ti); | |
211 session.save(this.peer); | |
212 this.storeMode = StoreMode.INSERT; | |
213 } | |
214 else { | |
215 this.peer = columns.get(0); | |
216 this.storeMode = StoreMode.UPDATE; | |
217 } | |
218 } | |
219 return this.peer; | |
214 } | 220 } |
215 | 221 |
216 } | 222 } |
217 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 223 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |