|
| 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 | +} |
0 commit comments