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; |
44 |
} |
} |
45 |
|
|
46 |
public void renameResultTables() throws SQLException { |
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)"); |
logger.info("Dropping old backup table (if exists)"); |
51 |
String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old"; |
String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old" + ext; |
52 |
conn.createStatement().executeUpdate(sql); |
conn.createStatement().executeUpdate(sql); |
53 |
|
|
54 |
logger.info("Rename tables"); |
logger.info("Rename tables"); |
55 |
sql = "RENAME TABLE fulddaekning.afstand_anden_rute TO fulddaekning.afstand_anden_rute_old, fulddaekning.afstand_anden_rute_ny TO fulddaekning.afstand_anden_rute"; |
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 |
|
|
57 |
|
logger.info("Executing: " + sql); |
58 |
conn.createStatement().executeUpdate(sql); |
conn.createStatement().executeUpdate(sql); |
59 |
} |
} |
60 |
|
|
61 |
public BoundingBox getBoundingbox(int postnr) throws SQLException { |
public BoundingBox getBoundingbox(int postnr) throws SQLException { |
62 |
|
|
63 |
String sql = |
String sql = |
64 |
"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 " + |
65 |
"from fulddaekning.adressetabel WHERE postnr=? and rute is null;"; |
"FROM fulddaekning.adressetabel WHERE postnr=? and rute is null;"; |
66 |
|
|
67 |
PreparedStatement stmt = conn.prepareStatement(sql); |
PreparedStatement stmt = conn.prepareStatement(sql); |
68 |
stmt.setInt(1, postnr); |
stmt.setInt(1, postnr); |
85 |
public Queue<Adresse> hentIkkedaekkedeAdresser(int postnr) throws SQLException { |
public Queue<Adresse> hentIkkedaekkedeAdresser(int postnr) throws SQLException { |
86 |
ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>(); |
ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>(); |
87 |
|
|
88 |
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 " + |
89 |
|
"FROM fulddaekning.adressetabel " + |
90 |
"WHERE rute IS NULL " + //Ingen dækning |
"WHERE rute IS NULL " + //Ingen dækning |
91 |
"AND postnr=? " + |
"AND postnr=? " + |
92 |
"AND latitude IS NOT NULL " + |
"AND latitude IS NOT NULL " + |
101 |
|
|
102 |
public List<Integer> hentPostnumre() throws SQLException { |
public List<Integer> hentPostnumre() throws SQLException { |
103 |
ArrayList<Integer> list = new ArrayList<Integer>(); |
ArrayList<Integer> list = new ArrayList<Integer>(); |
104 |
|
|
105 |
|
Constants consts = Constants.getInstance(); |
106 |
|
|
107 |
|
|
108 |
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 " + |
109 |
|
"FROM fulddaekning.adressetabel " + |
110 |
|
//"WHERE distributor = ? and rute is not null " + |
111 |
|
"WHERE postnr BETWEEN ? AND ? " + |
112 |
|
"GROUP BY postnr " + |
113 |
|
"ORDER by postnr"; |
114 |
PreparedStatement stmt = conn.prepareStatement(sql); |
PreparedStatement stmt = conn.prepareStatement(sql); |
115 |
|
//stmt.setString(1, Lookup.distributor ); |
116 |
|
stmt.setInt(1, consts.getMinPostnr()); |
117 |
|
stmt.setInt(2, consts.getMaxPostnr()); |
118 |
ResultSet res = stmt.executeQuery(); |
ResultSet res = stmt.executeQuery(); |
119 |
|
|
120 |
while (res.next()) { |
while (res.next()) { |
129 |
return list; |
return list; |
130 |
} |
} |
131 |
|
|
132 |
public ArrayList<Adresse> hentDaekkedeAdresser( BoundingBox bbox) throws SQLException { |
public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException { |
133 |
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 " + |
134 |
|
"FROM fulddaekning.adressetabel " + |
135 |
"WHERE rute IS NOT NULL " + |
"WHERE rute IS NOT NULL " + |
136 |
"AND latitude BETWEEN ? AND ? " + |
"AND latitude BETWEEN ? AND ? " + |
137 |
"AND longitude BETWEEN ? AND ? " + |
"AND longitude BETWEEN ? AND ? " + |
138 |
"AND distributor = 'DAO' "; |
"AND distributor = ? "; |
139 |
|
|
140 |
// 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) |
141 |
// 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 |
147 |
stmt.setDouble(2, bbox.latitudeMax); |
stmt.setDouble(2, bbox.latitudeMax); |
148 |
stmt.setDouble(3, bbox.longitudeMin); |
stmt.setDouble(3, bbox.longitudeMin); |
149 |
stmt.setDouble(4, bbox.longitudeMax); |
stmt.setDouble(4, bbox.longitudeMax); |
150 |
|
stmt.setString(5, Lookup.distributor); |
151 |
|
|
152 |
return hentAdresseListe( stmt ); |
List<Adresse> list = hentAdresseListe( stmt ); |
153 |
|
return list.toArray( new Adresse[ list.size() ] ); |
154 |
} |
} |
155 |
|
|
156 |
|
|
157 |
|
public Adresse[] hentAlleDaekkedeAdresser() throws SQLException { |
158 |
|
String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " + |
159 |
|
"FROM fulddaekning.adressetabel " + |
160 |
|
"WHERE rute IS NOT NULL " + |
161 |
|
"AND latitude IS NOT NULL " + |
162 |
|
"AND longitude IS NOT NULL " + |
163 |
|
"AND distributor = ? "; |
164 |
|
|
165 |
|
// Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets) |
166 |
|
// Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html |
167 |
|
//PreparedStatement stmt = conn.prepareStatement(sql); |
168 |
|
PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); |
169 |
|
stmt.setFetchSize(Integer.MIN_VALUE); |
170 |
|
|
171 |
|
stmt.setString(1, Lookup.distributor); |
172 |
|
|
173 |
|
List<Adresse> list = hentAdresseListe( stmt ); |
174 |
|
return list.toArray( new Adresse[ list.size() ] ); |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
|
|
179 |
public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException { |
public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException { |
180 |
/*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`) "+ |
205 |
|
|
206 |
saveStmt.setDouble(19, bedsteAfstand); |
saveStmt.setDouble(19, bedsteAfstand); |
207 |
|
|
208 |
saveStmt.executeUpdate(); |
saveStmt.addBatch(); |
209 |
saveStmt.clearParameters(); |
batchCount++; |
210 |
|
if (batchCount >= 100) { |
211 |
|
saveStmt.executeBatch(); |
212 |
|
batchCount = 0; |
213 |
|
} |
214 |
|
//saveStmt.executeUpdate(); |
215 |
|
//saveStmt.clearParameters(); |
216 |
|
|
217 |
//saveStmt.close(); |
//saveStmt.close(); |
218 |
|
} |
219 |
|
|
220 |
|
public synchronized void saveBatch() throws SQLException{ |
221 |
|
saveStmt.executeBatch(); |
222 |
|
batchCount = 0; |
223 |
} |
} |
224 |
|
|
225 |
|
|
226 |
|
|
227 |
protected ArrayList<Adresse> hentAdresseListe(PreparedStatement stmt) throws SQLException{ |
protected ArrayList<Adresse> hentAdresseListe(PreparedStatement stmt) throws SQLException{ |
228 |
ArrayList<Adresse> list = new ArrayList<Adresse>( 30000 ); |
ArrayList<Adresse> list = new ArrayList<Adresse>( 1000000 ); |
229 |
|
|
230 |
//logger.info("Starting query"); |
//logger.info("Starting query"); |
231 |
ResultSet res = stmt.executeQuery(); |
ResultSet res = stmt.executeQuery(); |
268 |
connectionProps.put("user", db_user); |
connectionProps.put("user", db_user); |
269 |
connectionProps.put("password", db_pass); |
connectionProps.put("password", db_pass); |
270 |
|
|
271 |
|
//For debug output, tilføj denne til JDBC url'en: &profileSQL=true |
272 |
conn = DriverManager.getConnection( |
conn = DriverManager.getConnection( |
273 |
"jdbc:mysql://" + |
"jdbc:mysql://" + |
274 |
db_host + |
db_host + |
275 |
":3306/", |
":3306/?rewriteBatchedStatements=true", |
276 |
connectionProps); |
connectionProps); |
277 |
logger.info("Connected to database"); |
logger.info("Connected to database"); |
278 |
return conn; |
return conn; |