--- dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/Database.java 2014/09/11 08:14:40 2200 +++ dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/Database.java 2014/12/10 08:26:12 2240 @@ -14,9 +14,11 @@ import java.util.logging.Logger; + public class Database { Logger logger = Logger.getLogger(Database.class.getName()); + int batchCount = 0; Connection conn; PreparedStatement saveStmt; @@ -42,23 +44,31 @@ } public void renameResultTables() throws SQLException { + Constants consts = Constants.getInstance(); + String ext = consts.getTableExtension(); + logger.info("Dropping old backup table (if exists)"); - String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old"; + String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old" + ext; conn.createStatement().executeUpdate(sql); logger.info("Rename tables"); - 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; + + logger.info("Executing: " + sql); conn.createStatement().executeUpdate(sql); } - public BoundingBox getBoundingbox(int postnr) throws SQLException { + public BoundingBox getBoundingbox(String postnr) throws SQLException { + String minPostnr = postnr.replace('x', '0'); + String maxPostnr = postnr.replace('x', '9'); String sql = "SELECT max(latitude) latmax, min(latitude) latmin, max(longitude) lngmax,min(longitude) lngmin " + - "FROM fulddaekning.adressetabel WHERE postnr=? and rute is null;"; + "FROM fulddaekning.adressetabel WHERE postnr BETWEEN ? and ? and rute is null;"; PreparedStatement stmt = conn.prepareStatement(sql); - stmt.setInt(1, postnr); + stmt.setString(1, minPostnr); + stmt.setString(2, maxPostnr); ResultSet res = stmt.executeQuery(); res.next(); //query returnerer altid 1 række @@ -75,38 +85,66 @@ return bbox; } - public Queue hentIkkedaekkedeAdresser(int postnr) throws SQLException { + public Queue hentIkkedaekkedeAdresser(String postnr) throws SQLException { + + String minPostnr = postnr.replace('x', '0'); + String maxPostnr = postnr.replace('x', '9'); + ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue(); - 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 " + + "FROM fulddaekning.adressetabel a " + + "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " + "WHERE rute IS NULL " + //Ingen dækning - "AND postnr=? " + + "AND a.postnr BETWEEN ? AND ? " + "AND latitude IS NOT NULL " + "AND longitude IS NOT NULL " + - "AND gadeid IS NOT NULL "; + "AND gadeid IS NOT NULL " + + "AND (a.distributor IS NULL OR a.distributor<>'LUKKET') "; PreparedStatement stmt = conn.prepareStatement(sql); - stmt.setInt(1, postnr); + stmt.setString(1, minPostnr); + stmt.setString(2, maxPostnr); queue.addAll( hentAdresseListe( stmt ) ); return queue; } - public List hentPostnumre() throws SQLException { - ArrayList list = new ArrayList(); - + public List hentPostnumre() throws SQLException { + ArrayList list = new ArrayList(); + + Constants consts = Constants.getInstance(); + /* String sql = "SELECT postnr " + "FROM fulddaekning.adressetabel " + - "WHERE distributor = ? and rute is not null " + + "WHERE postnr BETWEEN ? AND ? " + + "AND rute is null " + // Træk kun liste på postnumre hvor der er ikke-dækkede adresser "GROUP BY postnr " + "ORDER by postnr"; + */ + + + String sql = "SELECT rpad(left(postnr,?),'4', 'x') as postnr2 " + + "FROM fulddaekning.adressetabel " + + "WHERE postnr BETWEEN ? AND ? " + + "AND rute is null " + // Trae kun liste paa postnumre hvor der er ikke-daekede adresser + "AND (postnr NOT BETWEEN 3900 and 3999) " + //Skip alle groenlandske postnumre + "GROUP BY postnr2 " + + "ORDER by postnr2 "; + + + PreparedStatement stmt = conn.prepareStatement(sql); - stmt.setString(1, Lookup.distributor ); + //stmt.setString(1, Lookup.distributor ); + + stmt.setInt(1, consts.getPostnrGroup() ); + + stmt.setInt(2, consts.getMinPostnr()); + stmt.setInt(3, consts.getMaxPostnr()); ResultSet res = stmt.executeQuery(); while (res.next()) { - int postnr = res.getInt("postnr"); + String postnr = res.getString("postnr2"); list.add(postnr); } res.close(); @@ -117,13 +155,14 @@ return list; } - public ArrayList hentDaekkedeAdresser( BoundingBox bbox) throws SQLException { - String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " + - "FROM fulddaekning.adressetabel " + + public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException { + String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " + + "FROM fulddaekning.adressetabel a " + + "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " + "WHERE rute IS NOT NULL " + "AND latitude BETWEEN ? AND ? " + "AND longitude BETWEEN ? AND ? " + - "AND distributor = ? "; + "AND a.distributor = ? "; // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets) // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html @@ -137,10 +176,33 @@ stmt.setDouble(4, bbox.longitudeMax); stmt.setString(5, Lookup.distributor); - return hentAdresseListe( stmt ); - + List list = hentAdresseListe( stmt ); + return list.toArray( new Adresse[ list.size() ] ); } + + + public Adresse[] hentAlleDaekkedeAdresser() throws SQLException { + String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " + + "FROM fulddaekning.adressetabel a " + + "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " + + "WHERE rute IS NOT NULL " + + "AND latitude IS NOT NULL " + + "AND longitude IS NOT NULL " + + "AND a.distributor = ? "; + // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets) + // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html + //PreparedStatement stmt = conn.prepareStatement(sql); + PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); + stmt.setFetchSize(Integer.MIN_VALUE); + + stmt.setString(1, Lookup.distributor); + + List list = hentAdresseListe( stmt ); + return list.toArray( new Adresse[ list.size() ] ); + } + + public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException { /*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`) "+ @@ -171,18 +233,27 @@ saveStmt.setDouble(19, bedsteAfstand); - saveStmt.executeUpdate(); - saveStmt.clearParameters(); - + saveStmt.addBatch(); + batchCount++; + if (batchCount >= 100) { + saveStmt.executeBatch(); + batchCount = 0; + } + //saveStmt.executeUpdate(); + //saveStmt.clearParameters(); //saveStmt.close(); - + } + + public synchronized void saveBatch() throws SQLException{ + saveStmt.executeBatch(); + batchCount = 0; } protected ArrayList hentAdresseListe(PreparedStatement stmt) throws SQLException{ - ArrayList list = new ArrayList( 30000 ); + ArrayList list = new ArrayList( 1000000 ); //logger.info("Starting query"); ResultSet res = stmt.executeQuery(); @@ -199,6 +270,7 @@ adr.latitude = res.getDouble("latitude"); adr.longitude = res.getDouble("longitude"); adr.rute = res.getString("rute"); + adr.ho = res.getInt("ho"); list.add(adr); @@ -225,10 +297,11 @@ connectionProps.put("user", db_user); connectionProps.put("password", db_pass); + //For debug output, tilføj denne til JDBC url'en: &profileSQL=true conn = DriverManager.getConnection( "jdbc:mysql://" + db_host + - ":3306/", + ":3306/?rewriteBatchedStatements=true", connectionProps); logger.info("Connected to database"); return conn;