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

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

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

revision 2261 by torben, Mon Feb 9 14:39:37 2015 UTC revision 2423 by torben, Tue Mar 3 08:30:02 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 24  public class Database { Line 27  public class Database {
27          Connection conn;          Connection conn;
28          PreparedStatement saveStmt;          PreparedStatement saveStmt;
29                    
30          private HashMap<String,BoundingBox> bbCache = new HashMap<String,BoundingBox>();          Adresse alleAdresser[];
31            Adresse alleIkkeDaekkede[];
32            
33            
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 61  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 119  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;  
         }  
106    
107          public List<String> hentPostnumre() throws SQLException {                  List<Adresse> list = hentAdresseListe( stmt );
108                  ArrayList<String> list = new ArrayList<String>();                  alleIkkeDaekkede = list.toArray( new Adresse[ list.size() ] );
                   
                 Constants consts = Constants.getInstance();  
   
                 /*  
                 String sql = "SELECT postnr " +  
                                          "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                  }                  }
140                  res.close();          }
141                  stmt.close();          
142            public Queue<Adresse> hentIkkedaekkedeAdresserCache(int postnr)  {
143                  //list.add(8700);                  List<Adresse> postListe = ikkeDaekkedePrPost.get(postnr);
144                    
145                  return list;                  return new ConcurrentLinkedQueue<Adresse>(postListe);          
146          }          }
147    
148          public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {          
149                  String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +          public Adresse[] hentDaekkedeAdresserCache( BoundingBox bbox) {
150                                  "FROM fulddaekning.adressetabel a " +                  long start = System.currentTimeMillis();
151                                  "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +                  ArrayList<Adresse> list = new ArrayList<Adresse>();
152                                  "WHERE rute IS NOT NULL " +                  for (Adresse a : alleAdresser) {
153                                  "AND latitude BETWEEN ? AND ? " +                          if ( a.latitude > bbox.latitudeMin && a.latitude< bbox.latitudeMax && a.longitude> bbox.longitudeMin && a.longitude < bbox.longitudeMax) {
154                                  "AND longitude BETWEEN ? AND ? " +                                  list.add(a);
155                                  "AND a.distributor = ? ";                          }
156                    }
157                  // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)                  long stop = System.currentTimeMillis();
158                  // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html                  logger.info("Elapsed cache: " + (stop - start));
159                  //PreparedStatement stmt = conn.prepareStatement(sql);                  return list.toArray( new Adresse[ list.size() ] );              
                 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 );  
                 return list.toArray( new Adresse[ list.size() ] );  
160          }          }
161                    
162                    
163          public Adresse[] hentAlleDaekkedeAdresser() throws SQLException {          public Adresse[] hentAlleDaekkedeAdresser() throws SQLException {
164                  String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +                  if ( alleAdresser == null ) {
165                                  "FROM fulddaekning.adressetabel a " +                          String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
166                                  "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +                                          "FROM fulddaekning.adressetabel a " +
167                                  "WHERE rute IS NOT NULL " +                                          "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
168                                  "AND latitude IS NOT NULL " +                                          "WHERE rute IS NOT NULL " +
169                                  "AND longitude IS NOT NULL " +                                          "AND latitude IS NOT NULL " +
170                                  "AND a.distributor = ? ";                                          "AND longitude IS NOT NULL " +
171                                            "AND a.distributor = ? ";
172                  // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)          
173                  // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html                          // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)
174                  //PreparedStatement stmt = conn.prepareStatement(sql);                          // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html
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            
178                  stmt.setString(1, LookupMain.distributor);                          stmt.setString(1, LookupMain.distributor);
179            
180                  List<Adresse> list = hentAdresseListe( stmt );                          List<Adresse> list = hentAdresseListe( stmt );
181                  return list.toArray( new Adresse[ list.size() ] );                          alleAdresser = list.toArray( new Adresse[ list.size() ] );
182                    }
183                    return alleAdresser;
184          }          }
185                    
186                    
# Line 252  public class Database { Line 216  public class Database {
216    
217                  saveStmt.addBatch();                  saveStmt.addBatch();
218                  batchCount++;                  batchCount++;
219                  if (batchCount >= 100) {                  if (batchCount >= 1000) {
220                          saveStmt.executeBatch();                          saveStmt.executeBatch();
221                          batchCount = 0;                          batchCount = 0;
222                  }                  }
# Line 297  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 338  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.2261  
changed lines
  Added in v.2423

  ViewVC Help
Powered by ViewVC 1.1.20