• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

knowledgepixels / nanopub-query / 27531824842

15 Jun 2026 07:49AM UTC coverage: 59.58% (-0.3%) from 59.887%
27531824842

push

github

ashleycaselli
refactor(NanopubLoader): enhance logging messages for clarity and consistency

480 of 896 branches covered (53.57%)

Branch coverage included in aggregate %.

1420 of 2293 relevant lines covered (61.93%)

9.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.6
src/main/java/com/knowledgepixels/query/NanopubLoader.java
1
package com.knowledgepixels.query;
2

3
import net.trustyuri.TrustyUriUtils;
4
import org.apache.http.client.HttpClient;
5
import org.apache.http.impl.client.HttpClientBuilder;
6
import org.eclipse.rdf4j.common.exception.RDF4JException;
7
import org.eclipse.rdf4j.common.transaction.IsolationLevels;
8
import org.eclipse.rdf4j.model.*;
9
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
10
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
11
import org.eclipse.rdf4j.model.vocabulary.RDFS;
12
import org.eclipse.rdf4j.query.BindingSet;
13
import org.eclipse.rdf4j.query.QueryLanguage;
14
import org.eclipse.rdf4j.query.TupleQuery;
15
import org.eclipse.rdf4j.query.TupleQueryResult;
16
import org.eclipse.rdf4j.repository.RepositoryConnection;
17
import org.eclipse.rdf4j.repository.RepositoryResult;
18
import org.nanopub.Nanopub;
19
import org.nanopub.NanopubUtils;
20
import org.nanopub.SimpleCreatorPattern;
21
import org.nanopub.SimpleTimestampPattern;
22
import org.nanopub.extra.security.KeyDeclaration;
23
import org.nanopub.extra.security.MalformedCryptoElementException;
24
import org.nanopub.extra.security.NanopubSignatureElement;
25
import org.nanopub.extra.security.SignatureUtils;
26
import org.nanopub.extra.server.GetNanopub;
27
import org.nanopub.extra.setting.IntroNanopub;
28
import org.nanopub.vocabulary.NP;
29
import org.nanopub.vocabulary.NPA;
30
import org.nanopub.vocabulary.NPX;
31
import org.nanopub.vocabulary.PAV;
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

35
import java.security.GeneralSecurityException;
36
import java.util.*;
37
import java.util.concurrent.*;
38
import java.util.function.Consumer;
39

40
/**
41
 * Utility class for loading nanopublications into the database.
42
 */
43
public class NanopubLoader {
44

45
    private static HttpClient httpClient;
46
    private static final ThreadPoolExecutor loadingPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(4);
12✔
47

48
    /**
49
     * Cached count of nanopubs ever loaded into the {@code meta} repo. Maintained
50
     * for {@link MainVerticle}'s {@code Nanopub-Query-Loaded-Nanopub-Count}
51
     * response header. Mirrors the persisted {@code npa:hasNanopubCount} triple
52
     * that {@link #loadNanopubToRepo} maintains; invalidations don't decrement
53
     * (they're recorded as separate {@code npx:invalidates} markers), so this
54
     * is a cumulative count including superseded/retracted nanopubs — matching
55
     * the registry-side {@code Nanopub-Registry-Nanopub-Count} semantics.
56
     *
57
     * <p>The {@code meta} repo (not {@code full}) is the source because the meta
58
     * task is submitted only after all other per-nanopub tasks succeed (see
59
     * {@link #executeLoading}), making it the authoritative "fully completed
60
     * loads" indicator. The {@code full} repo is feature-flagged and may be
61
     * disabled, in which case it cannot be the source. Populated lazily on first
62
     * read; bumped post-commit on each fresh meta load.
63
     */
64
    static volatile Long loadedNanopubCount = null;
6✔
65

66
    /**
67
     * Cached checksum of nanopubs ever loaded into the {@code meta} repo.
68
     * Maintained for {@link MainVerticle}'s
69
     * {@code Nanopub-Query-Loaded-Nanopub-Checksum} response header. Mirrors the
70
     * persisted {@code npa:hasNanopubChecksum} triple that {@link #loadNanopubToRepo}
71
     * maintains — an order-independent XOR over the trusty URIs of all loaded
72
     * nanopubs, Base64-encoded. Sourced from {@code meta} for the same reasons
73
     * as {@link #loadedNanopubCount}; the two fields are bumped together so the
74
     * count and checksum always describe the same point in the load sequence.
75
     */
76
    static volatile String loadedNanopubChecksum = null;
6✔
77

78
    /**
79
     * Retry budget for the five (with #71 merged: six) structurally identical
80
     * retry loops in this file. Previously the shape was flat {@code 10 s × 30} —
81
     * five minutes of constant hammering at RDF4J that did not help a slow server.
82
     * The new shape is bounded exponential backoff with ±50 % jitter:
83
     * {@code base = 1, 2, 4, 8, 16, 32, 60, 60 s} for attempts 1…8, each perturbed
84
     * by up to half its base value. Jitter prevents the 4-thread loadingPool from
85
     * retrying in lock-step after a shared RDF4J failure (GC pause / overload spike).
86
     * Worst-case wall time per failing task drops from ~35 min (post-change-1
87
     * timeouts × 30 flat retries) to ~11 min (8 retries × 60 s timeout + backoff
88
     * sleeps). This is the figure that sets the circuit-breaker trip time in
89
     * {@link JellyNanopubLoader}.
90
     */
91
    private static final int MAX_RETRIES = 8;
92
    private static final long[] BACKOFF_BASE_MS =
105✔
93
            {1_000L, 2_000L, 4_000L, 8_000L, 16_000L, 32_000L, 60_000L, 60_000L};
94

95
    /**
96
     * Returns the sleep delay in ms for the given 1-indexed retry attempt. Delay
97
     * is {@link #BACKOFF_BASE_MS}{@code [attempt-1]} perturbed by ±50 % uniform
98
     * jitter, clamped to be non-negative.
99
     *
100
     * @param attempt 1-indexed retry attempt number
101
     * @return the computed sleep delay in ms
102
     */
103
    static long computeBackoffMillis(int attempt) {
104
        long base = BACKOFF_BASE_MS[Math.min(attempt - 1, BACKOFF_BASE_MS.length - 1)];
×
105
        long jitter = ThreadLocalRandom.current().nextLong(base + 1) - base / 2;
×
106
        return Math.max(0L, base + jitter);
×
107
    }
108

109
    private Nanopub np;
110
    private NanopubSignatureElement el = null;
9✔
111
    private List<Statement> metaStatements = new ArrayList<>();
15✔
112
    private List<Statement> nanopubStatements = new ArrayList<>();
15✔
113
    private List<Statement> literalStatements = new ArrayList<>();
15✔
114
    private List<Statement> invalidateStatements = new ArrayList<>();
15✔
115
    private List<Statement> textStatements, allStatements, invalidatingStatements;
116
    private List<Statement> spaceExtractionStatements = new ArrayList<>();
15✔
117
    private Calendar timestamp = null;
9✔
118
    private Statement pubkeyStatement, pubkeyStatementX;
119
    private List<String> notes = new ArrayList<>();
15✔
120
    private boolean aborted = false;
9✔
121
    private static final Logger logger = LoggerFactory.getLogger(NanopubLoader.class);
9✔
122

123

124
    NanopubLoader(Nanopub np, long counter) {
6✔
125
        this.np = np;
9✔
126
        if (counter >= 0) {
12✔
127
            logger.info("Loading nanopub #{}: <{}>", counter, np.getUri());
24✔
128
        } else {
129
            logger.info("Loading nanopub: <{}>", np.getUri());
15✔
130
        }
131

132
        // TODO Ensure proper synchronization and DB rollbacks
133

134
        // TODO Check for null characters ("\0"), which can cause problems in Virtuoso.
135

136
        String ac = TrustyUriUtils.getArtifactCode(np.getUri().toString());
15✔
137
        if (!np.getHeadUri().toString().contains(ac) || !np.getAssertionUri().toString().contains(ac) || !np.getProvenanceUri().toString().contains(ac) || !np.getPubinfoUri().toString().contains(ac)) {
72!
138
            notes.add("could not load nanopub as not all graphs contained the artifact code");
×
139
            aborted = true;
×
140
            return;
×
141
        }
142

143
        try {
144
            el = SignatureUtils.getSignatureElement(np);
12✔
145
        } catch (MalformedCryptoElementException ex) {
×
146
            notes.add("Signature error");
×
147
        }
3✔
148
        if (!hasValidSignature(el)) {
12✔
149
            // Audit trail for the silent-false path: without this, an aborted nanopub
150
            // is invisible in the admin repo and only detectable as a gap between the
151
            // stream counter and the loaded count. See issue around RDF4J-instability
152
            // load gaps (full vs registry, meta vs full) where signature validation
153
            // returned false but no GeneralSecurityException was thrown.
154
            if (notes.isEmpty()) {
12!
155
                notes.add("Invalid signature");
15✔
156
            }
157
            aborted = true;
9✔
158
            return;
3✔
159
        }
160

161
        pubkeyStatement = vf.createStatement(np.getUri(), NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY, vf.createLiteral(el.getPublicKeyString()), NPA.GRAPH);
39✔
162
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasValidSignatureForPublicKey, FULL_PUBKEY, npa:graph, meta, full pubkey if signature is valid
163
        metaStatements.add(pubkeyStatement);
18✔
164
        pubkeyStatementX = vf.createStatement(np.getUri(), NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY_HASH, vf.createLiteral(Utils.createHash(el.getPublicKeyString())), NPA.GRAPH);
42✔
165
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasValidSignatureForPublicKeyHash, PUBKEY_HASH, npa:graph, meta, hex-encoded SHA256 hash if signature is valid
166
        metaStatements.add(pubkeyStatementX);
18✔
167

168
        if (el.getSigners().size() == 1) {  // > 1 is deprecated
18!
169
            metaStatements.add(vf.createStatement(np.getUri(), NPX.SIGNED_BY, el.getSigners().iterator().next(), NPA.GRAPH));
48✔
170
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:signedBy, SIGNER, npa:graph, meta, ID of signer
171
        }
172

173
        Set<IRI> subIris = new HashSet<>();
12✔
174
        Set<IRI> otherNps = new HashSet<>();
12✔
175
        Set<IRI> invalidated = new HashSet<>();
12✔
176
        Set<IRI> retracted = new HashSet<>();
12✔
177
        Set<IRI> superseded = new HashSet<>();
12✔
178
        String combinedLiterals = "";
6✔
179
        for (Statement st : NanopubUtils.getStatements(np)) {
33✔
180
            nanopubStatements.add(st);
15✔
181

182
            if (st.getPredicate().toString().contains(ac)) {
18!
183
                subIris.add(st.getPredicate());
×
184
            } else {
185
                IRI b = getBaseTrustyUri(st.getPredicate());
12✔
186
                if (b != null) {
6!
187
                    otherNps.add(b);
×
188
                }
189
            }
190
            if (st.getPredicate().equals(NPX.RETRACTS) && st.getObject() instanceof IRI) {
15!
191
                retracted.add((IRI) st.getObject());
×
192
            }
193
            if (st.getPredicate().equals(NPX.INVALIDATES) && st.getObject() instanceof IRI) {
15!
194
                invalidated.add((IRI) st.getObject());
×
195
            }
196
            if (st.getSubject().equals(np.getUri()) && st.getObject() instanceof IRI) {
30✔
197
                if (st.getPredicate().equals(NPX.SUPERSEDES)) {
15✔
198
                    superseded.add((IRI) st.getObject());
18✔
199
                }
200
                if (st.getObject().toString().matches(".*[^A-Za-z0-9\\-_]RA[A-Za-z0-9\\-_]{43}")) {
18✔
201
                    metaStatements.add(vf.createStatement(np.getUri(), st.getPredicate(), st.getObject(), NPA.NETWORK_GRAPH));
39✔
202
                    // @ADMIN-TRIPLE-TABLE@ NANOPUB1, RELATION, NANOPUB2, npa:networkGraph, meta, any inter-nanopub relation found in NANOPUB1
203
                }
204
                if (st.getContext().equals(np.getPubinfoUri())) {
18✔
205
                    if (st.getPredicate().equals(NPX.INTRODUCES) || st.getPredicate().equals(NPX.DESCRIBES) || st.getPredicate().equals(NPX.EMBEDS)) {
45!
206
                        metaStatements.add(vf.createStatement(np.getUri(), st.getPredicate(), st.getObject(), NPA.GRAPH));
39✔
207
                        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:introduces, THING, npa:graph, meta, when such a triple is present in pubinfo of NANOPUB
208
                        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:describes, THING, npa:graph, meta, when such a triple is present in pubinfo of NANOPUB
209
                        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:embeds, THING, npa:graph, meta, when such a triple is present in pubinfo of NANOPUB
210
                    }
211
                }
212
            }
213
            if (st.getSubject().toString().contains(ac)) {
18✔
214
                subIris.add((IRI) st.getSubject());
21✔
215
            } else {
216
                IRI b = getBaseTrustyUri(st.getSubject());
12✔
217
                if (b != null) {
6!
218
                    otherNps.add(b);
×
219
                }
220
            }
221
            if (st.getObject() instanceof IRI) {
12✔
222
                if (st.getObject().toString().contains(ac)) {
18✔
223
                    subIris.add((IRI) st.getObject());
21✔
224
                } else {
225
                    IRI b = getBaseTrustyUri(st.getObject());
12✔
226
                    if (b != null) {
6✔
227
                        otherNps.add(b);
12✔
228
                    }
229
                }
3✔
230
            } else {
231
                combinedLiterals += st.getObject().stringValue().replaceAll("\\s+", " ") + "\n";
27✔
232
//                                if (st.getSubject().equals(np.getUri()) && !st.getSubject().equals(HAS_FILTER_LITERAL)) {
233
//                                        literalStatements.add(vf.createStatement(np.getUri(), st.getPredicate(), st.getObject(), LITERAL_GRAPH));
234
//                                } else {
235
//                                        literalStatements.add(vf.createStatement(np.getUri(), HAS_LITERAL, st.getObject(), LITERAL_GRAPH));
236
//                                }
237
            }
238
        }
3✔
239
        subIris.remove(np.getUri());
15✔
240
        subIris.remove(np.getAssertionUri());
15✔
241
        subIris.remove(np.getProvenanceUri());
15✔
242
        subIris.remove(np.getPubinfoUri());
15✔
243
        for (IRI i : subIris) {
30✔
244
            metaStatements.add(vf.createStatement(np.getUri(), NPA.HAS_SUB_IRI, i, NPA.GRAPH));
33✔
245
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasSubIri, SUB_IRI, npa:graph, meta, for any IRI minted in the namespace of the NANOPUB
246
        }
3✔
247
        for (IRI i : otherNps) {
30✔
248
            metaStatements.add(vf.createStatement(np.getUri(), NPA.REFERS_TO_NANOPUB, i, NPA.NETWORK_GRAPH));
33✔
249
            // @ADMIN-TRIPLE-TABLE@ NANOPUB1, npa:refersToNanopub, NANOPUB2, npa:networkGraph, meta, generic inter-nanopub relation
250
        }
3✔
251
        for (IRI i : invalidated) {
18!
252
            invalidateStatements.add(vf.createStatement(np.getUri(), NPX.INVALIDATES, i, NPA.GRAPH));
×
253
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:invalidates, INVALIDATED_NANOPUB, npa:graph, meta, if the NANOPUB retracts or supersedes another nanopub
254
        }
×
255
        for (IRI i : retracted) {
18!
256
            invalidateStatements.add(vf.createStatement(np.getUri(), NPX.INVALIDATES, i, NPA.GRAPH));
×
257
            metaStatements.add(vf.createStatement(np.getUri(), NPX.RETRACTS, i, NPA.GRAPH));
×
258
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:retracts, RETRACTED_NANOPUB, npa:graph, meta, if the NANOPUB retracts another nanopub
259
        }
×
260
        for (IRI i : superseded) {
30✔
261
            invalidateStatements.add(vf.createStatement(np.getUri(), NPX.INVALIDATES, i, NPA.GRAPH));
33✔
262
            metaStatements.add(vf.createStatement(np.getUri(), NPX.SUPERSEDES, i, NPA.GRAPH));
33✔
263
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:supersedes, SUPERSEDED_NANOPUB, npa:graph, meta, if the NANOPUB supersedes another nanopub
264
        }
3✔
265

266
        metaStatements.add(vf.createStatement(np.getUri(), NPA.HAS_HEAD_GRAPH, np.getHeadUri(), NPA.GRAPH));
36✔
267
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasHeadGraph, HEAD_GRAPH, npa:graph, meta, direct link to the head graph of the NANOPUB
268
        metaStatements.add(vf.createStatement(np.getUri(), NPA.HAS_GRAPH, np.getHeadUri(), NPA.GRAPH));
36✔
269
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasGraph, GRAPH, npa:graph, meta, generic link to all four graphs of the given NANOPUB
270
        metaStatements.add(vf.createStatement(np.getUri(), NP.HAS_ASSERTION, np.getAssertionUri(), NPA.GRAPH));
36✔
271
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, np:hasAssertion, ASSERTION_GRAPH, npa:graph, meta, direct link to the assertion graph of the NANOPUB
272
        metaStatements.add(vf.createStatement(np.getUri(), NPA.HAS_GRAPH, np.getAssertionUri(), NPA.GRAPH));
36✔
273
        metaStatements.add(vf.createStatement(np.getUri(), NP.HAS_PROVENANCE, np.getProvenanceUri(), NPA.GRAPH));
36✔
274
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, np:hasProvenance, PROVENANCE_GRAPH, npa:graph, meta, direct link to the provenance graph of the NANOPUB
275
        metaStatements.add(vf.createStatement(np.getUri(), NPA.HAS_GRAPH, np.getProvenanceUri(), NPA.GRAPH));
36✔
276
        metaStatements.add(vf.createStatement(np.getUri(), NP.HAS_PUBINFO, np.getPubinfoUri(), NPA.GRAPH));
36✔
277
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, np:hasPublicationInfo, PUBINFO_GRAPH, npa:graph, meta, direct link to the pubinfo graph of the NANOPUB
278
        metaStatements.add(vf.createStatement(np.getUri(), NPA.HAS_GRAPH, np.getPubinfoUri(), NPA.GRAPH));
36✔
279

280
        String artifactCode = TrustyUriUtils.getArtifactCode(np.getUri().stringValue());
15✔
281
        metaStatements.add(vf.createStatement(np.getUri(), NPA.ARTIFACT_CODE, vf.createLiteral(artifactCode), NPA.GRAPH));
39✔
282
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:artifactCode, ARTIFACT_CODE, npa:graph, meta, artifact code starting with 'RA...'
283

284
        if (isIntroNanopub(np)) {
9✔
285
            IntroNanopub introNp = new IntroNanopub(np);
15✔
286
            metaStatements.add(vf.createStatement(np.getUri(), NPA.IS_INTRODUCTION_OF, introNp.getUser(), NPA.GRAPH));
36✔
287
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:isIntroductionOf, AGENT, npa:graph, meta, linking intro nanopub to the agent it is introducing
288
            for (KeyDeclaration kc : introNp.getKeyDeclarations()) {
33✔
289
                metaStatements.add(vf.createStatement(np.getUri(), NPA.DECLARES_PUBKEY, vf.createLiteral(kc.getPublicKeyString()), NPA.GRAPH));
42✔
290
                // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:declaresPubkey, FULL_PUBKEY, npa:graph, meta, full pubkey declared by the given intro NANOPUB
291
            }
3✔
292
        }
293

294
        try {
295
            timestamp = SimpleTimestampPattern.getCreationTime(np);
12✔
296
        } catch (IllegalArgumentException ex) {
×
297
            notes.add("Illegal date/time");
×
298
        }
3✔
299
        if (timestamp != null) {
9!
300
            metaStatements.add(vf.createStatement(np.getUri(), DCTERMS.CREATED, vf.createLiteral(timestamp.getTime()), NPA.GRAPH));
45✔
301
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, dct:created, CREATION_DATE, npa:graph, meta, normalized creation timestamp
302
        }
303

304
        String literalFilter = "_pubkey_" + Utils.createHash(el.getPublicKeyString());
18✔
305
        for (IRI typeIri : NanopubUtils.getTypes(np)) {
33✔
306
            metaStatements.add(vf.createStatement(np.getUri(), NPX.HAS_NANOPUB_TYPE, typeIri, NPA.GRAPH));
33✔
307
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:hasNanopubType, NANOPUB_TYPE, npa:graph, meta, type of NANOPUB
308
            literalFilter += " _type_" + Utils.createHash(typeIri);
15✔
309
        }
3✔
310
        String label = NanopubUtils.getLabel(np);
9✔
311
        if (label != null) {
6!
312
            metaStatements.add(vf.createStatement(np.getUri(), RDFS.LABEL, vf.createLiteral(label), NPA.GRAPH));
39✔
313
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, rdfs:label, LABEL, npa:graph, meta, label of NANOPUB
314
        }
315
        String description = NanopubUtils.getDescription(np);
9✔
316
        if (description != null) {
6✔
317
            metaStatements.add(vf.createStatement(np.getUri(), DCTERMS.DESCRIPTION, vf.createLiteral(description), NPA.GRAPH));
39✔
318
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, dct:description, LABEL, npa:graph, meta, description of NANOPUB
319
        }
320
        for (IRI creatorIri : SimpleCreatorPattern.getCreators(np)) {
33✔
321
            metaStatements.add(vf.createStatement(np.getUri(), DCTERMS.CREATOR, creatorIri, NPA.GRAPH));
33✔
322
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, dct:creator, CREATOR, npa:graph, meta, creator of NANOPUB (can be several)
323
        }
3✔
324
        for (IRI authorIri : SimpleCreatorPattern.getAuthors(np)) {
21!
325
            metaStatements.add(vf.createStatement(np.getUri(), PAV.AUTHORED_BY, authorIri, NPA.GRAPH));
×
326
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, pav:authoredBy, AUTHOR, npa:graph, meta, author of NANOPUB (can be several)
327
        }
×
328

329
        if (!combinedLiterals.isEmpty()) {
9!
330
            literalStatements.add(vf.createStatement(np.getUri(), NPA.HAS_FILTER_LITERAL, vf.createLiteral(literalFilter + "\n" + combinedLiterals), NPA.GRAPH));
45✔
331
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasFilterLiteral, FILTER_LITERAL, npa:graph, literal, auxiliary literal for filtering by type and pubkey in text repo
332
        }
333

334
        // Any statements that express that the currently processed nanopub is already invalidated:
335
        invalidatingStatements = getInvalidatingStatements(np.getUri());
15✔
336

337
        metaStatements.addAll(invalidateStatements);
18✔
338

339
        allStatements = new ArrayList<>(nanopubStatements);
21✔
340
        allStatements.addAll(metaStatements);
18✔
341
        allStatements.addAll(invalidatingStatements);
18✔
342

343
        textStatements = new ArrayList<>(literalStatements);
21✔
344
        textStatements.addAll(metaStatements);
18✔
345
        textStatements.addAll(invalidatingStatements);
18✔
346

347
        if (FeatureFlags.spacesEnabled()) {
6!
348
            IRI signedBy = (el.getSigners().size() == 1) ? el.getSigners().iterator().next() : null;
42!
349
            String pubkeyHash = Utils.createHash(el.getPublicKeyString());
15✔
350
            Date createdAt = (timestamp != null) ? timestamp.getTime() : null;
24!
351
            SpacesExtractor.Context ctx = new SpacesExtractor.Context(ac, signedBy, pubkeyHash, createdAt);
24✔
352
            spaceExtractionStatements = SpacesExtractor.extract(np, ctx);
15✔
353
        }
354
    }
3✔
355

356
    /**
357
     * Get the HTTP client used for fetching nanopublications.
358
     *
359
     * @return the HTTP client
360
     */
361
    static HttpClient getHttpClient() {
362
        if (httpClient == null) {
6✔
363
            httpClient = HttpClientBuilder.create().setDefaultRequestConfig(Utils.getHttpRequestConfig()).build();
15✔
364
        }
365
        return httpClient;
6✔
366
    }
367

368
    /**
369
     * Load the given nanopublication into the database.
370
     *
371
     * @param nanopubUri Nanopublication identifier (URI)
372
     */
373
    public static void load(String nanopubUri) {
374
        if (isNanopubLoaded(nanopubUri)) {
9!
375
            logger.info("Skipping already-loaded nanopub: <{}>", nanopubUri);
×
376
        } else {
377
            Nanopub np = GetNanopub.get(nanopubUri, getHttpClient());
12✔
378
            load(np, -1);
9✔
379
        }
380
    }
3✔
381

382
    /**
383
     * Load a nanopub into the database.
384
     *
385
     * @param np      the nanopub to load
386
     * @param counter the load counter, only used for logging (or -1 if not known)
387
     * @throws RDF4JException if the loading fails
388
     */
389
    public static void load(Nanopub np, long counter) throws RDF4JException {
390
        NanopubLoader loader = new NanopubLoader(np, counter);
18✔
391
        loader.executeLoading();
6✔
392
    }
3✔
393

394
    @GeneratedFlagForDependentElements
395
    private void executeLoading() {
396
        var runningTasks = new ArrayList<Future<?>>();
397
        Consumer<Runnable> runTask = t -> runningTasks.add(loadingPool.submit(t));
×
398

399
        for (String note : notes) {
400
            loadNoteToRepo(np.getUri(), note);
401
        }
402

403
        if (!aborted) {
404
            // Submit all tasks except the "meta" task
405
            if (timestamp != null) {
406
                if (new Date().getTime() - timestamp.getTimeInMillis() < THIRTY_DAYS) {
407
                    if (FeatureFlags.last30dRepoEnabled()) {
408
                        runTask.accept(() -> loadNanopubToLatest(np.getUri(), allStatements));
×
409
                    }
410
                }
411
            }
412

413
            if (FeatureFlags.textRepoEnabled()) {
414
                runTask.accept(() -> loadNanopubToRepo(np.getUri(), textStatements, "text"));
×
415
            }
416
            if (FeatureFlags.fullRepoEnabled()) {
417
                runTask.accept(() -> loadNanopubToRepo(np.getUri(), allStatements, "full"));
×
418
            }
419
            // Note: "meta" task is deferred until all other tasks complete successfully
420

421
            runTask.accept(() -> loadNanopubToRepo(np.getUri(), allStatements, "pubkey_" + Utils.createHash(el.getPublicKeyString())));
×
422
            //                loadNanopubToRepo(np.getUri(), textStatements, "text-pubkey_" + Utils.createHash(el.getPublicKeyString()));
423
            for (IRI typeIri : NanopubUtils.getTypes(np)) {
424
                // Exclude locally minted IRIs:
425
                if (typeIri.stringValue().startsWith(np.getUri().stringValue())) {
426
                    continue;
427
                }
428
                if (!typeIri.stringValue().matches("https?://.*")) {
429
                    continue;
430
                }
431
                runTask.accept(() -> loadNanopubToRepo(np.getUri(), allStatements, "type_" + Utils.createHash(typeIri)));
×
432
                //                        loadNanopubToRepo(np.getUri(), textStatements, "text-type_" + Utils.createHash(typeIri));
433
            }
434
            //                for (IRI creatorIri : SimpleCreatorPattern.getCreators(np)) {
435
            //                        // Exclude locally minted IRIs:
436
            //                        if (creatorIri.stringValue().startsWith(np.getUri().stringValue())) continue;
437
            //                        if (!creatorIri.stringValue().matches("https?://.*")) continue;
438
            //                        loadNanopubToRepo(np.getUri(), allStatements, "user_" + Utils.createHash(creatorIri));
439
            //                        loadNanopubToRepo(np.getUri(), textStatements, "text-user_" + Utils.createHash(creatorIri));
440
            //                }
441
            //                for (IRI authorIri : SimpleCreatorPattern.getAuthors(np)) {
442
            //                        // Exclude locally minted IRIs:
443
            //                        if (authorIri.stringValue().startsWith(np.getUri().stringValue())) continue;
444
            //                        if (!authorIri.stringValue().matches("https?://.*")) continue;
445
            //                        loadNanopubToRepo(np.getUri(), allStatements, "user_" + Utils.createHash(authorIri));
446
            //                        loadNanopubToRepo(np.getUri(), textStatements, "text-user_" + Utils.createHash(authorIri));
447
            //                }
448

449
            // Write to the spaces repo only when the nanopub carries its own space-relevant
450
            // extractions. Invalidators of space-relevant nanopubs are propagated to spaces
451
            // symmetrically below (forward path in loadInvalidateStatements, reverse path in
452
            // the invalidatorPubkeys block) — mirrors the per-type-repo propagation added in
453
            // PR #103 (commit 09eeb32). The materialiser's invalidation joins
454
            // (?invNp npx:invalidates ?np + ?invNp npa:hasLoadNumber ?ln in the spaces repo's
455
            // npa:graph) stay populated because the invalidators that matter are exactly the
456
            // ones we propagate.
457
            boolean thisNpIsSpaceRelevant = FeatureFlags.spacesEnabled() && !spaceExtractionStatements.isEmpty();
458
            if (thisNpIsSpaceRelevant) {
459
                runTask.accept(() -> loadToSpacesRepo(np.getUri(), allStatements, spaceExtractionStatements));
×
460
            }
461

462
            for (Statement st : invalidateStatements) {
463
                runTask.accept(() -> loadInvalidateStatements(np, el.getPublicKeyString(), st, pubkeyStatement, pubkeyStatementX, allStatements));
×
464
            }
465

466
            // Reverse-order symmetry: when retractors were loaded before this nanopub,
467
            // getInvalidatingStatements (in the constructor) captured their
468
            // `npx:invalidates` markers into invalidatingStatements. Mirror what
469
            // loadInvalidateStatements does in the forward case — load each retractor's
470
            // full content into this nanopub's per-type repos (those types the
471
            // retractor doesn't itself carry), sourced from the retractor's per-pubkey
472
            // repo (the one shard guaranteed to be populated for every successfully
473
            // loaded nanopub). When this nanopub is space-relevant, additionally load
474
            // each retractor into the spaces repo so the materialiser's invalidation
475
            // join sees them regardless of load order.
476
            Map<IRI, String> invalidatorPubkeys = collectInvalidatorPubkeys(invalidatingStatements);
477
            if (!invalidatorPubkeys.isEmpty()) {
478
                Set<IRI> thisNpTypes = NanopubUtils.getTypes(np);
479
                for (Map.Entry<IRI, String> e : invalidatorPubkeys.entrySet()) {
480
                    IRI invIri = e.getKey();
481
                    String invPubkey = e.getValue();
482
                    runTask.accept(() -> loadInvalidatorIntoTypeRepos(invIri, invPubkey, np.getUri(), thisNpTypes));
×
483
                    if (thisNpIsSpaceRelevant) {
484
                        runTask.accept(() -> loadInvalidatorIntoSpacesRepo(invIri, invPubkey, np.getUri()));
×
485
                    }
486
                }
487
            }
488

489
            // Wait for all non-meta tasks to complete successfully before submitting the meta task.
490
            // On failure, cancel the remaining futures so orphaned tasks don't keep running in the
491
            // shared loadingPool and race with the next batch retry (which re-submits the same
492
            // nanopub against the same repos).
493
            for (var task : runningTasks) {
494
                try {
495
                    task.get();
496
                } catch (ExecutionException | InterruptedException ex) {
497
                    for (var t : runningTasks) {
498
                        if (!t.isDone()) {
499
                            t.cancel(true);
500
                        }
501
                    }
502
                    throw new RuntimeException("Error in nanopub loading thread", ex.getCause());
503
                }
504
            }
505

506
            // Now submit and wait for the "meta" task after all other tasks have completed successfully
507
            Future<?> metaTask = loadingPool.submit(() -> loadNanopubToRepo(np.getUri(), metaStatements, "meta"));
×
508
            try {
509
                metaTask.get();
510
            } catch (ExecutionException | InterruptedException ex) {
511
                throw new RuntimeException("Error in nanopub loading thread (meta task)", ex.getCause());
512
            }
513
        }
514
    }
515

516
    private static Long lastUpdateOfLatestRepo = null;
6✔
517
    private static long THIRTY_DAYS = 1000L * 60 * 60 * 24 * 30;
6✔
518
    private static long ONE_HOUR = 1000L * 60 * 60;
6✔
519

520
    @GeneratedFlagForDependentElements
521
    private static void loadNanopubToLatest(IRI npId, List<Statement> statements) {
522
        boolean success = false;
523
        int retries = 0;
524
        while (!success) {
525
            RepositoryConnection conn = TripleStore.get().getRepoConnection("last30d");
526
            try (conn) {
527
                // Read committed, because deleting old nanopubs is idempotent. Inserts do not collide
528
                // with deletes, because we are not inserting old nanopubs.
529
                conn.begin(IsolationLevels.READ_COMMITTED);
530
                conn.add(statements);
531
                if (lastUpdateOfLatestRepo == null || new Date().getTime() - lastUpdateOfLatestRepo > ONE_HOUR) {
532
                    logger.debug("Pruning nanopubs older than 30 days from last30d repo...");
533
                    Literal thirtyDaysAgo = vf.createLiteral(new Date(new Date().getTime() - THIRTY_DAYS));
534
                    TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, "SELECT * { graph <" + NPA.GRAPH + "> { " + "?np <" + DCTERMS.CREATED + "> ?date . " + "filter ( ?date < ?thirtydaysago ) " + "} }");
535
                    q.setBinding("thirtydaysago", thirtyDaysAgo);
536
                    try (TupleQueryResult r = q.evaluate()) {
537
                        while (r.hasNext()) {
538
                            BindingSet b = r.next();
539
                            IRI oldNpId = (IRI) b.getBinding("np").getValue();
540
                            logger.debug("Pruning expired nanopub from last30d repo: <{}>", oldNpId);
541
                            for (Value v : Utils.getObjectsForPattern(conn, NPA.GRAPH, oldNpId, NPA.HAS_GRAPH)) {
542
                                // Remove all four nanopub graphs:
543
                                conn.remove((Resource) null, (IRI) null, (Value) null, (IRI) v);
544
                            }
545
                            // Remove nanopubs in admin graphs:
546
                            conn.remove(oldNpId, null, null, NPA.GRAPH);
547
                            conn.remove(oldNpId, null, null, NPA.NETWORK_GRAPH);
548
                        }
549
                    }
550
                    lastUpdateOfLatestRepo = new Date().getTime();
551
                }
552
                conn.commit();
553
                success = true;
554
            } catch (Exception ex) {
555
                logger.warn("Failed to load nanopub <{}> to last30d repo: {}", npId, ex.getMessage(), ex);
556
                if (conn.isActive()) {
557
                    conn.rollback();
558
                }
559
            }
560
            if (!success) {
561
                retries++;
562
                if (retries >= MAX_RETRIES) {
563
                    throw new RuntimeException("Failed to load nanopub " + npId + " to last30d repo after " + MAX_RETRIES + " retries");
564
                }
565
                long delay = computeBackoffMillis(retries);
566
                logger.info("Retrying load of <{}> to last30d repo in {} ms (attempt {}/{})...", npId, delay, retries, MAX_RETRIES);
567
                try {
568
                    Thread.sleep(delay);
569
                } catch (InterruptedException x) {
570
                    Thread.currentThread().interrupt();
571
                }
572
            }
573
        }
574
    }
575

576
    @GeneratedFlagForDependentElements
577
    private static void loadNanopubToRepo(IRI npId, List<Statement> statements, String repoName) {
578
        boolean success = false;
579
        int retries = 0;
580
        while (!success) {
581
            RepositoryConnection conn = TripleStore.get().getRepoConnection(repoName);
582
            long newCountForCache = -1;
583
            String newChecksumForCache = null;
584
            try (conn) {
585
                // Serializable, because write skew would cause the chain of hashes to be broken.
586
                // The inserts must be done serially.
587
                conn.begin(IsolationLevels.SERIALIZABLE);
588
                var repoStatus = fetchRepoStatus(conn, npId);
589
                if (repoStatus.isLoaded) {
590
                    logger.debug("Skipping already-loaded nanopub <{}> in repo '{}'", npId, repoName);
591
                } else {
592
                    String newChecksum = NanopubUtils.updateXorChecksum(npId, repoStatus.checksum);
593
                    conn.remove(NPA.THIS_REPO, NPA.HAS_NANOPUB_COUNT, null, NPA.GRAPH);
594
                    conn.remove(NPA.THIS_REPO, NPA.HAS_NANOPUB_CHECKSUM, null, NPA.GRAPH);
595
                    conn.add(NPA.THIS_REPO, NPA.HAS_NANOPUB_COUNT, vf.createLiteral(repoStatus.count + 1), NPA.GRAPH);
596
                    // @ADMIN-TRIPLE-TABLE@ REPO, npa:hasNanopubCount, NANOPUB_COUNT, npa:graph, admin, number of nanopubs loaded
597
                    conn.add(NPA.THIS_REPO, NPA.HAS_NANOPUB_CHECKSUM, vf.createLiteral(newChecksum), NPA.GRAPH);
598
                    // @ADMIN-TRIPLE-TABLE@ REPO, npa:hasNanopubChecksum, NANOPUB_CHECKSUM, npa:graph, admin, checksum of all loaded nanopubs (order-independent XOR checksum on trusty URIs in Base64 notation)
599
                    conn.add(npId, NPA.HAS_LOAD_NUMBER, vf.createLiteral(repoStatus.count), NPA.GRAPH);
600
                    // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasLoadNumber, LOAD_NUMBER, npa:graph, admin, the sequential number at which this NANOPUB was loaded
601
                    conn.add(npId, NPA.HAS_LOAD_CHECKSUM, vf.createLiteral(newChecksum), NPA.GRAPH);
602
                    // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasLoadChecksum, LOAD_CHECKSUM, npa:graph, admin, the checksum of all loaded nanopubs after loading the given NANOPUB
603
                    conn.add(npId, NPA.HAS_LOAD_TIMESTAMP, vf.createLiteral(new Date()), NPA.GRAPH);
604
                    // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasLoadTimestamp, LOAD_TIMESTAMP, npa:graph, admin, the time point at which this NANOPUB was loaded
605
                    conn.add(statements);
606
                    if ("meta".equals(repoName)) {
607
                        newCountForCache = repoStatus.count + 1;
608
                        newChecksumForCache = newChecksum;
609
                    }
610
                }
611
                conn.commit();
612
                if (newCountForCache >= 0) {
613
                    loadedNanopubCount = newCountForCache;
614
                }
615
                if (newChecksumForCache != null) {
616
                    loadedNanopubChecksum = newChecksumForCache;
617
                }
618
                success = true;
619
            } catch (Exception ex) {
620
                logger.warn("Failed to load nanopub <{}> to repo '{}': {}", npId, repoName, ex.getMessage(), ex);
621
                if (conn.isActive()) {
622
                    conn.rollback();
623
                }
624
            }
625
            if (!success) {
626
                retries++;
627
                if (retries >= MAX_RETRIES) {
628
                    throw new RuntimeException("Failed to load nanopub " + npId + " to repo " + repoName + " after " + MAX_RETRIES + " retries");
629
                }
630
                long delay = computeBackoffMillis(retries);
631
                logger.info("Retrying load of <{}> to repo '{}' in {} ms (attempt {}/{})...", npId, repoName, delay, retries, MAX_RETRIES);
632
                try {
633
                    Thread.sleep(delay);
634
                } catch (InterruptedException x) {
635
                    Thread.currentThread().interrupt();
636
                }
637
            }
638
        }
639
    }
640

641
    /**
642
     * Writes the raw nanopub statements (all four graphs) into the {@code spaces}
643
     * repo alongside the pre-computed extraction statements (which target
644
     * {@code npa:spacesGraph}). Stamps the load-number on the nanopub IRI and bumps
645
     * {@code npa:thisRepo npa:currentLoadCounter} in {@code npa:graph}, all within
646
     * one serializable transaction.
647
     *
648
     * <p>Idempotent: if the nanopub already has a {@code npa:hasLoadNumber} stamp in
649
     * {@code npa:graph} of the {@code spaces} repo, this is a no-op.
650
     *
651
     * @param npId            nanopub IRI
652
     * @param nanopubTriples  raw nanopub statements (all four graphs + meta)
653
     * @param spaceExtraction summary triples destined for {@code npa:spacesGraph}
654
     */
655
    @GeneratedFlagForDependentElements
656
    private static void loadToSpacesRepo(IRI npId, List<Statement> nanopubTriples,
657
                                         List<Statement> spaceExtraction) {
658
        boolean success = false;
659
        int retries = 0;
660
        while (!success) {
661
            RepositoryConnection conn = TripleStore.get().getRepoConnection("spaces");
662
            try (conn) {
663
                conn.begin(IsolationLevels.SERIALIZABLE);
664
                // Idempotency: skip if this nanopub is already stamped in this repo.
665
                if (Utils.getObjectForPattern(conn, NPA.GRAPH, npId, NPA.HAS_LOAD_NUMBER) != null) {
666
                    logger.debug("Skipping already-loaded nanopub <{}> in spaces repo", npId);
667
                    conn.commit();
668
                    success = true;
669
                    continue;
670
                }
671
                long newCounter = fetchSpacesLoadCounter(conn) + 1;
672
                conn.remove(NPA.THIS_REPO,
673
                        com.knowledgepixels.query.vocabulary.SpacesVocab.CURRENT_LOAD_COUNTER,
674
                        null, NPA.GRAPH);
675
                conn.add(NPA.THIS_REPO,
676
                        com.knowledgepixels.query.vocabulary.SpacesVocab.CURRENT_LOAD_COUNTER,
677
                        vf.createLiteral(newCounter), NPA.GRAPH);
678
                conn.add(npId, NPA.HAS_LOAD_NUMBER, vf.createLiteral(newCounter), NPA.GRAPH);
679
                conn.add(nanopubTriples);
680
                conn.add(spaceExtraction);
681
                conn.commit();
682
                success = true;
683
            } catch (Exception ex) {
684
                logger.warn("Failed to load nanopub <{}> to spaces repo: {}", npId, ex.getMessage(), ex);
685
                if (conn.isActive()) {
686
                    conn.rollback();
687
                }
688
            }
689
            if (!success) {
690
                retries++;
691
                if (retries >= MAX_RETRIES) {
692
                    throw new RuntimeException("Failed to load nanopub " + npId + " to spaces repo after " + MAX_RETRIES + " retries");
693
                }
694
                long delay = computeBackoffMillis(retries);
695
                logger.info("Retrying load of <{}> to spaces repo in {} ms (attempt {}/{})...", npId, delay, retries, MAX_RETRIES);
696
                try {
697
                    Thread.sleep(delay);
698
                } catch (InterruptedException x) {
699
                    Thread.currentThread().interrupt();
700
                }
701
            }
702
        }
703
    }
704

705
    /**
706
     * Returns the cumulative count of nanopubs ever loaded into the {@code meta}
707
     * repo, or {@code null} if the value cannot be determined (e.g. the store
708
     * hasn't been initialised yet). Reads the persisted {@code npa:hasNanopubCount}
709
     * triple on first call and caches it in {@link #loadedNanopubCount};
710
     * subsequent fresh loads update the cache in-place.
711
     */
712
    public static Long getLoadedNanopubCount() {
713
        Long v = loadedNanopubCount;
6✔
714
        if (v != null) {
6✔
715
            return v;
6✔
716
        }
717
        try (RepositoryConnection conn = TripleStore.get().getRepoConnection("meta")) {
12✔
718
            Value val = Utils.getObjectForPattern(conn, NPA.GRAPH, NPA.THIS_REPO, NPA.HAS_NANOPUB_COUNT);
×
719
            if (val != null) {
×
720
                v = Long.parseLong(val.stringValue());
×
721
                loadedNanopubCount = v;
×
722
                return v;
×
723
            }
724
        } catch (NumberFormatException ex) {
×
725
            logger.warn("Malformed npa:hasNanopubCount literal in meta repo (value not parseable as long): {}", ex.getMessage(), ex);
×
726
        } catch (Exception ex) {
3✔
727
            logger.warn("Could not read npa:hasNanopubCount from meta repo", ex);
12✔
728
        }
×
729
        return null;
6✔
730
    }
731

732
    /**
733
     * Returns the order-independent XOR checksum (Base64-encoded) of trusty URIs
734
     * of all nanopubs ever loaded into the {@code meta} repo, or {@code null} if
735
     * the value cannot be determined (e.g. the store hasn't been initialised
736
     * yet). Reads the persisted {@code npa:hasNanopubChecksum} triple on first
737
     * call and caches it in {@link #loadedNanopubChecksum}; subsequent fresh
738
     * loads update the cache in-place alongside {@link #loadedNanopubCount}.
739
     */
740
    public static String getLoadedNanopubChecksum() {
741
        String v = loadedNanopubChecksum;
6✔
742
        if (v != null) {
6✔
743
            return v;
6✔
744
        }
745
        try (RepositoryConnection conn = TripleStore.get().getRepoConnection("meta")) {
12✔
746
            Value val = Utils.getObjectForPattern(conn, NPA.GRAPH, NPA.THIS_REPO, NPA.HAS_NANOPUB_CHECKSUM);
×
747
            if (val != null) {
×
748
                v = val.stringValue();
×
749
                loadedNanopubChecksum = v;
×
750
                return v;
×
751
            }
752
        } catch (Exception ex) {
3!
753
            logger.warn("Could not read npa:hasNanopubChecksum from meta repo", ex);
12✔
754
        }
×
755
        return null;
6✔
756
    }
757

758
    private static long fetchSpacesLoadCounter(RepositoryConnection conn) {
759
        Value v = Utils.getObjectForPattern(conn, NPA.GRAPH, NPA.THIS_REPO,
×
760
                com.knowledgepixels.query.vocabulary.SpacesVocab.CURRENT_LOAD_COUNTER);
761
        if (v == null) {
×
762
            return 0;
×
763
        }
764
        try {
765
            return Long.parseLong(v.stringValue());
×
766
        } catch (NumberFormatException ex) {
×
767
            logger.warn("Malformed npa:currentLoadCounter literal in spaces repo (value not parseable as long): \"{}\"", v.stringValue());
×
768
            return 0;
×
769
        }
770
    }
771

772
    private record RepoStatus(boolean isLoaded, long count, String checksum) {
×
773
    }
774

775
    /**
776
     * To execute before loading a nanopub: check if the nanopub is already loaded and what is the
777
     * current load counter and checksum. This effectively batches three queries into one.
778
     * This method must be called from within a transaction.
779
     *
780
     * @param conn repo connection
781
     * @param npId nanopub ID
782
     * @return the current status
783
     */
784
    @GeneratedFlagForDependentElements
785
    private static RepoStatus fetchRepoStatus(RepositoryConnection conn, IRI npId) {
786
        var result = conn.prepareTupleQuery(QueryLanguage.SPARQL, REPO_STATUS_QUERY_TEMPLATE.formatted(npId)).evaluate();
787
        try (result) {
788
            if (!result.hasNext()) {
789
                // This may happen if the repo was created, but is completely empty.
790
                return new RepoStatus(false, 0, NanopubUtils.INIT_CHECKSUM);
791
            }
792
            var row = result.next();
793
            return new RepoStatus(row.hasBinding("loadNumber"), Long.parseLong(row.getBinding("count").getValue().stringValue()), row.getBinding("checksum").getValue().stringValue());
794
        }
795
    }
796

797
    @GeneratedFlagForDependentElements
798
    private static void loadInvalidateStatements(Nanopub thisNp, String thisPubkey, Statement invalidateStatement, Statement pubkeyStatement, Statement pubkeyStatementX, List<Statement> thisAllStatements) {
799
        boolean success = false;
800
        int retries = 0;
801
        List<IRI> typesToLoadFullInto = new ArrayList<>();
802
        boolean targetIsSpaceRelevant = false;
803
        while (!success) {
804
            typesToLoadFullInto.clear();
805
            targetIsSpaceRelevant = false;
806
            List<RepositoryConnection> connections = new ArrayList<>();
807
            RepositoryConnection metaConn = TripleStore.get().getRepoConnection("meta");
808
            try {
809
                IRI invalidatedNpId = (IRI) invalidateStatement.getObject();
810
                // Basic isolation because here we only read append-only data.
811
                metaConn.begin(IsolationLevels.READ_COMMITTED);
812

813
                Value pubkeyValue = Utils.getObjectForPattern(metaConn, NPA.GRAPH, invalidatedNpId, NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY);
814
                if (pubkeyValue != null) {
815
                    String pubkey = pubkeyValue.stringValue();
816

817
                    if (!pubkey.equals(thisPubkey)) {
818
                        //logger.info("Adding invalidation expressed in " + thisNp.getUri() + " also to repo for pubkey " + pubkey);
819
                        connections.add(loadStatements("pubkey_" + Utils.createHash(pubkey), invalidateStatement, pubkeyStatement, pubkeyStatementX));
820
//                                                connections.add(loadStatements("text-pubkey_" + Utils.createHash(pubkey), invalidateStatement, pubkeyStatement));
821
                    }
822

823
                    Set<IRI> thisNpTypes = NanopubUtils.getTypes(thisNp);
824
                    for (Value v : Utils.getObjectsForPattern(metaConn, NPA.GRAPH, invalidatedNpId, NPX.HAS_NANOPUB_TYPE)) {
825
                        if (v instanceof IRI typeIri) {
826
                            if (!thisNpTypes.contains(typeIri)) {
827
                                // Defer until after the meta-read commits — full load goes
828
                                // through loadNanopubToRepo, which has its own transaction
829
                                // and retry loop (see post-loop block below).
830
                                typesToLoadFullInto.add(typeIri);
831
                            }
832
                            if (SpacesExtractor.TRIGGER_TYPES.contains(typeIri)) {
833
                                // Target carries a space-relevant type — propagate the
834
                                // retractor into the spaces repo too (deferred, same
835
                                // reason as above).
836
                                targetIsSpaceRelevant = true;
837
                            }
838
                        }
839
                    }
840

841
//                                        for (Value v : Utils.getObjectsForPattern(metaConn, NPA.GRAPH, invalidatedNpId, DCTERMS.CREATOR)) {
842
//                                                IRI creatorIri = (IRI) v;
843
//                                                if (!SimpleCreatorPattern.getCreators(thisNp).contains(creatorIri)) {
844
//                                                        //logger.info("Adding invalidation expressed in " + thisNp.getUri() + " also to repo for user " + creatorIri);
845
//                                                        connections.add(loadStatements("user_" + Utils.createHash(creatorIri), invalidateStatement, pubkeyStatement));
846
//                                                        connections.add(loadStatements("text-user_" + Utils.createHash(creatorIri), invalidateStatement, pubkeyStatement));
847
//                                                }
848
//                                        }
849
                }
850

851
                metaConn.commit();
852
                // TODO handle case that some commits succeed and some fail
853
                for (RepositoryConnection c : connections) c.commit();
854
                success = true;
855
            } catch (Exception ex) {
856
                logger.warn("Failed to load invalidation statements from <{}> to target repos: {}", thisNp.getUri(), ex.getMessage(), ex);
857
                if (metaConn.isActive()) {
858
                    metaConn.rollback();
859
                }
860
                for (RepositoryConnection c : connections) {
861
                    if (c.isActive()) {
862
                        c.rollback();
863
                    }
864
                }
865
            } finally {
866
                metaConn.close();
867
                for (RepositoryConnection c : connections) c.close();
868
            }
869
            if (!success) {
870
                retries++;
871
                if (retries >= MAX_RETRIES) {
872
                    throw new RuntimeException("Failed to load invalidate statements for " + thisNp.getUri() + " after " + MAX_RETRIES + " retries");
873
                }
874
                long delay = computeBackoffMillis(retries);
875
                logger.info("Retrying invalidation-statement load for <{}> in {} ms (attempt {}/{})...", thisNp.getUri(), delay, retries, MAX_RETRIES);
876
                try {
877
                    Thread.sleep(delay);
878
                } catch (InterruptedException x) {
879
                    Thread.currentThread().interrupt();
880
                }
881
            }
882
        }
883
        // Mirror the Registries' behaviour: index a retraction under the types of the
884
        // nanopub it invalidates, even when the retractor itself doesn't carry those
885
        // types. Load the full retracting nanopub (not just the npx:invalidates marker)
886
        // so a query against a type repo can fetch the retractor's own assertion /
887
        // provenance / pubinfo, not only the join handle.
888
        // loadNanopubToRepo is idempotent (early-exit on npa:hasLoadNumber) and runs
889
        // its own SERIALIZABLE transaction + retry loop, so it's safe to call here.
890
        for (IRI typeIri : typesToLoadFullInto) {
891
            loadNanopubToRepo(thisNp.getUri(), thisAllStatements, "type_" + Utils.createHash(typeIri));
892
        }
893
        // Same rationale for the spaces repo: when the invalidated nanopub is itself
894
        // space-relevant, the retractor needs to land in the spaces repo so the
895
        // materialiser's invalidation join (?invNp npx:invalidates ?np +
896
        // ?invNp npa:hasLoadNumber ?ln in npa:graph) finds it. spaceExtractionStatements
897
        // is empty here — the retractor is not space-relevant by itself, otherwise it
898
        // would have already been loaded to spaces by the regular spaces-load task.
899
        if (targetIsSpaceRelevant && FeatureFlags.spacesEnabled()) {
900
            loadToSpacesRepo(thisNp.getUri(), thisAllStatements, Collections.emptyList());
901
        }
902
    }
903

904
    /**
905
     * Extracts a map from invalidator IRI to that invalidator's pubkey literal
906
     * out of an {@code invalidatingStatements} list as produced by
907
     * {@link #getInvalidatingStatements}. The list interleaves
908
     * {@code (?inv, npx:invalidates, ?np)} and
909
     * {@code (?inv, npa:hasValidSignatureForPublicKey, ?pubkey)} triples per
910
     * invalidator; this helper picks out only the pubkey-binding triples.
911
     */
912
    private static Map<IRI, String> collectInvalidatorPubkeys(List<Statement> invalidatingStatements) {
913
        Map<IRI, String> result = new LinkedHashMap<>();
×
914
        for (Statement st : invalidatingStatements) {
×
915
            if (st.getPredicate().equals(NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY)
×
916
                && st.getSubject() instanceof IRI invIri) {
×
917
                result.put(invIri, st.getObject().stringValue());
×
918
            }
919
        }
×
920
        return result;
×
921
    }
922

923
    /**
924
     * Reverse-order counterpart of the forward-order full-content propagation in
925
     * {@link #loadInvalidateStatements}: when this nanopub had already-loaded
926
     * retractors at the time of its own load (captured by
927
     * {@link #getInvalidatingStatements}), load each retractor's full content
928
     * into this nanopub's per-type repos — restricted to those types the
929
     * retractor doesn't itself carry (the retractor's own load already populated
930
     * the type repos it covers).
931
     *
932
     * <p>Source is the retractor's per-pubkey repo, which is the one shard
933
     * unconditionally populated for every successfully-loaded nanopub. The
934
     * retractor's types are read from the meta repo so we can skip type repos
935
     * the retractor's own regular load already populated.
936
     */
937
    @GeneratedFlagForDependentElements
938
    private static void loadInvalidatorIntoTypeRepos(IRI invIri, String invPubkey, IRI thisNpId, Set<IRI> thisNpTypes) {
939
        Set<IRI> invTypes = readInvalidatorTypesFromMeta(invIri, thisNpId);
940

941
        List<IRI> typesToLoadInto = new ArrayList<>();
942
        for (IRI typeIri : thisNpTypes) {
943
            // Match the regular per-type load loop's exclusion of locally-minted IRIs.
944
            if (typeIri.stringValue().startsWith(thisNpId.stringValue())) {
945
                continue;
946
            }
947
            if (!typeIri.stringValue().matches("https?://.*")) {
948
                continue;
949
            }
950
            if (!invTypes.contains(typeIri)) {
951
                typesToLoadInto.add(typeIri);
952
            }
953
        }
954
        if (typesToLoadInto.isEmpty()) {
955
            return;
956
        }
957

958
        List<Statement> invContent = fetchNanopubAllStatementsFromPubkeyRepo(invIri, invPubkey);
959
        for (IRI typeIri : typesToLoadInto) {
960
            loadNanopubToRepo(invIri, invContent, "type_" + Utils.createHash(typeIri));
961
        }
962
    }
963

964
    /**
965
     * Reverse-order counterpart for the spaces repo: when a space-relevant nanopub
966
     * is loaded and {@link #getInvalidatingStatements} captured already-loaded
967
     * retractors of it, load each retractor's full content into the spaces repo so
968
     * the materialiser's invalidation join (?invNp npx:invalidates ?np +
969
     * ?invNp npa:hasLoadNumber ?ln in npa:graph) finds it regardless of the
970
     * load order between target and retractor.
971
     *
972
     * <p>Source is the retractor's per-pubkey repo (the one shard unconditionally
973
     * populated for every successfully-loaded nanopub). Passes an empty
974
     * {@code spaceExtraction} list to {@link #loadToSpacesRepo}: by construction
975
     * the retractor would already be in the spaces repo by its regular spaces-load
976
     * task if it carried space-relevant extractions of its own.
977
     * {@link #loadToSpacesRepo} is idempotent on {@code npa:hasLoadNumber}, so
978
     * the double-load case is a no-op.
979
     */
980
    @GeneratedFlagForDependentElements
981
    private static void loadInvalidatorIntoSpacesRepo(IRI invIri, String invPubkey, IRI thisNpId) {
982
        List<Statement> invContent = fetchNanopubAllStatementsFromPubkeyRepo(invIri, invPubkey);
983
        loadToSpacesRepo(invIri, invContent, Collections.emptyList());
984
    }
985

986
    @GeneratedFlagForDependentElements
987
    private static Set<IRI> readInvalidatorTypesFromMeta(IRI invIri, IRI thisNpId) {
988
        Set<IRI> invTypes = new HashSet<>();
989
        boolean success = false;
990
        int retries = 0;
991
        while (!success) {
992
            invTypes.clear();
993
            RepositoryConnection metaConn = TripleStore.get().getRepoConnection("meta");
994
            try (metaConn) {
995
                metaConn.begin(IsolationLevels.READ_COMMITTED);
996
                for (Value v : Utils.getObjectsForPattern(metaConn, NPA.GRAPH, invIri, NPX.HAS_NANOPUB_TYPE)) {
997
                    if (v instanceof IRI ti) {
998
                        invTypes.add(ti);
999
                    }
1000
                }
1001
                metaConn.commit();
1002
                success = true;
1003
            } catch (Exception ex) {
1004
                logger.warn("Failed to read types for invalidator <{}> (needed for target <{}>): {}", invIri, thisNpId, ex.getMessage(), ex);
1005
                if (metaConn.isActive()) {
1006
                    metaConn.rollback();
1007
                }
1008
            }
1009
            if (!success) {
1010
                retries++;
1011
                if (retries >= MAX_RETRIES) {
1012
                    throw new RuntimeException("Failed to read invalidator types for " + invIri + " after " + MAX_RETRIES + " retries");
1013
                }
1014
                long delay = computeBackoffMillis(retries);
1015
                logger.info("Retrying type-read for invalidator <{}> in {} ms (attempt {}/{})...", invIri, delay, retries, MAX_RETRIES);
1016
                try {
1017
                    Thread.sleep(delay);
1018
                } catch (InterruptedException x) {
1019
                    Thread.currentThread().interrupt();
1020
                }
1021
            }
1022
        }
1023
        return invTypes;
1024
    }
1025

1026
    /**
1027
     * Reads a nanopub's content back from its per-pubkey repo as a list of
1028
     * statements that mirrors what its original load produced into
1029
     * {@code allStatements}, minus the per-repo bookkeeping triples
1030
     * ({@code npa:hasLoadNumber}, {@code npa:hasLoadChecksum},
1031
     * {@code npa:hasLoadTimestamp}). {@link #loadNanopubToRepo} stamps those
1032
     * fresh on every destination repo, so they must be filtered out of the
1033
     * source set.
1034
     *
1035
     * <p>Fetched content:
1036
     * <ul>
1037
     *   <li>All triples in the nanopub's four named graphs, discovered via
1038
     *       {@code <npId> npa:hasGraph ?g} in {@code npa:graph}.</li>
1039
     *   <li>{@code (<npId>, ?p, ?o)} in {@code npa:graph}, excluding the per-repo
1040
     *       bookkeeping predicates above.</li>
1041
     *   <li>{@code (?inv, npx:invalidates, <npId>)} in {@code npa:graph}, plus
1042
     *       the matching {@code npa:hasValidSignatureForPublicKey[Hash]} triples
1043
     *       of each {@code ?inv}, so propagation carries the nanopub's full
1044
     *       invalidator history (which doesn't affect query results — see the
1045
     *       one-hop filter in {@code Utils#defaultQuery} — but keeps repos
1046
     *       consistent).</li>
1047
     *   <li>{@code (<npId>, ?p, ?o)} in {@code npa:networkGraph}.</li>
1048
     * </ul>
1049
     */
1050
    @GeneratedFlagForDependentElements
1051
    private static List<Statement> fetchNanopubAllStatementsFromPubkeyRepo(IRI npId, String pubkey) {
1052
        String repoName = "pubkey_" + Utils.createHash(pubkey);
1053
        boolean success = false;
1054
        int retries = 0;
1055
        List<Statement> result = new ArrayList<>();
1056
        while (!success) {
1057
            result.clear();
1058
            RepositoryConnection conn = TripleStore.get().getRepoConnection(repoName);
1059
            try (conn) {
1060
                // Append-only data + idempotent re-load downstream: READ_COMMITTED suffices.
1061
                conn.begin(IsolationLevels.READ_COMMITTED);
1062

1063
                List<IRI> npGraphs = new ArrayList<>();
1064
                try (RepositoryResult<Statement> r = conn.getStatements(npId, NPA.HAS_GRAPH, null, NPA.GRAPH)) {
1065
                    while (r.hasNext()) {
1066
                        Value o = r.next().getObject();
1067
                        if (o instanceof IRI iri) {
1068
                            npGraphs.add(iri);
1069
                        }
1070
                    }
1071
                }
1072

1073
                for (IRI g : npGraphs) {
1074
                    try (RepositoryResult<Statement> r = conn.getStatements(null, null, null, g)) {
1075
                        while (r.hasNext()) result.add(r.next());
1076
                    }
1077
                }
1078

1079
                try (RepositoryResult<Statement> r = conn.getStatements(npId, null, null, NPA.GRAPH)) {
1080
                    while (r.hasNext()) {
1081
                        Statement st = r.next();
1082
                        IRI p = st.getPredicate();
1083
                        if (p.equals(NPA.HAS_LOAD_NUMBER)
1084
                            || p.equals(NPA.HAS_LOAD_CHECKSUM)
1085
                            || p.equals(NPA.HAS_LOAD_TIMESTAMP)) {
1086
                            continue;
1087
                        }
1088
                        result.add(st);
1089
                    }
1090
                }
1091

1092
                Set<IRI> invalidators = new HashSet<>();
1093
                try (RepositoryResult<Statement> r = conn.getStatements(null, NPX.INVALIDATES, npId, NPA.GRAPH)) {
1094
                    while (r.hasNext()) {
1095
                        Statement st = r.next();
1096
                        result.add(st);
1097
                        if (st.getSubject() instanceof IRI invIri) {
1098
                            invalidators.add(invIri);
1099
                        }
1100
                    }
1101
                }
1102
                for (IRI invIri : invalidators) {
1103
                    try (RepositoryResult<Statement> r = conn.getStatements(invIri, NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY, null, NPA.GRAPH)) {
1104
                        while (r.hasNext()) result.add(r.next());
1105
                    }
1106
                    try (RepositoryResult<Statement> r = conn.getStatements(invIri, NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY_HASH, null, NPA.GRAPH)) {
1107
                        while (r.hasNext()) result.add(r.next());
1108
                    }
1109
                }
1110

1111
                try (RepositoryResult<Statement> r = conn.getStatements(npId, null, null, NPA.NETWORK_GRAPH)) {
1112
                    while (r.hasNext()) result.add(r.next());
1113
                }
1114

1115
                conn.commit();
1116
                success = true;
1117
            } catch (Exception ex) {
1118
                logger.warn("Failed to fetch content of nanopub <{}> from repo '{}': {}", npId, repoName, ex.getMessage(), ex);
1119
                if (conn.isActive()) {
1120
                    conn.rollback();
1121
                }
1122
            }
1123
            if (!success) {
1124
                retries++;
1125
                if (retries >= MAX_RETRIES) {
1126
                    throw new RuntimeException("Failed to fetch nanopub content from " + repoName + " for " + npId + " after " + MAX_RETRIES + " retries");
1127
                }
1128
                long delay = computeBackoffMillis(retries);
1129
                logger.info("Retrying content-fetch of <{}> from repo '{}' in {} ms (attempt {}/{})...", npId, repoName, delay, retries, MAX_RETRIES);
1130
                try {
1131
                    Thread.sleep(delay);
1132
                } catch (InterruptedException x) {
1133
                    Thread.currentThread().interrupt();
1134
                }
1135
            }
1136
        }
1137
        return result;
1138
    }
1139

1140
    @GeneratedFlagForDependentElements
1141
    private static RepositoryConnection loadStatements(String repoName, Statement... statements) {
1142
        RepositoryConnection conn = TripleStore.get().getRepoConnection(repoName);
1143
        // Basic isolation: we only append new statements
1144
        conn.begin(IsolationLevels.READ_COMMITTED);
1145
        for (Statement st : statements) {
1146
            conn.add(st);
1147
        }
1148
        return conn;
1149
    }
1150

1151
    @GeneratedFlagForDependentElements
1152
    static List<Statement> getInvalidatingStatements(IRI npId) {
1153
        List<Statement> invalidatingStatements = new ArrayList<>();
1154
        boolean success = false;
1155
        int retries = 0;
1156
        while (!success) {
1157
            RepositoryConnection conn = TripleStore.get().getRepoConnection("meta");
1158
            try (conn) {
1159
                // Basic isolation because here we only read append-only data.
1160
                conn.begin(IsolationLevels.READ_COMMITTED);
1161

1162
                TupleQueryResult r = conn.prepareTupleQuery(QueryLanguage.SPARQL, "SELECT * { graph <" + NPA.GRAPH + "> { " + "?np <" + NPX.INVALIDATES + "> <" + npId + "> ; <" + NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY + "> ?pubkey . " + "} }").evaluate();
1163
                try (r) {
1164
                    while (r.hasNext()) {
1165
                        BindingSet b = r.next();
1166
                        invalidatingStatements.add(vf.createStatement((IRI) b.getBinding("np").getValue(), NPX.INVALIDATES, npId, NPA.GRAPH));
1167
                        invalidatingStatements.add(vf.createStatement((IRI) b.getBinding("np").getValue(), NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY, b.getBinding("pubkey").getValue(), NPA.GRAPH));
1168
                    }
1169
                }
1170
                conn.commit();
1171
                success = true;
1172
            } catch (Exception ex) {
1173
                logger.warn("Failed to query existing invalidators of <{}> from meta repo: {}", npId, ex.getMessage(), ex);
1174
                if (conn.isActive()) {
1175
                    conn.rollback();
1176
                }
1177
            }
1178
            if (!success) {
1179
                retries++;
1180
                if (retries >= MAX_RETRIES) {
1181
                    throw new RuntimeException("Failed to get invalidating statements for " + npId + " after " + MAX_RETRIES + " retries");
1182
                }
1183
                long delay = computeBackoffMillis(retries);
1184
                logger.info("Retrying invalidator-query for <{}> in {} ms (attempt {}/{})...", npId, delay, retries, MAX_RETRIES);
1185
                try {
1186
                    Thread.sleep(delay);
1187
                } catch (InterruptedException x) {
1188
                    Thread.currentThread().interrupt();
1189
                }
1190
            }
1191
        }
1192
        return invalidatingStatements;
1193
    }
1194

1195
    @GeneratedFlagForDependentElements
1196
    private static void loadNoteToRepo(Resource subj, String note) {
1197
        boolean success = false;
1198
        int retries = 0;
1199
        while (!success) {
1200
            RepositoryConnection conn = TripleStore.get().getAdminRepoConnection();
1201
            try (conn) {
1202
                List<Statement> statements = new ArrayList<>();
1203
                statements.add(vf.createStatement(subj, NPA.NOTE, vf.createLiteral(note), NPA.GRAPH));
1204
                conn.add(statements);
1205
                success = true;
1206
            } catch (Exception ex) {
1207
                logger.warn("Failed to write note \"{}\" to admin repo for <{}>: {}", note, subj, ex.getMessage(), ex);
1208
            }
1209
            if (!success) {
1210
                retries++;
1211
                if (retries >= MAX_RETRIES) {
1212
                    throw new RuntimeException("Failed to load note to repo for " + subj + " after " + MAX_RETRIES + " retries");
1213
                }
1214
                long delay = computeBackoffMillis(retries);
1215
                logger.info("Retrying note-write for <{}> in {} ms (attempt {}/{})...", subj, delay, retries, MAX_RETRIES);
1216
                try {
1217
                    Thread.sleep(delay);
1218
                } catch (InterruptedException x) {
1219
                    Thread.currentThread().interrupt();
1220
                }
1221
            }
1222
        }
1223
    }
1224

1225
    static boolean hasValidSignature(NanopubSignatureElement el) {
1226
        if (el == null) {
6!
1227
            logger.warn("Signature validation skipped: signature element is null (nanopub has no signature)");
×
1228
            return false;
×
1229
        }
1230
        try {
1231
            if (SignatureUtils.hasValidSignature(el) && el.getPublicKeyString() != null) {
18!
1232
                return true;
6✔
1233
            }
1234
            logger.warn("Signature invalid for <{}> (pubkey: {})",
12✔
1235
                    el.getUri(), el.getPublicKeyString() != null ? el.getPublicKeyString() : "none");
21!
1236
        } catch (GeneralSecurityException ex) {
3✔
1237
            logger.warn("Signature verification threw a security exception for <{}>: {}", el.getUri(), ex.getMessage(), ex);
57✔
1238
        }
3✔
1239
        return false;
6✔
1240
    }
1241

1242
    private static IRI getBaseTrustyUri(Value v) {
1243
        if (!(v instanceof IRI)) {
9!
1244
            return null;
×
1245
        }
1246
        String s = v.stringValue();
9✔
1247
        if (!s.matches(".*[^A-Za-z0-9\\-_]RA[A-Za-z0-9\\-_]{43}([^A-Za-z0-9\\\\-_].{0,43})?")) {
12✔
1248
            return null;
6✔
1249
        }
1250
        return vf.createIRI(s.replaceFirst("^(.*[^A-Za-z0-9\\-_]RA[A-Za-z0-9\\-_]{43})([^A-Za-z0-9\\\\-_].{0,43})?$", "$1"));
21✔
1251
    }
1252

1253
    // TODO: Move this to nanopub library:
1254
    private static boolean isIntroNanopub(Nanopub np) {
1255
        for (Statement st : np.getAssertion()) {
33✔
1256
            if (st.getPredicate().equals(NPX.DECLARED_BY)) {
15✔
1257
                return true;
6✔
1258
            }
1259
        }
3✔
1260
        return false;
6✔
1261
    }
1262

1263
    /**
1264
     * Check if a nanopub is already loaded in the admin graph.
1265
     *
1266
     * @param npId the nanopub ID
1267
     * @return true if the nanopub is loaded, false otherwise
1268
     */
1269
    @GeneratedFlagForDependentElements
1270
    static boolean isNanopubLoaded(String npId) {
1271
        boolean loaded = false;
1272
        RepositoryConnection conn = TripleStore.get().getRepoConnection("meta");
1273
        try (conn) {
1274
            if (Utils.getObjectForPattern(conn, NPA.GRAPH, vf.createIRI(npId), NPA.HAS_LOAD_NUMBER) != null) {
1275
                loaded = true;
1276
            }
1277
        } catch (Exception ex) {
1278
            logger.warn("Could not check load status of <{}>: {}", npId, ex.getMessage(), ex);
1279
        }
1280
        return loaded;
1281
    }
1282

1283
    private static final ValueFactory vf = SimpleValueFactory.getInstance();
6✔
1284

1285
    // TODO remove the constants and use the ones from the nanopub library instead
1286

1287
    /**
1288
     * Template for the query that fetches the status of a repository.
1289
     */
1290
    // Template for .fetchRepoStatus
1291
    private static final String REPO_STATUS_QUERY_TEMPLATE = """
84✔
1292
            SELECT * { graph <%s> {
1293
              OPTIONAL { <%s> <%s> ?loadNumber . }
1294
              <%s> <%s> ?count ;
1295
                   <%s> ?checksum .
1296
            } }
1297
            """.formatted(NPA.GRAPH, "%s", NPA.HAS_LOAD_NUMBER, NPA.THIS_REPO, NPA.HAS_NANOPUB_COUNT, NPA.HAS_NANOPUB_CHECKSUM);
6✔
1298
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc