Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/ImportWst.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 org.dive4elements.river.model.Wst; | 11 import java.util.ArrayList; |
12 import org.dive4elements.river.model.River; | 12 import java.util.List; |
13 | 13 |
14 import org.apache.log4j.Logger; | 14 import org.apache.log4j.Logger; |
15 | 15 import org.dive4elements.river.importer.common.StoreMode; |
16 import org.dive4elements.river.model.River; | |
17 import org.dive4elements.river.model.Wst; | |
18 import org.hibernate.Query; | |
16 import org.hibernate.Session; | 19 import org.hibernate.Session; |
17 import org.hibernate.Query; | |
18 | |
19 import java.util.ArrayList; | |
20 import java.util.List; | |
21 | 20 |
22 /** Not (yet) db-mapped WST object. */ | 21 /** Not (yet) db-mapped WST object. */ |
23 public class ImportWst | 22 public class ImportWst |
24 { | 23 { |
25 private static Logger log = Logger.getLogger(ImportWst.class); | 24 private static Logger log = Logger.getLogger(ImportWst.class); |
27 public interface ImportWstColumnFactory { | 26 public interface ImportWstColumnFactory { |
28 ImportWstColumn create(ImportWst iw, int position); | 27 ImportWstColumn create(ImportWst iw, int position); |
29 } | 28 } |
30 | 29 |
31 public static final ImportWstColumnFactory COLUMN_FACTORY = | 30 public static final ImportWstColumnFactory COLUMN_FACTORY = |
32 new ImportWstColumnFactory() { | 31 new ImportWstColumnFactory() { |
33 @Override | 32 @Override |
34 public ImportWstColumn create(ImportWst importWst, int position) { | 33 public ImportWstColumn create(final ImportWst importWst, final int position) { |
35 return new ImportWstColumn(importWst, null, null, position); | 34 return new ImportWstColumn(importWst, null, null, position); |
36 } | 35 } |
37 }; | 36 }; |
38 | 37 |
39 protected String description; | 38 protected String description; |
40 | 39 |
41 protected Integer kind; | 40 protected Integer kind; |
42 | 41 |
46 | 45 |
47 protected ImportWstColumnFactory columnFactory; | 46 protected ImportWstColumnFactory columnFactory; |
48 | 47 |
49 protected boolean kmUp; | 48 protected boolean kmUp; |
50 | 49 |
50 protected StoreMode storeMode; | |
51 | |
51 /** Wst as in db. */ | 52 /** Wst as in db. */ |
52 protected Wst peer; | 53 protected Wst peer; |
53 | 54 |
54 public ImportWst() { | 55 public ImportWst() { |
55 this(COLUMN_FACTORY); | 56 this(COLUMN_FACTORY); |
56 } | 57 } |
57 | 58 |
58 public ImportWst(ImportWstColumnFactory columnFactory) { | 59 public ImportWst(final ImportWstColumnFactory columnFactory) { |
59 this.columnFactory = columnFactory; | 60 this.columnFactory = columnFactory; |
60 kind = 0; | 61 this.kind = 0; |
61 columns = new ArrayList<ImportWstColumn>(); | 62 this.columns = new ArrayList<>(); |
63 this.storeMode = StoreMode.NONE; | |
62 } | 64 } |
63 | 65 |
64 public ImportWst(String description) { | 66 public ImportWst(final String description) { |
65 this(description, COLUMN_FACTORY); | 67 this(description, COLUMN_FACTORY); |
66 } | 68 } |
67 | 69 |
68 public ImportWst( | 70 public ImportWst( |
69 String description, | 71 final String description, |
70 ImportWstColumnFactory columnFactory | 72 final ImportWstColumnFactory columnFactory |
71 ) { | 73 ) { |
72 this(columnFactory); | 74 this(columnFactory); |
73 this.description = description; | 75 this.description = description; |
74 } | 76 } |
75 | 77 |
76 public String getDescription() { | 78 public String getDescription() { |
77 return description; | 79 return this.description; |
78 } | 80 } |
79 | 81 |
80 public Integer getKind() { | 82 public Integer getKind() { |
81 return kind; | 83 return this.kind; |
82 } | 84 } |
83 | 85 |
84 public void setKind(Integer kind) { | 86 public void setKind(final Integer kind) { |
85 this.kind = kind; | 87 this.kind = kind; |
86 } | 88 } |
87 | 89 |
88 public boolean getKmUp() { | 90 public boolean getKmUp() { |
89 return kmUp; | 91 return this.kmUp; |
90 } | 92 } |
91 | 93 |
92 public void setKmUp(boolean kmUp) { | 94 public void setKmUp(final boolean kmUp) { |
93 this.kmUp = kmUp; | 95 this.kmUp = kmUp; |
94 } | 96 } |
95 | 97 |
96 public void setDescription(String description) { | 98 public void setDescription(final String description) { |
97 this.description = description; | 99 this.description = description; |
98 } | 100 } |
99 | 101 |
100 /** Create columns that can be accessed with getColumn. */ | 102 /** Create columns that can be accessed with getColumn. */ |
101 public void setNumberColumns(int numColumns) { | 103 public void setNumberColumns(final int numColumns) { |
102 for (int i = 0; i < numColumns; ++i) { | 104 for (int i = 0; i < numColumns; ++i) { |
103 columns.add(columnFactory.create(this, i)); | 105 this.columns.add(this.columnFactory.create(this, i)); |
104 } | 106 } |
105 } | 107 } |
106 | 108 |
107 public int getNumberColumns() { | 109 public int getNumberColumns() { |
108 return columns.size(); | 110 return this.columns.size(); |
109 } | 111 } |
110 | 112 |
111 public ImportWstColumn getColumn(int index) { | 113 public ImportWstColumn getColumn(final int index) { |
112 return columns.get(index); | 114 return this.columns.get(index); |
113 } | 115 } |
114 | 116 |
115 public List<ImportWstColumn> getColumns() { | 117 public List<ImportWstColumn> getColumns() { |
116 return columns; | 118 return this.columns; |
117 } | 119 } |
118 | 120 |
119 /** Adds a column. Assumes that columns wst is this instance. */ | 121 /** Adds a column. Assumes that columns wst is this instance. */ |
120 public void addColumn(ImportWstColumn column) { | 122 public void addColumn(final ImportWstColumn column) { |
121 columns.add(column); | 123 this.columns.add(column); |
122 } | 124 } |
123 | 125 |
124 public ImportUnit getUnit() { | 126 public ImportUnit getUnit() { |
125 return unit; | 127 return this.unit; |
126 } | 128 } |
127 | 129 |
128 public void setUnit(ImportUnit unit) { | 130 public void setUnit(final ImportUnit unit) { |
129 this.unit = unit; | 131 this.unit = unit; |
130 } | 132 } |
131 | 133 |
132 public void storeDependencies(River river) { | 134 public void storeDependencies(final River river) { |
133 | 135 |
134 log.info("store '" + description + "'"); | 136 log.info("store '" + this.description + "'"); |
135 getPeer(river); | 137 getPeer(river); |
136 | 138 |
137 for (ImportWstColumn column: columns) { | 139 for (final ImportWstColumn column: this.columns) { |
138 column.storeDependencies(river); | 140 column.storeDependencies(river); |
139 } | 141 } |
140 | 142 |
141 Session session = ImporterSession.getInstance().getDatabaseSession(); | 143 final Session session = ImporterSession.getInstance().getDatabaseSession(); |
142 session.flush(); | 144 session.flush(); |
143 } | 145 } |
144 | 146 |
145 public boolean guessWaterLevelIncreasing() { | 147 public boolean guessWaterLevelIncreasing() { |
146 int up = 0; | 148 int up = 0; |
147 for (ImportWstColumn column: columns) { | 149 for (final ImportWstColumn column: this.columns) { |
148 if (column.guessWaterLevelIncreasing()) ++up; | 150 if (column.guessWaterLevelIncreasing()) ++up; |
149 } | 151 } |
150 return up > columns.size() - up; | 152 return up > this.columns.size() - up; |
151 } | 153 } |
152 | 154 |
153 /** Get corresponding mapped wst (from database). */ | 155 /** Get corresponding mapped wst (from database). */ |
154 public Wst getPeer(River river) { | 156 public Wst getPeer(final River river) { |
155 if (peer == null) { | 157 if (this.peer == null) { |
156 Session session = ImporterSession.getInstance() | 158 final Session session = ImporterSession.getInstance() |
157 .getDatabaseSession(); | 159 .getDatabaseSession(); |
158 Query query = session.createQuery( | 160 final Query query = session.createQuery( |
159 "from Wst where " + | 161 "from Wst where " + |
160 "river=:river and description=:description and kind=:kind"); | 162 "river=:river and description=:description and kind=:kind"); |
161 query.setParameter("river", river); | 163 query.setParameter("river", river); |
162 query.setParameter("description", description); | 164 query.setParameter("description", this.description); |
163 query.setParameter("kind", kind); | 165 query.setParameter("kind", this.kind); |
164 List<Wst> wsts = query.list(); | 166 final List<Wst> wsts = query.list(); |
165 if (wsts.isEmpty()) { | 167 if (wsts.isEmpty()) { |
166 peer = new Wst(river, description, kind); | 168 this.peer = new Wst(river, this.description, this.kind); |
167 session.save(peer); | 169 session.save(this.peer); |
170 this.storeMode = StoreMode.INSERT; | |
168 } | 171 } |
169 else { | 172 else { |
170 peer = wsts.get(0); | 173 this.peer = wsts.get(0); |
174 this.storeMode = StoreMode.UPDATE; | |
171 } | 175 } |
172 | 176 |
173 } | 177 } |
174 return peer; | 178 return this.peer; |
175 } | 179 } |
176 } | 180 } |
177 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 181 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |