Debian Codegraph @

current/fun/Foo4.java

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