/[projects]/dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/LookupMain.java
ViewVC logotype

Contents of /dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/LookupMain.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2328 - (show annotations) (download)
Thu Feb 19 13:39:33 2015 UTC (9 years, 2 months ago) by torben
File size: 5663 byte(s)
Also cache all non-covered addresses in local memory
1 package dk.daoas.fulddaekning;
2
3 import java.io.File;
4 import java.io.FileReader;
5 import java.util.Set;
6 import java.util.concurrent.Executors;
7 import java.util.concurrent.ThreadFactory;
8 import java.util.concurrent.ThreadPoolExecutor;
9 import java.util.logging.FileHandler;
10 import java.util.logging.Logger;
11 import java.util.logging.SimpleFormatter;
12
13 public class LookupMain {
14
15
16 static final String CONFIG_FILENAME = "fulddaekning.properties";
17
18 static boolean rename_tables;
19 static SafeProperties conf;
20
21 static int max_workers;
22 static boolean verbose;
23
24 static String distributor;
25
26 final static Logger logger = Logger.getLogger( LookupMain.class.toString() );
27
28
29 static Statistik flestDaekkede = new Statistik();
30 static Statistik flestIkkeDaekkede = new Statistik();
31 static Statistik mestBrugteTid = new Statistik();
32 static Statistik stoersteDataset = new Statistik();
33
34 static Adresse[] alleDaekkedeAdresser;
35
36 static ThreadPoolExecutor threadPool;
37
38 private static void setupThreadPool() {
39 threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(max_workers, new WorkerThreadFactory() );
40 }
41
42
43 public static void main(String[] args) throws Exception {
44
45 //Setup j.u.l Logger
46 Logger root = Logger.getLogger("");
47 FileHandler fhandler = new FileHandler("fulddaekning.log"); // Ingen max størrelse, ingen rotation og ingen append
48 fhandler.setFormatter( new SimpleFormatter() );
49 root.addHandler( fhandler );
50
51
52
53
54 File confFile = new File( CONFIG_FILENAME );
55 if (! confFile.exists() ) {
56 logger.warning("Config file not found: " + CONFIG_FILENAME);
57 System.exit(1);
58 }
59
60 conf = new SafeProperties();
61 conf.load( new FileReader(confFile) );
62
63 max_workers = Integer.parseInt( conf.getSafeProperty("MAX_WORKERS") );
64 if (max_workers <= 0) {
65 logger.info("!!! AUTO-DETECT MAX_WORKERS !!!");
66 int cores = Runtime.getRuntime().availableProcessors();
67 cores -= 1;//Efterlad 1 core/cpu i reserve til systemet
68
69 max_workers = Math.max(1, cores); //Dog skal der som minimum være 1 core til beregning
70
71 }
72 logger.info("Starting with MAX_WORKERS:" + max_workers);
73 setupThreadPool();
74
75
76 verbose = Boolean.parseBoolean( conf.getSafeProperty("VERBOSE") );
77 logger.info("Starting with VERBOSE:" + verbose);
78
79 rename_tables = Boolean.parseBoolean( conf.getSafeProperty("RENAMETABLES") );
80 logger.info("Starting with RENAMETABLES:" + rename_tables);
81
82 distributor = conf.getSafeProperty("DISTRIBUTOR");
83 distributor = distributor.toUpperCase();
84 logger.info("Starting for DISTRIBUTOR:" + distributor);
85
86 Constants.init(distributor);
87 Constants consts = Constants.getInstance();
88
89 Database db = new Database(conf);
90 db.hentAlleIkkedaekkedeAdresser(consts.getMinPostnr(), consts.getMaxPostnr() );
91
92
93 boolean testRun= false;
94
95
96 long start = System.currentTimeMillis();
97
98 if (testRun == false) {
99
100 logger.info("Finder postnumre");
101 Set<Integer> postnumre = db.hentPostnumreCache();
102
103 // Først validerer vi BBox på alle postnummre, for at undgå fuldt stop midt i beregningen
104 for(int postnr : postnumre) { //
105 logger.info("Validerer BBox for " + postnr);
106 BoundingBox bbox = db.getBoundingbox(postnr);
107 bbox.validateBbox();
108 }
109
110
111 logger.info("Henter alle daekkede adresser");
112 alleDaekkedeAdresser = db.hentAlleDaekkedeAdresser();
113 logger.info( "AlleDaekkedeAdresser.length=" + alleDaekkedeAdresser.length);
114
115 //pre-check er ok - reset tmp tabel og start søgningen
116 db.resetResultTable();
117
118 for(int postnr : postnumre) {
119 Lookup lookup = new Lookup(postnr, db, threadPool);
120 lookup.doLookup();
121 }
122
123 if (rename_tables) {
124 db.renameResultTables();
125 } else {
126 logger.info( "Rename tables is disabled !!!" );
127 }
128
129
130
131 } else {
132 /// Test
133 db.resetResultTable();
134
135 if (consts.doExtendedLookup()) {
136 alleDaekkedeAdresser = db.hentAlleDaekkedeAdresser();
137 logger.info( "AlleDaekkedeAdresser.length=" + alleDaekkedeAdresser.length);
138 }
139
140 Lookup lookup = new Lookup(2700, db, threadPool);
141 lookup.doLookup();
142 }
143
144 threadPool.shutdown();
145
146 long now = System.currentTimeMillis();
147 long elapsed = now - start ;
148
149 logger.info("Mest brugte tid: " + mestBrugteTid);
150 logger.info("Flest Ikke-dækkede, " + flestIkkeDaekkede);
151 logger.info("Flest Dækkede, " + flestDaekkede);
152 logger.info("Største Dataset, " + stoersteDataset);
153 logger.info("Fuld load done : " + formatMilliSeconds(elapsed) );
154 }
155
156
157 public static void saveStatistics(Statistik stat) {
158 if (stat.antalDaekkede > flestDaekkede.antalDaekkede) {
159 flestDaekkede = stat;
160 }
161 if (stat.antalIkkeDaekkede > flestIkkeDaekkede.antalIkkeDaekkede) {
162 flestIkkeDaekkede = stat;
163 }
164
165 if (stat.totalDataset > mestBrugteTid.totalDataset) {
166 stoersteDataset = stat;
167 }
168
169 if (stat.forbrugtTid > mestBrugteTid.forbrugtTid) {
170 mestBrugteTid = stat;
171 }
172 }
173
174 static String formatMilliSeconds(long milliseconds) {
175 int mseconds = (int) milliseconds % 1000;
176 int seconds = (int) (milliseconds / 1000) % 60 ;
177 int minutes = (int) ((milliseconds / (1000*60)) % 60);
178 int hours = (int) ((milliseconds / (1000*60*60)) % 24);
179
180 return String.format("%02d:%02d:%02d.%03d", hours, minutes, seconds, mseconds);
181 }
182
183 static class WorkerThreadFactory implements ThreadFactory {
184 int count = 0;
185
186 @Override
187 public Thread newThread(Runnable r) {
188 return new Thread(r, "lookupWorker/" + count++);
189 }
190 }
191 }

  ViewVC Help
Powered by ViewVC 1.1.20