1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
|
package fileIO;
import java.io.InputStream;
import java.util.Arrays;
import java.util.concurrent.ArrayBlockingQueue;
import shared.Timer;
import shared.Tools;
/**
* Runs a ByteFile1 in a separate thread. Can speed up disk reading, particularly of compressed files, at cost of slightly more work done.
* Drop-in compatible with ByteFile1.
* @author Brian Bushnell
* @date Sep 23, 2013
*
*/
public final class ByteFile2 extends ByteFile {
public static void main(String[] args){
ByteFile2 tf=new ByteFile2(args.length>0 ? args[0] : "stdin", true);
long first=0, last=100;
boolean speedtest=false;
if(args.length>1){
if(args[1].equalsIgnoreCase("speedtest")){
speedtest=true;
first=0;
last=Long.MAX_VALUE;
}else{
first=Integer.parseInt(args[1]);
last=first+100;
}
}
if(args.length>2){
last=Integer.parseInt(args[2]);
}
speedtest(tf, first, last, !speedtest);
tf.close();
tf.reset();
tf.close();
}
private static void speedtest(ByteFile2 tf, long first, long last, boolean reprint){
Timer t=new Timer();
long lines=0;
long bytes=0;
for(long i=0; i<first; i++){tf.nextLine();}
if(reprint){
for(long i=first; i<last; i++){
byte[] s=tf.nextLine();
if(s==null){break;}
lines++;
bytes+=s.length;
System.out.println(new String(s));
}
System.err.println("\n");
System.err.println("Lines: "+lines);
System.err.println("Bytes: "+bytes);
}else{
for(long i=first; i<last; i++){
byte[] s=tf.nextLine();
if(s==null){break;}
lines++;
bytes+=s.length;
}
}
t.stop();
if(!reprint){
System.err.println(Tools.timeLinesBytesProcessed(t, lines, bytes, 8));
}
}
// public ByteFile2(String name()){this(name(), false);}
public ByteFile2(String fname, boolean allowSubprocess_){
this(FileFormat.testInput(fname, FileFormat.TEXT, null, allowSubprocess_, false));
}
public ByteFile2(FileFormat ff){
super(ff);
if(verbose){System.err.println("ByteFile2("+ff+")");}
open();
}
@Override
public final void reset(){
close();
open();
superReset();
}
@Override
public synchronized final boolean close(){
if(verbose){System.err.println("ByteFile2("+name()+").close()");}
if(isOpen()){
// errorState|=ReadWrite.killProcess(name());
thread.shutdown();
while(thread.getState()!=Thread.State.TERMINATED){
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
thread.bf1.close();
}
thread=null;
currentList=null;
currentLoc=0;
// assert(numIn==numOut) : numIn+", "+numOut;
pushBack=null;
if(verbose){System.err.println("ByteFile2("+name()+").close() returned "+errorState);}
return errorState;
}
@Override
public final byte[] nextLine(){
if(pushBack!=null){//Commenting out does not seem to improve speed here.
byte[] temp=pushBack;
pushBack=null;
return temp;
}
// if(verbose){System.err.println("Reading line.");}
// byte[] r=null;
byte[][] temp=currentList;
int tempLoc=currentLoc;
if(temp==null || tempLoc>=temp.length || temp[tempLoc]==null){
boolean b=getBuffer();
if(!b){
if(verbose2){System.err.println("nextLine()->getBuffer() returned false.");}
return null;
}
temp=currentList;
tempLoc=currentLoc;
if(temp==null || temp==poison || temp[tempLoc]==null){
return null;
}
}
//TODO: This is a race condition; currentList can be changed to null. A defensive copy could be created.
//Note that I read the above warning and added "temp" and "temploc" but I'm not sure if that fixed anything.
assert(temp!=null && temp!=poison);
assert(tempLoc<temp.length);
assert(temp[tempLoc]!=null);
byte[] r=temp[tempLoc];
assert(r!=null);
currentLoc++;
// numOut++;
return r;
}
private boolean getBuffer(){
if(verbose2){System.err.println("Getting new buffer.");}
currentLoc=0;
final BF1Thread bft=thread;
if(bft==null){
currentList=null;
if(verbose2){System.err.println("No buffers available. thread="+thread+", shutdown="+(thread==null ? "X" : ""+thread.shutdown));}
return false;
}
if(currentList==poison){
if(verbose2){System.err.println("A: Current list is poison.");}
return false;
}
if(currentList!=null){
Arrays.fill(currentList, null); //MUST be done or lines get recycled at end of file.
while(currentList!=null){
try {
if(verbose2){System.err.println("adding to qEmpty list size "+currentList.length+"\n"+toString(currentList));}
bft.qEmpty.put(currentList);
currentList=null;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
assert(currentList==null);
while(currentList==null){
try {
assert(bft!=null);
if(verbose2){System.err.println("C: qFull.size()="+bft.qFull.size());}
currentList=bft.qFull.take();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(verbose2){
if(currentList==poison){
System.err.println("B: Current list is poison.");
}else{
System.err.println("getBuffer fetched a new buffer of size "+currentList.length);
}
}
return currentList!=poison;
}
private final synchronized BF1Thread open(){
if(verbose2){System.err.println("ByteFile2("+name()+").open()");}
assert(thread==null);
currentList=null;
currentLoc=0;
// numIn=0;
// numOut=0;
thread=new BF1Thread(ff);
thread.start();
return thread;
}
private class BF1Thread extends Thread{
// public BF1Thread(String fname){
// bf1=new ByteFile1(fname, false, allowSubprocess);
// qFull=new ArrayBlockingQueue<byte[][]>(buffs+2);
// qEmpty=new ArrayBlockingQueue<byte[][]>(buffs+2);
// for(int i=0; i<buffs; i++){
// try {
// qEmpty.put(new byte[bufflen][]);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// }
public BF1Thread(FileFormat ff){
bf1=new ByteFile1(ff);
qFull=new ArrayBlockingQueue<byte[][]>(buffs+2);
qEmpty=new ArrayBlockingQueue<byte[][]>(buffs+2);
for(int i=0; i<buffs; i++){
try {
qEmpty.put(new byte[bufflen][]);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void run(){
if(verbose){System.err.println("ByteFile2("+name()+").run()");}
byte[] s=null;
byte[][] list=null;
while(list==null){
try {
list = qEmpty.take();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
synchronized(this){
if(list==poison || shutdown){
shutdown();
return;
}
}
int loc=0;
long bases=0;
//At this point, list is not null
for(s=bf1.nextLine(); s!=null; s=bf1.nextLine()){
bases+=s.length;
assert(list!=null) : "Somehow the list became null for "+bf1.name()+" at line "+cntr;
list[loc]=s;
loc++;
// numIn++;
// if(verbose){System.err.println("Added line "+numIn);}
if(loc>=bufflen || bases>=buffcapacity){
if(verbose2){System.err.println("Capacity exceeded.");}
while(list!=null){
try {
// synchronized(this){
// if(!shutdown){
if(verbose2){
System.err.println("A: Adding to qFull list of size "+loc);
System.err.println(ByteFile2.toString(list));
}
cntr+=list.length;
qFull.put(list);
if(verbose2){System.err.println("A: qFull.size()="+qFull.size());}
// }
// }
list=null;
loc=0;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//At this point, list is null
if(shutdown){
if(verbose2){System.err.println("Break 1");}
break;
}
while(list==null){
if(verbose2){System.err.println("Taking empty list.");}
try {
list = qEmpty.take();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//At this point, list is not null
bases=0;
if(list==poison){
if(verbose2){System.err.println("Break 2");}
break;
}
//At this point, list is not null
}
}
if(verbose2){System.err.println("Run loop exit.");}
while(list!=null && loc>0){
try {
// synchronized(this){
// if(!shutdown){
if(verbose2){System.err.println("B: Adding list of size "+loc);}
qFull.put(list);
if(verbose2){System.err.println("B: qFull.size()="+qFull.size());}
// }
// }
list=null;
loc=0;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//At this point, list is null
shutdown();
if(verbose){System.err.println("ByteFile2("+name()+").run() finished");}
}
synchronized void shutdown(){
if(verbose || verbose2){System.err.println("ByteFile2("+name()+").shutdown()");}
if(shutdown){return;}
shutdown=true;
if(verbose2){System.err.println("Adding poison.");}
qFull.add(poison);
qEmpty.add(poison);
if(verbose2){System.err.println("D: qFull.size()="+qFull.size());}
if(verbose || verbose2){System.err.println("ByteFile2("+name()+").shutdown() finished");}
}
private boolean shutdown=false;
final ByteFile1 bf1;
final ArrayBlockingQueue<byte[][]> qFull;
final ArrayBlockingQueue<byte[][]> qEmpty;
}
@Override
public boolean isOpen(){
final byte[][] list=currentList;
final int loc=currentLoc;
if(list!=null && loc<list.length && list[loc]!=null){return true;}
final BF1Thread bft=thread;
if(bft==null){
return false;
}
return true;
// synchronized(bft){
// //NOTE!!! This cannot be used because qFull.size() will not return a correctly synchronized value. Poll() may work.
// assert(bft.bf1.isOpen() || !bft.qFull.isEmpty()) : bft.bf1.isOpen()+", "+bft.qFull.isEmpty()+", "+bft.qFull.size();
// return (bft.bf1.isOpen() || !bft.qFull.isEmpty());
// }
}
@Override
public final void pushBack(byte[] line){
assert(pushBack==null);
pushBack=line;
}
// @Override
// public void pushBack(byte[] line) {
// if(bstart>line.length){
// bstart--;
// buffer[bstart]='\n';
// for(int i=0, j=bstart-line.length; i<line.length; i++, j++){
// buffer[j]=line[i];
// }
// bstart=bstart-line.length;
// return;
// }
//
// int bLen=bstop-bstart;
// int newLen=bLen+line.length+1;
// int rShift=line.length+1-bstart;
// assert(rShift>0) : bstop+", "+bstart+", "+line.length;
// while(newLen>buffer.length){
// //This could get big if pushback is used often,
// //unless special steps are taken to prevent it, like leaving extra space for pushbacks.
// buffer=Arrays.copyOf(buffer, buffer.length*2);
// }
//
// Tools.shiftRight(buffer, rShift);
//
// for(int i=0; i<line.length; i++){
// buffer[i]=line[i];
// }
// buffer[line.length]='\n';
// bstart=0;
// bstop=newLen;
// }
/** For debugging */
private static String toString(byte[][] x){
StringBuilder sb=new StringBuilder();
for(byte[] z : x){
sb.append(z==null ? "null" : new String(z)).append('\n');
}
return sb.toString();
}
@Override
public final InputStream is(){return thread==null ? null : thread.bf1.is();}
@Override
public final long lineNum(){return thread==null ? -1 : thread.bf1.lineNum();}
long cntr;
private BF1Thread thread=null;
private byte[][] currentList=null;
private int currentLoc=0;
// private int currentSize=0;
// private long numIn=0, numOut=0;
private byte[] pushBack=null;
static final byte[][] poison=new byte[0][];
public static boolean verbose=false;
private static final boolean verbose2=false;
private static final int bufflen=1000;
private static final int buffs=4;
private static final int buffcapacity=256000;
private boolean errorState=false;
}
|