| 1 |
package fun; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
|
| 5 |
import fileIO.ByteFile; |
| 6 |
import fileIO.ByteFile1; |
| 7 |
import fileIO.ByteFile2; |
| 8 |
import fileIO.ByteStreamWriter; |
| 9 |
import fileIO.FileFormat; |
| 10 |
import fileIO.ReadWrite; |
| 11 |
import shared.Parse; |
| 12 |
import shared.Parser; |
| 13 |
import shared.PreParser; |
| 14 |
import shared.Shared; |
| 15 |
import shared.Timer; |
| 16 |
import shared.Tools; |
| 17 |
import structures.LongList; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class Foo { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public static void main(String[] args){ |
| 38 |
|
| 39 |
Timer t=new Timer(); |
| 40 |
|
| 41 |
|
| 42 |
Foo x=new Foo(args); |
| 43 |
|
| 44 |
|
| 45 |
x.process(t); |
| 46 |
|
| 47 |
|
| 48 |
Shared.closeStream(x.outstream); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public Foo(String[] args){ |
| 56 |
|
| 57 |
{ |
| 58 |
PreParser pp=new PreParser(args, null, false); |
| 59 |
args=pp.args; |
| 60 |
outstream=pp.outstream; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 65 |
ReadWrite.setZipThreads(Shared.threads()); |
| 66 |
|
| 67 |
{ |
| 68 |
final Parser parser=parse(args); |
| 69 |
overwrite=parser.overwrite; |
| 70 |
append=parser.append; |
| 71 |
|
| 72 |
in1=parser.in1; |
| 73 |
|
| 74 |
out1=parser.out1; |
| 75 |
} |
| 76 |
|
| 77 |
validateParams(); |
| 78 |
fixExtensions(); |
| 79 |
checkFileExistence(); |
| 80 |
checkStatics(); |
| 81 |
|
| 82 |
ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, overwrite, append, false); |
| 83 |
ffoutInvalid=FileFormat.testOutput(outInvalid, FileFormat.TXT, null, true, overwrite, append, false); |
| 84 |
ffin1=FileFormat.testInput(in1, FileFormat.TXT, null, true, true); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
private Parser parse(String[] args){ |
| 93 |
|
| 94 |
|
| 95 |
Parser parser=new Parser(); |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
for(int i=0; i<args.length; i++){ |
| 103 |
String arg=args[i]; |
| 104 |
|
| 105 |
|
| 106 |
String[] split=arg.split("="); |
| 107 |
String a=split[0].toLowerCase(); |
| 108 |
String b=split.length>1 ? split[1] : null; |
| 109 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 110 |
|
| 111 |
if(a.equals("invalid")){ |
| 112 |
outInvalid=b; |
| 113 |
}else if(a.equals("lines")){ |
| 114 |
maxLines=Long.parseLong(b); |
| 115 |
if(maxLines<0){maxLines=Long.MAX_VALUE;} |
| 116 |
}else if(a.equals("verbose")){ |
| 117 |
verbose=Parse.parseBoolean(b); |
| 118 |
ByteFile1.verbose=verbose; |
| 119 |
ByteFile2.verbose=verbose; |
| 120 |
ReadWrite.verbose=verbose; |
| 121 |
}else if(a.equals("parse_flag_goes_here")){ |
| 122 |
long fake_variable=Parse.parseKMG(b); |
| 123 |
|
| 124 |
}else if(parser.parse(arg, a, b)){ |
| 125 |
|
| 126 |
}else{ |
| 127 |
outstream.println("Unknown parameter "+args[i]); |
| 128 |
assert(false) : "Unknown parameter "+args[i]; |
| 129 |
throw new RuntimeException("Unknown parameter "+args[i]); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return parser; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
private void fixExtensions(){ |
| 138 |
in1=Tools.fixExtension(in1); |
| 139 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
private void checkFileExistence(){ |
| 144 |
|
| 145 |
if(!Tools.testOutputFiles(overwrite, append, false, out1)){ |
| 146 |
outstream.println((out1==null)+", "+out1); |
| 147 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out1+"\n"); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
if(!Tools.testInputFiles(false, true, in1)){ |
| 152 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
if(!Tools.testForDuplicateFiles(true, in1, out1)){ |
| 157 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
private static void checkStatics(){ |
| 163 |
|
| 164 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 165 |
ByteFile.FORCE_MODE_BF2=true; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
private boolean validateParams(){ |
| 176 |
|
| 177 |
|
| 178 |
return true; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
void process(Timer t){ |
| 187 |
|
| 188 |
ByteFile bf=ByteFile.makeByteFile(ffin1); |
| 189 |
ByteStreamWriter bsw=makeBSW(ffout1); |
| 190 |
ByteStreamWriter bswInvalid=makeBSW(ffoutInvalid); |
| 191 |
|
| 192 |
|
| 193 |
if(bsw!=null){ |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
processInner(bf, bsw, bswInvalid); |
| 198 |
|
| 199 |
errorState|=bf.close(); |
| 200 |
if(bsw!=null){errorState|=bsw.poisonAndWait();} |
| 201 |
if(bswInvalid!=null){errorState|=bswInvalid.poisonAndWait();} |
| 202 |
|
| 203 |
t.stop(); |
| 204 |
|
| 205 |
outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8)); |
| 206 |
|
| 207 |
outstream.println(); |
| 208 |
outstream.println("Valid Lines: \t"+linesOut); |
| 209 |
outstream.println("Invalid Lines: \t"+(linesProcessed-linesOut)); |
| 210 |
|
| 211 |
|
| 212 |
if(errorState){ |
| 213 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
private void processInner(ByteFile bf, ByteStreamWriter bsw, ByteStreamWriter bswInvalid){ |
| 230 |
byte[] line=bf.nextLine(); |
| 231 |
|
| 232 |
byte delimiter=(byte)'|'; |
| 233 |
|
| 234 |
LongList sizes=new LongList(100000000); |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
while(line!=null){ |
| 243 |
linesProcessed++; |
| 244 |
bytesProcessed+=(line.length+1); |
| 245 |
if(line.length>0){ |
| 246 |
int a=0, b=0; |
| 247 |
|
| 248 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 249 |
assert(b>a) : "Missing term : '"+new String(line)+"'"; |
| 250 |
|
| 251 |
b++; |
| 252 |
a=b; |
| 253 |
|
| 254 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 255 |
assert(b>a) : "Missing term : '"+new String(line)+"'"; |
| 256 |
|
| 257 |
b++; |
| 258 |
a=b; |
| 259 |
|
| 260 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 261 |
assert(b>a) : "Missing term : '"+new String(line)+"'"; |
| 262 |
|
| 263 |
b++; |
| 264 |
a=b; |
| 265 |
|
| 266 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 267 |
assert(b>a) : "Missing term : '"+new String(line)+"'"; |
| 268 |
long size=Parse.parseLong(line, a, b); |
| 269 |
b++; |
| 270 |
a=b; |
| 271 |
|
| 272 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 273 |
assert(b>a) : "Missing term : '"+new String(line)+"'"; |
| 274 |
|
| 275 |
b++; |
| 276 |
a=b; |
| 277 |
|
| 278 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 279 |
assert(b>a) : "Missing term : '"+new String(line)+"'"; |
| 280 |
|
| 281 |
b++; |
| 282 |
a=b; |
| 283 |
|
| 284 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 285 |
assert(b>a) : "Missing term : '"+new String(line)+"'"; |
| 286 |
if(line[a]=='F') { |
| 287 |
linesOut++; |
| 288 |
sizes.add(size); |
| 289 |
} |
| 290 |
b++; |
| 291 |
a=b; |
| 292 |
} |
| 293 |
if(linesProcessed>=maxLines) {break;} |
| 294 |
line=bf.nextLine(); |
| 295 |
} |
| 296 |
sizes.sort(); |
| 297 |
|
| 298 |
long total=sizes.sumLong(); |
| 299 |
long mode=sizes.mode(); |
| 300 |
long mean=sizes.sumLong()/sizes.size; |
| 301 |
long median=sizes.get((int)(sizes.size*0.5)); |
| 302 |
System.out.println("total lines:\t"+Tools.padKMB(linesProcessed, 0)+"\t("+linesProcessed+")"); |
| 303 |
System.out.println("total size: \t"+Tools.padKMB(total, 0)+"\t("+total+")"+"\t"+"("+((total/(1024L*1024L*1024L*1024L)))+" tebibytes)"); |
| 304 |
System.out.println("median size:\t"+median); |
| 305 |
System.out.println("mean size: \t"+mean); |
| 306 |
System.out.println("mode size: \t"+mode); |
| 307 |
System.out.println("P80 size: \t"+sizes.get((int)(sizes.size*0.8))); |
| 308 |
System.out.println("P90 size: \t"+sizes.get((int)(sizes.size*0.9))); |
| 309 |
System.out.println("P95 size: \t"+sizes.get((int)(sizes.size*0.95))); |
| 310 |
System.out.println("P99 size: \t"+sizes.get((int)(sizes.size*0.99))); |
| 311 |
} |
| 312 |
|
| 313 |
private static ByteStreamWriter makeBSW(FileFormat ff){ |
| 314 |
if(ff==null){return null;} |
| 315 |
ByteStreamWriter bsw=new ByteStreamWriter(ff); |
| 316 |
bsw.start(); |
| 317 |
return bsw; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
private String in1=null; |
| 326 |
|
| 327 |
|
| 328 |
private String out1=null; |
| 329 |
|
| 330 |
|
| 331 |
private String outInvalid=null; |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
private long linesProcessed=0; |
| 336 |
private long linesOut=0; |
| 337 |
private long bytesProcessed=0; |
| 338 |
private long bytesOut=0; |
| 339 |
|
| 340 |
private long maxLines=Long.MAX_VALUE; |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
private final FileFormat ffin1; |
| 348 |
|
| 349 |
private final FileFormat ffout1; |
| 350 |
|
| 351 |
private final FileFormat ffoutInvalid; |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
private PrintStream outstream=System.err; |
| 359 |
|
| 360 |
public static boolean verbose=false; |
| 361 |
|
| 362 |
public boolean errorState=false; |
| 363 |
|
| 364 |
private boolean overwrite=true; |
| 365 |
|
| 366 |
private boolean append=false; |
| 367 |
|
| 368 |
} |