34
34
35
35
/**
36
36
* @author markus geipel
37
- *
37
+ *
38
38
*/
39
39
public abstract class AbstractTripleSort extends DefaultObjectPipe <Triple , ObjectReceiver <Triple >> implements Listener {
40
40
/**
@@ -46,7 +46,7 @@ public enum Compare {
46
46
47
47
/**
48
48
* sort order
49
- *
49
+ *
50
50
*/
51
51
public enum Order {
52
52
INCREASING {
@@ -98,8 +98,10 @@ protected final void setSortOrder(final Order order) {
98
98
public final void process (final Triple namedValue ) {
99
99
if (memoryLow ) {
100
100
try {
101
- nextBatch ();
102
- } catch (IOException e ) {
101
+ if (!buffer .isEmpty ()) {
102
+ nextBatch ();
103
+ }
104
+ } catch (final IOException e ) {
103
105
throw new MetafactureException ("Error writing to temp file after sorting" , e );
104
106
} finally {
105
107
memoryLow = false ;
@@ -115,7 +117,7 @@ private void nextBatch() throws IOException {
115
117
final ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream (tempFile ));
116
118
117
119
try {
118
- for (Triple triple : buffer ) {
120
+ for (final Triple triple : buffer ) {
119
121
triple .write (out );
120
122
}
121
123
} finally {
@@ -130,7 +132,7 @@ public final void onCloseStream() {
130
132
131
133
if (tempFiles .isEmpty ()) {
132
134
Collections .sort (buffer , createComparator (compare , order ));
133
- for (Triple triple : buffer ) {
135
+ for (final Triple triple : buffer ) {
134
136
sortedTriple (triple );
135
137
}
136
138
onFinished ();
@@ -148,7 +150,7 @@ public int compare(final SortedTripleFileFacade o1, final SortedTripleFileFacade
148
150
});
149
151
try {
150
152
nextBatch ();
151
- for (File file : tempFiles ) {
153
+ for (final File file : tempFiles ) {
152
154
queue .add (new SortedTripleFileFacade (file ));
153
155
}
154
156
@@ -163,10 +165,10 @@ public int compare(final SortedTripleFileFacade o1, final SortedTripleFileFacade
163
165
}
164
166
}
165
167
onFinished ();
166
- } catch (IOException e ) {
168
+ } catch (final IOException e ) {
167
169
throw new MetafactureException ("Error merging temp files" , e );
168
170
} finally {
169
- for (SortedTripleFileFacade sortedFileFacade : queue ) {
171
+ for (final SortedTripleFileFacade sortedFileFacade : queue ) {
170
172
sortedFileFacade .close ();
171
173
}
172
174
}
@@ -228,7 +230,7 @@ public int compare(final Triple o1, final Triple o2) {
228
230
@ Override
229
231
public final void onResetStream () {
230
232
buffer .clear ();
231
- for (File file : tempFiles ) {
233
+ for (final File file : tempFiles ) {
232
234
if (file .exists ()) {
233
235
file .delete ();
234
236
}
0 commit comments