/[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 2157 by torben, Sat May 10 08:24:55 2014 UTC revision 2230 by torben, Mon Sep 29 18:15:48 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 24  public class Database { Line 26  public class Database {
26          public Database(SafeProperties conf)  throws SQLException,IOException {          public Database(SafeProperties conf)  throws SQLException,IOException {
27                  this.conn = getConnection( conf );                        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 32  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 65  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,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " +
89                                    "FROM fulddaekning.adressetabel " +
90                                  "WHERE rute IS NULL " +  //Ingen dækning                                  "WHERE rute IS NULL " +  //Ingen dækning
91                                  "AND postnr=?  " +                                  "AND postnr=?  " +
92                                  "AND latitude IS NOT NULL " +                                  "AND latitude IS NOT NULL " +
# Line 80  public class Database { Line 101  public class Database {
101    
102          public List<Integer> hentPostnumre() throws SQLException {          public List<Integer> hentPostnumre() throws SQLException {
103                  ArrayList<Integer> list = new ArrayList<Integer>();                  ArrayList<Integer> list = new ArrayList<Integer>();
104                    
105                    Constants consts = Constants.getInstance();
106    
107    
108                  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 " +
109                                             "FROM fulddaekning.adressetabel " +
110                                             //"WHERE distributor = ? and rute is not null " +
111                                             "WHERE postnr BETWEEN ? AND ? " +
112                                             "GROUP BY postnr " +
113                                             "ORDER by postnr";
114                  PreparedStatement stmt = conn.prepareStatement(sql);                  PreparedStatement stmt = conn.prepareStatement(sql);
115                    //stmt.setString(1, Lookup.distributor );
116                    stmt.setInt(1, consts.getMinPostnr());
117                    stmt.setInt(2, consts.getMaxPostnr());
118                  ResultSet res = stmt.executeQuery();                  ResultSet res = stmt.executeQuery();
119    
120                  while (res.next()) {                  while (res.next()) {
# Line 98  public class Database { Line 129  public class Database {
129                  return list;                  return list;
130          }          }
131    
132          public ArrayList<Adresse> hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {          public Adresse[] hentDaekkedeAdresser( BoundingBox bbox) throws SQLException {
133                  String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute FROM fulddaekning.adressetabel " +                  String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " +
134                                    "FROM fulddaekning.adressetabel " +
135                                  "WHERE rute IS NOT NULL " +                                  "WHERE rute IS NOT NULL " +
136                                  "AND latitude BETWEEN ? AND ? " +                                  "AND latitude BETWEEN ? AND ? " +
137                                  "AND longitude BETWEEN ? AND ? " +                                  "AND longitude BETWEEN ? AND ? " +
138                                  "AND distributor = 'DAO' ";                                  "AND distributor = ? ";
139    
140                  // 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)
141                  // 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 115  public class Database { Line 147  public class Database {
147                  stmt.setDouble(2, bbox.latitudeMax);                  stmt.setDouble(2, bbox.latitudeMax);
148                  stmt.setDouble(3, bbox.longitudeMin);                  stmt.setDouble(3, bbox.longitudeMin);
149                  stmt.setDouble(4, bbox.longitudeMax);                  stmt.setDouble(4, bbox.longitudeMax);
150                    stmt.setString(5, Lookup.distributor);
151    
152                  return hentAdresseListe( stmt );                  List<Adresse> list = hentAdresseListe( stmt );
153                    return list.toArray( new Adresse[ list.size() ] );
154          }          }
155            
156            
157            public Adresse[] hentAlleDaekkedeAdresser() throws SQLException {
158                    String sql = "SELECT id,postnr,adresse,gadeid,husnr,husnrbogstav,latitude,longitude,rute " +
159                                    "FROM fulddaekning.adressetabel " +
160                                    "WHERE rute IS NOT NULL " +
161                                    "AND latitude IS NOT NULL " +
162                                    "AND longitude IS NOT NULL " +
163                                    "AND distributor = ? ";
164    
165                    // Forward only + concur_read_only + fetchsize tvinger driver til at hente en række af gangen (bedre performance ved store result sets)
166                    // Se http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html
167                    //PreparedStatement stmt = conn.prepareStatement(sql);
168                    PreparedStatement stmt = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
169                    stmt.setFetchSize(Integer.MIN_VALUE);
170    
171                    stmt.setString(1, Lookup.distributor);
172    
173                    List<Adresse> list = hentAdresseListe( stmt );
174                    return list.toArray( new Adresse[ list.size() ] );
175            }
176            
177            
178    
179          public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException {          public synchronized void gemResultat(Adresse orgAdresse, Adresse bedsteAdresse, double bedsteAfstand) throws SQLException {
180                  /*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 150  public class Database { Line 205  public class Database {
205    
206                  saveStmt.setDouble(19, bedsteAfstand);                  saveStmt.setDouble(19, bedsteAfstand);
207    
208                  saveStmt.executeUpdate();                        saveStmt.addBatch();
209                  saveStmt.clearParameters();                  batchCount++;
210                    if (batchCount >= 100) {
211                            saveStmt.executeBatch();
212                            batchCount = 0;
213                    }
214                    //saveStmt.executeUpdate();    
215                    //saveStmt.clearParameters();
216    
217                  //saveStmt.close();                      //saveStmt.close();    
218            }
219            
220            public synchronized void saveBatch() throws SQLException{
221                    saveStmt.executeBatch();
222                    batchCount = 0;
223          }          }
224    
225    
226    
227          protected ArrayList<Adresse> hentAdresseListe(PreparedStatement stmt) throws SQLException{          protected ArrayList<Adresse> hentAdresseListe(PreparedStatement stmt) throws SQLException{
228                  ArrayList<Adresse> list = new ArrayList<Adresse>( 30000 );                  ArrayList<Adresse> list = new ArrayList<Adresse>( 1000000 );
229    
230                  //logger.info("Starting query");                  //logger.info("Starting query");
231                  ResultSet res = stmt.executeQuery();                  ResultSet res = stmt.executeQuery();
# Line 204  public class Database { Line 268  public class Database {
268              connectionProps.put("user", db_user);              connectionProps.put("user", db_user);
269              connectionProps.put("password", db_pass);              connectionProps.put("password", db_pass);
270    
271                //For debug output, tilføj denne til JDBC url'en: &profileSQL=true    
272              conn = DriverManager.getConnection(              conn = DriverManager.getConnection(
273                             "jdbc:mysql://" +                             "jdbc:mysql://" +
274                             db_host +                             db_host +
275                             ":3306/",                             ":3306/?rewriteBatchedStatements=true",
276                             connectionProps);                             connectionProps);
277              logger.info("Connected to database");              logger.info("Connected to database");
278              return conn;              return conn;

Legend:
Removed from v.2157  
changed lines
  Added in v.2230

  ViewVC Help
Powered by ViewVC 1.1.20