/[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 2263 by torben, Tue Feb 10 16:27:15 2015 UTC revision 2331 by torben, Fri Feb 20 08:58:19 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          private HashMap<String,BoundingBox> bbCache = new HashMap<String,BoundingBox>();          
34            DeduplicateHelper<String> husnrbogstavCache = new DeduplicateHelper<String>();
35            DeduplicateHelper<String> ruteCache = new DeduplicateHelper<String>();
36            
37            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 63  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 121  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                    logger.info("Analyserer ikke-daekkede adresser");
111                                    
112                  String sql = "SELECT rpad(left(postnr,?),'4', 'x') as postnr2 " +                  for (Adresse a : alleIkkeDaekkede) {
113                                           "FROM fulddaekning.adressetabel " +                          
114                                           "WHERE postnr BETWEEN ? AND ? " +                          List<Adresse> postListe;
115                                           "AND rute is null " + // Trae kun liste paa postnumre hvor der er ikke-daekede adresser                          
116                                           "AND (postnr NOT BETWEEN 3900 and 3999) " + //Skip alle groenlandske postnumre                          BoundingBox bbox;
117                                           "GROUP BY postnr2 " +                          
118                                           "ORDER by postnr2 ";                          if (! postnumre.contains(a.postnr )) {
119                                                    postnumre.add( a.postnr );
120                                                                            
121                                                                            bbox = new BoundingBox();
122                  PreparedStatement stmt = conn.prepareStatement(sql);                                  postListe = new ArrayList<Adresse>();
123                  //stmt.setString(1, Lookup.distributor );                                  
124                                    bbCache.put( a.postnr, bbox);                                                                                  
125                  stmt.setInt(1, consts.getPostnrGroup() );                                  ikkeDaekkedePrPost.put(a.postnr, postListe);
126                                    
127                  stmt.setInt(2, consts.getMinPostnr());                          } else {
128                  stmt.setInt(3, consts.getMaxPostnr());                                   bbox = bbCache.get( a.postnr);
129                  ResultSet res = stmt.executeQuery();                                   postListe = ikkeDaekkedePrPost.get(a.postnr);
130                            }
131                  while (res.next()) {                          
132                          String postnr = res.getString("postnr2");                          bbox.latitudeMax = Math.max(bbox.latitudeMax, a.latitude);
133                          list.add(postnr);                          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 229  public class Database { Line 172  public class Database {
172                    
173                          // 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)
174                          // 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
                         //PreparedStatement stmt = conn.prepareStatement(sql);  
175                          PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);                          PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
176                          stmt.setFetchSize(Integer.MIN_VALUE);                          stmt.setFetchSize(Integer.MIN_VALUE);
177                    
# Line 319  public class Database { Line 261  public class Database {
261                          adr.adresse = res.getString(3);                          adr.adresse = res.getString(3);
262                          adr.gadeid = res.getInt(4);                          adr.gadeid = res.getInt(4);
263                          adr.husnr = res.getInt(5);                          adr.husnr = res.getInt(5);
264                          adr.husnrbogstav = res.getString(6);                          adr.husnrbogstav = husnrbogstavCache.getInstance( res.getString(6) );
265                          adr.latitude = res.getDouble(7);                          adr.latitude = res.getDouble(7);
266                          adr.longitude = res.getDouble(8);                          adr.longitude = res.getDouble(8);
267                          adr.rute = res.getString(9);                          adr.rute =  ruteCache.getInstance( res.getString(9) );
268                          adr.ho = res.getInt(10);                          adr.ho = res.getInt(10);
269    
270                          list.add(adr);                          list.add(adr);
# Line 360  public class Database { Line 302  public class Database {
302              return conn;              return conn;
303          }          }
304    
305            
306            // //////////////////////////////////////////////////////////////////
307            /*
308            @Deprecated
309            private BoundingBox getBoundingboxFromDb_old(String postnr) throws SQLException {
310                    String minPostnr = postnr.replace('x', '0');
311                    String maxPostnr = postnr.replace('x', '9');
312    
313                    String sql =
314                                    "SELECT max(latitude) latmax, min(latitude) latmin, max(longitude) lngmax,min(longitude) lngmin  " +
315                                    "FROM fulddaekning.adressetabel WHERE postnr BETWEEN ? and ? and rute is null;";
316    
317                    PreparedStatement stmt = conn.prepareStatement(sql);
318                    stmt.setString(1, minPostnr);
319                    stmt.setString(2, maxPostnr);
320    
321                    ResultSet res = stmt.executeQuery();
322                    res.next(); //query returnerer altid 1 række
323    
324                    BoundingBox bbox = new BoundingBox();
325                    bbox.latitudeMax = res.getDouble("latmax");
326                    bbox.latitudeMin = res.getDouble("latmin");
327                    bbox.longitudeMax = res.getDouble("lngmax");
328                    bbox.longitudeMin = res.getDouble("lngmin");
329    
330                    res.close();
331                    stmt.close();
332    
333                    return bbox;
334            }
335    
336            @Deprecated
337            public Adresse[] hentDaekkedeAdresser_old( BoundingBox bbox) throws SQLException {
338                    long start = System.currentTimeMillis();
339                    String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
340                                    "FROM fulddaekning.adressetabel a " +
341                                    "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
342                                    "WHERE rute IS NOT NULL " +
343                                    "AND latitude BETWEEN ? AND ? " +
344                                    "AND longitude BETWEEN ? AND ? " +
345                                    "AND a.distributor = ? ";
346    
347                    // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)
348                    // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html
349                    //PreparedStatement stmt = conn.prepareStatement(sql);
350                    PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
351                    stmt.setFetchSize(Integer.MIN_VALUE);
352    
353                    stmt.setDouble(1, bbox.latitudeMin);
354                    stmt.setDouble(2, bbox.latitudeMax);
355                    stmt.setDouble(3, bbox.longitudeMin);
356                    stmt.setDouble(4, bbox.longitudeMax);
357                    stmt.setString(5, LookupMain.distributor);
358    
359                    List<Adresse> list = hentAdresseListe( stmt );
360                    long stop = System.currentTimeMillis();
361                    logger.info("Elapsed DB: " + (stop - start));
362                    return list.toArray( new Adresse[ list.size() ] );
363            }
364            
365            @Deprecated
366            public Queue<Adresse> hentIkkedaekkedeAdresser_old(String postnr)  throws SQLException {
367                    
368                    String minPostnr = postnr.replace('x', '0');
369                    String maxPostnr = postnr.replace('x', '9');
370                    
371                    ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>();
372    
373                    String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
374                                    "FROM fulddaekning.adressetabel a " +
375                                    "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
376                                    "WHERE rute IS NULL " +  //Ingen dækning
377                                    "AND a.postnr BETWEEN ? AND ? " +
378                                    "AND latitude IS NOT NULL " +
379                                    "AND longitude IS NOT NULL " +
380                                    "AND gadeid IS NOT NULL " +
381                                    "AND (a.distributor IS NULL OR a.distributor<>'LUKKET') ";              
382                    PreparedStatement stmt = conn.prepareStatement(sql);
383                    stmt.setString(1, minPostnr);
384                    stmt.setString(2, maxPostnr);
385    
386                    queue.addAll( hentAdresseListe( stmt ) );
387                    return queue;
388            }
389            
390            @Deprecated
391            public List<String> hentPostnumre_old() throws SQLException {
392                    ArrayList<String> list = new ArrayList<String>();
393                    
394                    Constants consts = Constants.getInstance();
395    
396                    /*
397                    String sql = "SELECT postnr " +
398                                             "FROM fulddaekning.adressetabel " +
399                                             "WHERE postnr BETWEEN ? AND ? " +
400                                             "AND rute is null " + // Træk kun liste på postnumre hvor der er ikke-dækkede adresser
401                                             "GROUP BY postnr " +
402                                             "ORDER by postnr";
403                    * /
404                    
405                    
406                    String sql = "SELECT rpad(left(postnr,?),'4', 'x') as postnr2 " +
407                                             "FROM fulddaekning.adressetabel " +
408                                             "WHERE postnr BETWEEN ? AND ? " +
409                                             "AND rute is null " + // Trae kun liste paa postnumre hvor der er ikke-daekede adresser
410                                             "AND (postnr NOT BETWEEN 3900 and 3999) " + //Skip alle groenlandske postnumre
411                                             "GROUP BY postnr2 " +
412                                             "ORDER by postnr2 ";
413                    
414                                            
415                                            
416                    PreparedStatement stmt = conn.prepareStatement(sql);
417                    //stmt.setString(1, Lookup.distributor );
418    
419                    stmt.setInt(1, consts.getPostnrGroup() );
420    
421                    stmt.setInt(2, consts.getMinPostnr());
422                    stmt.setInt(3, consts.getMaxPostnr());
423                    ResultSet res = stmt.executeQuery();
424    
425                    while (res.next()) {
426                            String postnr = res.getString("postnr2");
427                            list.add(postnr);
428                    }
429                    res.close();
430                    stmt.close();
431    
432                    //list.add(8700);
433    
434                    return list;
435            }*/
436    
437            
438  }  }

Legend:
Removed from v.2263  
changed lines
  Added in v.2331

  ViewVC Help
Powered by ViewVC 1.1.20