--- dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/Database.java 2014/12/09 20:39:45 2238 +++ dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/Database.java 2015/02/19 10:36:40 2327 @@ -7,6 +7,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Properties; import java.util.Queue; @@ -22,6 +23,13 @@ Connection conn; PreparedStatement saveStmt; + + Adresse alleAdresser[]; + + DeduplicateHelper husnrbogstavCache = new DeduplicateHelper(); + DeduplicateHelper ruteCache = new DeduplicateHelper(); + + private HashMap bbCache = new HashMap(); public Database(SafeProperties conf) throws SQLException,IOException { this.conn = getConnection( conf ); @@ -56,11 +64,23 @@ logger.info("Executing: " + sql); conn.createStatement().executeUpdate(sql); - } - + } + public BoundingBox getBoundingbox(String postnr) throws SQLException { - String minPostnr = postnr.replace("x", "0"); - String maxPostnr = postnr.replace("x", "9"); + BoundingBox bb = bbCache.get(postnr); + if ( bb == null ) { + bb = getBoundingboxFromDb(postnr); + bbCache.put(postnr, bb); + } else { + logger.info("Serving BB from cache"); + } + + return bb.clone();//never return the original / cached object + } + + private BoundingBox getBoundingboxFromDb(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 " + @@ -84,11 +104,13 @@ return bbox; } + + public Queue hentIkkedaekkedeAdresser(String postnr) throws SQLException { - String minPostnr = postnr.replace("x", "0"); - String maxPostnr = postnr.replace("x", "9"); + String minPostnr = postnr.replace('x', '0'); + String maxPostnr = postnr.replace('x', '9'); ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue(); @@ -124,10 +146,11 @@ */ - String sql = "SELECT concat(left(postnr,3),'x') as postnr2 " + + String sql = "SELECT rpad(left(postnr,?),'4', 'x') as postnr2 " + "FROM fulddaekning.adressetabel " + "WHERE postnr BETWEEN ? AND ? " + - "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 + "AND (postnr NOT BETWEEN 3900 and 3999) " + //Skip alle groenlandske postnumre "GROUP BY postnr2 " + "ORDER by postnr2 "; @@ -135,8 +158,11 @@ PreparedStatement stmt = conn.prepareStatement(sql); //stmt.setString(1, Lookup.distributor ); - stmt.setInt(1, consts.getMinPostnr()); - stmt.setInt(2, consts.getMaxPostnr()); + + stmt.setInt(1, consts.getPostnrGroup() ); + + stmt.setInt(2, consts.getMinPostnr()); + stmt.setInt(3, consts.getMaxPostnr()); ResultSet res = stmt.executeQuery(); while (res.next()) { @@ -151,7 +177,9 @@ return list; } + @Deprecated public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException { + long start = System.currentTimeMillis(); 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) " + @@ -170,32 +198,50 @@ stmt.setDouble(2, bbox.latitudeMax); stmt.setDouble(3, bbox.longitudeMin); stmt.setDouble(4, bbox.longitudeMax); - stmt.setString(5, Lookup.distributor); + stmt.setString(5, LookupMain.distributor); List list = hentAdresseListe( stmt ); + long stop = System.currentTimeMillis(); + logger.info("Elapsed DB: " + (stop - start)); return list.toArray( new Adresse[ list.size() ] ); } + public Adresse[] hentDaekkedeAdresserCache( BoundingBox bbox) { + long start = System.currentTimeMillis(); + ArrayList list = new ArrayList(); + for (Adresse a : alleAdresser) { + if ( a.latitude > bbox.latitudeMin && a.latitude< bbox.latitudeMax && a.longitude> bbox.longitudeMin && a.longitude < bbox.longitudeMax) { + list.add(a); + } + } + long stop = System.currentTimeMillis(); + logger.info("Elapsed cache: " + (stop - start)); + 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() ] ); + if ( alleAdresser == null ) { + 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, LookupMain.distributor); + + List list = hentAdresseListe( stmt ); + alleAdresser = list.toArray( new Adresse[ list.size() ] ); + } + return alleAdresser; } @@ -257,6 +303,8 @@ while (res.next()) { Adresse adr = new Adresse(); + + /* adr.id = res.getInt("id"); adr.postnr = res.getInt("postnr"); adr.adresse = res.getString("adresse"); @@ -267,6 +315,18 @@ adr.longitude = res.getDouble("longitude"); adr.rute = res.getString("rute"); adr.ho = res.getInt("ho"); + */ + + adr.id = res.getInt(1); + adr.postnr = res.getInt(2); + adr.adresse = res.getString(3); + adr.gadeid = res.getInt(4); + adr.husnr = res.getInt(5); + adr.husnrbogstav = husnrbogstavCache.getInstance( res.getString(6) ); + adr.latitude = res.getDouble(7); + adr.longitude = res.getDouble(8); + adr.rute = ruteCache.getInstance( res.getString(9) ); + adr.ho = res.getInt(10); list.add(adr);