Skip to content

Commit 758b038

Browse files
committed
data generator
1 parent ccd8078 commit 758b038

2 files changed

Lines changed: 135 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Eclipse RDF4J contributors.
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Distribution License v1.0
6+
* which accompanies this distribution, and is available at
7+
* http://www.eclipse.org/org/documents/edl-v10.php.
8+
*
9+
* SPDX-License-Identifier: BSD-3-Clause
10+
*******************************************************************************/
11+
// Some portions generated by Codex
12+
package org.eclipse.rdf4j.benchmark.common;
13+
14+
import java.io.IOException;
15+
import java.io.OutputStream;
16+
import java.io.UncheckedIOException;
17+
import java.nio.file.AtomicMoveNotSupportedException;
18+
import java.nio.file.Files;
19+
import java.nio.file.Path;
20+
import java.nio.file.StandardCopyOption;
21+
import java.util.Locale;
22+
import java.util.Objects;
23+
24+
import org.eclipse.rdf4j.benchmark.rio.util.ThemeDataSetGenerator;
25+
import org.eclipse.rdf4j.benchmark.rio.util.ThemeDataSetGenerator.Theme;
26+
import org.eclipse.rdf4j.rio.RDFFormat;
27+
import org.eclipse.rdf4j.rio.Rio;
28+
29+
public final class ThemeDataSetCache {
30+
31+
public static final String CACHE_DIR_PROPERTY = "rdf4j.benchmark.themeCacheDir";
32+
33+
private static final String DEFAULT_CACHE_DIR_NAME = "rdf4j-theme-benchmark-cache";
34+
35+
private ThemeDataSetCache() {
36+
}
37+
38+
public static Path getOrCreateNQuads(Theme theme) {
39+
Path cacheDir = resolveCacheDir();
40+
return getOrCreateNQuads(theme, cacheDir);
41+
}
42+
43+
public static Path getOrCreateNQuads(Theme theme, Path cacheDir) {
44+
Objects.requireNonNull(theme, "theme");
45+
Objects.requireNonNull(cacheDir, "cacheDir");
46+
47+
try {
48+
Files.createDirectories(cacheDir);
49+
Path cacheFile = cacheDir.resolve(fileName(theme));
50+
if (Files.exists(cacheFile) && Files.size(cacheFile) > 0) {
51+
return cacheFile;
52+
}
53+
54+
Path tempFile = Files.createTempFile(cacheDir, fileStem(theme), ".tmp");
55+
try (OutputStream output = Files.newOutputStream(tempFile)) {
56+
ThemeDataSetGenerator.generate(theme, Rio.createWriter(RDFFormat.NQUADS, output));
57+
}
58+
try {
59+
Files.move(tempFile, cacheFile, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
60+
} catch (AtomicMoveNotSupportedException e) {
61+
Files.move(tempFile, cacheFile, StandardCopyOption.REPLACE_EXISTING);
62+
}
63+
return cacheFile;
64+
} catch (IOException e) {
65+
throw new UncheckedIOException("Failed to cache theme dataset for " + theme, e);
66+
}
67+
}
68+
69+
private static Path resolveCacheDir() {
70+
String configured = System.getProperty(CACHE_DIR_PROPERTY);
71+
if (configured != null && !configured.isBlank()) {
72+
return Path.of(configured);
73+
}
74+
return Path.of(System.getProperty("java.io.tmpdir"), DEFAULT_CACHE_DIR_NAME);
75+
}
76+
77+
private static String fileName(Theme theme) {
78+
return fileStem(theme) + ".nq";
79+
}
80+
81+
private static String fileStem(Theme theme) {
82+
return theme.name().toLowerCase(Locale.ROOT);
83+
}
84+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Eclipse RDF4J contributors.
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Distribution License v1.0
6+
* which accompanies this distribution, and is available at
7+
* http://www.eclipse.org/org/documents/edl-v10.php.
8+
*
9+
* SPDX-License-Identifier: BSD-3-Clause
10+
*******************************************************************************/
11+
// Some portions generated by Codex
12+
package org.eclipse.rdf4j.benchmark.common;
13+
14+
import static org.junit.jupiter.api.Assertions.assertFalse;
15+
import static org.junit.jupiter.api.Assertions.assertNotNull;
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
17+
18+
import java.io.InputStream;
19+
import java.lang.reflect.Method;
20+
import java.nio.file.Files;
21+
import java.nio.file.Path;
22+
23+
import org.apache.commons.io.FileUtils;
24+
import org.eclipse.rdf4j.benchmark.rio.util.ThemeDataSetGenerator.Theme;
25+
import org.eclipse.rdf4j.model.Model;
26+
import org.eclipse.rdf4j.rio.RDFFormat;
27+
import org.eclipse.rdf4j.rio.Rio;
28+
import org.junit.jupiter.api.Test;
29+
30+
class ThemeDataSetCacheTest {
31+
32+
@Test
33+
void cachesNQuadsToDisk() throws Exception {
34+
Path tempDir = Files.createTempDirectory("theme-cache");
35+
try {
36+
Class<?> cacheClass = Class.forName("org.eclipse.rdf4j.benchmark.common.ThemeDataSetCache");
37+
Method method = cacheClass.getMethod("getOrCreateNQuads", Theme.class, Path.class);
38+
Path cacheFile = (Path) method.invoke(null, Theme.SOCIAL_MEDIA, tempDir);
39+
assertNotNull(cacheFile, "Cache file should be returned");
40+
assertTrue(cacheFile.toString().endsWith(".nq"), "Cache should be a .nq file");
41+
assertTrue(Files.exists(cacheFile), "Cache file should exist");
42+
assertTrue(Files.size(cacheFile) > 0, "Cache file should be non-empty");
43+
try (InputStream input = Files.newInputStream(cacheFile)) {
44+
Model model = Rio.parse(input, "", RDFFormat.NQUADS);
45+
assertFalse(model.isEmpty(), "Cached model should have statements");
46+
}
47+
} finally {
48+
FileUtils.deleteDirectory(tempDir.toFile());
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)