Skip to content

Commit 969a803

Browse files
committed
Added TripleReader and TripleWriter modules to read and write triples
from and to a file
1 parent a797c03 commit 969a803

File tree

4 files changed

+209
-1
lines changed

4 files changed

+209
-1
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.sink;
17+
18+
import java.io.BufferedOutputStream;
19+
import java.io.FileOutputStream;
20+
import java.io.IOException;
21+
import java.io.ObjectOutputStream;
22+
23+
import org.culturegraph.mf.exceptions.MetafactureException;
24+
import org.culturegraph.mf.framework.DefaultObjectReceiver;
25+
import org.culturegraph.mf.types.Triple;
26+
27+
/**
28+
* @author Christoph Böhme
29+
*
30+
*/
31+
public final class TripleWriter extends DefaultObjectReceiver<Triple> {
32+
33+
public static final int BUFFERSIZE = 2048;
34+
35+
private final String filename;
36+
37+
private ObjectOutputStream outputStream;
38+
39+
public TripleWriter(final String filename) {
40+
this.filename = filename;
41+
resetStream();
42+
}
43+
44+
@Override
45+
public void process(final Triple obj) {
46+
try {
47+
obj.write(outputStream);
48+
} catch (IOException e) {
49+
throw new MetafactureException(e);
50+
}
51+
}
52+
53+
@Override
54+
public void resetStream() {
55+
try {
56+
if (outputStream != null) {
57+
outputStream.close();
58+
}
59+
outputStream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(filename), BUFFERSIZE));
60+
} catch (IOException e) {
61+
throw new MetafactureException(e);
62+
}
63+
}
64+
65+
@Override
66+
public void closeStream() {
67+
try {
68+
if (outputStream != null) {
69+
outputStream.close();
70+
}
71+
} catch (IOException e) {
72+
throw new MetafactureException(e);
73+
}
74+
}
75+
76+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.source;
17+
18+
import java.io.BufferedInputStream;
19+
import java.io.EOFException;
20+
import java.io.FileInputStream;
21+
import java.io.IOException;
22+
import java.io.ObjectInputStream;
23+
24+
import org.culturegraph.mf.exceptions.MetafactureException;
25+
import org.culturegraph.mf.framework.DefaultObjectPipe;
26+
import org.culturegraph.mf.framework.ObjectReceiver;
27+
import org.culturegraph.mf.types.Triple;
28+
29+
/**
30+
* @author Christoph Böhme
31+
*
32+
*/
33+
public final class TripleReader extends
34+
DefaultObjectPipe<String, ObjectReceiver<Triple>> {
35+
36+
public static final int BUFFERSIZE = 2048;
37+
38+
@Override
39+
public void process(final String filename) {
40+
try {
41+
final ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(filename), BUFFERSIZE));
42+
43+
try {
44+
while (true) {
45+
getReceiver().process(Triple.read(in));
46+
}
47+
} catch (EOFException e) {
48+
} finally {
49+
in.close();
50+
}
51+
} catch (IOException e) {
52+
throw new MetafactureException(e);
53+
}
54+
}
55+
56+
}

src/main/java/org/culturegraph/mf/types/Triple.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.io.ObjectInputStream;
2020
import java.io.ObjectOutputStream;
2121

22+
import org.culturegraph.mf.exceptions.ShouldNeverHappenException;
23+
2224
/**
2325
* Stores an immutable name-value-pair. The hash code is precomputed during
2426
* instantiation.
@@ -104,13 +106,18 @@ public String getSubject() {
104106
}
105107

106108
public static Triple read(final ObjectInputStream in) throws IOException {
107-
return new Triple(in.readUTF(), in.readUTF(), in.readUTF());
109+
try {
110+
return new Triple(in.readUTF(), in.readUTF(), in.readUTF(), (ObjectType) in.readObject());
111+
} catch (ClassNotFoundException e) {
112+
throw new ShouldNeverHappenException(e);
113+
}
108114
}
109115

110116
public void write(final ObjectOutputStream out) throws IOException {
111117
out.writeUTF(subject);
112118
out.writeUTF(predicate);
113119
out.writeUTF(object);
120+
out.writeObject(objectType);
114121
}
115122

116123
@Override
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.sink;
17+
18+
import static org.mockito.Mockito.verify;
19+
20+
import java.io.File;
21+
import java.io.IOException;
22+
23+
import org.culturegraph.mf.framework.ObjectReceiver;
24+
import org.culturegraph.mf.stream.source.TripleReader;
25+
import org.culturegraph.mf.types.Triple;
26+
import org.culturegraph.mf.types.Triple.ObjectType;
27+
import org.junit.Rule;
28+
import org.junit.Test;
29+
import org.junit.rules.TemporaryFolder;
30+
import org.mockito.Mock;
31+
import org.mockito.MockitoAnnotations;
32+
33+
/**
34+
* @author Christoph Böhme
35+
*
36+
*/
37+
public final class TripleReaderWriterTest {
38+
39+
private static final Triple TRIPLE1 = new Triple("S", "P", "O1");
40+
private static final Triple TRIPLE2 = new Triple("S", "P", "O2", ObjectType.ENTITY);
41+
42+
// NO CHECKSTYLE VisibilityModifier FOR 3 LINES:
43+
// JUnit requires rules to be public
44+
@Rule
45+
public TemporaryFolder tempFolder = new TemporaryFolder();
46+
47+
@Mock
48+
private ObjectReceiver<Triple> receiver;
49+
50+
@Test
51+
public void testShouldWriteAndReadTriples() throws IOException {
52+
MockitoAnnotations.initMocks(this);
53+
54+
final File file = tempFolder.newFile();
55+
final TripleWriter tripleWriter = new TripleWriter(file.getAbsolutePath());
56+
final TripleReader tripleReader = new TripleReader();
57+
tripleReader.setReceiver(receiver);
58+
59+
tripleWriter.process(TRIPLE1);
60+
tripleWriter.process(TRIPLE2);
61+
tripleWriter.closeStream();
62+
63+
tripleReader.process(file.getAbsolutePath());
64+
65+
verify(receiver).process(TRIPLE1);
66+
verify(receiver).process(TRIPLE2);
67+
}
68+
69+
}

0 commit comments

Comments
 (0)