7 |
import java.sql.ResultSet; |
import java.sql.ResultSet; |
8 |
import java.sql.SQLException; |
import java.sql.SQLException; |
9 |
import java.util.ArrayList; |
import java.util.ArrayList; |
10 |
|
import java.util.HashMap; |
11 |
import java.util.List; |
import java.util.List; |
12 |
import java.util.Properties; |
import java.util.Properties; |
13 |
import java.util.Queue; |
import java.util.Queue; |
23 |
|
|
24 |
Connection conn; |
Connection conn; |
25 |
PreparedStatement saveStmt; |
PreparedStatement saveStmt; |
26 |
|
|
27 |
|
Adresse alleAdresser[]; |
28 |
|
|
29 |
|
DeduplicateHelper<String> husnrbogstavCache = new DeduplicateHelper<String>(); |
30 |
|
DeduplicateHelper<String> ruteCache = new DeduplicateHelper<String>(); |
31 |
|
|
32 |
|
private HashMap<String,BoundingBox> bbCache = new HashMap<String,BoundingBox>(); |
33 |
|
|
34 |
public Database(SafeProperties conf) throws SQLException,IOException { |
public Database(SafeProperties conf) throws SQLException,IOException { |
35 |
this.conn = getConnection( conf ); |
this.conn = getConnection( conf ); |
64 |
|
|
65 |
logger.info("Executing: " + sql); |
logger.info("Executing: " + sql); |
66 |
conn.createStatement().executeUpdate(sql); |
conn.createStatement().executeUpdate(sql); |
67 |
} |
} |
68 |
|
|
69 |
public BoundingBox getBoundingbox(String postnr) throws SQLException { |
public BoundingBox getBoundingbox(String postnr) throws SQLException { |
70 |
String minPostnr = postnr.replace("x", "0"); |
BoundingBox bb = bbCache.get(postnr); |
71 |
String maxPostnr = postnr.replace("x", "9"); |
if ( bb == null ) { |
72 |
|
bb = getBoundingboxFromDb(postnr); |
73 |
|
bbCache.put(postnr, bb); |
74 |
|
} else { |
75 |
|
logger.info("Serving BB from cache"); |
76 |
|
} |
77 |
|
|
78 |
|
return bb.clone();//never return the original / cached object |
79 |
|
} |
80 |
|
|
81 |
|
private BoundingBox getBoundingboxFromDb(String postnr) throws SQLException { |
82 |
|
String minPostnr = postnr.replace('x', '0'); |
83 |
|
String maxPostnr = postnr.replace('x', '9'); |
84 |
|
|
85 |
String sql = |
String sql = |
86 |
"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 " + |
104 |
|
|
105 |
return bbox; |
return bbox; |
106 |
} |
} |
107 |
|
|
108 |
|
|
109 |
|
|
110 |
public Queue<Adresse> hentIkkedaekkedeAdresser(String postnr) throws SQLException { |
public Queue<Adresse> hentIkkedaekkedeAdresser(String postnr) throws SQLException { |
111 |
|
|
112 |
String minPostnr = postnr.replace("x", "0"); |
String minPostnr = postnr.replace('x', '0'); |
113 |
String maxPostnr = postnr.replace("x", "9"); |
String maxPostnr = postnr.replace('x', '9'); |
114 |
|
|
115 |
ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>(); |
ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>(); |
116 |
|
|
146 |
*/ |
*/ |
147 |
|
|
148 |
|
|
149 |
String sql = "SELECT concat(left(postnr,3),'x') as postnr2 " + |
String sql = "SELECT rpad(left(postnr,?),'4', 'x') as postnr2 " + |
150 |
"FROM fulddaekning.adressetabel " + |
"FROM fulddaekning.adressetabel " + |
151 |
"WHERE postnr BETWEEN ? AND ? " + |
"WHERE postnr BETWEEN ? AND ? " + |
152 |
"AND rute is null " + // Træk kun liste på postnumre hvor der er ikke-dækkede adresser |
"AND rute is null " + // Trae kun liste paa postnumre hvor der er ikke-daekede adresser |
153 |
|
"AND (postnr NOT BETWEEN 3900 and 3999) " + //Skip alle groenlandske postnumre |
154 |
"GROUP BY postnr2 " + |
"GROUP BY postnr2 " + |
155 |
"ORDER by postnr2 "; |
"ORDER by postnr2 "; |
156 |
|
|
158 |
|
|
159 |
PreparedStatement stmt = conn.prepareStatement(sql); |
PreparedStatement stmt = conn.prepareStatement(sql); |
160 |
//stmt.setString(1, Lookup.distributor ); |
//stmt.setString(1, Lookup.distributor ); |
161 |
stmt.setInt(1, consts.getMinPostnr()); |
|
162 |
stmt.setInt(2, consts.getMaxPostnr()); |
stmt.setInt(1, consts.getPostnrGroup() ); |
163 |
|
|
164 |
|
stmt.setInt(2, consts.getMinPostnr()); |
165 |
|
stmt.setInt(3, consts.getMaxPostnr()); |
166 |
ResultSet res = stmt.executeQuery(); |
ResultSet res = stmt.executeQuery(); |
167 |
|
|
168 |
while (res.next()) { |
while (res.next()) { |
177 |
return list; |
return list; |
178 |
} |
} |
179 |
|
|
180 |
|
@Deprecated |
181 |
public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException { |
public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException { |
182 |
|
long start = System.currentTimeMillis(); |
183 |
String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " + |
String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " + |
184 |
"FROM fulddaekning.adressetabel a " + |
"FROM fulddaekning.adressetabel a " + |
185 |
"LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " + |
"LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " + |
198 |
stmt.setDouble(2, bbox.latitudeMax); |
stmt.setDouble(2, bbox.latitudeMax); |
199 |
stmt.setDouble(3, bbox.longitudeMin); |
stmt.setDouble(3, bbox.longitudeMin); |
200 |
stmt.setDouble(4, bbox.longitudeMax); |
stmt.setDouble(4, bbox.longitudeMax); |
201 |
stmt.setString(5, Lookup.distributor); |
stmt.setString(5, LookupMain.distributor); |
202 |
|
|
203 |
List<Adresse> list = hentAdresseListe( stmt ); |
List<Adresse> list = hentAdresseListe( stmt ); |
204 |
|
long stop = System.currentTimeMillis(); |
205 |
|
logger.info("Elapsed DB: " + (stop - start)); |
206 |
return list.toArray( new Adresse[ list.size() ] ); |
return list.toArray( new Adresse[ list.size() ] ); |
207 |
} |
} |
208 |
|
|
209 |
|
public Adresse[] hentDaekkedeAdresserCache( BoundingBox bbox) { |
210 |
|
long start = System.currentTimeMillis(); |
211 |
|
ArrayList<Adresse> list = new ArrayList<Adresse>(); |
212 |
|
for (Adresse a : alleAdresser) { |
213 |
|
if ( a.latitude > bbox.latitudeMin && a.latitude< bbox.latitudeMax && a.longitude> bbox.longitudeMin && a.longitude < bbox.longitudeMax) { |
214 |
|
list.add(a); |
215 |
|
} |
216 |
|
} |
217 |
|
long stop = System.currentTimeMillis(); |
218 |
|
logger.info("Elapsed cache: " + (stop - start)); |
219 |
|
return list.toArray( new Adresse[ list.size() ] ); |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
public Adresse[] hentAlleDaekkedeAdresser() throws SQLException { |
public Adresse[] hentAlleDaekkedeAdresser() throws SQLException { |
224 |
String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " + |
if ( alleAdresser == null ) { |
225 |
"FROM fulddaekning.adressetabel a " + |
String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " + |
226 |
"LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " + |
"FROM fulddaekning.adressetabel a " + |
227 |
"WHERE rute IS NOT NULL " + |
"LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " + |
228 |
"AND latitude IS NOT NULL " + |
"WHERE rute IS NOT NULL " + |
229 |
"AND longitude IS NOT NULL " + |
"AND latitude IS NOT NULL " + |
230 |
"AND a.distributor = ? "; |
"AND longitude IS NOT NULL " + |
231 |
|
"AND a.distributor = ? "; |
232 |
// Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets) |
|
233 |
// Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html |
// Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets) |
234 |
//PreparedStatement stmt = conn.prepareStatement(sql); |
// Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html |
235 |
PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); |
//PreparedStatement stmt = conn.prepareStatement(sql); |
236 |
stmt.setFetchSize(Integer.MIN_VALUE); |
PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); |
237 |
|
stmt.setFetchSize(Integer.MIN_VALUE); |
238 |
stmt.setString(1, Lookup.distributor); |
|
239 |
|
stmt.setString(1, LookupMain.distributor); |
240 |
List<Adresse> list = hentAdresseListe( stmt ); |
|
241 |
return list.toArray( new Adresse[ list.size() ] ); |
List<Adresse> list = hentAdresseListe( stmt ); |
242 |
|
alleAdresser = list.toArray( new Adresse[ list.size() ] ); |
243 |
|
} |
244 |
|
return alleAdresser; |
245 |
} |
} |
246 |
|
|
247 |
|
|
303 |
|
|
304 |
while (res.next()) { |
while (res.next()) { |
305 |
Adresse adr = new Adresse(); |
Adresse adr = new Adresse(); |
306 |
|
|
307 |
|
/* |
308 |
adr.id = res.getInt("id"); |
adr.id = res.getInt("id"); |
309 |
adr.postnr = res.getInt("postnr"); |
adr.postnr = res.getInt("postnr"); |
310 |
adr.adresse = res.getString("adresse"); |
adr.adresse = res.getString("adresse"); |
315 |
adr.longitude = res.getDouble("longitude"); |
adr.longitude = res.getDouble("longitude"); |
316 |
adr.rute = res.getString("rute"); |
adr.rute = res.getString("rute"); |
317 |
adr.ho = res.getInt("ho"); |
adr.ho = res.getInt("ho"); |
318 |
|
*/ |
319 |
|
|
320 |
|
adr.id = res.getInt(1); |
321 |
|
adr.postnr = res.getInt(2); |
322 |
|
adr.adresse = res.getString(3); |
323 |
|
adr.gadeid = res.getInt(4); |
324 |
|
adr.husnr = res.getInt(5); |
325 |
|
adr.husnrbogstav = husnrbogstavCache.getInstance( res.getString(6) ); |
326 |
|
adr.latitude = res.getDouble(7); |
327 |
|
adr.longitude = res.getDouble(8); |
328 |
|
adr.rute = ruteCache.getInstance( res.getString(9) ); |
329 |
|
adr.ho = res.getInt(10); |
330 |
|
|
331 |
list.add(adr); |
list.add(adr); |
332 |
|
|