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

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

  ViewVC Help
Powered by ViewVC 1.1.20