1 |
package dk.daoas.fulddaekning; |
package dk.daoas.fulddaekning; |
2 |
|
|
3 |
|
import geocode.GeoPoint; |
4 |
|
|
5 |
import java.io.IOException; |
import java.io.IOException; |
6 |
import java.sql.Connection; |
import java.sql.Connection; |
7 |
import java.sql.DriverManager; |
import java.sql.DriverManager; |
9 |
import java.sql.ResultSet; |
import java.sql.ResultSet; |
10 |
import java.sql.SQLException; |
import java.sql.SQLException; |
11 |
import java.util.ArrayList; |
import java.util.ArrayList; |
12 |
|
import java.util.HashMap; |
13 |
import java.util.List; |
import java.util.List; |
14 |
|
import java.util.Map; |
15 |
import java.util.Properties; |
import java.util.Properties; |
16 |
import java.util.Queue; |
import java.util.Set; |
17 |
import java.util.concurrent.ConcurrentLinkedQueue; |
import java.util.TreeSet; |
18 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
19 |
|
|
20 |
|
|
21 |
|
|
22 |
public class Database { |
public class Database { |
23 |
Logger logger = Logger.getLogger(Database.class.getName()); |
Logger logger = Logger.getLogger(Database.class.getName()); |
24 |
|
|
25 |
|
int batchCount = 0; |
26 |
|
|
27 |
|
Connection conn; |
28 |
|
PreparedStatement saveStmt; |
29 |
|
|
30 |
|
List<GeoPoint> alleAdresser; |
31 |
|
Adresse alleIkkeDaekkede[]; |
32 |
|
|
33 |
|
Map<Short,List<GeoPoint>> daekkedeAdresserHO = new HashMap<Short,List<GeoPoint>>(); |
34 |
|
|
35 |
|
|
36 |
|
|
|
Connection conn; |
|
|
Connection writeConn; |
|
37 |
|
|
|
PreparedStatement saveStmt; |
|
38 |
|
|
39 |
public Database(Properties conf) throws SQLException,IOException { |
|
40 |
conn = getConnection(conf); |
DeduplicateHelper<String> husnrbogstavCache = new DeduplicateHelper<String>(); |
41 |
conn.setReadOnly(true); |
DeduplicateHelper<String> ruteCache = new DeduplicateHelper<String>(); |
42 |
|
|
43 |
writeConn = getConnection(conf); |
Set<Short> postnumre = new TreeSet<Short>(); |
44 |
|
|
45 |
|
//Map<Short, List<Adresse>> ikkeDaekkedePrPost = new HashMap<Short, List<Adresse>>(); |
46 |
|
|
47 |
|
|
48 |
|
private HashMap<Short,BoundingBox> bbCache = new HashMap<Short,BoundingBox>(); |
49 |
|
|
50 |
|
public Database(SafeProperties conf) throws SQLException,IOException { |
51 |
|
this.conn = getConnection( conf ); |
52 |
|
|
53 |
|
String sql = "INSERT INTO fulddaekning.afstand_anden_rute_ny (orgId,orgPostnr, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgLatitude,orgLongitude,orgRute,id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,afstand,`timestamp`) "+ |
54 |
|
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, now() )"; |
55 |
|
|
56 |
|
saveStmt = conn.prepareStatement(sql); |
57 |
|
|
58 |
|
} |
59 |
|
|
60 |
|
public void resetResultTable() throws SQLException { |
61 |
|
logger.info("Dropping old result table (if exists)"); |
62 |
|
String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_ny"; |
63 |
|
conn.createStatement().executeUpdate(sql); |
64 |
|
|
65 |
|
logger.info("Create new result table"); |
66 |
|
sql = "CREATE TABLE fulddaekning.afstand_anden_rute_ny LIKE fulddaekning.afstand_anden_rute"; |
67 |
|
conn.createStatement().executeUpdate(sql); |
68 |
|
} |
69 |
|
|
70 |
|
public void renameResultTables() throws SQLException { |
71 |
|
Constants consts = Constants.getInstance(); |
72 |
|
String ext = consts.getTableExtension(); |
73 |
|
|
74 |
|
logger.info("Dropping old backup table (if exists)"); |
75 |
|
String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old" + ext; |
76 |
|
conn.createStatement().executeUpdate(sql); |
77 |
|
|
78 |
|
logger.info("Rename tables"); |
79 |
|
String sql2 = "RENAME TABLE fulddaekning.afstand_anden_rute" + ext + " TO fulddaekning.afstand_anden_rute_old" + ext + ", fulddaekning.afstand_anden_rute_ny TO fulddaekning.afstand_anden_rute" + ext; |
80 |
|
|
81 |
|
logger.info("Executing: " + sql2); |
82 |
|
conn.createStatement().executeUpdate(sql2); |
83 |
|
} |
84 |
|
|
85 |
|
public BoundingBox getBoundingbox(short postnr) { |
86 |
|
BoundingBox bb = bbCache.get(postnr); |
87 |
|
return bb.clone();//never return the original / cached object |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
|
public Set<Short> hentPostnumreCache() { |
92 |
|
return postnumre; |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
|
public Adresse[] hentAlleIkkedaekkedeAdresser(int minPostnr, int maxPostnr) throws SQLException { |
97 |
|
|
98 |
String sql = "INSERT INTO fulddaekning.afstand_anden_rute_thn (orgId,orgPostnr, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgLatitude,orgLongitude,orgRute,id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,afstand,`timestamp`) "+ |
logger.info("Henter alle IKKE-daekkede adresser"); |
|
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, now() )"; |
|
99 |
|
|
100 |
saveStmt = writeConn.prepareStatement(sql); |
String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " + |
101 |
|
"FROM fulddaekning.adressetabel a " + |
102 |
|
"LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " + |
103 |
|
"WHERE rute IS NULL " + //Ingen dækning |
104 |
|
"AND a.postnr BETWEEN ? AND ? " + |
105 |
|
"AND a.postnr NOT BETWEEN 3900 and 3999 " + //Skip greenland |
106 |
|
"AND a.postnr NOT BETWEEN 9000 AND 9499 " + // Skip nordjylland (DAO) |
107 |
|
"AND latitude IS NOT NULL " + |
108 |
|
"AND longitude IS NOT NULL " + |
109 |
|
"AND gadeid IS NOT NULL " + |
110 |
|
"AND (a.distributor IS NULL OR a.distributor<>'LUKKET') "; |
111 |
|
PreparedStatement stmt = conn.prepareStatement(sql); |
112 |
|
stmt.setInt(1, minPostnr); |
113 |
|
stmt.setInt(2, maxPostnr); |
114 |
|
|
115 |
|
List<Adresse> list = hentAdresseListe( stmt ); |
116 |
|
alleIkkeDaekkede = list.toArray( new Adresse[ list.size() ] ); |
117 |
|
|
118 |
|
logger.info("Analyserer ikke-daekkede adresser"); |
119 |
|
|
120 |
|
for (Adresse a : alleIkkeDaekkede) { |
121 |
|
|
122 |
|
|
123 |
|
BoundingBox bbox; |
124 |
|
|
125 |
|
if (! postnumre.contains(a.postnr )) { |
126 |
|
postnumre.add( a.postnr ); |
127 |
|
|
128 |
|
bbox = new BoundingBox(); |
129 |
|
|
130 |
|
bbCache.put( a.postnr, bbox); |
131 |
|
|
132 |
|
} else { |
133 |
|
bbox = bbCache.get( a.postnr); |
134 |
|
} |
135 |
|
|
136 |
|
bbox.latitudeMax = Math.max(bbox.latitudeMax, a.latitude); |
137 |
|
bbox.latitudeMin = Math.min(bbox.latitudeMin, a.latitude); |
138 |
|
bbox.longitudeMax = Math.max(bbox.longitudeMax, a.longitude); |
139 |
|
bbox.longitudeMin = Math.min(bbox.longitudeMin, a.longitude); |
140 |
|
|
141 |
|
|
142 |
|
} |
143 |
|
return alleIkkeDaekkede; |
144 |
} |
} |
145 |
|
|
146 |
public void resetResultTable() throws SQLException { |
|
147 |
logger.info("Truncating result table"); |
|
148 |
String sql = "TRUNCATE TABLE fulddaekning.afstand_anden_rute_thn"; |
|
149 |
writeConn.createStatement().executeUpdate(sql); |
public Map<Short, List<GeoPoint>> getDaekkedeAdresserHO() { |
150 |
|
return daekkedeAdresserHO; |
151 |
|
} |
152 |
|
|
153 |
|
public int hentAlleDaekkedeAdresser() throws SQLException { |
154 |
|
if ( alleAdresser == null ) { |
155 |
|
String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " + |
156 |
|
"FROM fulddaekning.adressetabel a " + |
157 |
|
"LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " + |
158 |
|
"WHERE rute IS NOT NULL " + |
159 |
|
"AND latitude IS NOT NULL " + |
160 |
|
"AND longitude IS NOT NULL " + |
161 |
|
"AND a.distributor = ? "; |
162 |
|
|
163 |
|
// Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets) |
164 |
|
// Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html |
165 |
|
PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); |
166 |
|
stmt.setFetchSize(Integer.MIN_VALUE); |
167 |
|
|
168 |
|
stmt.setString(1, LookupMain.distributor); |
169 |
|
|
170 |
|
List<Adresse> list = hentAdresseListe( stmt ); |
171 |
|
|
172 |
|
alleAdresser = new ArrayList<GeoPoint>(); |
173 |
|
alleAdresser.addAll(list); |
174 |
|
|
175 |
|
for(Adresse addr : list) { |
176 |
|
short ho = addr.ho; |
177 |
|
List<GeoPoint> hoListe = daekkedeAdresserHO.get(ho); |
178 |
|
if ( hoListe == null) { |
179 |
|
hoListe = new ArrayList<GeoPoint>(); |
180 |
|
daekkedeAdresserHO.put(ho, hoListe); |
181 |
|
} |
182 |
|
|
183 |
|
hoListe.add(addr); |
184 |
|
|
185 |
|
} |
186 |
|
} |
187 |
|
|
188 |
|
return alleAdresser.size(); |
189 |
} |
} |
190 |
|
|
191 |
public Connection getConnection(Properties conf) throws SQLException, IOException { |
|
192 |
|
|
193 |
|
public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException { |
194 |
|
/*String sql = "INSERT INTO fulddaekning.afstand_anden_rute_thn (orgId,orgPostnr, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgLatitude,orgLongitude,orgRute,id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,afstand,`timestamp`) "+ |
195 |
|
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, now() )"; |
196 |
|
|
197 |
|
PreparedStatement saveStmt = conn.prepareStatement(sql);*/ |
198 |
|
|
199 |
|
saveStmt.setInt(1, orgAdresse.id); |
200 |
|
saveStmt.setShort(2, orgAdresse.postnr); |
201 |
|
saveStmt.setString(3, orgAdresse.adresse); |
202 |
|
saveStmt.setInt(4, orgAdresse.gadeid); |
203 |
|
saveStmt.setShort(5, orgAdresse.husnr); |
204 |
|
saveStmt.setString(6, orgAdresse.husnrbogstav); |
205 |
|
saveStmt.setDouble(7, orgAdresse.latitude); |
206 |
|
saveStmt.setDouble(8, orgAdresse.longitude); |
207 |
|
saveStmt.setString(9, orgAdresse.rute); |
208 |
|
|
209 |
|
|
210 |
|
saveStmt.setInt(10, bedsteAdresse.id); |
211 |
|
saveStmt.setShort(11, bedsteAdresse.postnr); |
212 |
|
saveStmt.setString(12, bedsteAdresse.adresse); |
213 |
|
saveStmt.setInt(13, bedsteAdresse.gadeid); |
214 |
|
saveStmt.setShort(14, bedsteAdresse.husnr); |
215 |
|
saveStmt.setString(15, bedsteAdresse.husnrbogstav); |
216 |
|
saveStmt.setDouble(16, bedsteAdresse.latitude); |
217 |
|
saveStmt.setDouble(17, bedsteAdresse.longitude); |
218 |
|
saveStmt.setString(18, bedsteAdresse.rute); |
219 |
|
|
220 |
|
saveStmt.setDouble(19, bedsteAfstand); |
221 |
|
|
222 |
|
saveStmt.addBatch(); |
223 |
|
batchCount++; |
224 |
|
if (batchCount >= 1000) { |
225 |
|
saveStmt.executeBatch(); |
226 |
|
batchCount = 0; |
227 |
|
} |
228 |
|
//saveStmt.executeUpdate(); |
229 |
|
//saveStmt.clearParameters(); |
230 |
|
|
231 |
|
//saveStmt.close(); |
232 |
|
} |
233 |
|
|
234 |
|
public synchronized void saveBatch() throws SQLException{ |
235 |
|
saveStmt.executeBatch(); |
236 |
|
batchCount = 0; |
237 |
|
} |
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
protected ArrayList<Adresse> hentAdresseListe(PreparedStatement stmt) throws SQLException{ |
242 |
|
ArrayList<Adresse> list = new ArrayList<Adresse>( 1000000 ); |
243 |
|
|
244 |
|
//logger.info("Starting query"); |
245 |
|
ResultSet res = stmt.executeQuery(); |
246 |
|
//logger.info("Starting exec query done"); |
247 |
|
|
248 |
|
while (res.next()) { |
249 |
|
|
250 |
|
double latitude = res.getDouble(7); |
251 |
|
double longitude = res.getDouble(8); |
252 |
|
|
253 |
|
|
254 |
|
Adresse adr = new Adresse(latitude,longitude); |
255 |
|
|
256 |
|
adr.id = res.getInt(1); |
257 |
|
adr.postnr = res.getShort(2); |
258 |
|
adr.adresse = res.getString(3); |
259 |
|
adr.gadeid = res.getInt(4); |
260 |
|
adr.husnr = res.getShort(5); |
261 |
|
adr.husnrbogstav = husnrbogstavCache.getInstance( res.getString(6) ); |
262 |
|
//adr.latitude = res.getDouble(7); |
263 |
|
//adr.longitude = res.getDouble(8); |
264 |
|
adr.rute = ruteCache.getInstance( res.getString(9) ); |
265 |
|
adr.ho = res.getShort(10); |
266 |
|
|
267 |
|
list.add(adr); |
268 |
|
|
269 |
|
//logger.info( "Adress:" + adr); |
270 |
|
} |
271 |
|
|
272 |
|
res.close(); |
273 |
|
stmt.close(); |
274 |
|
|
275 |
|
return list; |
276 |
|
} |
277 |
|
|
278 |
|
public Connection getConnection(SafeProperties conf) throws SQLException, IOException { |
279 |
|
|
280 |
String db_host = conf.getProperty("DB_HOST"); |
String db_host = conf.getSafeProperty("DB_HOST"); |
281 |
String db_user = conf.getProperty("DB_USER"); |
String db_user = conf.getSafeProperty("DB_USER"); |
282 |
String db_pass = conf.getProperty("DB_PASS"); |
String db_pass = conf.getSafeProperty("DB_PASS"); |
283 |
|
|
284 |
|
|
285 |
|
|
289 |
connectionProps.put("user", db_user); |
connectionProps.put("user", db_user); |
290 |
connectionProps.put("password", db_pass); |
connectionProps.put("password", db_pass); |
291 |
|
|
292 |
conn = DriverManager.getConnection( |
//For debug output, tilføj denne til JDBC url'en: &profileSQL=true |
293 |
|
conn = DriverManager.getConnection( |
294 |
"jdbc:mysql://" + |
"jdbc:mysql://" + |
295 |
db_host + |
db_host + |
296 |
":3306/", |
":3306/?rewriteBatchedStatements=true", |
297 |
connectionProps); |
connectionProps); |
298 |
logger.info("Connected to database"); |
logger.info("Connected to database"); |
299 |
return conn; |
return conn; |
300 |
} |
} |
301 |
|
|
302 |
|
|
303 |
|
// ////////////////////////////////////////////////////////////////// |
304 |
|
|
305 |
public BoundingBox getBoundingbox(int postnr) throws SQLException { |
/* |
306 |
|
public Queue<Adresse> hentIkkedaekkedeAdresserCache(short postnr) { |
307 |
|
List<Adresse> postListe = ikkeDaekkedePrPost.get(postnr); |
308 |
|
|
309 |
|
return new ConcurrentLinkedQueue<Adresse>(postListe); |
310 |
|
} |
311 |
|
|
312 |
|
public Adresse[] hentDaekkedeAdresserCache( BoundingBox bbox, Adresse adresseListe[] ) { |
313 |
|
long start = System.currentTimeMillis(); |
314 |
|
ArrayList<Adresse> list = new ArrayList<Adresse>(); |
315 |
|
for (Adresse a : adresseListe) { |
316 |
|
if ( a.latitude > bbox.latitudeMin && a.latitude< bbox.latitudeMax && a.longitude> bbox.longitudeMin && a.longitude < bbox.longitudeMax) { |
317 |
|
list.add(a); |
318 |
|
} |
319 |
|
} |
320 |
|
long stop = System.currentTimeMillis(); |
321 |
|
logger.info("Elapsed cache: " + (stop - start)); |
322 |
|
return list.toArray( new Adresse[ list.size() ] ); |
323 |
|
} |
324 |
|
|
325 |
|
|
326 |
|
/*public Adresse[] hentDaekkedeAdresserCache( BoundingBox bbox) { |
327 |
|
return hentDaekkedeAdresserCache(bbox, alleAdresser); |
328 |
|
}*/ |
329 |
|
|
330 |
|
/* |
331 |
|
@Deprecated |
332 |
|
private BoundingBox getBoundingboxFromDb_old(String postnr) throws SQLException { |
333 |
|
String minPostnr = postnr.replace('x', '0'); |
334 |
|
String maxPostnr = postnr.replace('x', '9'); |
335 |
|
|
336 |
String sql = |
String sql = |
337 |
"select max(latitude) latmax, min(latitude) latmin, max(longitude) lngmax,min(longitude) lngmin " + |
"SELECT max(latitude) latmax, min(latitude) latmin, max(longitude) lngmax,min(longitude) lngmin " + |
338 |
"from fulddaekning.adressetabel WHERE postnr=? and rute is null;"; |
"FROM fulddaekning.adressetabel WHERE postnr BETWEEN ? and ? and rute is null;"; |
339 |
|
|
340 |
PreparedStatement stmt = conn.prepareStatement(sql); |
PreparedStatement stmt = conn.prepareStatement(sql); |
341 |
stmt.setInt(1, postnr); |
stmt.setString(1, minPostnr); |
342 |
|
stmt.setString(2, maxPostnr); |
343 |
|
|
344 |
ResultSet res = stmt.executeQuery(); |
ResultSet res = stmt.executeQuery(); |
345 |
res.next(); //query returnerer altid 1 række |
res.next(); //query returnerer altid 1 række |
346 |
|
|
347 |
BoundingBox bbox = new BoundingBox(); |
BoundingBox bbox = new BoundingBox(); |
348 |
bbox.latitudeMax = res.getDouble("latmax"); |
bbox.latitudeMax = res.getDouble("latmax"); |
349 |
bbox.latitudeMin = res.getDouble("latmin"); |
bbox.latitudeMin = res.getDouble("latmin"); |
350 |
bbox.longitudeMax = res.getDouble("lngmax"); |
bbox.longitudeMax = res.getDouble("lngmax"); |
351 |
bbox.longitudeMin = res.getDouble("lngmin"); |
bbox.longitudeMin = res.getDouble("lngmin"); |
352 |
|
|
353 |
res.close(); |
res.close(); |
354 |
stmt.close(); |
stmt.close(); |
355 |
|
|
356 |
return bbox; |
return bbox; |
357 |
} |
} |
358 |
|
|
359 |
public Queue<Adresse> hentIkkedaekkedeAdresser(int postnr) throws SQLException { |
@Deprecated |
360 |
ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>(); |
public Adresse[] hentDaekkedeAdresser_old( BoundingBox bbox) throws SQLException { |
361 |
|
long start = System.currentTimeMillis(); |
362 |
String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute FROM fulddaekning.adressetabel " + |
String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " + |
363 |
"WHERE rute IS NULL " + //Ingen dækning |
"FROM fulddaekning.adressetabel a " + |
364 |
"AND postnr=? " + |
"LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " + |
|
"AND latitude IS NOT NULL " + |
|
|
"AND longitude IS NOT NULL " + |
|
|
"AND gadeid IS NOT NULL "; |
|
|
PreparedStatement stmt = conn.prepareStatement(sql); |
|
|
stmt.setInt(1, postnr); |
|
|
|
|
|
queue.addAll( hentAdresseListe( stmt ) ); |
|
|
return queue; |
|
|
} |
|
|
|
|
|
public List<Integer> hentPostnumre() throws SQLException { |
|
|
ArrayList<Integer> list = new ArrayList<Integer>(); |
|
|
|
|
|
|
|
|
String sql = "SELECT postnr FROM fulddaekning.adressetabel WHERE distributor = 'DAO' and rute is not null GROUP BY postnr ORDER by postnr"; |
|
|
PreparedStatement stmt = conn.prepareStatement(sql); |
|
|
ResultSet res = stmt.executeQuery(); |
|
|
|
|
|
while (res.next()) { |
|
|
int postnr = res.getInt("postnr"); |
|
|
list.add(postnr); |
|
|
} |
|
|
res.close(); |
|
|
stmt.close(); |
|
|
|
|
|
//list.add(8700); |
|
|
|
|
|
return list; |
|
|
} |
|
|
|
|
|
public ArrayList<Adresse> hentDaekkedeAdresser( BoundingBox bbox) throws SQLException { |
|
|
String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute FROM fulddaekning.adressetabel " + |
|
365 |
"WHERE rute IS NOT NULL " + |
"WHERE rute IS NOT NULL " + |
366 |
"AND latitude BETWEEN ? AND ? " + |
"AND latitude BETWEEN ? AND ? " + |
367 |
"AND longitude BETWEEN ? AND ? " + |
"AND longitude BETWEEN ? AND ? " + |
368 |
"AND distributor = 'DAO' "; |
"AND a.distributor = ? "; |
369 |
|
|
370 |
// Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets) |
// Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets) |
371 |
// Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html |
// Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html |
372 |
//PreparedStatement stmt = conn.prepareStatement(sql); |
//PreparedStatement stmt = conn.prepareStatement(sql); |
373 |
PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); |
PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); |
374 |
stmt.setFetchSize(Integer.MIN_VALUE); |
stmt.setFetchSize(Integer.MIN_VALUE); |
375 |
|
|
376 |
stmt.setDouble(1, bbox.latitudeMin); |
stmt.setDouble(1, bbox.latitudeMin); |
377 |
stmt.setDouble(2, bbox.latitudeMax); |
stmt.setDouble(2, bbox.latitudeMax); |
378 |
stmt.setDouble(3, bbox.longitudeMin); |
stmt.setDouble(3, bbox.longitudeMin); |
379 |
stmt.setDouble(4, bbox.longitudeMax); |
stmt.setDouble(4, bbox.longitudeMax); |
380 |
|
stmt.setString(5, LookupMain.distributor); |
|
return hentAdresseListe( stmt ); |
|
|
|
|
|
} |
|
381 |
|
|
382 |
|
List<Adresse> list = hentAdresseListe( stmt ); |
383 |
|
long stop = System.currentTimeMillis(); |
384 |
|
logger.info("Elapsed DB: " + (stop - start)); |
385 |
|
return list.toArray( new Adresse[ list.size() ] ); |
386 |
|
} |
387 |
|
|
388 |
public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException { |
@Deprecated |
389 |
/*String sql = "INSERT INTO fulddaekning.afstand_anden_rute_thn (orgId,orgPostnr, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgLatitude,orgLongitude,orgRute,id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,afstand,`timestamp`) "+ |
public Queue<Adresse> hentIkkedaekkedeAdresser_old(String postnr) throws SQLException { |
|
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, now() )"; |
|
|
|
|
|
PreparedStatement saveStmt = conn.prepareStatement(sql);*/ |
|
|
|
|
|
saveStmt.setInt(1, orgAdresse.id); |
|
|
saveStmt.setInt(2, orgAdresse.postnr); |
|
|
saveStmt.setString(3, orgAdresse.adresse); |
|
|
saveStmt.setInt(4, orgAdresse.gadeid); |
|
|
saveStmt.setInt(5, orgAdresse.husnr); |
|
|
saveStmt.setString(6, orgAdresse.husnrbogstav); |
|
|
saveStmt.setDouble(7, orgAdresse.latitude); |
|
|
saveStmt.setDouble(8, orgAdresse.longitude); |
|
|
saveStmt.setString(9, orgAdresse.rute); |
|
|
|
|
390 |
|
|
391 |
saveStmt.setInt(10, bedsteAdresse.id); |
String minPostnr = postnr.replace('x', '0'); |
392 |
saveStmt.setInt(11, bedsteAdresse.postnr); |
String maxPostnr = postnr.replace('x', '9'); |
|
saveStmt.setString(12, bedsteAdresse.adresse); |
|
|
saveStmt.setInt(13, bedsteAdresse.gadeid); |
|
|
saveStmt.setInt(14, bedsteAdresse.husnr); |
|
|
saveStmt.setString(15, bedsteAdresse.husnrbogstav); |
|
|
saveStmt.setDouble(16, bedsteAdresse.latitude); |
|
|
saveStmt.setDouble(17, bedsteAdresse.longitude); |
|
|
saveStmt.setString(18, bedsteAdresse.rute); |
|
|
|
|
|
saveStmt.setDouble(19, bedsteAfstand); |
|
|
|
|
|
saveStmt.executeUpdate(); |
|
|
saveStmt.clearParameters(); |
|
|
|
|
|
|
|
|
//saveStmt.close(); |
|
393 |
|
|
394 |
|
ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>(); |
395 |
|
|
396 |
|
String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " + |
397 |
|
"FROM fulddaekning.adressetabel a " + |
398 |
|
"LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " + |
399 |
|
"WHERE rute IS NULL " + //Ingen dækning |
400 |
|
"AND a.postnr BETWEEN ? AND ? " + |
401 |
|
"AND latitude IS NOT NULL " + |
402 |
|
"AND longitude IS NOT NULL " + |
403 |
|
"AND gadeid IS NOT NULL " + |
404 |
|
"AND (a.distributor IS NULL OR a.distributor<>'LUKKET') "; |
405 |
|
PreparedStatement stmt = conn.prepareStatement(sql); |
406 |
|
stmt.setString(1, minPostnr); |
407 |
|
stmt.setString(2, maxPostnr); |
408 |
|
|
409 |
|
queue.addAll( hentAdresseListe( stmt ) ); |
410 |
|
return queue; |
411 |
} |
} |
412 |
|
|
413 |
|
@Deprecated |
414 |
|
public List<String> hentPostnumre_old() throws SQLException { |
415 |
|
ArrayList<String> list = new ArrayList<String>(); |
416 |
|
|
417 |
|
Constants consts = Constants.getInstance(); |
418 |
|
|
419 |
|
/* |
420 |
|
String sql = "SELECT postnr " + |
421 |
|
"FROM fulddaekning.adressetabel " + |
422 |
|
"WHERE postnr BETWEEN ? AND ? " + |
423 |
|
"AND rute is null " + // Træk kun liste på postnumre hvor der er ikke-dækkede adresser |
424 |
|
"GROUP BY postnr " + |
425 |
|
"ORDER by postnr"; |
426 |
|
* / |
427 |
|
|
428 |
|
|
429 |
|
String sql = "SELECT rpad(left(postnr,?),'4', 'x') as postnr2 " + |
430 |
|
"FROM fulddaekning.adressetabel " + |
431 |
|
"WHERE postnr BETWEEN ? AND ? " + |
432 |
|
"AND rute is null " + // Trae kun liste paa postnumre hvor der er ikke-daekede adresser |
433 |
|
"AND (postnr NOT BETWEEN 3900 and 3999) " + //Skip alle groenlandske postnumre |
434 |
|
"GROUP BY postnr2 " + |
435 |
|
"ORDER by postnr2 "; |
436 |
|
|
437 |
|
|
438 |
protected ArrayList<Adresse> hentAdresseListe(PreparedStatement stmt) throws SQLException{ |
|
439 |
ArrayList<Adresse> list = new ArrayList<Adresse>( 30000 ); |
PreparedStatement stmt = conn.prepareStatement(sql); |
440 |
|
//stmt.setString(1, Lookup.distributor ); |
441 |
//logger.info("Starting query"); |
|
442 |
|
stmt.setInt(1, consts.getPostnrGroup() ); |
443 |
|
|
444 |
|
stmt.setInt(2, consts.getMinPostnr()); |
445 |
|
stmt.setInt(3, consts.getMaxPostnr()); |
446 |
ResultSet res = stmt.executeQuery(); |
ResultSet res = stmt.executeQuery(); |
447 |
//logger.info("Starting exec query done"); |
|
|
|
|
448 |
while (res.next()) { |
while (res.next()) { |
449 |
Adresse adr = new Adresse(); |
String postnr = res.getString("postnr2"); |
450 |
adr.id = res.getInt("id"); |
list.add(postnr); |
|
adr.postnr = res.getInt("postnr"); |
|
|
adr.adresse = res.getString("adresse"); |
|
|
adr.gadeid = res.getInt("gadeid"); |
|
|
adr.husnr = res.getInt("husnr"); |
|
|
adr.husnrbogstav = res.getString("husnrbogstav"); |
|
|
adr.latitude = res.getDouble("latitude"); |
|
|
adr.longitude = res.getDouble("longitude"); |
|
|
adr.rute = res.getString("rute"); |
|
|
|
|
|
list.add(adr); |
|
|
|
|
|
//logger.info( "Adress:" + adr); |
|
451 |
} |
} |
|
|
|
452 |
res.close(); |
res.close(); |
453 |
stmt.close(); |
stmt.close(); |
454 |
|
|
455 |
|
//list.add(8700); |
456 |
|
|
457 |
return list; |
return list; |
458 |
} |
}*/ |
459 |
|
|
460 |
|
|
|
|
|
461 |
} |
} |