Debian Codegraph @

current/fun/Foo6.java

raw symbols SCIP
1 package fun;
2
3 import java.io.PrintStream;
4 import java.text.SimpleDateFormat;
5 import java.util.Date;
6 import java.util.TimeZone;
7
8 import fileIO.ByteFile;
9 import fileIO.ByteFile1;
10 import fileIO.ByteFile2;
11 import fileIO.ByteStreamWriter;
12 import fileIO.FileFormat;
13 import fileIO.ReadWrite;
14 import shared.Parse;
15 import shared.Parser;
16 import shared.PreParser;
17 import shared.Shared;
18 import shared.Timer;
19 import shared.Tools;
20 import structures.LongList;
21
22 /**
23 * Reads a text file.
24 * Prints it to another text file.
25 * Filters out invalid lines and prints them to an optional third file.
26 * @author Brian Bushnell
27 * @date May 9, 2016
28 *
29 */
30 public class Foo6 {
31
32 /*--------------------------------------------------------------*/
33 /*---------------- Initialization ----------------*/
34 /*--------------------------------------------------------------*/
35
36 /**
37 * Code entrance from the command line.
38 * @param args Command line arguments
39 */
40 public static void main(String[] args){
41 //Start a timer immediately upon code entrance.
42 Timer t=new Timer();
43
44 //Create an instance of this class
45 Foo6 x=new Foo6(args);
46
47 //Run the object
48 x.process(t);
49
50 //Close the print stream if it was redirected
51 Shared.closeStream(x.outstream);
52 }
53
54 /**
55 * Constructor.
56 * @param args Command line arguments
57 */
58 public Foo6(String[] args){
59
60 {//Preparse block for help, config files, and outstream
61 PreParser pp=new PreParser(args, /*getClass()*/null, false);
62 args=pp.args;
63 outstream=pp.outstream;
64 }
65
66 //Set shared static variables prior to parsing
67 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
68 ReadWrite.setZipThreads(Shared.threads());
69
70 {//Parse the arguments
71 final Parser parser=parse(args);
72 overwrite=parser.overwrite;
73 append=parser.append;
74
75 in1=parser.in1;
76
77 out1=parser.out1;
78 }
79
80 validateParams();
81 fixExtensions(); //Add or remove .gz or .bz2 as needed
82 checkFileExistence(); //Ensure files can be read and written
83 checkStatics(); //Adjust file-related static fields as needed for this program
84
85 ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, overwrite, append, false);
86 ffoutInvalid=FileFormat.testOutput(outInvalid, FileFormat.TXT, null, true, overwrite, append, false);
87 ffin1=FileFormat.testInput(in1, FileFormat.TXT, null, true, true);
88 }
89
90 /*--------------------------------------------------------------*/
91 /*---------------- Initialization Helpers ----------------*/
92 /*--------------------------------------------------------------*/
93
94 /** Parse arguments from the command line */
95 private Parser parse(String[] args){
96
97 //Create a parser object
98 Parser parser=new Parser();
99
100 //Set any necessary Parser defaults here
101 //parser.foo=bar;
102 // parser.out1="stdout";
103
104 //Parse each argument
105 for(int i=0; i<args.length; i++){
106 String arg=args[i];
107
108 //Break arguments into their constituent parts, in the form of "a=b"
109 String[] split=arg.split("=");
110 String a=split[0].toLowerCase();
111 String b=split.length>1 ? split[1] : null;
112 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
113
114 if(a.equals("invalid")){
115 outInvalid=b;
116 }else if(a.equals("lines")){
117 maxLines=Long.parseLong(b);
118 if(maxLines<0){maxLines=Long.MAX_VALUE;}
119 }else if(a.equals("verbose")){
120 verbose=Parse.parseBoolean(b);
121 ByteFile1.verbose=verbose;
122 ByteFile2.verbose=verbose;
123 ReadWrite.verbose=verbose;
124 }else if(a.equals("parse_flag_goes_here")){
125 long fake_variable=Parse.parseKMG(b);
126 //Set a variable here
127 }else if(parser.parse(arg, a, b)){
128 //do nothing
129 }else{
130 outstream.println("Unknown parameter "+args[i]);
131 assert(false) : "Unknown parameter "+args[i];
132 throw new RuntimeException("Unknown parameter "+args[i]);
133 }
134 }
135
136 return parser;
137 }
138
139 /** Add or remove .gz or .bz2 as needed */
140 private void fixExtensions(){
141 in1=Tools.fixExtension(in1);
142 if(in1==null){throw new RuntimeException("Error - at least one input file is required.");}
143 }
144
145 /** Ensure files can be read and written */
146 private void checkFileExistence(){
147 //Ensure output files can be written
148 if(!Tools.testOutputFiles(overwrite, append, false, out1)){
149 outstream.println((out1==null)+", "+out1);
150 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out1+"\n");
151 }
152
153 //Ensure input files can be read
154 if(!Tools.testInputFiles(false, true, in1)){
155 throw new RuntimeException("\nCan't read some input files.\n");
156 }
157
158 //Ensure that no file was specified multiple times
159 if(!Tools.testForDuplicateFiles(true, in1, out1)){
160 throw new RuntimeException("\nSome file names were specified multiple times.\n");
161 }
162 }
163
164 /** Adjust file-related static fields as needed for this program */
165 private static void checkStatics(){
166 //Adjust the number of threads for input file reading
167 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){
168 ByteFile.FORCE_MODE_BF2=true;
169 }
170
171 // if(!ByteFile.FORCE_MODE_BF2){
172 // ByteFile.FORCE_MODE_BF2=false;
173 // ByteFile.FORCE_MODE_BF1=true;
174 // }
175 }
176
177 /** Ensure parameter ranges are within bounds and required parameters are set */
178 private boolean validateParams(){
179 // assert(minfoo>0 && minfoo<=maxfoo) : minfoo+", "+maxfoo;
180 // assert(false) : "TODO";
181 return true;
182 }
183
184 /*--------------------------------------------------------------*/
185 /*---------------- Outer Methods ----------------*/
186 /*--------------------------------------------------------------*/
187
188 /** Create streams and process all data */
189 void process(Timer t){
190
191 ByteFile bf=ByteFile.makeByteFile(ffin1);
192 ByteStreamWriter bsw=makeBSW(ffout1);
193 ByteStreamWriter bswInvalid=makeBSW(ffoutInvalid);
194
195 // assert(false) : "Header goes here.";
196 if(bsw!=null){
197 // assert(false) : "Header goes here.";
198 }
199
200 processInner(bf, bsw, bswInvalid);
201
202 errorState|=bf.close();
203 if(bsw!=null){errorState|=bsw.poisonAndWait();}
204 if(bswInvalid!=null){errorState|=bswInvalid.poisonAndWait();}
205
206 t.stop();
207
208 // outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8));
209 //
210 // outstream.println();
211 // outstream.println("Valid Lines: \t"+linesOut);
212 // outstream.println("Invalid Lines: \t"+(linesProcessed-linesOut));
213
214 //Throw an exception of there was an error in a thread
215 if(errorState){
216 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
217 }
218 }
219
220 /*--------------------------------------------------------------*/
221 /*---------------- Inner Methods ----------------*/
222 /*--------------------------------------------------------------*/
223
224 private void processInner(ByteFile bf, ByteStreamWriter bsw, ByteStreamWriter bswInvalid){
225
226 Timer t=new Timer();
227
228 byte[] line=bf.nextLine();
229 LongList list=new LongList(100000000);
230
231 long sum=0;
232 while(line!=null){
233 linesProcessed++;
234 bytesProcessed+=(line.length+1);
235 long size=processLine(line, list);
236 if(size>=0){sum+=size;}
237 if(linesProcessed>=maxLines) {break;}
238 line=bf.nextLine();
239 }
240
241 t.stop("parsing: ");
242 t.start();
243
244 final long tebi=1024L*1024L*1024L*1024L;
245 final long tera=1000L*1000L*1000L*1000L;
246
247 {
248 list.sort();
249 t.stop("sorting1: ");
250 t.start();
251 final int[] idxArray=new int[] {10, 20, 30, 40, 50, 60, 70, 80, 90, 95};
252 final int[] pairArray=idxArray.clone();
253 for(int i=0; i<idxArray.length; i++) {pairArray[i]=(int)(idxArray[i]*.01*list.size);}
254 long tsum=0;
255
256 for(int i=0, nextIdx=0, nextPair=pairArray[0]; i<list.size && nextIdx<idxArray.length; i++) {
257 final long v=list.get(i);
258 final long size=getSize(v), time=getTime(v);
259 tsum+=size;
260 if(i>=nextPair) {
261 String s=idxArray[nextIdx]+" percent of files have not been accessed since: "+
262 timeString(time*1000)+" ("+(tsum/tebi)+" tebibytes)";
263 System.out.println(s);
264 nextIdx++;
265 if(nextIdx<idxArray.length) {nextPair=pairArray[nextIdx];}
266 }
267 }
268 t.stop("stats1: ");
269 t.start();
270 }
271
272 for(int i=0; i<list.size; i++) {list.array[i]=getSize(list.array[i]);}
273 t.stop("recoding: ");
274 t.start();
275 list.sort();
276 t.stop("sorting2: ");
277 t.start();
278 long mean=sum/list.size;
279 long median=list.get((int)(list.size*0.5));
280 System.out.println("total size: \t"+(sum/tera)+" TB \t("+sum+")"+"\t"+"("+((sum/tebi))+" tebibytes)");
281 System.out.println("mean size: \t"+mean+" bytes");
282 System.out.println("P50 size: \t"+median+" bytes");
283 System.out.println("P80 size: \t"+list.get((int)(list.size*0.8))+" bytes");
284 System.out.println("P90 size: \t"+list.get((int)(list.size*0.9))+" bytes");
285 System.out.println("P95 size: \t"+list.get((int)(list.size*0.95))+" bytes");
286 t.stop("stats2: ");
287 t.start();
288 }
289
290 static final byte delimiter=(byte)'|';
291
292 long processLine(byte[] line, LongList list) {
293 int a=0, b=0;
294
295 while(b<line.length && line[b]!=delimiter){b++;}
296 b++;
297 a=b;
298
299 while(b<line.length && line[b]!=delimiter){b++;}
300 b++;
301 a=b;
302
303 while(b<line.length && line[b]!=delimiter){b++;}
304 b++;
305 a=b;
306
307 while(b<line.length && line[b]!=delimiter){b++;}
308 assert(b>a) : "Missing term : '"+new String(line)+"'";
309 long size=Parse.parseLong(line, a, b);
310 b++;
311 a=b;
312
313 while(b<line.length && line[b]!=delimiter){b++;}
314 b++;
315 a=b;
316
317 while(b<line.length && line[b]!=delimiter){b++;}
318 b++;
319 a=b;
320
321 while(b<line.length && line[b]!=delimiter){b++;}
322 if(line[a]!='F') {return -1;}
323 b++;
324 a=b;
325
326 while(b<line.length && line[b]!=delimiter){b++;}
327 b++;
328 a=b;
329
330 while(b<line.length && line[b]!=delimiter){b++;}
331 b++;
332 a=b;
333
334 while(b<line.length && line[b]!=delimiter){b++;}
335 b++;
336 a=b;
337
338 while(b<line.length && line[b]!=delimiter){b++;}
339 b++;
340 a=b;
341
342 while(b<line.length && line[b]!=delimiter){b++;}
343 long time=Parse.parseLong(line, a, b);
344 b++;
345 a=b;
346
347 linesOut++;
348 list.add(combine(time, size));
349 return size;
350 }
351
352 private static ByteStreamWriter makeBSW(FileFormat ff){
353 if(ff==null){return null;}
354 ByteStreamWriter bsw=new ByteStreamWriter(ff);
355 bsw.start();
356 return bsw;
357 }
358
359 static String timeString(long time){
360 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
361 sdf.setTimeZone(TimeZone.getTimeZone("PST"));
362 // sdf.setTimeZone(TimeZone.getDefault());
363 return sdf.format(new Date(time));
364 }
365
366 static final int LOWER_BITS=31;
367 static final int MANTISSA_BITS=24;
368 static final int EXP_BITS=LOWER_BITS-MANTISSA_BITS;
369 static final int UPPER_BITS=64-MANTISSA_BITS;
370 static final long LOWER_MASK=~((-1L)<<LOWER_BITS);
371 static final long MANTISSA_MASK=~((-1L)<<MANTISSA_BITS);
372 static final long compress(long raw) {
373 if(raw<=MANTISSA_MASK){return raw;}
374 int leading=Long.numberOfLeadingZeros(raw);
375 int exp=UPPER_BITS-leading;
376 assert(exp>=1);
377 return (raw>>>exp)|(exp<<MANTISSA_BITS);
378 }
379 static final long decompress(long f) {
380 if(f<=MANTISSA_MASK){return f;}
381 int exp=(int)(f>>>MANTISSA_BITS);
382 assert(exp>=1);
383 return (f&MANTISSA_MASK)<<exp;
384 }
385 static final long combine(long time, long size) {
386 return (time<<LOWER_BITS) | compress(size);
387 }
388 static final long getTime(long combined) {
389 return combined>>>LOWER_BITS;
390 }
391 static final long getSize(long combined) {
392 return decompress(combined&LOWER_MASK);
393 }
394
395 /*--------------------------------------------------------------*/
396 /*---------------- Fields ----------------*/
397 /*--------------------------------------------------------------*/
398
399 /** Primary input file path */
400 private String in1=null;
401
402 /** Primary output file path */
403 private String out1=null;
404
405 /** Junk output file path */
406 private String outInvalid=null;
407
408 /*--------------------------------------------------------------*/
409
410 private long linesProcessed=0;
411 private long linesOut=0;
412 private long bytesProcessed=0;
413 private long bytesOut=0;
414
415 private long maxLines=Long.MAX_VALUE;
416
417 /*--------------------------------------------------------------*/
418 /*---------------- Final Fields ----------------*/
419 /*--------------------------------------------------------------*/
420
421 /** Input File */
422 private final FileFormat ffin1;
423 /** Output File */
424 private final FileFormat ffout1;
425 /** Optional Output File for Junk */
426 private final FileFormat ffoutInvalid;
427
428 /*--------------------------------------------------------------*/
429 /*---------------- Common Fields ----------------*/
430 /*--------------------------------------------------------------*/
431
432 /** Print status messages to this output stream */
433 private PrintStream outstream=System.err;
434 /** Print verbose messages */
435 public static boolean verbose=false;
436 /** True if an error was encountered */
437 public boolean errorState=false;
438 /** Overwrite existing output files */
439 private boolean overwrite=true;
440 /** Append to existing output files */
441 private boolean append=false;
442
443 }