/[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 2151 by torben, Wed May 7 07:59:03 2014 UTC revision 2231 by torben, Wed Oct 8 08:44:56 2014 UTC
# Line 1  Line 1 
1  package dk.daoas.fulddaekning;  package dk.daoas.fulddaekning;
2    
3    import java.io.IOException;
4  import java.sql.Connection;  import java.sql.Connection;
5    import java.sql.DriverManager;
6  import java.sql.PreparedStatement;  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.List;  import java.util.List;
11    import java.util.Properties;
12  import java.util.Queue;  import java.util.Queue;
13  import java.util.concurrent.ConcurrentLinkedQueue;  import java.util.concurrent.ConcurrentLinkedQueue;
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;
25    
26          public Database(Connection conn)  throws SQLException {          public Database(SafeProperties conf)  throws SQLException,IOException {
27                  this.conn = conn;                                                this.conn = getConnection( conf );      
28    
29                  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_ny (orgId,orgPostnr, orgAdresse,orgGadeid,orgHusnr,orgHusnrBogstav,orgLatitude,orgLongitude,orgRute,id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,afstand,`timestamp`) "+
30                                  "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, now() )";                                  "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, now() )";
31    
32                  saveStmt = conn.prepareStatement(sql);                            saveStmt = conn.prepareStatement(sql);          
# Line 29  public class Database { Line 34  public class Database {
34          }          }
35    
36          public void resetResultTable() throws SQLException {          public void resetResultTable() throws SQLException {
37                  logger.info("Truncating result table");                  logger.info("Dropping old result table (if exists)");
38                  String sql = "TRUNCATE TABLE fulddaekning.afstand_anden_rute_thn";                  String sql = "DROP TABLE IF EXISTS fulddaekning.afstand_anden_rute_ny";
39                    conn.createStatement().executeUpdate(sql);
40                    
41                    logger.info("Create new result table");
42                    sql = "CREATE TABLE fulddaekning.afstand_anden_rute_ny LIKE fulddaekning.afstand_anden_rute";
43                    conn.createStatement().executeUpdate(sql);              
44            }      
45            
46            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)");
51                    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");
55                    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);              
59          }                }      
60    
61          public BoundingBox getBoundingbox(int postnr) throws SQLException {          public BoundingBox getBoundingbox(int postnr) throws SQLException {
62    
63                  String sql =                  String sql =
64                                  "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  " +
65                                                  "from fulddaekning.adressetabel WHERE postnr=? and rute is null;";                                  "FROM fulddaekning.adressetabel WHERE postnr=? and rute is null;";
66    
67                  PreparedStatement stmt = conn.prepareStatement(sql);                  PreparedStatement stmt = conn.prepareStatement(sql);
68                  stmt.setInt(1, postnr);                  stmt.setInt(1, postnr);
# Line 62  public class Database { Line 85  public class Database {
85          public Queue<Adresse> hentIkkedaekkedeAdresser(int postnr)  throws SQLException {          public Queue<Adresse> hentIkkedaekkedeAdresser(int postnr)  throws SQLException {
86                  ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>();                  ConcurrentLinkedQueue<Adresse> queue = new ConcurrentLinkedQueue<Adresse>();
87    
88                  String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute FROM fulddaekning.adressetabel " +                  String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
89                                    "FROM fulddaekning.adressetabel a " +
90                                    "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
91                                  "WHERE rute IS NULL " +  //Ingen dækning                                  "WHERE rute IS NULL " +  //Ingen dækning
92                                  "AND postnr=?  " +                                  "AND a.postnr=?  " +
93                                  "AND latitude IS NOT NULL " +                                  "AND latitude IS NOT NULL " +
94                                  "AND longitude IS NOT NULL " +                                  "AND longitude IS NOT NULL " +
95                                  "AND gadeid IS NOT NULL ";                                  "AND gadeid IS NOT NULL ";
# Line 77  public class Database { Line 102  public class Database {
102    
103          public List<Integer> hentPostnumre() throws SQLException {          public List<Integer> hentPostnumre() throws SQLException {
104                  ArrayList<Integer> list = new ArrayList<Integer>();                  ArrayList<Integer> list = new ArrayList<Integer>();
105                    
106                    Constants consts = Constants.getInstance();
107    
108    
109                  String sql = "SELECT postnr FROM fulddaekning.adressetabel WHERE distributor = 'DAO' and rute is not null GROUP BY postnr ORDER by postnr";                  String sql = "SELECT postnr " +
110                                             "FROM fulddaekning.adressetabel " +
111                                             //"WHERE distributor = ? and rute is not null " +
112                                             "WHERE postnr BETWEEN ? AND ? " +
113                                             "GROUP BY postnr " +
114                                             "ORDER by postnr";
115                  PreparedStatement stmt = conn.prepareStatement(sql);                  PreparedStatement stmt = conn.prepareStatement(sql);
116                    //stmt.setString(1, Lookup.distributor );
117                    stmt.setInt(1, consts.getMinPostnr());
118                    stmt.setInt(2, consts.getMaxPostnr());
119                  ResultSet res = stmt.executeQuery();                  ResultSet res = stmt.executeQuery();
120    
121                  while (res.next()) {                  while (res.next()) {
# Line 95  public class Database { Line 130  public class Database {
130                  return list;                  return list;
131          }          }
132    
133          public ArrayList<Adresse> hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {          public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {
134                  String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute FROM fulddaekning.adressetabel " +                  String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
135                                    "FROM fulddaekning.adressetabel a " +
136                                    "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
137                                  "WHERE rute IS NOT NULL " +                                  "WHERE rute IS NOT NULL " +
138                                  "AND latitude BETWEEN ? AND ? " +                                  "AND latitude BETWEEN ? AND ? " +
139                                  "AND longitude BETWEEN ? AND ? " +                                  "AND longitude BETWEEN ? AND ? " +
140                                  "AND distributor = 'DAO' ";                                  "AND a.distributor = ? ";
141    
142                  // 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)
143                  // 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 112  public class Database { Line 149  public class Database {
149                  stmt.setDouble(2, bbox.latitudeMax);                  stmt.setDouble(2, bbox.latitudeMax);
150                  stmt.setDouble(3, bbox.longitudeMin);                  stmt.setDouble(3, bbox.longitudeMin);
151                  stmt.setDouble(4, bbox.longitudeMax);                  stmt.setDouble(4, bbox.longitudeMax);
152                    stmt.setString(5, Lookup.distributor);
153    
154                  return hentAdresseListe( stmt );                  List<Adresse> list = hentAdresseListe( stmt );
155                    return list.toArray( new Adresse[ list.size() ] );
156          }          }
157            
158            
159            public Adresse[] hentAlleDaekkedeAdresser() throws SQLException {
160                    String sql = "SELECT id,a.postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute,p.distributor as ho " +
161                                    "FROM fulddaekning.adressetabel a " +
162                                    "LEFT JOIN bogleveringer.postnummerdistributor p on (a.postnr=p.postnr) " +
163                                    "WHERE rute IS NOT NULL " +
164                                    "AND latitude IS NOT NULL " +
165                                    "AND longitude IS NOT NULL " +
166                                    "AND a.distributor = ? ";
167    
168                    // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)
169                    // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html
170                    //PreparedStatement stmt = conn.prepareStatement(sql);
171                    PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
172                    stmt.setFetchSize(Integer.MIN_VALUE);
173    
174                    stmt.setString(1, Lookup.distributor);
175    
176                    List<Adresse> list = hentAdresseListe( stmt );
177                    return list.toArray( new Adresse[ list.size() ] );
178            }
179            
180            
181    
182          public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException {          public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException {
183                  /*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 147  public class Database { Line 208  public class Database {
208    
209                  saveStmt.setDouble(19, bedsteAfstand);                  saveStmt.setDouble(19, bedsteAfstand);
210    
211                  saveStmt.executeUpdate();                        saveStmt.addBatch();
212                  saveStmt.clearParameters();                  batchCount++;
213                    if (batchCount >= 100) {
214                            saveStmt.executeBatch();
215                            batchCount = 0;
216                    }
217                    //saveStmt.executeUpdate();    
218                    //saveStmt.clearParameters();
219    
220                  //saveStmt.close();                      //saveStmt.close();    
221            }
222            
223            public synchronized void saveBatch() throws SQLException{
224                    saveStmt.executeBatch();
225                    batchCount = 0;
226          }          }
227    
228    
229    
230          protected ArrayList<Adresse> hentAdresseListe(PreparedStatement stmt) throws SQLException{          protected ArrayList<Adresse> hentAdresseListe(PreparedStatement stmt) throws SQLException{
231                  ArrayList<Adresse> list = new ArrayList<Adresse>( 30000 );                  ArrayList<Adresse> list = new ArrayList<Adresse>( 1000000 );
232    
233                  //logger.info("Starting query");                  //logger.info("Starting query");
234                  ResultSet res = stmt.executeQuery();                  ResultSet res = stmt.executeQuery();
# Line 175  public class Database { Line 245  public class Database {
245                          adr.latitude = res.getDouble("latitude");                          adr.latitude = res.getDouble("latitude");
246                          adr.longitude = res.getDouble("longitude");                          adr.longitude = res.getDouble("longitude");
247                          adr.rute = res.getString("rute");                          adr.rute = res.getString("rute");
248                            adr.ho = res.getInt("ho");
249    
250                          list.add(adr);                          list.add(adr);
251    
# Line 187  public class Database { Line 258  public class Database {
258                  return list;                  return list;
259          }          }
260    
261            public Connection getConnection(SafeProperties conf) throws SQLException, IOException {
262                    
263                    String db_host = conf.getSafeProperty("DB_HOST");
264                    String db_user = conf.getSafeProperty("DB_USER");
265                    String db_pass = conf.getSafeProperty("DB_PASS");
266    
267                    
268                    
269    
270                Connection conn = null;
271                Properties connectionProps = new Properties();
272                connectionProps.put("user", db_user);
273                connectionProps.put("password", db_pass);
274    
275                //For debug output, tilføj denne til JDBC url'en: &profileSQL=true    
276                conn = DriverManager.getConnection(
277                               "jdbc:mysql://" +
278                               db_host +
279                               ":3306/?rewriteBatchedStatements=true",
280                               connectionProps);
281                logger.info("Connected to database");
282                return conn;
283            }
284    
285  }  }

Legend:
Removed from v.2151  
changed lines
  Added in v.2231

  ViewVC Help
Powered by ViewVC 1.1.20