/[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 2237 by torben, Tue Dec 9 15:49:43 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 {
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          public BoundingBox getBoundingbox(int postnr) throws SQLException {          private BoundingBox getBoundingboxFromDb(String postnr) throws SQLException {
79                    String minPostnr = postnr.replace('x', '0');
80                    String maxPostnr = postnr.replace('x', '9');
81    
82                  String sql =                  String sql =
83                                  "SELECT max(latitude) latmax, min(latitude) latmin, max(longitude) lngmax,min(longitude) lngmin  " +                                  "SELECT max(latitude) latmax, min(latitude) latmin, max(longitude) lngmax,min(longitude) lngmin  " +
84                                  "FROM fulddaekning.adressetabel WHERE postnr=? and rute is null;";                                  "FROM fulddaekning.adressetabel WHERE postnr BETWEEN ? and ? and rute is null;";
85    
86                  PreparedStatement stmt = conn.prepareStatement(sql);                  PreparedStatement stmt = conn.prepareStatement(sql);
87                  stmt.setInt(1, postnr);                  stmt.setString(1, minPostnr);
88                    stmt.setString(2, maxPostnr);
89    
90                  ResultSet res = stmt.executeQuery();                  ResultSet res = stmt.executeQuery();
91                  res.next(); //query returnerer altid 1 række                  res.next(); //query returnerer altid 1 række
# Line 81  public class Database { Line 101  public class Database {
101    
102                  return bbox;                  return bbox;
103          }          }
104            
105            
106    
107          public Queue<Adresse> hentIkkedaekkedeAdresser(int postnr)  throws SQLException {          public Queue<Adresse> hentIkkedaekkedeAdresser(String postnr)  throws SQLException {
108                    
109                    String minPostnr = postnr.replace('x', '0');
110                    String maxPostnr = postnr.replace('x', '9');
111                    
112                  ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>();                  ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>();
113    
114                  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 " +
115                                  "FROM fulddaekning.adressetabel a " +                                  "FROM fulddaekning.adressetabel a " +
116                                  "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +                                  "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
117                                  "WHERE rute IS NULL " +  //Ingen dækning                                  "WHERE rute IS NULL " +  //Ingen dækning
118                                  "AND a.postnr=?  " +                                  "AND a.postnr BETWEEN ? AND ? " +
119                                  "AND latitude IS NOT NULL " +                                  "AND latitude IS NOT NULL " +
120                                  "AND longitude IS NOT NULL " +                                  "AND longitude IS NOT NULL " +
121                                  "AND gadeid IS NOT NULL " +                                  "AND gadeid IS NOT NULL " +
122                                  "AND (a.distributor IS NULL OR a.distributor<>'LUKKET') ";                                                "AND (a.distributor IS NULL OR a.distributor<>'LUKKET') ";              
123                  PreparedStatement stmt = conn.prepareStatement(sql);                  PreparedStatement stmt = conn.prepareStatement(sql);
124                  stmt.setInt(1, postnr);                  stmt.setString(1, minPostnr);
125                    stmt.setString(2, maxPostnr);
126    
127                  queue.addAll( hentAdresseListe( stmt ) );                  queue.addAll( hentAdresseListe( stmt ) );
128                  return queue;                  return queue;
129          }          }
130    
131          public List<Integer> hentPostnumre() throws SQLException {          public List<String> hentPostnumre() throws SQLException {
132                  ArrayList<Integer> list = new ArrayList<Integer>();                  ArrayList<String> list = new ArrayList<String>();
133                                    
134                  Constants consts = Constants.getInstance();                  Constants consts = Constants.getInstance();
135    
136                    /*
137                  String sql = "SELECT postnr " +                  String sql = "SELECT postnr " +
138                                           "FROM fulddaekning.adressetabel " +                                           "FROM fulddaekning.adressetabel " +
139                                           "WHERE postnr BETWEEN ? AND ? " +                                           "WHERE postnr BETWEEN ? AND ? " +
140                                           "AND rute is null " + // Træk kun liste på postnumre hvor der er ikke-dækkede adresser                                           "AND rute is null " + // Træk kun liste på postnumre hvor der er ikke-dækkede adresser
141                                           "GROUP BY postnr " +                                           "GROUP BY postnr " +
142                                           "ORDER by postnr";                                           "ORDER by postnr";
143                    */
144                    
145                    
146                    String sql = "SELECT rpad(left(postnr,?),'4', 'x') as postnr2 " +
147                                             "FROM fulddaekning.adressetabel " +
148                                             "WHERE postnr BETWEEN ? AND ? " +
149                                             "AND rute is null " + // Trae kun liste paa postnumre hvor der er ikke-daekede adresser
150                                             "AND (postnr NOT BETWEEN 3900 and 3999) " + //Skip alle groenlandske postnumre
151                                             "GROUP BY postnr2 " +
152                                             "ORDER by postnr2 ";
153                    
154                                            
155                                            
156                  PreparedStatement stmt = conn.prepareStatement(sql);                  PreparedStatement stmt = conn.prepareStatement(sql);
157                  //stmt.setString(1, Lookup.distributor );                  //stmt.setString(1, Lookup.distributor );
158                  stmt.setInt(1, consts.getMinPostnr());  
159                  stmt.setInt(2, consts.getMaxPostnr());                  stmt.setInt(1, consts.getPostnrGroup() );
160    
161                    stmt.setInt(2, consts.getMinPostnr());
162                    stmt.setInt(3, consts.getMaxPostnr());
163                  ResultSet res = stmt.executeQuery();                  ResultSet res = stmt.executeQuery();
164    
165                  while (res.next()) {                  while (res.next()) {
166                          int postnr = res.getInt("postnr");                          String postnr = res.getString("postnr2");
167                          list.add(postnr);                          list.add(postnr);
168                  }                  }
169                  res.close();                  res.close();
# Line 131  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 150  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 237  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 247  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.2237  
changed lines
  Added in v.2263

  ViewVC Help
Powered by ViewVC 1.1.20