Debian Codegraph @

current/stream/ConcurrentGenericReadOutputStream.java

raw symbols SCIP
1 package stream;
2
3 import java.io.File;
4 import java.lang.Thread.State;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7
8 import fileIO.FileFormat;
9
10 /**
11 * @author Brian Bushnell
12 * @date Jan 26, 2015
13 *
14 */
15 public final class ConcurrentGenericReadOutputStream extends ConcurrentReadOutputStream {
16
17
18 /*--------------------------------------------------------------*/
19 /*---------------- Initialization ----------------*/
20 /*--------------------------------------------------------------*/
21
22 ConcurrentGenericReadOutputStream(FileFormat ff1_, FileFormat ff2_, String qf1, String qf2, int rswBuffers, CharSequence header, boolean useSharedHeader){
23 super(ff1_, ff2_);
24
25 if(verbose){
26 System.err.println("ConcurrentGenericReadOutputStream("+ff1+", "+ff2+", "+qf1+", "+qf2+", "+rswBuffers+", "+useSharedHeader+")");
27 }
28
29 assert(ff1!=null);
30 assert(!ff1.text() && !ff1.unknownFormat()) : "Unknown format for "+ff1;
31
32 if(ff1.hasName() && ff1.devnull()){
33 File f=new File(ff1.name());
34 assert(ff1.overwrite() || !f.exists() || ff1.name().equals("/dev/null")) : f.getAbsolutePath()+" already exists; please delete it.";
35 if(ff2!=null){assert(!ff1.name().equals(ff2.name())) : ff1.name()+"=="+ff2.name();}
36 }
37
38 readstream1=new ReadStreamByteWriter(ff1, qf1, true, rswBuffers, header, useSharedHeader);
39 readstream2=ff1.stdio() || ff2==null ? null : new ReadStreamByteWriter(ff2, qf2, false, rswBuffers, header, useSharedHeader);
40
41 if(readstream2==null && readstream1!=null){
42 // System.out.println("ConcurrentReadOutputStream detected interleaved output.");
43 readstream1.OUTPUT_INTERLEAVED=true;
44 }
45
46 table=(ordered ? new HashMap<Long, ArrayList<Read>>(MAX_CAPACITY) : null);
47
48 assert(readstream1==null || readstream1.read1==true);
49 assert(readstream2==null || (readstream2.read1==false));
50 }
51
52 @Override
53 public synchronized void start(){
54 if(started){
55 System.err.println("Resetting output stream.");
56 nextListID=0;
57 throw new RuntimeException();
58 }else{
59 started=true;
60 if(readstream1!=null){readstream1.start();}
61 if(readstream2!=null){readstream2.start();}
62 }
63 }
64
65 /*--------------------------------------------------------------*/
66 /*---------------- Outer Methods ----------------*/
67 /*--------------------------------------------------------------*/
68
69 @Override
70 public synchronized void add(ArrayList<Read> list, long listnum){
71
72 if(ordered){
73 int size=table.size();
74 // System.err.print(size+", ");
75 final boolean flag=(size>=HALF_LIMIT);
76 if(listnum>nextListID && size>=ADD_LIMIT){
77 if(printBufferNotification){
78 System.err.println("Output buffer became full; key "+listnum+" waiting on "+nextListID+".");
79 printBufferNotification=false;
80 }
81 while(listnum>nextListID && size>=HALF_LIMIT){
82 try {
83 this.wait(20000);
84 } catch (InterruptedException e) {
85 e.printStackTrace();
86 }
87 size=table.size();
88 }
89 if(printBufferNotification){
90 System.err.println("Output buffer became clear for key "+listnum+"; next="+nextListID+", size="+size);
91 }
92 }
93 addOrdered(list, listnum);
94 assert(listnum!=nextListID);
95 if(flag && listnum<nextListID){this.notifyAll();}
96 }else{
97 addDisordered(list, listnum);
98 }
99 }
100
101 @Override
102 public synchronized void close(){
103
104 if(table!=null && !table.isEmpty()){
105 errorState=true;
106 System.err.println("Error: An unfinished ReadOutputStream was closed.");
107 }
108 //assert(table==null || table.isEmpty()); //Seems like a race condition. Probably, I should wait at this point until the condition is true before proceeding.
109
110 // readstream1.addList(null);
111 // if(readstream2!=null){readstream2.addList(null);}
112 readstream1.poison();
113 if(readstream2!=null){readstream2.poison();}
114 }
115
116 @Override
117 public void join(){
118 while(readstream1!=null && readstream1.getState()!=Thread.State.TERMINATED){
119 try {
120 readstream1.join();
121 } catch (InterruptedException e) {
122 // TODO Auto-generated catch block
123 e.printStackTrace();
124 }
125 }
126 while(readstream2!=null && readstream2.getState()!=Thread.State.TERMINATED){
127 try {
128 if(readstream2!=null){readstream2.join();}
129 } catch (InterruptedException e) {
130 // TODO Auto-generated catch block
131 e.printStackTrace();
132 }
133 }
134 assert(table==null || table.isEmpty());
135 finishedSuccessfully=true;
136 }
137
138 @Override
139 public synchronized void resetNextListID(){
140 for(int i=0; i<2000 && !table.isEmpty(); i++){
141 try {this.wait(2000);}
142 catch (InterruptedException e) {e.printStackTrace();}
143 }
144 if(!table.isEmpty()){
145 System.err.println("WARNING! resetNextListID() waited a long time and the table never cleared. Process may have stalled.");
146 }
147 while(!table.isEmpty()){
148 try {this.wait(2000);}
149 catch (InterruptedException e) {e.printStackTrace();}
150 }
151 nextListID=0;
152 }
153
154 @Override
155 public final String fname(){
156 // if(STANDARD_OUT){return "stdout";}
157 return readstream1.fname();
158 }
159
160 @Override
161 public boolean errorState(){
162 return errorState || (readstream1!=null && readstream1.errorState()) || (readstream2!=null && readstream2.errorState());
163 }
164
165 @Override
166 public boolean finishedSuccessfully(){
167 return finishedSuccessfully && (readstream1==null || readstream1.finishedSuccessfully()) && (readstream2==null || readstream2.finishedSuccessfully());
168 }
169
170 /*--------------------------------------------------------------*/
171 /*---------------- Inner Methods ----------------*/
172 /*--------------------------------------------------------------*/
173
174
175 private synchronized void addOrdered(ArrayList<Read> list, long listnum){
176 // System.err.println("RTOS got "+listnum+" of size "+(list==null ? "null" : list.size())+
177 // " with first read id "+(list==null || list.isEmpty() || list.get(0)==null ? "null" : ""+list.get(0).numericID));
178 assert(list!=null) : listnum;
179 assert(listnum>=nextListID) : listnum+", "+nextListID;
180 // assert(list.isEmpty() || list.get(0)==null || list.get(0).numericID>=nextReadID) : list.get(0).numericID+", "+nextReadID;
181 assert(!table.containsKey(listnum));
182
183 table.put(listnum, new ArrayList<Read>(list));
184
185 while(table.containsKey(nextListID)){
186 // System.err.println("Writing list "+first.get(0).numericID);
187 ArrayList<Read> value=table.remove(nextListID);
188 write(value);
189 nextListID++;
190 }
191 if(table.isEmpty()){notifyAll();}
192 }
193
194 private synchronized void addDisordered(ArrayList<Read> list, long listnum){
195 assert(list!=null);
196 assert(table==null);
197 write(new ArrayList<Read>(list));
198 }
199
200 private synchronized void write(ArrayList<Read> list){
201 if(readstream1!=null){
202 if(readstream1.getState()==State.TERMINATED){throw new RuntimeException("Writing to a terminated thread.");}
203 readstream1.addList(list);
204 }
205 if(readstream2!=null){
206 if(readstream1.getState()==State.TERMINATED){throw new RuntimeException("Writing to a terminated thread.");}
207 readstream2.addList(list);
208 }
209 }
210
211 /*--------------------------------------------------------------*/
212 /*---------------- Getters ----------------*/
213 /*--------------------------------------------------------------*/
214
215 @Override
216 public final ReadStreamWriter getRS1(){return readstream1;}
217 @Override
218 public final ReadStreamWriter getRS2(){return readstream2;}
219
220 /*--------------------------------------------------------------*/
221 /*---------------- Fields ----------------*/
222 /*--------------------------------------------------------------*/
223
224 private final ReadStreamWriter readstream1;
225 private final ReadStreamWriter readstream2;
226 private long nextListID=0;
227
228 /** Number of lists held before the stream blocks */
229 private final int MAX_CAPACITY=256;
230 private final int ADD_LIMIT=MAX_CAPACITY-2;
231 private final int HALF_LIMIT=ADD_LIMIT/2;
232
233 /** For ordered output */
234 private final HashMap<Long, ArrayList<Read>> table;
235
236 {if(HALF_LIMIT<1){throw new RuntimeException("Capacity too low.");}}
237
238 /*--------------------------------------------------------------*/
239 /*---------------- Static Fields ----------------*/
240 /*--------------------------------------------------------------*/
241
242 private boolean printBufferNotification=true;
243
244 }