1 |
package dk.daoas.fulddaekning; |
package dk.daoas.fulddaekning; |
2 |
|
|
3 |
|
import java.io.IOException; |
4 |
import java.sql.Connection; |
import java.sql.Connection; |
5 |
|
import java.sql.DriverManager; |
6 |
import java.sql.PreparedStatement; |
import java.sql.PreparedStatement; |
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.List; |
import java.util.List; |
11 |
|
import java.util.Properties; |
12 |
import java.util.Queue; |
import java.util.Queue; |
13 |
import java.util.concurrent.ConcurrentLinkedQueue; |
import java.util.concurrent.ConcurrentLinkedQueue; |
14 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
15 |
|
|
16 |
|
|
17 |
|
|
18 |
public class Database { |
public class Database { |
19 |
Logger logger = Logger.getLogger(Database.class.getName()); |
Logger logger = Logger.getLogger(Database.class.getName()); |
20 |
|
|
21 |
|
int batchCount = 0; |
22 |
|
|
23 |
Connection conn; |
Connection conn; |
24 |
PreparedStatement saveStmt; |
PreparedStatement saveStmt; |
25 |
|
|
26 |
public Database(Connection conn) throws SQLException { |
public Database(SafeProperties conf) throws SQLException,IOException { |
27 |
this.conn = conn; |
this.conn = getConnection( conf ); |
28 |
|
|
29 |
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`) "+ |
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`) "+ |
30 |
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, now() )"; |
"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, now() )"; |
31 |
|
|
32 |
saveStmt = conn.prepareStatement(sql); |
saveStmt = conn.prepareStatement(sql); |
34 |
} |
} |
35 |
|
|
36 |
public void resetResultTable() throws SQLException { |
public void resetResultTable() throws SQLException { |
37 |
logger.info("Truncating result table"); |
logger.info("Dropping old result table (if exists)"); |
38 |
String sql = "TRUNCATE TABLE fulddaekning.afstand_anden_rute_thn"; |
String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_ny"; |
39 |
conn.createStatement().executeUpdate(sql); |
conn.createStatement().executeUpdate(sql); |
40 |
|
|
41 |
|
logger.info("Create new result table"); |
42 |
|
sql = "CREATE TABLE fulddaekning.afstand_anden_rute_ny LIKE fulddaekning.afstand_anden_rute"; |
43 |
|
conn.createStatement().executeUpdate(sql); |
44 |
|
} |
45 |
|
|
46 |
|
public void renameResultTables() throws SQLException { |
47 |
|
Constants consts = Constants.getInstance(); |
48 |
|
String ext = consts.getTableExtension(); |
49 |
|
|
50 |
|
logger.info("Dropping old backup table (if exists)"); |
51 |
|
String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old" + ext; |
52 |
|
conn.createStatement().executeUpdate(sql); |
53 |
|
|
54 |
|
logger.info("Rename tables"); |
55 |
|
sql = "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; |
56 |
|
conn.createStatement().executeUpdate(sql); |
57 |
} |
} |
58 |
|
|
59 |
public BoundingBox getBoundingbox(int postnr) throws SQLException { |
public BoundingBox getBoundingbox(int postnr) throws SQLException { |
60 |
|
|
61 |
String sql = |
String sql = |
62 |
"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 " + |
63 |
"from fulddaekning.adressetabel WHERE postnr=? and rute is null;"; |
"FROM fulddaekning.adressetabel WHERE postnr=? and rute is null;"; |
64 |
|
|
65 |
PreparedStatement stmt = conn.prepareStatement(sql); |
PreparedStatement stmt = conn.prepareStatement(sql); |
66 |
stmt.setInt(1, postnr); |
stmt.setInt(1, postnr); |
83 |
public Queue<Adresse> hentIkkedaekkedeAdresser(int postnr) throws SQLException { |
public Queue<Adresse> hentIkkedaekkedeAdresser(int postnr) throws SQLException { |
84 |
ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>(); |
ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>(); |
85 |
|
|
86 |
String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute FROM fulddaekning.adressetabel " + |
String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " + |
87 |
|
"FROM fulddaekning.adressetabel " + |
88 |
"WHERE rute IS NULL " + //Ingen dækning |
"WHERE rute IS NULL " + //Ingen dækning |
89 |
"AND postnr=? " + |
"AND postnr=? " + |
90 |
"AND latitude IS NOT NULL " + |
"AND latitude IS NOT NULL " + |
99 |
|
|
100 |
public List<Integer> hentPostnumre() throws SQLException { |
public List<Integer> hentPostnumre() throws SQLException { |
101 |
ArrayList<Integer> list = new ArrayList<Integer>(); |
ArrayList<Integer> list = new ArrayList<Integer>(); |
102 |
|
|
103 |
|
Constants consts = Constants.getInstance(); |
104 |
|
|
105 |
|
|
106 |
String sql = "SELECT postnr FROM fulddaekning.adressetabel WHERE distributor = 'DAO' and rute is not null GROUP BY postnr ORDER by postnr"; |
String sql = "SELECT postnr " + |
107 |
|
"FROM fulddaekning.adressetabel " + |
108 |
|
//"WHERE distributor = ? and rute is not null " + |
109 |
|
"WHERE postnr BETWEEN ? AND ? " + |
110 |
|
"GROUP BY postnr " + |
111 |
|
"ORDER by postnr"; |
112 |
PreparedStatement stmt = conn.prepareStatement(sql); |
PreparedStatement stmt = conn.prepareStatement(sql); |
113 |
|
//stmt.setString(1, Lookup.distributor ); |
114 |
|
stmt.setInt(1, consts.getMinPostnr()); |
115 |
|
stmt.setInt(2, consts.getMaxPostnr()); |
116 |
ResultSet res = stmt.executeQuery(); |
ResultSet res = stmt.executeQuery(); |
117 |
|
|
118 |
while (res.next()) { |
while (res.next()) { |
127 |
return list; |
return list; |
128 |
} |
} |
129 |
|
|
130 |
public ArrayList<Adresse> hentDaekkedeAdresser( BoundingBox bbox) throws SQLException { |
public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException { |
131 |
String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute FROM fulddaekning.adressetabel " + |
String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " + |
132 |
|
"FROM fulddaekning.adressetabel " + |
133 |
"WHERE rute IS NOT NULL " + |
"WHERE rute IS NOT NULL " + |
134 |
"AND latitude BETWEEN ? AND ? " + |
"AND latitude BETWEEN ? AND ? " + |
135 |
"AND longitude BETWEEN ? AND ? " + |
"AND longitude BETWEEN ? AND ? " + |
136 |
"AND distributor = 'DAO' "; |
"AND distributor = ? "; |
137 |
|
|
138 |
// 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) |
139 |
// 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 |
145 |
stmt.setDouble(2, bbox.latitudeMax); |
stmt.setDouble(2, bbox.latitudeMax); |
146 |
stmt.setDouble(3, bbox.longitudeMin); |
stmt.setDouble(3, bbox.longitudeMin); |
147 |
stmt.setDouble(4, bbox.longitudeMax); |
stmt.setDouble(4, bbox.longitudeMax); |
148 |
|
stmt.setString(5, Lookup.distributor); |
149 |
|
|
150 |
return hentAdresseListe( stmt ); |
List<Adresse> list = hentAdresseListe( stmt ); |
151 |
|
return list.toArray( new Adresse[ list.size() ] ); |
152 |
} |
} |
153 |
|
|
154 |
|
|
155 |
|
public Adresse[] hentAlleDaekkedeAdresser() throws SQLException { |
156 |
|
String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " + |
157 |
|
"FROM fulddaekning.adressetabel " + |
158 |
|
"WHERE rute IS NOT NULL " + |
159 |
|
"AND latitude IS NOT NULL" + |
160 |
|
"AND longitude IS NOT NULL " + |
161 |
|
"AND 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); |
166 |
|
PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); |
167 |
|
stmt.setFetchSize(Integer.MIN_VALUE); |
168 |
|
|
169 |
|
stmt.setString(1, Lookup.distributor); |
170 |
|
|
171 |
|
List<Adresse> list = hentAdresseListe( stmt ); |
172 |
|
return list.toArray( new Adresse[ list.size() ] ); |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException { |
public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException { |
178 |
/*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`) "+ |
/*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`) "+ |
203 |
|
|
204 |
saveStmt.setDouble(19, bedsteAfstand); |
saveStmt.setDouble(19, bedsteAfstand); |
205 |
|
|
206 |
saveStmt.executeUpdate(); |
saveStmt.addBatch(); |
207 |
saveStmt.clearParameters(); |
batchCount++; |
208 |
|
if (batchCount >= 100) { |
209 |
|
saveStmt.executeBatch(); |
210 |
|
batchCount = 0; |
211 |
|
} |
212 |
|
//saveStmt.executeUpdate(); |
213 |
|
//saveStmt.clearParameters(); |
214 |
|
|
215 |
//saveStmt.close(); |
//saveStmt.close(); |
216 |
|
} |
217 |
|
|
218 |
|
public synchronized void saveBatch() throws SQLException{ |
219 |
|
saveStmt.executeBatch(); |
220 |
|
batchCount = 0; |
221 |
} |
} |
222 |
|
|
223 |
|
|
224 |
|
|
225 |
protected ArrayList<Adresse> hentAdresseListe(PreparedStatement stmt) throws SQLException{ |
protected ArrayList<Adresse> hentAdresseListe(PreparedStatement stmt) throws SQLException{ |
226 |
ArrayList<Adresse> list = new ArrayList<Adresse>( 30000 ); |
ArrayList<Adresse> list = new ArrayList<Adresse>( 1000000 ); |
227 |
|
|
228 |
//logger.info("Starting query"); |
//logger.info("Starting query"); |
229 |
ResultSet res = stmt.executeQuery(); |
ResultSet res = stmt.executeQuery(); |
252 |
return list; |
return list; |
253 |
} |
} |
254 |
|
|
255 |
|
public Connection getConnection(SafeProperties conf) throws SQLException, IOException { |
256 |
|
|
257 |
|
String db_host = conf.getSafeProperty("DB_HOST"); |
258 |
|
String db_user = conf.getSafeProperty("DB_USER"); |
259 |
|
String db_pass = conf.getSafeProperty("DB_PASS"); |
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
Connection conn = null; |
265 |
|
Properties connectionProps = new Properties(); |
266 |
|
connectionProps.put("user", db_user); |
267 |
|
connectionProps.put("password", db_pass); |
268 |
|
|
269 |
|
//For debug output, tilføj denne til JDBC url'en: &profileSQL=true |
270 |
|
conn = DriverManager.getConnection( |
271 |
|
"jdbc:mysql://" + |
272 |
|
db_host + |
273 |
|
":3306/?rewriteBatchedStatements=true", |
274 |
|
connectionProps); |
275 |
|
logger.info("Connected to database"); |
276 |
|
return conn; |
277 |
|
} |
278 |
|
|
279 |
} |
} |