| 1 |
package bloom; |
| 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.ByteBuilder; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class ParseBloomFilter { |
| 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 |
ParseBloomFilter x=new ParseBloomFilter(args); |
| 43 |
|
| 44 |
|
| 45 |
x.process(t); |
| 46 |
|
| 47 |
|
| 48 |
Shared.closeStream(x.outstream); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public ParseBloomFilter(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==null ? "stdout.txt" : parser.out1); |
| 75 |
} |
| 76 |
|
| 77 |
fixExtensions(); |
| 78 |
checkFileExistence(); |
| 79 |
checkStatics(); |
| 80 |
|
| 81 |
ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, overwrite, append, false); |
| 82 |
ffoutInvalid=FileFormat.testOutput(outInvalid, FileFormat.TXT, null, true, overwrite, append, false); |
| 83 |
ffin1=FileFormat.testInput(in1, FileFormat.TXT, null, true, true); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
private Parser parse(String[] args){ |
| 92 |
|
| 93 |
Parser parser=new Parser(); |
| 94 |
for(int i=0; i<args.length; i++){ |
| 95 |
String arg=args[i]; |
| 96 |
String[] split=arg.split("="); |
| 97 |
String a=split[0].toLowerCase(); |
| 98 |
String b=split.length>1 ? split[1] : null; |
| 99 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 100 |
|
| 101 |
if(a.equals("invalid")){ |
| 102 |
outInvalid=b; |
| 103 |
}else if(a.equals("lines")){ |
| 104 |
maxLines=Long.parseLong(b); |
| 105 |
if(maxLines<0){maxLines=Long.MAX_VALUE;} |
| 106 |
}else if(a.equals("verbose")){ |
| 107 |
verbose=Parse.parseBoolean(b); |
| 108 |
ByteFile1.verbose=verbose; |
| 109 |
ByteFile2.verbose=verbose; |
| 110 |
ReadWrite.verbose=verbose; |
| 111 |
}else if(parser.parse(arg, a, b)){ |
| 112 |
|
| 113 |
}else{ |
| 114 |
outstream.println("Unknown parameter "+args[i]); |
| 115 |
assert(false) : "Unknown parameter "+args[i]; |
| 116 |
|
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return parser; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
private void fixExtensions(){ |
| 125 |
in1=Tools.fixExtension(in1); |
| 126 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
private void checkFileExistence(){ |
| 131 |
|
| 132 |
if(!Tools.testOutputFiles(overwrite, append, false, out1)){ |
| 133 |
outstream.println((out1==null)+", "+out1); |
| 134 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out1+"\n"); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
if(!Tools.testInputFiles(false, true, in1)){ |
| 139 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
if(!Tools.testForDuplicateFiles(true, in1, out1)){ |
| 144 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
private static void checkStatics(){ |
| 150 |
|
| 151 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 152 |
ByteFile.FORCE_MODE_BF2=true; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
void process(Timer t){ |
| 166 |
|
| 167 |
ByteFile bf=ByteFile.makeByteFile(ffin1); |
| 168 |
ByteStreamWriter bsw=makeBSW(ffout1); |
| 169 |
ByteStreamWriter bswInvalid=makeBSW(ffoutInvalid); |
| 170 |
|
| 171 |
|
| 172 |
if(bsw!=null){ |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
processInner(bf, bsw, bswInvalid); |
| 177 |
|
| 178 |
errorState|=bf.close(); |
| 179 |
if(bsw!=null){errorState|=bsw.poisonAndWait();} |
| 180 |
if(bswInvalid!=null){errorState|=bswInvalid.poisonAndWait();} |
| 181 |
|
| 182 |
t.stop(); |
| 183 |
|
| 184 |
outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8)); |
| 185 |
|
| 186 |
outstream.println(); |
| 187 |
outstream.println("Valid Lines: \t"+linesOut); |
| 188 |
outstream.println("Invalid Lines: \t"+(linesProcessed-linesOut)); |
| 189 |
|
| 190 |
if(errorState){ |
| 191 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
private void processInner(ByteFile bf, ByteStreamWriter bsw, ByteStreamWriter bswInvalid){ |
| 200 |
byte[] line=bf.nextLine(); |
| 201 |
ByteBuilder bb=new ByteBuilder(); |
| 202 |
|
| 203 |
while(line!=null){ |
| 204 |
if(line.length>0){ |
| 205 |
if(maxLines>0 && linesProcessed>=maxLines){break;} |
| 206 |
linesProcessed++; |
| 207 |
bytesProcessed+=(line.length+1); |
| 208 |
|
| 209 |
final boolean header=(line[0]=='#'); |
| 210 |
String s=new String(line); |
| 211 |
|
| 212 |
if(header){ |
| 213 |
bb.nl(); |
| 214 |
bb.append(line); |
| 215 |
}else if(s.startsWith("Executing bloom.BloomFilterWrapper")){ |
| 216 |
|
| 217 |
bb.nl(); |
| 218 |
String[] split=s.replace(' ', '\t').replace(',', '\t').split("\t"); |
| 219 |
for(String sub : split){ |
| 220 |
if(sub.startsWith("t=")){ |
| 221 |
bb.append(sub.substring(2)).tab(); |
| 222 |
break; |
| 223 |
} |
| 224 |
} |
| 225 |
}else if(s.startsWith("Keys Counted:") || s.startsWith("Increments:")){ |
| 226 |
String[] split=Tools.whitespacePlus.split(s); |
| 227 |
bb.append(split[split.length-1]).tab(); |
| 228 |
}else if(s.startsWith("Filter creation:")){ |
| 229 |
String[] split=Tools.whitespacePlus.split(s); |
| 230 |
bb.append(split[split.length-2]).tab(); |
| 231 |
}else{ |
| 232 |
if(bswInvalid!=null){ |
| 233 |
bswInvalid.println(line); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
line=bf.nextLine(); |
| 238 |
} |
| 239 |
|
| 240 |
bb.nl(); |
| 241 |
bytesOut+=bb.length; |
| 242 |
bsw.print(bb.toBytes()); |
| 243 |
bb.clear(); |
| 244 |
} |
| 245 |
|
| 246 |
private static ByteStreamWriter makeBSW(FileFormat ff){ |
| 247 |
if(ff==null){return null;} |
| 248 |
ByteStreamWriter bsw=new ByteStreamWriter(ff); |
| 249 |
bsw.start(); |
| 250 |
return bsw; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
private String in1=null; |
| 258 |
private String out1=null; |
| 259 |
private String outInvalid=null; |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
private long linesProcessed=0; |
| 264 |
private long linesOut=0; |
| 265 |
private long bytesProcessed=0; |
| 266 |
private long bytesOut=0; |
| 267 |
|
| 268 |
private long maxLines=Long.MAX_VALUE; |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
private final FileFormat ffin1; |
| 275 |
private final FileFormat ffout1; |
| 276 |
private final FileFormat ffoutInvalid; |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
private PrintStream outstream=System.err; |
| 283 |
public static boolean verbose=false; |
| 284 |
public boolean errorState=false; |
| 285 |
private boolean overwrite=true; |
| 286 |
private boolean append=false; |
| 287 |
|
| 288 |
} |