/[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 2240 by torben, Wed Dec 10 08:26:12 2014 UTC revision 2327 by torben, Thu Feb 19 10:36:40 2015 UTC
# Line 7  import java.sql.PreparedStatement; Line 7  import java.sql.PreparedStatement;
7  import java.sql.ResultSet;  import java.sql.ResultSet;
8  import java.sql.SQLException;  import java.sql.SQLException;
9  import java.util.ArrayList;  import java.util.ArrayList;
10    import java.util.HashMap;
11  import java.util.List;  import java.util.List;
12  import java.util.Properties;  import java.util.Properties;
13  import java.util.Queue;  import java.util.Queue;
# Line 22  public class Database { Line 23  public class Database {
23    
24          Connection conn;          Connection conn;
25          PreparedStatement saveStmt;          PreparedStatement saveStmt;
26            
27            Adresse alleAdresser[];
28            
29            DeduplicateHelper<String> husnrbogstavCache = new DeduplicateHelper<String>();
30            DeduplicateHelper<String> ruteCache = new DeduplicateHelper<String>();
31            
32            private HashMap<String,BoundingBox> bbCache = new HashMap<String,BoundingBox>();
33    
34          public Database(SafeProperties conf)  throws SQLException,IOException {          public Database(SafeProperties conf)  throws SQLException,IOException {
35                  this.conn = getConnection( conf );                        this.conn = getConnection( conf );      
# Line 56  public class Database { Line 64  public class Database {
64    
65                  logger.info("Executing: " + sql);                  logger.info("Executing: " + sql);
66                  conn.createStatement().executeUpdate(sql);                                conn.createStatement().executeUpdate(sql);              
67          }                }
68            
69          public BoundingBox getBoundingbox(String postnr) throws SQLException {          public BoundingBox getBoundingbox(String postnr) throws SQLException {
70                    BoundingBox bb = bbCache.get(postnr);
71                    if ( bb == null ) {
72                            bb = getBoundingboxFromDb(postnr);
73                            bbCache.put(postnr, bb);
74                    } else {
75                            logger.info("Serving BB from cache");
76                    }
77                    
78                    return bb.clone();//never return the original / cached object
79            }
80    
81            private BoundingBox getBoundingboxFromDb(String postnr) throws SQLException {
82                  String minPostnr = postnr.replace('x', '0');                  String minPostnr = postnr.replace('x', '0');
83                  String maxPostnr = postnr.replace('x', '9');                  String maxPostnr = postnr.replace('x', '9');
84    
# Line 84  public class Database { Line 104  public class Database {
104    
105                  return bbox;                  return bbox;
106          }          }
107            
108            
109    
110          public Queue<Adresse> hentIkkedaekkedeAdresser(String postnr)  throws SQLException {          public Queue<Adresse> hentIkkedaekkedeAdresser(String postnr)  throws SQLException {
111                                    
# Line 155  public class Database { Line 177  public class Database {
177                  return list;                  return list;
178          }          }
179    
180            @Deprecated
181          public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {          public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {
182                    long start = System.currentTimeMillis();
183                  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 " +
184                                  "FROM fulddaekning.adressetabel a " +                                  "FROM fulddaekning.adressetabel a " +
185                                  "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +                                  "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
# Line 174  public class Database { Line 198  public class Database {
198                  stmt.setDouble(2, bbox.latitudeMax);                  stmt.setDouble(2, bbox.latitudeMax);
199                  stmt.setDouble(3, bbox.longitudeMin);                  stmt.setDouble(3, bbox.longitudeMin);
200                  stmt.setDouble(4, bbox.longitudeMax);                  stmt.setDouble(4, bbox.longitudeMax);
201                  stmt.setString(5, Lookup.distributor);                  stmt.setString(5, LookupMain.distributor);
202    
203                  List<Adresse> list = hentAdresseListe( stmt );                  List<Adresse> list = hentAdresseListe( stmt );
204                    long stop = System.currentTimeMillis();
205                    logger.info("Elapsed DB: " + (stop - start));
206                  return list.toArray( new Adresse[ list.size() ] );                  return list.toArray( new Adresse[ list.size() ] );
207          }          }
208                    
209            public Adresse[] hentDaekkedeAdresserCache( BoundingBox bbox) {
210                    long start = System.currentTimeMillis();
211                    ArrayList<Adresse> list = new ArrayList<Adresse>();
212                    for (Adresse a : alleAdresser) {
213                            if ( a.latitude > bbox.latitudeMin && a.latitude< bbox.latitudeMax && a.longitude> bbox.longitudeMin && a.longitude < bbox.longitudeMax) {
214                                    list.add(a);
215                            }
216                    }
217                    long stop = System.currentTimeMillis();
218                    logger.info("Elapsed cache: " + (stop - start));
219                    return list.toArray( new Adresse[ list.size() ] );              
220            }
221            
222                    
223          public Adresse[] hentAlleDaekkedeAdresser() throws SQLException {          public Adresse[] hentAlleDaekkedeAdresser() throws SQLException {
224                  String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +                  if ( alleAdresser == null ) {
225                                  "FROM fulddaekning.adressetabel a " +                          String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
226                                  "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +                                          "FROM fulddaekning.adressetabel a " +
227                                  "WHERE rute IS NOT NULL " +                                          "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
228                                  "AND latitude IS NOT NULL " +                                          "WHERE rute IS NOT NULL " +
229                                  "AND longitude IS NOT NULL " +                                          "AND latitude IS NOT NULL " +
230                                  "AND a.distributor = ? ";                                          "AND longitude IS NOT NULL " +
231                                            "AND a.distributor = ? ";
232                  // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)          
233                  // 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)
234                  //PreparedStatement stmt = conn.prepareStatement(sql);                          // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html
235                  PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);                          //PreparedStatement stmt = conn.prepareStatement(sql);
236                  stmt.setFetchSize(Integer.MIN_VALUE);                          PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
237                            stmt.setFetchSize(Integer.MIN_VALUE);
238                  stmt.setString(1, Lookup.distributor);          
239                            stmt.setString(1, LookupMain.distributor);
240                  List<Adresse> list = hentAdresseListe( stmt );          
241                  return list.toArray( new Adresse[ list.size() ] );                          List<Adresse> list = hentAdresseListe( stmt );
242                            alleAdresser = list.toArray( new Adresse[ list.size() ] );
243                    }
244                    return alleAdresser;
245          }          }
246                    
247                    
# Line 261  public class Database { Line 303  public class Database {
303    
304                  while (res.next()) {                  while (res.next()) {
305                          Adresse adr = new Adresse();                          Adresse adr = new Adresse();
306    
307                            /*
308                          adr.id = res.getInt("id");                          adr.id = res.getInt("id");
309                          adr.postnr = res.getInt("postnr");                          adr.postnr = res.getInt("postnr");
310                          adr.adresse = res.getString("adresse");                          adr.adresse = res.getString("adresse");
# Line 271  public class Database { Line 315  public class Database {
315                          adr.longitude = res.getDouble("longitude");                          adr.longitude = res.getDouble("longitude");
316                          adr.rute = res.getString("rute");                          adr.rute = res.getString("rute");
317                          adr.ho = res.getInt("ho");                          adr.ho = res.getInt("ho");
318                            */
319            
320                            adr.id = res.getInt(1);
321                            adr.postnr = res.getInt(2);
322                            adr.adresse = res.getString(3);
323                            adr.gadeid = res.getInt(4);
324                            adr.husnr = res.getInt(5);
325                            adr.husnrbogstav = husnrbogstavCache.getInstance( res.getString(6) );
326                            adr.latitude = res.getDouble(7);
327                            adr.longitude = res.getDouble(8);
328                            adr.rute =  ruteCache.getInstance( res.getString(9) );
329                            adr.ho = res.getInt(10);
330    
331                          list.add(adr);                          list.add(adr);
332    

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

  ViewVC Help
Powered by ViewVC 1.1.20