/[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 2203 by torben, Thu Sep 11 14:46:59 2014 UTC revision 2241 by torben, Wed Dec 10 09:50:33 2014 UTC
# Line 14  import java.util.concurrent.ConcurrentLi Line 14  import java.util.concurrent.ConcurrentLi
14  import java.util.logging.Logger;  import java.util.logging.Logger;
15    
16    
17    
18  public class Database {  public class Database {
19          Logger logger = Logger.getLogger(Database.class.getName());          Logger logger = Logger.getLogger(Database.class.getName());
20    
21            int batchCount = 0;
22    
23          Connection conn;          Connection conn;
24          PreparedStatement saveStmt;          PreparedStatement saveStmt;
# Line 42  public class Database { Line 44  public class Database {
44          }                }      
45                    
46          public void renameResultTables() throws SQLException {          public void renameResultTables() throws SQLException {
47                    Constants consts = Constants.getInstance();
48                    String ext = consts.getTableExtension();
49                    
50                  logger.info("Dropping old backup table (if exists)");                  logger.info("Dropping old backup table (if exists)");
51                  String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old";                  String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_old" + ext;
52                  conn.createStatement().executeUpdate(sql);                  conn.createStatement().executeUpdate(sql);
53                                    
54                  logger.info("Rename tables");                  logger.info("Rename tables");
55                  sql = "RENAME TABLE fulddaekning.afstand_anden_rute TO fulddaekning.afstand_anden_rute_old, fulddaekning.afstand_anden_rute_ny TO fulddaekning.afstand_anden_rute";                  sql = "RENAME TABLE fulddaekning.afstand_anden_rute" + ext + " TO fulddaekning.afstand_anden_rute_old" + ext + ", fulddaekning.afstand_anden_rute_ny TO fulddaekning.afstand_anden_rute" + ext;
56    
57                    logger.info("Executing: " + sql);
58                  conn.createStatement().executeUpdate(sql);                                conn.createStatement().executeUpdate(sql);              
59          }                }      
60    
61          public BoundingBox getBoundingbox(int postnr) throws SQLException {          public BoundingBox getBoundingbox(String postnr) throws SQLException {
62                    String minPostnr = postnr.replace('x', '0');
63                    String maxPostnr = postnr.replace('x', '9');
64    
65                  String sql =                  String sql =
66                                  "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  " +
67                                  "FROM fulddaekning.adressetabel WHERE postnr=? and rute is null;";                                  "FROM fulddaekning.adressetabel WHERE postnr BETWEEN ? and ? and rute is null;";
68    
69                  PreparedStatement stmt = conn.prepareStatement(sql);                  PreparedStatement stmt = conn.prepareStatement(sql);
70                  stmt.setInt(1, postnr);                  stmt.setString(1, minPostnr);
71                    stmt.setString(2, maxPostnr);
72    
73                  ResultSet res = stmt.executeQuery();                  ResultSet res = stmt.executeQuery();
74                  res.next(); //query returnerer altid 1 række                  res.next(); //query returnerer altid 1 række
# Line 75  public class Database { Line 85  public class Database {
85                  return bbox;                  return bbox;
86          }          }
87    
88          public Queue<Adresse> hentIkkedaekkedeAdresser(int postnr)  throws SQLException {          public Queue<Adresse> hentIkkedaekkedeAdresser(String postnr)  throws SQLException {
89                    
90                    String minPostnr = postnr.replace('x', '0');
91                    String maxPostnr = postnr.replace('x', '9');
92                    
93                  ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>();                  ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>();
94    
95                  String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " +                  String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
96                                  "FROM fulddaekning.adressetabel " +                                  "FROM fulddaekning.adressetabel a " +
97                                    "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
98                                  "WHERE rute IS NULL " +  //Ingen dækning                                  "WHERE rute IS NULL " +  //Ingen dækning
99                                  "AND postnr=?  " +                                  "AND a.postnr BETWEEN ? AND ? " +
100                                  "AND latitude IS NOT NULL " +                                  "AND latitude IS NOT NULL " +
101                                  "AND longitude IS NOT NULL " +                                  "AND longitude IS NOT NULL " +
102                                  "AND gadeid IS NOT NULL ";                                  "AND gadeid IS NOT NULL " +
103                                    "AND (a.distributor IS NULL OR a.distributor<>'LUKKET') ";              
104                  PreparedStatement stmt = conn.prepareStatement(sql);                  PreparedStatement stmt = conn.prepareStatement(sql);
105                  stmt.setInt(1, postnr);                  stmt.setString(1, minPostnr);
106                    stmt.setString(2, maxPostnr);
107    
108                  queue.addAll( hentAdresseListe( stmt ) );                  queue.addAll( hentAdresseListe( stmt ) );
109                  return queue;                  return queue;
110          }          }
111    
112          public List<Integer> hentPostnumre() throws SQLException {          public List<String> hentPostnumre() throws SQLException {
113                  ArrayList<Integer> list = new ArrayList<Integer>();                  ArrayList<String> list = new ArrayList<String>();
114                                    
115                  Constants consts = Constants.getInstance();                  Constants consts = Constants.getInstance();
116    
117                    /*
118                  String sql = "SELECT postnr " +                  String sql = "SELECT postnr " +
119                                           "FROM fulddaekning.adressetabel " +                                           "FROM fulddaekning.adressetabel " +
                                          //"WHERE distributor = ? and rute is not null " +  
120                                           "WHERE postnr BETWEEN ? AND ? " +                                           "WHERE postnr BETWEEN ? AND ? " +
121                                             "AND rute is null " + // Træk kun liste på postnumre hvor der er ikke-dækkede adresser
122                                           "GROUP BY postnr " +                                           "GROUP BY postnr " +
123                                           "ORDER by postnr";                                           "ORDER by postnr";
124                    */
125                    
126                    
127                    String sql = "SELECT rpad(left(postnr,?),'4', 'x') as postnr2 " +
128                                             "FROM fulddaekning.adressetabel " +
129                                             "WHERE postnr BETWEEN ? AND ? " +
130                                             "AND rute is null " + // Trae kun liste paa postnumre hvor der er ikke-daekede adresser
131                                             "AND (postnr NOT BETWEEN 3900 and 3999) " + //Skip alle groenlandske postnumre
132                                             "GROUP BY postnr2 " +
133                                             "ORDER by postnr2 ";
134                    
135                                            
136                                            
137                  PreparedStatement stmt = conn.prepareStatement(sql);                  PreparedStatement stmt = conn.prepareStatement(sql);
138                  //stmt.setString(1, Lookup.distributor );                  //stmt.setString(1, Lookup.distributor );
139                  stmt.setInt(1, consts.getMinPostnr());  
140                  stmt.setInt(2, consts.getMaxPostnr());                  stmt.setInt(1, consts.getPostnrGroup() );
141    
142                    stmt.setInt(2, consts.getMinPostnr());
143                    stmt.setInt(3, consts.getMaxPostnr());
144                  ResultSet res = stmt.executeQuery();                  ResultSet res = stmt.executeQuery();
145    
146                  while (res.next()) {                  while (res.next()) {
147                          int postnr = res.getInt("postnr");                          String postnr = res.getString("postnr2");
148                          list.add(postnr);                          list.add(postnr);
149                  }                  }
150                  res.close();                  res.close();
# Line 122  public class Database { Line 155  public class Database {
155                  return list;                  return list;
156          }          }
157    
158          public ArrayList<Adresse> hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {          public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {
159                  String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " +                  String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
160                                  "FROM fulddaekning.adressetabel " +                                  "FROM fulddaekning.adressetabel a " +
161                                    "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
162                                  "WHERE rute IS NOT NULL " +                                  "WHERE rute IS NOT NULL " +
163                                  "AND latitude BETWEEN ? AND ? " +                                  "AND latitude BETWEEN ? AND ? " +
164                                  "AND longitude BETWEEN ? AND ? " +                                  "AND longitude BETWEEN ? AND ? " +
165                                  "AND distributor = ? ";                                  "AND a.distributor = ? ";
166    
167                  // 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)
168                  // 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
# Line 140  public class Database { Line 174  public class Database {
174                  stmt.setDouble(2, bbox.latitudeMax);                  stmt.setDouble(2, bbox.latitudeMax);
175                  stmt.setDouble(3, bbox.longitudeMin);                  stmt.setDouble(3, bbox.longitudeMin);
176                  stmt.setDouble(4, bbox.longitudeMax);                  stmt.setDouble(4, bbox.longitudeMax);
177                  stmt.setString(5, Lookup.distributor);                  stmt.setString(5, LookupMain.distributor);
   
                 return hentAdresseListe( stmt );  
178    
179                    List<Adresse> list = hentAdresseListe( stmt );
180                    return list.toArray( new Adresse[ list.size() ] );
181          }          }
182            
183            
184            public Adresse[] hentAlleDaekkedeAdresser() throws SQLException {
185                    String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
186                                    "FROM fulddaekning.adressetabel a " +
187                                    "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
188                                    "WHERE rute IS NOT NULL " +
189                                    "AND latitude IS NOT NULL " +
190                                    "AND longitude IS NOT NULL " +
191                                    "AND a.distributor = ? ";
192    
193                    // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)
194                    // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html
195                    //PreparedStatement stmt = conn.prepareStatement(sql);
196                    PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
197                    stmt.setFetchSize(Integer.MIN_VALUE);
198    
199                    stmt.setString(1, LookupMain.distributor);
200    
201                    List<Adresse> list = hentAdresseListe( stmt );
202                    return list.toArray( new Adresse[ list.size() ] );
203            }
204            
205            
206    
207          public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException {          public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException {
208                  /*String sql = "INSERT INTO fulddaekning.afstand_anden_rute_thn (orgId,orgPostnr, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgLatitude,orgLongitude,orgRute,id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,afstand,`timestamp`) "+                  /*String sql = "INSERT INTO fulddaekning.afstand_anden_rute_thn (orgId,orgPostnr, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgLatitude,orgLongitude,orgRute,id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,afstand,`timestamp`) "+
# Line 176  public class Database { Line 233  public class Database {
233    
234                  saveStmt.setDouble(19, bedsteAfstand);                  saveStmt.setDouble(19, bedsteAfstand);
235    
236                  saveStmt.executeUpdate();                        saveStmt.addBatch();
237                  saveStmt.clearParameters();                  batchCount++;
238                    if (batchCount >= 100) {
239                            saveStmt.executeBatch();
240                            batchCount = 0;
241                    }
242                    //saveStmt.executeUpdate();    
243                    //saveStmt.clearParameters();
244    
245                  //saveStmt.close();                      //saveStmt.close();    
246            }
247            
248            public synchronized void saveBatch() throws SQLException{
249                    saveStmt.executeBatch();
250                    batchCount = 0;
251          }          }
252    
253    
254    
255          protected ArrayList<Adresse> hentAdresseListe(PreparedStatement stmt) throws SQLException{          protected ArrayList<Adresse> hentAdresseListe(PreparedStatement stmt) throws SQLException{
256                  ArrayList<Adresse> list = new ArrayList<Adresse>( 30000 );                  ArrayList<Adresse> list = new ArrayList<Adresse>( 1000000 );
257    
258                  //logger.info("Starting query");                  //logger.info("Starting query");
259                  ResultSet res = stmt.executeQuery();                  ResultSet res = stmt.executeQuery();
# Line 204  public class Database { Line 270  public class Database {
270                          adr.latitude = res.getDouble("latitude");                          adr.latitude = res.getDouble("latitude");
271                          adr.longitude = res.getDouble("longitude");                          adr.longitude = res.getDouble("longitude");
272                          adr.rute = res.getString("rute");                          adr.rute = res.getString("rute");
273                            adr.ho = res.getInt("ho");
274    
275                          list.add(adr);                          list.add(adr);
276    
# Line 230  public class Database { Line 297  public class Database {
297              connectionProps.put("user", db_user);              connectionProps.put("user", db_user);
298              connectionProps.put("password", db_pass);              connectionProps.put("password", db_pass);
299    
300                //For debug output, tilføj denne til JDBC url'en: &profileSQL=true    
301              conn = DriverManager.getConnection(              conn = DriverManager.getConnection(
302                             "jdbc:mysql://" +                             "jdbc:mysql://" +
303                             db_host +                             db_host +
304                             ":3306/",                             ":3306/?rewriteBatchedStatements=true",
305                             connectionProps);                             connectionProps);
306              logger.info("Connected to database");              logger.info("Connected to database");
307              return conn;              return conn;

Legend:
Removed from v.2203  
changed lines
  Added in v.2241

  ViewVC Help
Powered by ViewVC 1.1.20