Skip to content

Commit bf02f92

Browse files
committed
Merge pull request #169 from cboehme/fix-issue-160
fix #160
2 parents 6cd529f + 097e11a commit bf02f92

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

src/main/java/org/culturegraph/mf/stream/pipe/sort/AbstractTripleSort.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
/**
3636
* @author markus geipel
37-
*
37+
*
3838
*/
3939
public abstract class AbstractTripleSort extends DefaultObjectPipe<Triple, ObjectReceiver<Triple>> implements Listener {
4040
/**
@@ -46,7 +46,7 @@ public enum Compare {
4646

4747
/**
4848
* sort order
49-
*
49+
*
5050
*/
5151
public enum Order {
5252
INCREASING {
@@ -98,8 +98,10 @@ protected final void setSortOrder(final Order order) {
9898
public final void process(final Triple namedValue) {
9999
if (memoryLow) {
100100
try {
101-
nextBatch();
102-
} catch (IOException e) {
101+
if (!buffer.isEmpty()) {
102+
nextBatch();
103+
}
104+
} catch (final IOException e) {
103105
throw new MetafactureException("Error writing to temp file after sorting", e);
104106
} finally {
105107
memoryLow = false;
@@ -115,7 +117,7 @@ private void nextBatch() throws IOException {
115117
final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(tempFile));
116118

117119
try {
118-
for (Triple triple : buffer) {
120+
for (final Triple triple : buffer) {
119121
triple.write(out);
120122
}
121123
} finally {
@@ -130,7 +132,7 @@ public final void onCloseStream() {
130132

131133
if (tempFiles.isEmpty()) {
132134
Collections.sort(buffer, createComparator(compare, order));
133-
for (Triple triple : buffer) {
135+
for (final Triple triple : buffer) {
134136
sortedTriple(triple);
135137
}
136138
onFinished();
@@ -148,7 +150,7 @@ public int compare(final SortedTripleFileFacade o1, final SortedTripleFileFacade
148150
});
149151
try {
150152
nextBatch();
151-
for (File file : tempFiles) {
153+
for (final File file : tempFiles) {
152154
queue.add(new SortedTripleFileFacade(file));
153155
}
154156

@@ -163,10 +165,10 @@ public int compare(final SortedTripleFileFacade o1, final SortedTripleFileFacade
163165
}
164166
}
165167
onFinished();
166-
} catch (IOException e) {
168+
} catch (final IOException e) {
167169
throw new MetafactureException("Error merging temp files", e);
168170
} finally {
169-
for (SortedTripleFileFacade sortedFileFacade : queue) {
171+
for (final SortedTripleFileFacade sortedFileFacade : queue) {
170172
sortedFileFacade.close();
171173
}
172174
}
@@ -228,7 +230,7 @@ public int compare(final Triple o1, final Triple o2) {
228230
@Override
229231
public final void onResetStream() {
230232
buffer.clear();
231-
for (File file : tempFiles) {
233+
for (final File file : tempFiles) {
232234
if (file.exists()) {
233235
file.delete();
234236
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2013 Deutsche Nationalbibliothek
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.culturegraph.mf.stream.pipe.sort;
17+
18+
import org.culturegraph.mf.types.Triple;
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
22+
/**
23+
* Tests for class {@link AbstractTripleSort}.
24+
*
25+
* @author Christoph Böhme
26+
*
27+
*/
28+
public final class AbstractTripleSortTest {
29+
30+
private static final Triple T1 = new Triple("s", "p", "o");
31+
32+
// NO CHECKSTYLE IllegalType FOR 3 LINES:
33+
// AbstractFormatter is the system under test. To keep the test
34+
// case concise no named mock implementation is created.
35+
private AbstractTripleSort tripleSort;
36+
37+
@Before
38+
public void setup() {
39+
tripleSort = new AbstractTripleSort() {
40+
@Override
41+
protected void sortedTriple(final Triple namedValue) {}
42+
};
43+
}
44+
45+
@Test
46+
public void shouldNotFailIfFlushingBeforeFirstRecord() {
47+
tripleSort.memoryLow(0, 0);
48+
tripleSort.process(T1);
49+
tripleSort.closeStream();
50+
}
51+
52+
}

0 commit comments

Comments
 (0)