Skip to content

Commit 070f3c5

Browse files
committed
chore(logging): enhance logging messages for RDF processing and error handling
1 parent 9a161e9 commit 070f3c5

2 files changed

Lines changed: 40 additions & 11 deletions

File tree

src/main/java/net/trustyuri/rdf/CheckLargeRdf.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public static void main(String[] args) throws IOException, TrustyUriException {
3939
CheckLargeRdf t = new CheckLargeRdf(file);
4040
boolean valid = t.check();
4141
if (valid) {
42-
logger.info("Correct hash: {}", t.ac.toString());
42+
logger.info("Hash is correct for file: {}", file.getAbsolutePath() + " with artifact code: " + t.ac.toString());
4343
} else {
44-
logger.error("*** INCORRECT HASH ***");
44+
logger.error("Hash is incorrect for file: {}", file.getAbsolutePath());
4545
}
4646
}
4747

@@ -83,7 +83,7 @@ public void handleStatement(Statement st) throws RDFHandlerException {
8383
try {
8484
preOut.write(s.getBytes(StandardCharsets.UTF_8));
8585
} catch (IOException ex) {
86-
ex.printStackTrace();
86+
logger.error("Error writing to temporary file for artifact code {}: {}", r.getArtifactCode(), ex.getMessage());
8787
}
8888
}
8989

src/main/java/net/trustyuri/rdf/TransformLargeRdf.java

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.eclipse.rdf4j.model.Statement;
1010
import org.eclipse.rdf4j.rio.*;
1111
import org.eclipse.rdf4j.rio.helpers.AbstractRDFHandler;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
1214

1315
import java.io.*;
1416
import java.nio.charset.Charset;
@@ -23,6 +25,8 @@
2325
*/
2426
public class TransformLargeRdf {
2527

28+
private static final Logger logger = LoggerFactory.getLogger(TransformLargeRdf.class);
29+
2630
/**
2731
* Transforms the given RDF file.
2832
*
@@ -69,11 +73,13 @@ public TransformLargeRdf(File inputFile, String baseName) {
6973
* @throws TrustyUriException if there is an error with the trusty URI, for example if the file is not a valid RDF file or if the base name is invalid
7074
*/
7175
public IRI transform(TransformRdfSetting setting) throws IOException, TrustyUriException {
76+
logger.info("Starting RDF transformation: input='{}', baseName='{}'", inputFile, baseName);
7277
baseUri = TransformRdf.getBaseURI(baseName);
7378
md = RdfHasher.getDigest();
7479
inputDir = inputFile.getParent();
7580
TrustyUriResource r = new TrustyUriResource(inputFile);
7681
RDFFormat format = r.getFormat(RDFFormat.TURTLE);
82+
logger.info("Detected RDF format: {}", format);
7783

7884
String name = baseName;
7985
if (baseName.indexOf("/") > 0) {
@@ -87,6 +93,8 @@ public IRI transform(TransformRdfSetting setting) throws IOException, TrustyUriE
8793

8894
RDFParser p = RdfUtils.getParser(format);
8995
File sortInFile = new File(inputDir, fileName + ".temp.sort-in");
96+
logger.info("Preprocessing RDF into sortable form: {}", sortInFile);
97+
9098
final FileOutputStream preOut = new FileOutputStream(sortInFile);
9199
p.setRDFHandler(new RdfPreprocessor(new AbstractRDFHandler() {
92100

@@ -96,7 +104,8 @@ public void handleStatement(Statement st) throws RDFHandlerException {
96104
try {
97105
preOut.write(s.getBytes());
98106
} catch (IOException ex) {
99-
ex.printStackTrace();
107+
logger.error("Failed to write preprocessed statement to temp file: {}", sortInFile, ex);
108+
throw new RDFHandlerException("Error writing preprocessed RDF", ex);
100109
}
101110
}
102111

@@ -105,6 +114,7 @@ public void handleStatement(Statement st) throws RDFHandlerException {
105114
try {
106115
p.parse(reader, "");
107116
} catch (RDF4JException ex) {
117+
logger.error("Failed to parse RDF input file: {}", inputFile, ex);
108118
throw new TrustyUriException(ex);
109119
} finally {
110120
reader.close();
@@ -113,15 +123,26 @@ public void handleStatement(Statement st) throws RDFHandlerException {
113123

114124
File sortOutFile = new File(inputDir, fileName + ".temp.sort-out");
115125
File sortTempDir = new File(inputDir, fileName + ".temp");
116-
sortTempDir.mkdir();
126+
if (!sortTempDir.mkdir()) {
127+
logger.warn("Could not create temp directory: {}", sortTempDir);
128+
}
129+
130+
logger.info("Sorting statements (external sort)...");
117131
Comparator<String> cmp = new SerStatementComparator();
118132
Charset cs = Charset.defaultCharset();
119133
System.gc();
120134
List<File> tempFiles = ExternalSort.sortInBatch(sortInFile, cmp, 1024, cs, sortTempDir, false);
135+
logger.info("Created {} sorted temp files", tempFiles.size());
136+
121137
ExternalSort.mergeSortedFiles(tempFiles, sortOutFile, cmp, cs);
122-
sortInFile.delete();
123-
sortTempDir.delete();
138+
if (!sortInFile.delete()) {
139+
logger.warn("Failed to delete temp file: {}", sortInFile);
140+
}
141+
if (!sortTempDir.delete()) {
142+
logger.warn("Failed to delete temp dir: {}", sortTempDir);
143+
}
124144

145+
logger.info("Hashing sorted statements...");
125146
BufferedReader br = new BufferedReader(new FileReader(sortOutFile));
126147
String line;
127148
Statement previous = null;
@@ -133,6 +154,7 @@ public void handleStatement(Statement st) throws RDFHandlerException {
133154
previous = st;
134155
}
135156
br.close();
157+
logger.info("Hashing completed.");
136158

137159
ArtifactCode artifactCode = RdfHasher.getArtifactCode(md);
138160
String acFileName = fileName;
@@ -141,11 +163,16 @@ public void handleStatement(Statement st) throws RDFHandlerException {
141163
} else {
142164
acFileName += "." + artifactCode + ext;
143165
}
166+
File outputFile;
144167
OutputStream out;
145168
if (inputFile.getName().matches(".*\\.(gz|gzip)")) {
146-
out = new GZIPOutputStream(new FileOutputStream(new File(inputDir, acFileName + ".gz")));
169+
outputFile = new File(inputDir, acFileName + ".gz");
170+
logger.info("Writing compressed output: {}", outputFile);
171+
out = new GZIPOutputStream(new FileOutputStream(outputFile));
147172
} else {
148-
out = new FileOutputStream(new File(inputDir, acFileName));
173+
outputFile = new File(inputDir, acFileName);
174+
logger.info("Writing output: {}", outputFile);
175+
out = new FileOutputStream(outputFile);
149176
}
150177
RDFWriter writer = Rio.createWriter(format, new OutputStreamWriter(out, StandardCharsets.UTF_8));
151178
final HashAdder replacer = new HashAdder(baseUri, artifactCode, writer, null);
@@ -163,13 +190,15 @@ public void handleStatement(Statement st) throws RDFHandlerException {
163190
}
164191
replacer.endRDF();
165192
} catch (RDFHandlerException ex) {
193+
logger.error("Failed during RDF writing phase", ex);
166194
throw new TrustyUriException(ex);
167195
} finally {
168196
br.close();
169-
sortOutFile.delete();
197+
if (!sortOutFile.delete()) {
198+
logger.warn("Failed to delete temp file: {}", sortOutFile);
199+
}
170200
}
171201
out.close();
172-
173202
return RdfUtils.getTrustyUri(baseUri, artifactCode.toString(), setting);
174203
}
175204

0 commit comments

Comments
 (0)