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; |
26 |
public Database(SafeProperties conf) throws SQLException,IOException { |
public Database(SafeProperties conf) throws SQLException,IOException { |
27 |
this.conn = getConnection( conf ); |
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 |
|
logger.info("Dropping old backup table (if exists)"); |
48 |
|
String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old"; |
49 |
|
conn.createStatement().executeUpdate(sql); |
50 |
|
|
51 |
|
logger.info("Rename tables"); |
52 |
|
sql = "RENAME TABLE fulddaekning.afstand_anden_rute TO fulddaekning.afstand_anden_rute_old, fulddaekning.afstand_anden_rute_ny TO fulddaekning.afstand_anden_rute"; |
53 |
|
conn.createStatement().executeUpdate(sql); |
54 |
} |
} |
55 |
|
|
56 |
public BoundingBox getBoundingbox(int postnr) throws SQLException { |
public BoundingBox getBoundingbox(int postnr) throws SQLException { |
57 |
|
|
58 |
String sql = |
String sql = |
59 |
"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 " + |
60 |
"from fulddaekning.adressetabel WHERE postnr=? and rute is null;"; |
"FROM fulddaekning.adressetabel WHERE postnr=? and rute is null;"; |
61 |
|
|
62 |
PreparedStatement stmt = conn.prepareStatement(sql); |
PreparedStatement stmt = conn.prepareStatement(sql); |
63 |
stmt.setInt(1, postnr); |
stmt.setInt(1, postnr); |
80 |
public Queue<Adresse> hentIkkedaekkedeAdresser(int postnr) throws SQLException { |
public Queue<Adresse> hentIkkedaekkedeAdresser(int postnr) throws SQLException { |
81 |
ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>(); |
ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>(); |
82 |
|
|
83 |
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 " + |
84 |
|
"FROM fulddaekning.adressetabel " + |
85 |
"WHERE rute IS NULL " + //Ingen dækning |
"WHERE rute IS NULL " + //Ingen dækning |
86 |
"AND postnr=? " + |
"AND postnr=? " + |
87 |
"AND latitude IS NOT NULL " + |
"AND latitude IS NOT NULL " + |
96 |
|
|
97 |
public List<Integer> hentPostnumre() throws SQLException { |
public List<Integer> hentPostnumre() throws SQLException { |
98 |
ArrayList<Integer> list = new ArrayList<Integer>(); |
ArrayList<Integer> list = new ArrayList<Integer>(); |
99 |
|
|
100 |
|
Constants consts = Constants.getInstance(); |
101 |
|
|
102 |
|
|
103 |
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 " + |
104 |
|
"FROM fulddaekning.adressetabel " + |
105 |
|
//"WHERE distributor = ? and rute is not null " + |
106 |
|
"WHERE postnr BETWEEN ? AND ? " + |
107 |
|
"GROUP BY postnr " + |
108 |
|
"ORDER by postnr"; |
109 |
PreparedStatement stmt = conn.prepareStatement(sql); |
PreparedStatement stmt = conn.prepareStatement(sql); |
110 |
|
//stmt.setString(1, Lookup.distributor ); |
111 |
|
stmt.setInt(1, consts.getMinPostnr()); |
112 |
|
stmt.setInt(2, consts.getMaxPostnr()); |
113 |
ResultSet res = stmt.executeQuery(); |
ResultSet res = stmt.executeQuery(); |
114 |
|
|
115 |
while (res.next()) { |
while (res.next()) { |
124 |
return list; |
return list; |
125 |
} |
} |
126 |
|
|
127 |
public ArrayList<Adresse> hentDaekkedeAdresser( BoundingBox bbox) throws SQLException { |
public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException { |
128 |
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 " + |
129 |
|
"FROM fulddaekning.adressetabel " + |
130 |
"WHERE rute IS NOT NULL " + |
"WHERE rute IS NOT NULL " + |
131 |
"AND latitude BETWEEN ? AND ? " + |
"AND latitude BETWEEN ? AND ? " + |
132 |
"AND longitude BETWEEN ? AND ? " + |
"AND longitude BETWEEN ? AND ? " + |
133 |
"AND distributor = 'DAO' "; |
"AND distributor = ? "; |
134 |
|
|
135 |
// 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) |
136 |
// 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 |
142 |
stmt.setDouble(2, bbox.latitudeMax); |
stmt.setDouble(2, bbox.latitudeMax); |
143 |
stmt.setDouble(3, bbox.longitudeMin); |
stmt.setDouble(3, bbox.longitudeMin); |
144 |
stmt.setDouble(4, bbox.longitudeMax); |
stmt.setDouble(4, bbox.longitudeMax); |
145 |
|
stmt.setString(5, Lookup.distributor); |
146 |
|
|
147 |
return hentAdresseListe( stmt ); |
List<Adresse> list = hentAdresseListe( stmt ); |
148 |
|
return list.toArray( new Adresse[ list.size() ] ); |
149 |
|
|
150 |
} |
} |
151 |
|
|
179 |
|
|
180 |
saveStmt.setDouble(19, bedsteAfstand); |
saveStmt.setDouble(19, bedsteAfstand); |
181 |
|
|
182 |
saveStmt.executeUpdate(); |
saveStmt.addBatch(); |
183 |
saveStmt.clearParameters(); |
batchCount++; |
184 |
|
if (batchCount >= 100) { |
185 |
|
saveStmt.executeBatch(); |
186 |
|
batchCount = 0; |
187 |
|
} |
188 |
|
//saveStmt.executeUpdate(); |
189 |
|
//saveStmt.clearParameters(); |
190 |
|
|
191 |
//saveStmt.close(); |
//saveStmt.close(); |
192 |
|
} |
193 |
|
|
194 |
|
public synchronized void saveBatch() throws SQLException{ |
195 |
|
saveStmt.executeBatch(); |
196 |
|
batchCount = 0; |
197 |
} |
} |
198 |
|
|
199 |
|
|
200 |
|
|
201 |
protected ArrayList<Adresse> hentAdresseListe(PreparedStatement stmt) throws SQLException{ |
protected ArrayList<Adresse> hentAdresseListe(PreparedStatement stmt) throws SQLException{ |
202 |
ArrayList<Adresse> list = new ArrayList<Adresse>( 30000 ); |
ArrayList<Adresse> list = new ArrayList<Adresse>( 1000000 ); |
203 |
|
|
204 |
//logger.info("Starting query"); |
//logger.info("Starting query"); |
205 |
ResultSet res = stmt.executeQuery(); |
ResultSet res = stmt.executeQuery(); |
242 |
connectionProps.put("user", db_user); |
connectionProps.put("user", db_user); |
243 |
connectionProps.put("password", db_pass); |
connectionProps.put("password", db_pass); |
244 |
|
|
245 |
|
//For debug output, tilføj denne til JDBC url'en: &profileSQL=true |
246 |
conn = DriverManager.getConnection( |
conn = DriverManager.getConnection( |
247 |
"jdbc:mysql://" + |
"jdbc:mysql://" + |
248 |
db_host + |
db_host + |
249 |
":3306/", |
":3306/?rewriteBatchedStatements=true", |
250 |
connectionProps); |
connectionProps); |
251 |
logger.info("Connected to database"); |
logger.info("Connected to database"); |
252 |
return conn; |
return conn; |