/[projects]/dao/DaoAdresseService/src/dk/daoas/daoadresseservice/db/DatabaseLayer.java
ViewVC logotype

Contents of /dao/DaoAdresseService/src/dk/daoas/daoadresseservice/db/DatabaseLayer.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2320 - (show annotations) (download)
Tue Feb 17 08:58:41 2015 UTC (9 years, 3 months ago) by torben
File size: 5279 byte(s)
Use deduplication in a more concistent way
1 package dk.daoas.daoadresseservice.db;
2
3
4 import java.sql.Connection;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Statement;
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12
13 import dk.daoas.daoadresseservice.DaekningsType;
14 import dk.daoas.daoadresseservice.beans.Address;
15 import dk.daoas.daoadresseservice.beans.ExtendedBean;
16 import dk.daoas.daoadresseservice.beans.HundredePctBean;
17 import dk.daoas.daoadresseservice.util.DeduplicateHelper;
18
19 public class DatabaseLayer {
20
21 public static List<Address> getAllAdresses() throws SQLException {
22
23 String sql = "SELECT id,vejnavn,husnr,husnrbogstav,kommunekode,vejkode,postnr,gadeid,upper(distributor) AS distributor,dbkbane,koreliste,rute "
24 + "FROM fulddaekning.adressetabel "
25 + "WHERE gadeid IS NOT NULL "
26 //+ "AND postnr = 8700" //DEBUG only
27 ;
28
29 Connection conn = DBConnection.getConnection();
30 Statement stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
31 stmt.setFetchSize(Integer.MIN_VALUE);
32 ResultSet res = stmt.executeQuery(sql);
33
34 List<Address> list = new ArrayList<Address>(2600000);//initial capacity 2.6 mio
35
36 DeduplicateHelper<String> vejnavnCache = new DeduplicateHelper<String>();
37 DeduplicateHelper<String> distributorCache = new DeduplicateHelper<String>();
38 DeduplicateHelper<String> korelisteCache = new DeduplicateHelper<String>();
39 DeduplicateHelper<String> ruteCache = new DeduplicateHelper<String>();
40
41
42 while (res.next()) {
43
44 Address a = new Address();
45 a.id = res.getInt(1);
46 a.vejnavn = vejnavnCache.getInstance( res.getString(2) );
47 a.husnr = res.getInt(3);
48 a.husnrbogstav = res.getString(4);
49 a.kommunekode = res.getInt(5);
50 a.vejkode = res.getInt(6);
51 a.postnr = res.getInt(7);
52 a.gadeid = res.getLong(8);
53 a.distributor = distributorCache.getInstance(res.getString(9));
54 a.dbkBane = res.getInt(10);
55 a.koreliste = korelisteCache.getInstance( res.getString(11) );
56 a.rute = ruteCache.getInstance( res.getString(12) );
57
58 //a.vasketVejnavn = AddressUtils.vaskVejnavn(a.vejnavn);
59
60 if (a.rute != null && a.rute.length()> 0) {
61 a.daekningsType = DaekningsType.DAEKNING_DIREKTE;
62 } else {
63 a.daekningsType = DaekningsType.DAEKNING_IKKEDAEKKET;
64 }
65
66 list.add(a);
67 }
68 res.close();
69 stmt.close();
70 conn.close();
71
72 System.out.println("Loaded " + list.size() + " adresses");
73
74 return list;
75 }
76
77 public static List<ExtendedBean> getExtendedAdresslist() throws SQLException {
78
79 String sql = "select orgid, a.id as targetid, afstand, LOWER(type) as type from fulddaekning.afstand_anden_rute a " +
80 "join odbc.transporttype t " +
81 "on t.Art = 'Transpost' " +
82 "and ( (t.Type = 'Cykel' and a.Afstand < 1.001) or (t.Type = 'Scooter' and a.Afstand < 1.201) or (t.Type = 'Bil' and a.Afstand < 2.601) ) " +
83 "and t.Rute = a.Rute " +
84
85 "UNION ALL " +
86
87 "SELECT orgid, a.id as targetid, afstand,'' as type FROM fulddaekning.afstand_anden_rute_bk a " +
88 "left join bogleveringer.postnummerdistributor d on d.PostNr = a.orgPostnr " +
89 "WHERE d.Distributor <> 10057"
90 ;
91
92 Connection conn = DBConnection.getConnection();
93 Statement stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
94 stmt.setFetchSize(Integer.MIN_VALUE);
95
96 ResultSet res = stmt.executeQuery(sql);
97
98 DeduplicateHelper<String> transportCache = new DeduplicateHelper<String>();
99
100 List<ExtendedBean> list = new ArrayList<ExtendedBean>( 350000); //Initial capacity 350K
101 while (res.next()) {
102
103 ExtendedBean eb = new ExtendedBean();
104 eb.orgId = res.getInt(1);
105 eb.targetId = res.getInt(2);
106 eb.afstand = res.getDouble(3);
107 eb.transport = transportCache.getInstance(res.getString(4));
108
109 list.add(eb);
110 }
111
112 res.close();
113 stmt.close();
114 conn.close();
115
116 System.out.println("Loaded " + list.size() + " extendedbeans");
117
118 return list;
119 }
120
121 public static Map<Integer,HundredePctBean> get100PctList() throws SQLException {
122 String sql = "SELECT postnr,UPPER(distributor) as distributor,rute,koreliste,dbkbane " +
123 "FROM bogleveringer.adresser_udenfor_daekning";
124
125 Connection conn = DBConnection.getConnection();
126 Statement stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
127 ResultSet res = stmt.executeQuery(sql);
128
129 Map<Integer,HundredePctBean> map = new HashMap<Integer,HundredePctBean>();
130
131 DeduplicateHelper<String> distributorCache = new DeduplicateHelper<String>();
132
133 while (res.next()) {
134
135
136 HundredePctBean bean = new HundredePctBean();
137 bean.postnr = res.getInt(1);
138 bean.distributor = distributorCache.getInstance(res.getString(2));
139 bean.rute = res.getString(3);
140 bean.koreliste = res.getString(4);
141 bean.dbkBane = res.getInt(5);
142
143 map.put(bean.postnr, bean);
144 }
145
146 res.close();
147 stmt.close();
148 conn.close();
149
150 System.out.println("Loaded " + map.size() + " 100pct beans");
151
152 return map;
153
154 }
155
156 }

  ViewVC Help
Powered by ViewVC 1.1.20