/[projects]/dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/Database.java
ViewVC logotype

Diff of /dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/Database.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2327 by torben, Thu Feb 19 10:36:40 2015 UTC revision 2328 by torben, Thu Feb 19 13:39:33 2015 UTC
# Line 9  import java.sql.SQLException; Line 9  import java.sql.SQLException;
9  import java.util.ArrayList;  import java.util.ArrayList;
10  import java.util.HashMap;  import java.util.HashMap;
11  import java.util.List;  import java.util.List;
12    import java.util.Map;
13  import java.util.Properties;  import java.util.Properties;
14  import java.util.Queue;  import java.util.Queue;
15    import java.util.Set;
16    import java.util.TreeSet;
17  import java.util.concurrent.ConcurrentLinkedQueue;  import java.util.concurrent.ConcurrentLinkedQueue;
18  import java.util.logging.Logger;  import java.util.logging.Logger;
19    
# Line 25  public class Database { Line 28  public class Database {
28          PreparedStatement saveStmt;          PreparedStatement saveStmt;
29                    
30          Adresse alleAdresser[];          Adresse alleAdresser[];
31            Adresse alleIkkeDaekkede[];
32            
33                    
34          DeduplicateHelper<String> husnrbogstavCache = new DeduplicateHelper<String>();          DeduplicateHelper<String> husnrbogstavCache = new DeduplicateHelper<String>();
35          DeduplicateHelper<String> ruteCache = new DeduplicateHelper<String>();          DeduplicateHelper<String> ruteCache = new DeduplicateHelper<String>();
36                    
37          private HashMap<String,BoundingBox> bbCache = new HashMap<String,BoundingBox>();          Set<Integer> postnumre = new TreeSet<Integer>();
38            
39            Map<Integer, List<Adresse>> ikkeDaekkedePrPost = new HashMap<Integer, List<Adresse>>();
40            
41            
42            private HashMap<Integer,BoundingBox> bbCache = new HashMap<Integer,BoundingBox>();
43    
44          public Database(SafeProperties conf)  throws SQLException,IOException {          public Database(SafeProperties conf)  throws SQLException,IOException {
45                  this.conn = getConnection( conf );                        this.conn = getConnection( conf );      
# Line 66  public class Database { Line 76  public class Database {
76                  conn.createStatement().executeUpdate(sql);                                conn.createStatement().executeUpdate(sql);              
77          }          }
78                    
79          public BoundingBox getBoundingbox(String postnr) throws SQLException {          public BoundingBox getBoundingbox(int postnr)  {
80                  BoundingBox bb = bbCache.get(postnr);                  BoundingBox bb = bbCache.get(postnr);
                 if ( bb == null ) {  
                         bb = getBoundingboxFromDb(postnr);  
                         bbCache.put(postnr, bb);  
                 } else {  
                         logger.info("Serving BB from cache");  
                 }  
                   
81                  return bb.clone();//never return the original / cached object                  return bb.clone();//never return the original / cached object
82          }          }
   
         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  " +  
                                 "FROM fulddaekning.adressetabel WHERE postnr BETWEEN ? and ? and rute is null;";  
   
                 PreparedStatement stmt = conn.prepareStatement(sql);  
                 stmt.setString(1, minPostnr);  
                 stmt.setString(2, maxPostnr);  
   
                 ResultSet res = stmt.executeQuery();  
                 res.next(); //query returnerer altid 1 række  
   
                 BoundingBox bbox = new BoundingBox();  
                 bbox.latitudeMax = res.getDouble("latmax");  
                 bbox.latitudeMin = res.getDouble("latmin");  
                 bbox.longitudeMax = res.getDouble("lngmax");  
                 bbox.longitudeMin = res.getDouble("lngmin");  
   
                 res.close();  
                 stmt.close();  
   
                 return bbox;  
         }  
83                    
84                    
85            public Set<Integer> hentPostnumreCache() {
86                    return postnumre;
87            }
88    
89          public Queue<Adresse> hentIkkedaekkedeAdresser(String postnr)  throws SQLException {  
90                            public void hentAlleIkkedaekkedeAdresser(int minPostnr, int maxPostnr)  throws SQLException {
                 String minPostnr = postnr.replace('x', '0');  
                 String maxPostnr = postnr.replace('x', '9');  
91                                    
92                  ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>();                  logger.info("Henter alle IKKE-daekkede adresser");
93    
94                  String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +                  String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
95                                  "FROM fulddaekning.adressetabel a " +                                  "FROM fulddaekning.adressetabel a " +
# Line 124  public class Database { Line 101  public class Database {
101                                  "AND gadeid IS NOT NULL " +                                  "AND gadeid IS NOT NULL " +
102                                  "AND (a.distributor IS NULL OR a.distributor<>'LUKKET') ";                                                "AND (a.distributor IS NULL OR a.distributor<>'LUKKET') ";              
103                  PreparedStatement stmt = conn.prepareStatement(sql);                  PreparedStatement stmt = conn.prepareStatement(sql);
104                  stmt.setString(1, minPostnr);                  stmt.setInt(1, minPostnr);
105                  stmt.setString(2, maxPostnr);                  stmt.setInt(2, maxPostnr);
   
                 queue.addAll( hentAdresseListe( stmt ) );  
                 return queue;  
         }  
   
         public List<String> hentPostnumre() throws SQLException {  
                 ArrayList<String> list = new ArrayList<String>();  
                   
                 Constants consts = Constants.getInstance();  
106    
107                  /*                  List<Adresse> list = hentAdresseListe( stmt );
108                  String sql = "SELECT postnr " +                  alleIkkeDaekkede = list.toArray( new Adresse[ list.size() ] );
                                          "FROM fulddaekning.adressetabel " +  
                                          "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";  
                 */  
                   
109                                    
110                  String sql = "SELECT rpad(left(postnr,?),'4', 'x') as postnr2 " +                  logger.info("Analyserer ikke-daekkede adresser");
                                          "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 ";  
111                                    
112                                                            for (Adresse a : alleIkkeDaekkede) {
113                                                                    
114                  PreparedStatement stmt = conn.prepareStatement(sql);                          List<Adresse> postListe;
115                  //stmt.setString(1, Lookup.distributor );                          
116                            BoundingBox bbox;
117                  stmt.setInt(1, consts.getPostnrGroup() );                          
118                            if (! postnumre.contains(a.postnr )) {
119                  stmt.setInt(2, consts.getMinPostnr());                                  postnumre.add( a.postnr );
120                  stmt.setInt(3, consts.getMaxPostnr());                                  
121                  ResultSet res = stmt.executeQuery();                                  bbox = new BoundingBox();
122                                    postListe = new ArrayList<Adresse>();
123                  while (res.next()) {                                  
124                          String postnr = res.getString("postnr2");                                  bbCache.put( a.postnr, bbox);                                                                                  
125                          list.add(postnr);                                  ikkeDaekkedePrPost.put(a.postnr, postListe);
126                                    
127                            } else {
128                                     bbox = bbCache.get( a.postnr);
129                                     postListe = ikkeDaekkedePrPost.get(a.postnr);
130                            }
131                            
132                            bbox.latitudeMax = Math.max(bbox.latitudeMax, a.latitude);
133                            bbox.latitudeMin = Math.min(bbox.latitudeMin, a.latitude);
134                            bbox.longitudeMax = Math.max(bbox.longitudeMax, a.longitude);
135                            bbox.longitudeMin = Math.min(bbox.longitudeMin, a.longitude);
136                            
137                            postListe.add(a);
138                            
139                  }                  }
                 res.close();  
                 stmt.close();  
   
                 //list.add(8700);  
   
                 return list;  
140          }          }
141            
142          @Deprecated          public Queue<Adresse> hentIkkedaekkedeAdresserCache(int postnr)  {
143          public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {                  List<Adresse> postListe = ikkeDaekkedePrPost.get(postnr);
144                  long start = System.currentTimeMillis();                  
145                  String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +                  return new ConcurrentLinkedQueue<Adresse>(postListe);          
                                 "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 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.setDouble(1, bbox.latitudeMin);  
                 stmt.setDouble(2, bbox.latitudeMax);  
                 stmt.setDouble(3, bbox.longitudeMin);  
                 stmt.setDouble(4, bbox.longitudeMax);  
                 stmt.setString(5, LookupMain.distributor);  
   
                 List<Adresse> list = hentAdresseListe( stmt );  
                 long stop = System.currentTimeMillis();  
                 logger.info("Elapsed DB: " + (stop - start));  
                 return list.toArray( new Adresse[ list.size() ] );  
146          }          }
147    
148                    
149          public Adresse[] hentDaekkedeAdresserCache( BoundingBox bbox) {          public Adresse[] hentDaekkedeAdresserCache( BoundingBox bbox) {
150                  long start = System.currentTimeMillis();                  long start = System.currentTimeMillis();
# Line 363  public class Database { Line 303  public class Database {
303              return conn;              return conn;
304          }          }
305    
306            
307            // //////////////////////////////////////////////////////////////////
308            
309            @Deprecated
310            private BoundingBox getBoundingboxFromDb_old(String postnr) throws SQLException {
311                    String minPostnr = postnr.replace('x', '0');
312                    String maxPostnr = postnr.replace('x', '9');
313    
314                    String sql =
315                                    "SELECT max(latitude) latmax, min(latitude) latmin, max(longitude) lngmax,min(longitude) lngmin  " +
316                                    "FROM fulddaekning.adressetabel WHERE postnr BETWEEN ? and ? and rute is null;";
317    
318                    PreparedStatement stmt = conn.prepareStatement(sql);
319                    stmt.setString(1, minPostnr);
320                    stmt.setString(2, maxPostnr);
321    
322                    ResultSet res = stmt.executeQuery();
323                    res.next(); //query returnerer altid 1 række
324    
325                    BoundingBox bbox = new BoundingBox();
326                    bbox.latitudeMax = res.getDouble("latmax");
327                    bbox.latitudeMin = res.getDouble("latmin");
328                    bbox.longitudeMax = res.getDouble("lngmax");
329                    bbox.longitudeMin = res.getDouble("lngmin");
330    
331                    res.close();
332                    stmt.close();
333    
334                    return bbox;
335            }
336    
337            @Deprecated
338            public Adresse[] hentDaekkedeAdresser_old( BoundingBox bbox) throws SQLException {
339                    long start = System.currentTimeMillis();
340                    String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
341                                    "FROM fulddaekning.adressetabel a " +
342                                    "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
343                                    "WHERE rute IS NOT NULL " +
344                                    "AND latitude BETWEEN ? AND ? " +
345                                    "AND longitude BETWEEN ? AND ? " +
346                                    "AND a.distributor = ? ";
347    
348                    // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)
349                    // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html
350                    //PreparedStatement stmt = conn.prepareStatement(sql);
351                    PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
352                    stmt.setFetchSize(Integer.MIN_VALUE);
353    
354                    stmt.setDouble(1, bbox.latitudeMin);
355                    stmt.setDouble(2, bbox.latitudeMax);
356                    stmt.setDouble(3, bbox.longitudeMin);
357                    stmt.setDouble(4, bbox.longitudeMax);
358                    stmt.setString(5, LookupMain.distributor);
359    
360                    List<Adresse> list = hentAdresseListe( stmt );
361                    long stop = System.currentTimeMillis();
362                    logger.info("Elapsed DB: " + (stop - start));
363                    return list.toArray( new Adresse[ list.size() ] );
364            }
365            
366            @Deprecated
367            public Queue<Adresse> hentIkkedaekkedeAdresser_old(String postnr)  throws SQLException {
368                    
369                    String minPostnr = postnr.replace('x', '0');
370                    String maxPostnr = postnr.replace('x', '9');
371                    
372                    ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>();
373    
374                    String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
375                                    "FROM fulddaekning.adressetabel a " +
376                                    "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
377                                    "WHERE rute IS NULL " +  //Ingen dækning
378                                    "AND a.postnr BETWEEN ? AND ? " +
379                                    "AND latitude IS NOT NULL " +
380                                    "AND longitude IS NOT NULL " +
381                                    "AND gadeid IS NOT NULL " +
382                                    "AND (a.distributor IS NULL OR a.distributor<>'LUKKET') ";              
383                    PreparedStatement stmt = conn.prepareStatement(sql);
384                    stmt.setString(1, minPostnr);
385                    stmt.setString(2, maxPostnr);
386    
387                    queue.addAll( hentAdresseListe( stmt ) );
388                    return queue;
389            }
390            
391            @Deprecated
392            public List<String> hentPostnumre_old() throws SQLException {
393                    ArrayList<String> list = new ArrayList<String>();
394                    
395                    Constants consts = Constants.getInstance();
396    
397                    /*
398                    String sql = "SELECT postnr " +
399                                             "FROM fulddaekning.adressetabel " +
400                                             "WHERE postnr BETWEEN ? AND ? " +
401                                             "AND rute is null " + // Træk kun liste på postnumre hvor der er ikke-dækkede adresser
402                                             "GROUP BY postnr " +
403                                             "ORDER by postnr";
404                    */
405                    
406                    
407                    String sql = "SELECT rpad(left(postnr,?),'4', 'x') as postnr2 " +
408                                             "FROM fulddaekning.adressetabel " +
409                                             "WHERE postnr BETWEEN ? AND ? " +
410                                             "AND rute is null " + // Trae kun liste paa postnumre hvor der er ikke-daekede adresser
411                                             "AND (postnr NOT BETWEEN 3900 and 3999) " + //Skip alle groenlandske postnumre
412                                             "GROUP BY postnr2 " +
413                                             "ORDER by postnr2 ";
414                    
415                                            
416                                            
417                    PreparedStatement stmt = conn.prepareStatement(sql);
418                    //stmt.setString(1, Lookup.distributor );
419    
420                    stmt.setInt(1, consts.getPostnrGroup() );
421    
422                    stmt.setInt(2, consts.getMinPostnr());
423                    stmt.setInt(3, consts.getMaxPostnr());
424                    ResultSet res = stmt.executeQuery();
425    
426                    while (res.next()) {
427                            String postnr = res.getString("postnr2");
428                            list.add(postnr);
429                    }
430                    res.close();
431                    stmt.close();
432    
433                    //list.add(8700);
434    
435                    return list;
436            }
437    
438            
439  }  }

Legend:
Removed from v.2327  
changed lines
  Added in v.2328

  ViewVC Help
Powered by ViewVC 1.1.20