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

knowledgepixels / nanopub-query / 24888210076

24 Apr 2026 11:55AM UTC coverage: 64.515% (+5.3%) from 59.265%
24888210076

push

github

web-flow
Merge pull request #79 from knowledgepixels/feature/62-spaces-extraction-v2

feat: v2 spaces extraction layer (#62)

381 of 670 branches covered (56.87%)

Branch coverage included in aggregate %.

1028 of 1514 relevant lines covered (67.9%)

10.25 hits per line

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

70.8
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.nanopub.Nanopub;
18
import org.nanopub.NanopubUtils;
19
import org.nanopub.SimpleCreatorPattern;
20
import org.nanopub.SimpleTimestampPattern;
21
import org.nanopub.extra.security.KeyDeclaration;
22
import org.nanopub.extra.security.MalformedCryptoElementException;
23
import org.nanopub.extra.security.NanopubSignatureElement;
24
import org.nanopub.extra.security.SignatureUtils;
25
import org.nanopub.extra.server.GetNanopub;
26
import org.nanopub.extra.setting.IntroNanopub;
27
import org.nanopub.vocabulary.NP;
28
import org.nanopub.vocabulary.NPA;
29
import org.nanopub.vocabulary.NPX;
30
import org.nanopub.vocabulary.PAV;
31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
33

34

35
import java.security.GeneralSecurityException;
36
import java.util.*;
37
import java.util.concurrent.ExecutionException;
38
import java.util.concurrent.Executors;
39
import java.util.concurrent.Future;
40
import java.util.concurrent.ThreadLocalRandom;
41
import java.util.concurrent.ThreadPoolExecutor;
42
import java.util.function.Consumer;
43

44
/**
45
 * Utility class for loading nanopublications into the database.
46
 */
47
public class NanopubLoader {
48

49
    private static HttpClient httpClient;
50
    private static final ThreadPoolExecutor loadingPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(4);
12✔
51

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

69
    /**
70
     * Returns the sleep delay in ms for the given 1-indexed retry attempt. Delay
71
     * is {@link #BACKOFF_BASE_MS}{@code [attempt-1]} perturbed by ±50 % uniform
72
     * jitter, clamped to be non-negative.
73
     *
74
     * @param attempt 1-indexed retry attempt number
75
     * @return the computed sleep delay in ms
76
     */
77
    static long computeBackoffMillis(int attempt) {
78
        long base = BACKOFF_BASE_MS[Math.min(attempt - 1, BACKOFF_BASE_MS.length - 1)];
×
79
        long jitter = ThreadLocalRandom.current().nextLong(base + 1) - base / 2;
×
80
        return Math.max(0L, base + jitter);
×
81
    }
82
    private Nanopub np;
83
    private NanopubSignatureElement el = null;
9✔
84
    private List<Statement> metaStatements = new ArrayList<>();
15✔
85
    private List<Statement> nanopubStatements = new ArrayList<>();
15✔
86
    private List<Statement> literalStatements = new ArrayList<>();
15✔
87
    private List<Statement> invalidateStatements = new ArrayList<>();
15✔
88
    private List<Statement> textStatements, allStatements;
89
    private List<Statement> spaceExtractionStatements = new ArrayList<>();
15✔
90
    private Calendar timestamp = null;
9✔
91
    private Statement pubkeyStatement, pubkeyStatementX;
92
    private List<String> notes = new ArrayList<>();
15✔
93
    private boolean aborted = false;
9✔
94
    private static final Logger log = LoggerFactory.getLogger(NanopubLoader.class);
9✔
95

96

97
    NanopubLoader(Nanopub np, long counter) {
6✔
98
        this.np = np;
9✔
99
        if (counter >= 0) {
12✔
100
            log.info("Loading {}: {}", counter, np.getUri());
24✔
101
        } else {
102
            log.info("Loading: {}", np.getUri());
15✔
103
        }
104

105
        // TODO Ensure proper synchronization and DB rollbacks
106

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

109
        String ac = TrustyUriUtils.getArtifactCode(np.getUri().toString());
15✔
110
        if (!np.getHeadUri().toString().contains(ac) || !np.getAssertionUri().toString().contains(ac) || !np.getProvenanceUri().toString().contains(ac) || !np.getPubinfoUri().toString().contains(ac)) {
72!
111
            notes.add("could not load nanopub as not all graphs contained the artifact code");
×
112
            aborted = true;
×
113
            return;
×
114
        }
115

116
        try {
117
            el = SignatureUtils.getSignatureElement(np);
12✔
118
        } catch (MalformedCryptoElementException ex) {
×
119
            notes.add("Signature error");
×
120
        }
3✔
121
        if (!hasValidSignature(el)) {
12✔
122
            aborted = true;
9✔
123
            return;
3✔
124
        }
125

126
        pubkeyStatement = vf.createStatement(np.getUri(), NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY, vf.createLiteral(el.getPublicKeyString()), NPA.GRAPH);
39✔
127
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasValidSignatureForPublicKey, FULL_PUBKEY, npa:graph, meta, full pubkey if signature is valid
128
        metaStatements.add(pubkeyStatement);
18✔
129
        pubkeyStatementX = vf.createStatement(np.getUri(), NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY_HASH, vf.createLiteral(Utils.createHash(el.getPublicKeyString())), NPA.GRAPH);
42✔
130
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasValidSignatureForPublicKeyHash, PUBKEY_HASH, npa:graph, meta, hex-encoded SHA256 hash if signature is valid
131
        metaStatements.add(pubkeyStatementX);
18✔
132

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

138
        Set<IRI> subIris = new HashSet<>();
12✔
139
        Set<IRI> otherNps = new HashSet<>();
12✔
140
        Set<IRI> invalidated = new HashSet<>();
12✔
141
        Set<IRI> retracted = new HashSet<>();
12✔
142
        Set<IRI> superseded = new HashSet<>();
12✔
143
        String combinedLiterals = "";
6✔
144
        for (Statement st : NanopubUtils.getStatements(np)) {
33✔
145
            nanopubStatements.add(st);
15✔
146

147
            if (st.getPredicate().toString().contains(ac)) {
18!
148
                subIris.add(st.getPredicate());
×
149
            } else {
150
                IRI b = getBaseTrustyUri(st.getPredicate());
12✔
151
                if (b != null) otherNps.add(b);
6!
152
            }
153
            if (st.getPredicate().equals(NPX.RETRACTS) && st.getObject() instanceof IRI) {
15!
154
                retracted.add((IRI) st.getObject());
×
155
            }
156
            if (st.getPredicate().equals(NPX.INVALIDATES) && st.getObject() instanceof IRI) {
15!
157
                invalidated.add((IRI) st.getObject());
×
158
            }
159
            if (st.getSubject().equals(np.getUri()) && st.getObject() instanceof IRI) {
30✔
160
                if (st.getPredicate().equals(NPX.SUPERSEDES)) {
15✔
161
                    superseded.add((IRI) st.getObject());
18✔
162
                }
163
                if (st.getObject().toString().matches(".*[^A-Za-z0-9\\-_]RA[A-Za-z0-9\\-_]{43}")) {
18✔
164
                    metaStatements.add(vf.createStatement(np.getUri(), st.getPredicate(), st.getObject(), NPA.NETWORK_GRAPH));
39✔
165
                    // @ADMIN-TRIPLE-TABLE@ NANOPUB1, RELATION, NANOPUB2, npa:networkGraph, meta, any inter-nanopub relation found in NANOPUB1
166
                }
167
                if (st.getContext().equals(np.getPubinfoUri())) {
18✔
168
                    if (st.getPredicate().equals(NPX.INTRODUCES) || st.getPredicate().equals(NPX.DESCRIBES) || st.getPredicate().equals(NPX.EMBEDS)) {
45!
169
                        metaStatements.add(vf.createStatement(np.getUri(), st.getPredicate(), st.getObject(), NPA.GRAPH));
39✔
170
                        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:introduces, THING, npa:graph, meta, when such a triple is present in pubinfo of NANOPUB
171
                        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:describes, THING, npa:graph, meta, when such a triple is present in pubinfo of NANOPUB
172
                        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:embeds, THING, npa:graph, meta, when such a triple is present in pubinfo of NANOPUB
173
                    }
174
                }
175
            }
176
            if (st.getSubject().toString().contains(ac)) {
18✔
177
                subIris.add((IRI) st.getSubject());
21✔
178
            } else {
179
                IRI b = getBaseTrustyUri(st.getSubject());
12✔
180
                if (b != null) otherNps.add(b);
6!
181
            }
182
            if (st.getObject() instanceof IRI) {
12✔
183
                if (st.getObject().toString().contains(ac)) {
18✔
184
                    subIris.add((IRI) st.getObject());
21✔
185
                } else {
186
                    IRI b = getBaseTrustyUri(st.getObject());
12✔
187
                    if (b != null) otherNps.add(b);
18✔
188
                }
3✔
189
            } else {
190
                combinedLiterals += st.getObject().stringValue().replaceAll("\\s+", " ") + "\n";
27✔
191
//                                if (st.getSubject().equals(np.getUri()) && !st.getSubject().equals(HAS_FILTER_LITERAL)) {
192
//                                        literalStatements.add(vf.createStatement(np.getUri(), st.getPredicate(), st.getObject(), LITERAL_GRAPH));
193
//                                } else {
194
//                                        literalStatements.add(vf.createStatement(np.getUri(), HAS_LITERAL, st.getObject(), LITERAL_GRAPH));
195
//                                }
196
            }
197
        }
3✔
198
        subIris.remove(np.getUri());
15✔
199
        subIris.remove(np.getAssertionUri());
15✔
200
        subIris.remove(np.getProvenanceUri());
15✔
201
        subIris.remove(np.getPubinfoUri());
15✔
202
        for (IRI i : subIris) {
30✔
203
            metaStatements.add(vf.createStatement(np.getUri(), NPA.HAS_SUB_IRI, i, NPA.GRAPH));
33✔
204
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasSubIri, SUB_IRI, npa:graph, meta, for any IRI minted in the namespace of the NANOPUB
205
        }
3✔
206
        for (IRI i : otherNps) {
30✔
207
            metaStatements.add(vf.createStatement(np.getUri(), NPA.REFERS_TO_NANOPUB, i, NPA.NETWORK_GRAPH));
33✔
208
            // @ADMIN-TRIPLE-TABLE@ NANOPUB1, npa:refersToNanopub, NANOPUB2, npa:networkGraph, meta, generic inter-nanopub relation
209
        }
3✔
210
        for (IRI i : invalidated) {
18!
211
            invalidateStatements.add(vf.createStatement(np.getUri(), NPX.INVALIDATES, i, NPA.GRAPH));
×
212
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:invalidates, INVALIDATED_NANOPUB, npa:graph, meta, if the NANOPUB retracts or supersedes another nanopub
213
        }
×
214
        for (IRI i : retracted) {
18!
215
            invalidateStatements.add(vf.createStatement(np.getUri(), NPX.INVALIDATES, i, NPA.GRAPH));
×
216
            metaStatements.add(vf.createStatement(np.getUri(), NPX.RETRACTS, i, NPA.GRAPH));
×
217
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:retracts, RETRACTED_NANOPUB, npa:graph, meta, if the NANOPUB retracts another nanopub
218
        }
×
219
        for (IRI i : superseded) {
30✔
220
            invalidateStatements.add(vf.createStatement(np.getUri(), NPX.INVALIDATES, i, NPA.GRAPH));
33✔
221
            metaStatements.add(vf.createStatement(np.getUri(), NPX.SUPERSEDES, i, NPA.GRAPH));
33✔
222
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:supersedes, SUPERSEDED_NANOPUB, npa:graph, meta, if the NANOPUB supersedes another nanopub
223
        }
3✔
224

225
        metaStatements.add(vf.createStatement(np.getUri(), NPA.HAS_HEAD_GRAPH, np.getHeadUri(), NPA.GRAPH));
36✔
226
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasHeadGraph, HEAD_GRAPH, npa:graph, meta, direct link to the head graph of the NANOPUB
227
        metaStatements.add(vf.createStatement(np.getUri(), NPA.HAS_GRAPH, np.getHeadUri(), NPA.GRAPH));
36✔
228
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasGraph, GRAPH, npa:graph, meta, generic link to all four graphs of the given NANOPUB
229
        metaStatements.add(vf.createStatement(np.getUri(), NP.HAS_ASSERTION, np.getAssertionUri(), NPA.GRAPH));
36✔
230
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, np:hasAssertion, ASSERTION_GRAPH, npa:graph, meta, direct link to the assertion graph of the NANOPUB
231
        metaStatements.add(vf.createStatement(np.getUri(), NPA.HAS_GRAPH, np.getAssertionUri(), NPA.GRAPH));
36✔
232
        metaStatements.add(vf.createStatement(np.getUri(), NP.HAS_PROVENANCE, np.getProvenanceUri(), NPA.GRAPH));
36✔
233
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, np:hasProvenance, PROVENANCE_GRAPH, npa:graph, meta, direct link to the provenance graph of the NANOPUB
234
        metaStatements.add(vf.createStatement(np.getUri(), NPA.HAS_GRAPH, np.getProvenanceUri(), NPA.GRAPH));
36✔
235
        metaStatements.add(vf.createStatement(np.getUri(), NP.HAS_PUBINFO, np.getPubinfoUri(), NPA.GRAPH));
36✔
236
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, np:hasPublicationInfo, PUBINFO_GRAPH, npa:graph, meta, direct link to the pubinfo graph of the NANOPUB
237
        metaStatements.add(vf.createStatement(np.getUri(), NPA.HAS_GRAPH, np.getPubinfoUri(), NPA.GRAPH));
36✔
238

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

243
        if (isIntroNanopub(np)) {
9✔
244
            IntroNanopub introNp = new IntroNanopub(np);
15✔
245
            metaStatements.add(vf.createStatement(np.getUri(), NPA.IS_INTRODUCTION_OF, introNp.getUser(), NPA.GRAPH));
36✔
246
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:isIntroductionOf, AGENT, npa:graph, meta, linking intro nanopub to the agent it is introducing
247
            for (KeyDeclaration kc : introNp.getKeyDeclarations()) {
33✔
248
                metaStatements.add(vf.createStatement(np.getUri(), NPA.DECLARES_PUBKEY, vf.createLiteral(kc.getPublicKeyString()), NPA.GRAPH));
42✔
249
                // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:declaresPubkey, FULL_PUBKEY, npa:graph, meta, full pubkey declared by the given intro NANOPUB
250
            }
3✔
251
        }
252

253
        try {
254
            timestamp = SimpleTimestampPattern.getCreationTime(np);
12✔
255
        } catch (IllegalArgumentException ex) {
×
256
            notes.add("Illegal date/time");
×
257
        }
3✔
258
        if (timestamp != null) {
9!
259
            metaStatements.add(vf.createStatement(np.getUri(), DCTERMS.CREATED, vf.createLiteral(timestamp.getTime()), NPA.GRAPH));
45✔
260
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, dct:created, CREATION_DATE, npa:graph, meta, normalized creation timestamp
261
        }
262

263
        String literalFilter = "_pubkey_" + Utils.createHash(el.getPublicKeyString());
18✔
264
        for (IRI typeIri : NanopubUtils.getTypes(np)) {
33✔
265
            metaStatements.add(vf.createStatement(np.getUri(), NPX.HAS_NANOPUB_TYPE, typeIri, NPA.GRAPH));
33✔
266
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:hasNanopubType, NANOPUB_TYPE, npa:graph, meta, type of NANOPUB
267
            literalFilter += " _type_" + Utils.createHash(typeIri);
15✔
268
        }
3✔
269
        String label = NanopubUtils.getLabel(np);
9✔
270
        if (label != null) {
6!
271
            metaStatements.add(vf.createStatement(np.getUri(), RDFS.LABEL, vf.createLiteral(label), NPA.GRAPH));
39✔
272
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, rdfs:label, LABEL, npa:graph, meta, label of NANOPUB
273
        }
274
        String description = NanopubUtils.getDescription(np);
9✔
275
        if (description != null) {
6✔
276
            metaStatements.add(vf.createStatement(np.getUri(), DCTERMS.DESCRIPTION, vf.createLiteral(description), NPA.GRAPH));
39✔
277
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, dct:description, LABEL, npa:graph, meta, description of NANOPUB
278
        }
279
        for (IRI creatorIri : SimpleCreatorPattern.getCreators(np)) {
33✔
280
            metaStatements.add(vf.createStatement(np.getUri(), DCTERMS.CREATOR, creatorIri, NPA.GRAPH));
33✔
281
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, dct:creator, CREATOR, npa:graph, meta, creator of NANOPUB (can be several)
282
        }
3✔
283
        for (IRI authorIri : SimpleCreatorPattern.getAuthors(np)) {
21!
284
            metaStatements.add(vf.createStatement(np.getUri(), PAV.AUTHORED_BY, authorIri, NPA.GRAPH));
×
285
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, pav:authoredBy, AUTHOR, npa:graph, meta, author of NANOPUB (can be several)
286
        }
×
287

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

293
        // Any statements that express that the currently processed nanopub is already invalidated:
294
        List<Statement> invalidatingStatements = getInvalidatingStatements(np.getUri());
12✔
295

296
        metaStatements.addAll(invalidateStatements);
18✔
297

298
        allStatements = new ArrayList<>(nanopubStatements);
21✔
299
        allStatements.addAll(metaStatements);
18✔
300
        allStatements.addAll(invalidatingStatements);
15✔
301

302
        textStatements = new ArrayList<>(literalStatements);
21✔
303
        textStatements.addAll(metaStatements);
18✔
304
        textStatements.addAll(invalidatingStatements);
15✔
305

306
        if (FeatureFlags.spacesEnabled()) {
6!
307
            IRI signedBy = (el.getSigners().size() == 1) ? el.getSigners().iterator().next() : null;
42!
308
            String pubkeyHash = Utils.createHash(el.getPublicKeyString());
15✔
309
            Date createdAt = (timestamp != null) ? timestamp.getTime() : null;
24!
310
            SpacesExtractor.Context ctx = new SpacesExtractor.Context(ac, signedBy, pubkeyHash, createdAt);
24✔
311
            spaceExtractionStatements = SpacesExtractor.extract(np, ctx);
15✔
312
        }
313
    }
3✔
314

315
    /**
316
     * Get the HTTP client used for fetching nanopublications.
317
     *
318
     * @return the HTTP client
319
     */
320
    static HttpClient getHttpClient() {
321
        if (httpClient == null) {
6✔
322
            httpClient = HttpClientBuilder.create().setDefaultRequestConfig(Utils.getHttpRequestConfig()).build();
15✔
323
        }
324
        return httpClient;
6✔
325
    }
326

327
    /**
328
     * Load the given nanopublication into the database.
329
     *
330
     * @param nanopubUri Nanopublication identifier (URI)
331
     */
332
    public static void load(String nanopubUri) {
333
        if (isNanopubLoaded(nanopubUri)) {
9!
334
            log.info("Already loaded: {}", nanopubUri);
×
335
        } else {
336
            Nanopub np = GetNanopub.get(nanopubUri, getHttpClient());
12✔
337
            load(np, -1);
9✔
338
        }
339
    }
3✔
340

341
    /**
342
     * Load a nanopub into the database.
343
     *
344
     * @param np      the nanopub to load
345
     * @param counter the load counter, only used for logging (or -1 if not known)
346
     * @throws RDF4JException if the loading fails
347
     */
348
    public static void load(Nanopub np, long counter) throws RDF4JException {
349
        NanopubLoader loader = new NanopubLoader(np, counter);
18✔
350
        loader.executeLoading();
6✔
351
    }
3✔
352

353
    @GeneratedFlagForDependentElements
354
    private void executeLoading() {
355
        var runningTasks = new ArrayList<Future<?>>();
356
        Consumer<Runnable> runTask = t -> runningTasks.add(loadingPool.submit(t));
×
357

358
        for (String note : notes) {
359
            loadNoteToRepo(np.getUri(), note);
360
        }
361

362
        if (!aborted) {
363
            // Submit all tasks except the "meta" task
364
            if (timestamp != null) {
365
                if (new Date().getTime() - timestamp.getTimeInMillis() < THIRTY_DAYS) {
366
                    if (FeatureFlags.last30dRepoEnabled()) {
367
                        runTask.accept(() -> loadNanopubToLatest(np.getUri(), allStatements));
×
368
                    }
369
                }
370
            }
371

372
            if (FeatureFlags.textRepoEnabled()) {
373
                runTask.accept(() -> loadNanopubToRepo(np.getUri(), textStatements, "text"));
×
374
            }
375
            if (FeatureFlags.fullRepoEnabled()) {
376
                runTask.accept(() -> loadNanopubToRepo(np.getUri(), allStatements, "full"));
×
377
            }
378
            // Note: "meta" task is deferred until all other tasks complete successfully
379

380
            runTask.accept(() -> loadNanopubToRepo(np.getUri(), allStatements, "pubkey_" + Utils.createHash(el.getPublicKeyString())));
×
381
            //                loadNanopubToRepo(np.getUri(), textStatements, "text-pubkey_" + Utils.createHash(el.getPublicKeyString()));
382
            for (IRI typeIri : NanopubUtils.getTypes(np)) {
383
                // Exclude locally minted IRIs:
384
                if (typeIri.stringValue().startsWith(np.getUri().stringValue())) continue;
385
                if (!typeIri.stringValue().matches("https?://.*")) continue;
386
                runTask.accept(() -> loadNanopubToRepo(np.getUri(), allStatements, "type_" + Utils.createHash(typeIri)));
×
387
                //                        loadNanopubToRepo(np.getUri(), textStatements, "text-type_" + Utils.createHash(typeIri));
388
            }
389
            //                for (IRI creatorIri : SimpleCreatorPattern.getCreators(np)) {
390
            //                        // Exclude locally minted IRIs:
391
            //                        if (creatorIri.stringValue().startsWith(np.getUri().stringValue())) continue;
392
            //                        if (!creatorIri.stringValue().matches("https?://.*")) continue;
393
            //                        loadNanopubToRepo(np.getUri(), allStatements, "user_" + Utils.createHash(creatorIri));
394
            //                        loadNanopubToRepo(np.getUri(), textStatements, "text-user_" + Utils.createHash(creatorIri));
395
            //                }
396
            //                for (IRI authorIri : SimpleCreatorPattern.getAuthors(np)) {
397
            //                        // Exclude locally minted IRIs:
398
            //                        if (authorIri.stringValue().startsWith(np.getUri().stringValue())) continue;
399
            //                        if (!authorIri.stringValue().matches("https?://.*")) continue;
400
            //                        loadNanopubToRepo(np.getUri(), allStatements, "user_" + Utils.createHash(authorIri));
401
            //                        loadNanopubToRepo(np.getUri(), textStatements, "text-user_" + Utils.createHash(authorIri));
402
            //                }
403

404
            if (FeatureFlags.spacesEnabled() && !spaceExtractionStatements.isEmpty()) {
405
                runTask.accept(() -> loadToSpacesRepo(np.getUri(), allStatements, spaceExtractionStatements));
×
406
            }
407

408
            for (Statement st : invalidateStatements) {
409
                runTask.accept(() -> loadInvalidateStatements(np, el.getPublicKeyString(), st, pubkeyStatement, pubkeyStatementX));
×
410
            }
411

412
            // Wait for all non-meta tasks to complete successfully before submitting the meta task
413
            for (var task : runningTasks) {
414
                try {
415
                    task.get();
416
                } catch (ExecutionException | InterruptedException ex) {
417
                    throw new RuntimeException("Error in nanopub loading thread", ex.getCause());
418
                }
419
            }
420

421
            // Now submit and wait for the "meta" task after all other tasks have completed successfully
422
            Future<?> metaTask = loadingPool.submit(() -> loadNanopubToRepo(np.getUri(), metaStatements, "meta"));
×
423
            try {
424
                metaTask.get();
425
            } catch (ExecutionException | InterruptedException ex) {
426
                throw new RuntimeException("Error in nanopub loading thread (meta task)", ex.getCause());
427
            }
428
        }
429
    }
430

431
    private static Long lastUpdateOfLatestRepo = null;
6✔
432
    private static long THIRTY_DAYS = 1000L * 60 * 60 * 24 * 30;
6✔
433
    private static long ONE_HOUR = 1000L * 60 * 60;
6✔
434

435
    @GeneratedFlagForDependentElements
436
    private static void loadNanopubToLatest(IRI npId, List<Statement> statements) {
437
        boolean success = false;
438
        int retries = 0;
439
        while (!success) {
440
            RepositoryConnection conn = TripleStore.get().getRepoConnection("last30d");
441
            try (conn) {
442
                // Read committed, because deleting old nanopubs is idempotent. Inserts do not collide
443
                // with deletes, because we are not inserting old nanopubs.
444
                conn.begin(IsolationLevels.READ_COMMITTED);
445
                conn.add(statements);
446
                if (lastUpdateOfLatestRepo == null || new Date().getTime() - lastUpdateOfLatestRepo > ONE_HOUR) {
447
                    log.trace("Remove old nanopubs...");
448
                    Literal thirtyDaysAgo = vf.createLiteral(new Date(new Date().getTime() - THIRTY_DAYS));
449
                    TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, "SELECT * { graph <" + NPA.GRAPH + "> { " + "?np <" + DCTERMS.CREATED + "> ?date . " + "filter ( ?date < ?thirtydaysago ) " + "} }");
450
                    q.setBinding("thirtydaysago", thirtyDaysAgo);
451
                    try (TupleQueryResult r = q.evaluate()) {
452
                        while (r.hasNext()) {
453
                            BindingSet b = r.next();
454
                            IRI oldNpId = (IRI) b.getBinding("np").getValue();
455
                            log.trace("Remove old nanopub: {}", oldNpId);
456
                            for (Value v : Utils.getObjectsForPattern(conn, NPA.GRAPH, oldNpId, NPA.HAS_GRAPH)) {
457
                                // Remove all four nanopub graphs:
458
                                conn.remove((Resource) null, (IRI) null, (Value) null, (IRI) v);
459
                            }
460
                            // Remove nanopubs in admin graphs:
461
                            conn.remove(oldNpId, null, null, NPA.GRAPH);
462
                            conn.remove(oldNpId, null, null, NPA.NETWORK_GRAPH);
463
                        }
464
                    }
465
                    lastUpdateOfLatestRepo = new Date().getTime();
466
                }
467
                conn.commit();
468
                success = true;
469
            } catch (Exception ex) {
470
                log.warn("Could not load nanopub {} to last30d repo.", npId, ex);
471
                if (conn.isActive()) conn.rollback();
472
            }
473
            if (!success) {
474
                retries++;
475
                if (retries >= MAX_RETRIES) {
476
                    throw new RuntimeException("Failed to load nanopub " + npId + " to last30d repo after " + MAX_RETRIES + " retries");
477
                }
478
                long delay = computeBackoffMillis(retries);
479
                log.info("Retrying in {} ms for nanopub {} in last30d (attempt {}/{})...", delay, npId, retries, MAX_RETRIES);
480
                try {
481
                    Thread.sleep(delay);
482
                } catch (InterruptedException x) {
483
                    Thread.currentThread().interrupt();
484
                }
485
            }
486
        }
487
    }
488

489
    @GeneratedFlagForDependentElements
490
    private static void loadNanopubToRepo(IRI npId, List<Statement> statements, String repoName) {
491
        boolean success = false;
492
        int retries = 0;
493
        while (!success) {
494
            RepositoryConnection conn = TripleStore.get().getRepoConnection(repoName);
495
            try (conn) {
496
                // Serializable, because write skew would cause the chain of hashes to be broken.
497
                // The inserts must be done serially.
498
                conn.begin(IsolationLevels.SERIALIZABLE);
499
                var repoStatus = fetchRepoStatus(conn, npId);
500
                if (repoStatus.isLoaded) {
501
                    log.info("Already loaded: {}", npId);
502
                } else {
503
                    String newChecksum = NanopubUtils.updateXorChecksum(npId, repoStatus.checksum);
504
                    conn.remove(NPA.THIS_REPO, NPA.HAS_NANOPUB_COUNT, null, NPA.GRAPH);
505
                    conn.remove(NPA.THIS_REPO, NPA.HAS_NANOPUB_CHECKSUM, null, NPA.GRAPH);
506
                    conn.add(NPA.THIS_REPO, NPA.HAS_NANOPUB_COUNT, vf.createLiteral(repoStatus.count + 1), NPA.GRAPH);
507
                    // @ADMIN-TRIPLE-TABLE@ REPO, npa:hasNanopubCount, NANOPUB_COUNT, npa:graph, admin, number of nanopubs loaded
508
                    conn.add(NPA.THIS_REPO, NPA.HAS_NANOPUB_CHECKSUM, vf.createLiteral(newChecksum), NPA.GRAPH);
509
                    // @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)
510
                    conn.add(npId, NPA.HAS_LOAD_NUMBER, vf.createLiteral(repoStatus.count), NPA.GRAPH);
511
                    // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasLoadNumber, LOAD_NUMBER, npa:graph, admin, the sequential number at which this NANOPUB was loaded
512
                    conn.add(npId, NPA.HAS_LOAD_CHECKSUM, vf.createLiteral(newChecksum), NPA.GRAPH);
513
                    // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasLoadChecksum, LOAD_CHECKSUM, npa:graph, admin, the checksum of all loaded nanopubs after loading the given NANOPUB
514
                    conn.add(npId, NPA.HAS_LOAD_TIMESTAMP, vf.createLiteral(new Date()), NPA.GRAPH);
515
                    // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasLoadTimestamp, LOAD_TIMESTAMP, npa:graph, admin, the time point at which this NANOPUB was loaded
516
                    conn.add(statements);
517
                }
518
                conn.commit();
519
                success = true;
520
            } catch (Exception ex) {
521
                log.warn("Could not load nanopub {} to repo {}.", npId, repoName, ex);
522
                if (conn.isActive()) conn.rollback();
523
            }
524
            if (!success) {
525
                retries++;
526
                if (retries >= MAX_RETRIES) {
527
                    throw new RuntimeException("Failed to load nanopub " + npId + " to repo " + repoName + " after " + MAX_RETRIES + " retries");
528
                }
529
                long delay = computeBackoffMillis(retries);
530
                log.info("Retrying in {} ms for nanopub {} in repo {} (attempt {}/{})...", delay, npId, repoName, retries, MAX_RETRIES);
531
                try {
532
                    Thread.sleep(delay);
533
                } catch (InterruptedException x) {
534
                    Thread.currentThread().interrupt();
535
                }
536
            }
537
        }
538
    }
539

540
    /**
541
     * Writes the raw nanopub statements (all four graphs) into the {@code spaces}
542
     * repo alongside the pre-computed extraction statements (which target
543
     * {@code npa:spacesGraph}). Stamps the load-number on the nanopub IRI and bumps
544
     * {@code npa:thisRepo npa:currentLoadCounter} in {@code npa:graph}, all within
545
     * one serializable transaction.
546
     *
547
     * <p>Idempotent: if the nanopub already has a {@code npa:hasLoadNumber} stamp in
548
     * {@code npa:graph} of the {@code spaces} repo, this is a no-op.
549
     *
550
     * @param npId            nanopub IRI
551
     * @param nanopubTriples  raw nanopub statements (all four graphs + meta)
552
     * @param spaceExtraction summary triples destined for {@code npa:spacesGraph}
553
     */
554
    @GeneratedFlagForDependentElements
555
    private static void loadToSpacesRepo(IRI npId, List<Statement> nanopubTriples,
556
                                         List<Statement> spaceExtraction) {
557
        boolean success = false;
558
        int retries = 0;
559
        while (!success) {
560
            RepositoryConnection conn = TripleStore.get().getRepoConnection("spaces");
561
            try (conn) {
562
                conn.begin(IsolationLevels.SERIALIZABLE);
563
                // Idempotency: skip if this nanopub is already stamped in this repo.
564
                if (Utils.getObjectForPattern(conn, NPA.GRAPH, npId, NPA.HAS_LOAD_NUMBER) != null) {
565
                    conn.commit();
566
                    success = true;
567
                    continue;
568
                }
569
                long newCounter = fetchSpacesLoadCounter(conn) + 1;
570
                conn.remove(NPA.THIS_REPO,
571
                        com.knowledgepixels.query.vocabulary.SpacesVocab.CURRENT_LOAD_COUNTER,
572
                        null, NPA.GRAPH);
573
                conn.add(NPA.THIS_REPO,
574
                        com.knowledgepixels.query.vocabulary.SpacesVocab.CURRENT_LOAD_COUNTER,
575
                        vf.createLiteral(newCounter), NPA.GRAPH);
576
                conn.add(npId, NPA.HAS_LOAD_NUMBER, vf.createLiteral(newCounter), NPA.GRAPH);
577
                conn.add(nanopubTriples);
578
                conn.add(spaceExtraction);
579
                conn.commit();
580
                success = true;
581
            } catch (Exception ex) {
582
                log.warn("Could not load nanopub {} to spaces repo.", npId, ex);
583
                if (conn.isActive()) conn.rollback();
584
            }
585
            if (!success) {
586
                retries++;
587
                if (retries >= MAX_RETRIES) {
588
                    throw new RuntimeException("Failed to load nanopub " + npId + " to spaces repo after " + MAX_RETRIES + " retries");
589
                }
590
                long delay = computeBackoffMillis(retries);
591
                log.info("Retrying in {} ms for nanopub {} in spaces repo (attempt {}/{})...", delay, npId, retries, MAX_RETRIES);
592
                try {
593
                    Thread.sleep(delay);
594
                } catch (InterruptedException x) {
595
                    Thread.currentThread().interrupt();
596
                }
597
            }
598
        }
599
    }
600

601
    private static long fetchSpacesLoadCounter(RepositoryConnection conn) {
602
        Value v = Utils.getObjectForPattern(conn, NPA.GRAPH, NPA.THIS_REPO,
×
603
                com.knowledgepixels.query.vocabulary.SpacesVocab.CURRENT_LOAD_COUNTER);
604
        if (v == null) return 0;
×
605
        try {
606
            return Long.parseLong(v.stringValue());
×
607
        } catch (NumberFormatException ex) {
×
608
            log.warn("Invalid npa:currentLoadCounter literal in spaces repo: {}", v);
×
609
            return 0;
×
610
        }
611
    }
612

613
    private record RepoStatus(boolean isLoaded, long count, String checksum) {
×
614
    }
615

616
    /**
617
     * To execute before loading a nanopub: check if the nanopub is already loaded and what is the
618
     * current load counter and checksum. This effectively batches three queries into one.
619
     * This method must be called from within a transaction.
620
     *
621
     * @param conn repo connection
622
     * @param npId nanopub ID
623
     * @return the current status
624
     */
625
    @GeneratedFlagForDependentElements
626
    private static RepoStatus fetchRepoStatus(RepositoryConnection conn, IRI npId) {
627
        var result = conn.prepareTupleQuery(QueryLanguage.SPARQL, REPO_STATUS_QUERY_TEMPLATE.formatted(npId)).evaluate();
628
        try (result) {
629
            if (!result.hasNext()) {
630
                // This may happen if the repo was created, but is completely empty.
631
                return new RepoStatus(false, 0, NanopubUtils.INIT_CHECKSUM);
632
            }
633
            var row = result.next();
634
            return new RepoStatus(row.hasBinding("loadNumber"), Long.parseLong(row.getBinding("count").getValue().stringValue()), row.getBinding("checksum").getValue().stringValue());
635
        }
636
    }
637

638
    @GeneratedFlagForDependentElements
639
    private static void loadInvalidateStatements(Nanopub thisNp, String thisPubkey, Statement invalidateStatement, Statement pubkeyStatement, Statement pubkeyStatementX) {
640
        boolean success = false;
641
        int retries = 0;
642
        while (!success) {
643
            List<RepositoryConnection> connections = new ArrayList<>();
644
            RepositoryConnection metaConn = TripleStore.get().getRepoConnection("meta");
645
            try {
646
                IRI invalidatedNpId = (IRI) invalidateStatement.getObject();
647
                // Basic isolation because here we only read append-only data.
648
                metaConn.begin(IsolationLevels.READ_COMMITTED);
649

650
                Value pubkeyValue = Utils.getObjectForPattern(metaConn, NPA.GRAPH, invalidatedNpId, NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY);
651
                if (pubkeyValue != null) {
652
                    String pubkey = pubkeyValue.stringValue();
653

654
                    if (!pubkey.equals(thisPubkey)) {
655
                        //log.info("Adding invalidation expressed in " + thisNp.getUri() + " also to repo for pubkey " + pubkey);
656
                        connections.add(loadStatements("pubkey_" + Utils.createHash(pubkey), invalidateStatement, pubkeyStatement, pubkeyStatementX));
657
//                                                connections.add(loadStatements("text-pubkey_" + Utils.createHash(pubkey), invalidateStatement, pubkeyStatement));
658
                    }
659

660
                    // Collect target types so we can both propagate per-type AND decide
661
                    // whether the target is space-relevant in a single pass over meta.
662
                    Set<IRI> targetTypes = new HashSet<>();
663
                    for (Value v : Utils.getObjectsForPattern(metaConn, NPA.GRAPH, invalidatedNpId, NPX.HAS_NANOPUB_TYPE)) {
664
                        if (v instanceof IRI typeIri) targetTypes.add(typeIri);
665
                    }
666
                    Set<IRI> thisNpTypes = NanopubUtils.getTypes(thisNp);
667
                    for (IRI typeIri : targetTypes) {
668
                        if (!thisNpTypes.contains(typeIri)) {
669
                            //log.info("Adding invalidation expressed in " + thisNp.getUri() + " also to repo for type " + typeIri);
670
                            connections.add(loadStatements("type_" + Utils.createHash(typeIri), invalidateStatement, pubkeyStatement, pubkeyStatementX));
671
//                                                        connections.add(loadStatements("text-type_" + Utils.createHash(typeIri), invalidateStatement, pubkeyStatement));
672
                        }
673
                    }
674

675
                    // Emit an npa:Invalidation entry into the spaces repo if the target
676
                    // had any space-relevant type. Piggybacks on the type lookup above
677
                    // so we don't re-read meta.
678
                    if (FeatureFlags.spacesEnabled() && SpacesExtractor.isSpaceRelevant(targetTypes)) {
679
                        List<Statement> invEntry = buildInvalidationEntry(thisNp, invalidatedNpId, targetTypes, thisPubkey);
680
                        if (!invEntry.isEmpty()) {
681
                            connections.add(loadStatements("spaces", invEntry.toArray(new Statement[0])));
682
                        }
683
                    }
684

685
//                                        for (Value v : Utils.getObjectsForPattern(metaConn, NPA.GRAPH, invalidatedNpId, DCTERMS.CREATOR)) {
686
//                                                IRI creatorIri = (IRI) v;
687
//                                                if (!SimpleCreatorPattern.getCreators(thisNp).contains(creatorIri)) {
688
//                                                        //log.info("Adding invalidation expressed in " + thisNp.getUri() + " also to repo for user " + creatorIri);
689
//                                                        connections.add(loadStatements("user_" + Utils.createHash(creatorIri), invalidateStatement, pubkeyStatement));
690
//                                                        connections.add(loadStatements("text-user_" + Utils.createHash(creatorIri), invalidateStatement, pubkeyStatement));
691
//                                                }
692
//                                        }
693
                }
694

695
                metaConn.commit();
696
                // TODO handle case that some commits succeed and some fail
697
                for (RepositoryConnection c : connections) c.commit();
698
                success = true;
699
            } catch (Exception ex) {
700
                log.warn("Could not load invalidate statements for {}.", thisNp.getUri(), ex);
701
                if (metaConn.isActive()) metaConn.rollback();
702
                for (RepositoryConnection c : connections) {
703
                    if (c.isActive()) c.rollback();
704
                }
705
            } finally {
706
                metaConn.close();
707
                for (RepositoryConnection c : connections) c.close();
708
            }
709
            if (!success) {
710
                retries++;
711
                if (retries >= MAX_RETRIES) {
712
                    throw new RuntimeException("Failed to load invalidate statements for " + thisNp.getUri() + " after " + MAX_RETRIES + " retries");
713
                }
714
                long delay = computeBackoffMillis(retries);
715
                log.info("Retrying in {} ms for invalidate statements of {} (attempt {}/{})...", delay, thisNp.getUri(), retries, MAX_RETRIES);
716
                try {
717
                    Thread.sleep(delay);
718
                } catch (InterruptedException x) {
719
                    Thread.currentThread().interrupt();
720
                }
721
            }
722
        }
723
    }
724

725
    /**
726
     * Builds the statements for an {@code npa:Invalidation} entry describing a
727
     * space-relevant retraction/supersession. Caller writes these into the
728
     * {@code spaces} repo.
729
     *
730
     * @param thisNp        the invalidator nanopub
731
     * @param invalidatedNp IRI of the invalidated target
732
     * @param targetTypes   the target's types (already read from the meta repo)
733
     * @param thisPubkey    the invalidator's signing pubkey
734
     * @return the invalidation-entry statements, or an empty list if no signer is known
735
     */
736
    private static List<Statement> buildInvalidationEntry(Nanopub thisNp, IRI invalidatedNp,
737
                                                          Set<IRI> targetTypes, String thisPubkey) {
738
        String artifactCode = TrustyUriUtils.getArtifactCode(thisNp.getUri().toString());
×
739
        if (artifactCode == null || artifactCode.isEmpty()) return Collections.emptyList();
×
740
        IRI signer = null;
×
741
        for (Statement st : thisNp.getPubinfo()) {
×
742
            if (!st.getSubject().equals(thisNp.getUri())) continue;
×
743
            if (!st.getPredicate().equals(NPX.SIGNED_BY)) continue;
×
744
            if (st.getObject() instanceof IRI signerIri) {
×
745
                signer = signerIri;
×
746
                break;
×
747
            }
748
        }
×
749
        Date createdAt = null;
×
750
        try {
751
            Calendar ts = SimpleTimestampPattern.getCreationTime(thisNp);
×
752
            if (ts != null) createdAt = ts.getTime();
×
753
        } catch (IllegalArgumentException ignored) {
×
754
            // pubinfo timestamp missing or malformed; extraction entry simply omits dct:created.
755
        }
×
756
        SpacesExtractor.Context ctx = new SpacesExtractor.Context(
×
757
                artifactCode, signer, Utils.createHash(thisPubkey), createdAt);
×
758
        return SpacesExtractor.extractInvalidation(thisNp, invalidatedNp, targetTypes, ctx);
×
759
    }
760

761
    @GeneratedFlagForDependentElements
762
    private static RepositoryConnection loadStatements(String repoName, Statement... statements) {
763
        RepositoryConnection conn = TripleStore.get().getRepoConnection(repoName);
764
        // Basic isolation: we only append new statements
765
        conn.begin(IsolationLevels.READ_COMMITTED);
766
        for (Statement st : statements) {
767
            conn.add(st);
768
        }
769
        return conn;
770
    }
771

772
    @GeneratedFlagForDependentElements
773
    static List<Statement> getInvalidatingStatements(IRI npId) {
774
        List<Statement> invalidatingStatements = new ArrayList<>();
775
        boolean success = false;
776
        int retries = 0;
777
        while (!success) {
778
            RepositoryConnection conn = TripleStore.get().getRepoConnection("meta");
779
            try (conn) {
780
                // Basic isolation because here we only read append-only data.
781
                conn.begin(IsolationLevels.READ_COMMITTED);
782

783
                TupleQueryResult r = conn.prepareTupleQuery(QueryLanguage.SPARQL, "SELECT * { graph <" + NPA.GRAPH + "> { " + "?np <" + NPX.INVALIDATES + "> <" + npId + "> ; <" + NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY + "> ?pubkey . " + "} }").evaluate();
784
                try (r) {
785
                    while (r.hasNext()) {
786
                        BindingSet b = r.next();
787
                        invalidatingStatements.add(vf.createStatement((IRI) b.getBinding("np").getValue(), NPX.INVALIDATES, npId, NPA.GRAPH));
788
                        invalidatingStatements.add(vf.createStatement((IRI) b.getBinding("np").getValue(), NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY, b.getBinding("pubkey").getValue(), NPA.GRAPH));
789
                    }
790
                }
791
                conn.commit();
792
                success = true;
793
            } catch (Exception ex) {
794
                log.warn("Could not load invalidating statements for {}.", npId, ex);
795
                if (conn.isActive()) conn.rollback();
796
            }
797
            if (!success) {
798
                retries++;
799
                if (retries >= MAX_RETRIES) {
800
                    throw new RuntimeException("Failed to get invalidating statements for " + npId + " after " + MAX_RETRIES + " retries");
801
                }
802
                long delay = computeBackoffMillis(retries);
803
                log.info("Retrying in {} ms for invalidating statements of {} (attempt {}/{})...", delay, npId, retries, MAX_RETRIES);
804
                try {
805
                    Thread.sleep(delay);
806
                } catch (InterruptedException x) {
807
                    Thread.currentThread().interrupt();
808
                }
809
            }
810
        }
811
        return invalidatingStatements;
812
    }
813

814
    @GeneratedFlagForDependentElements
815
    private static void loadNoteToRepo(Resource subj, String note) {
816
        boolean success = false;
817
        int retries = 0;
818
        while (!success) {
819
            RepositoryConnection conn = TripleStore.get().getAdminRepoConnection();
820
            try (conn) {
821
                List<Statement> statements = new ArrayList<>();
822
                statements.add(vf.createStatement(subj, NPA.NOTE, vf.createLiteral(note), NPA.GRAPH));
823
                conn.add(statements);
824
                success = true;
825
            } catch (Exception ex) {
826
                log.warn("Could not load note to repo for {}.", subj, ex);
827
            }
828
            if (!success) {
829
                retries++;
830
                if (retries >= MAX_RETRIES) {
831
                    throw new RuntimeException("Failed to load note to repo for " + subj + " after " + MAX_RETRIES + " retries");
832
                }
833
                long delay = computeBackoffMillis(retries);
834
                log.info("Retrying in {} ms for note on {} (attempt {}/{})...", delay, subj, retries, MAX_RETRIES);
835
                try {
836
                    Thread.sleep(delay);
837
                } catch (InterruptedException x) {
838
                    Thread.currentThread().interrupt();
839
                }
840
            }
841
        }
842
    }
843

844
    static boolean hasValidSignature(NanopubSignatureElement el) {
845
        try {
846
            if (el != null && SignatureUtils.hasValidSignature(el) && el.getPublicKeyString() != null) {
24!
847
                return true;
6✔
848
            }
849
        } catch (GeneralSecurityException ex) {
3✔
850
            log.warn("Signature validation failed for signature element {}", el.getUri(), ex);
18✔
851
        }
3✔
852
        return false;
6✔
853
    }
854

855
    private static IRI getBaseTrustyUri(Value v) {
856
        if (!(v instanceof IRI)) return null;
9!
857
        String s = v.stringValue();
9✔
858
        if (!s.matches(".*[^A-Za-z0-9\\-_]RA[A-Za-z0-9\\-_]{43}([^A-Za-z0-9\\\\-_].{0,43})?")) {
12✔
859
            return null;
6✔
860
        }
861
        return vf.createIRI(s.replaceFirst("^(.*[^A-Za-z0-9\\-_]RA[A-Za-z0-9\\-_]{43})([^A-Za-z0-9\\\\-_].{0,43})?$", "$1"));
21✔
862
    }
863

864
    // TODO: Move this to nanopub library:
865
    private static boolean isIntroNanopub(Nanopub np) {
866
        for (Statement st : np.getAssertion()) {
33✔
867
            if (st.getPredicate().equals(NPX.DECLARED_BY)) return true;
21✔
868
        }
3✔
869
        return false;
6✔
870
    }
871

872
    /**
873
     * Check if a nanopub is already loaded in the admin graph.
874
     *
875
     * @param npId the nanopub ID
876
     * @return true if the nanopub is loaded, false otherwise
877
     */
878
    @GeneratedFlagForDependentElements
879
    static boolean isNanopubLoaded(String npId) {
880
        boolean loaded = false;
881
        RepositoryConnection conn = TripleStore.get().getRepoConnection("meta");
882
        try (conn) {
883
            if (Utils.getObjectForPattern(conn, NPA.GRAPH, vf.createIRI(npId), NPA.HAS_LOAD_NUMBER) != null) {
884
                loaded = true;
885
            }
886
        } catch (Exception ex) {
887
            log.warn("Could not check whether nanopub is loaded.", ex);
888
        }
889
        return loaded;
890
    }
891

892
    private static ValueFactory vf = SimpleValueFactory.getInstance();
6✔
893

894
    // TODO remove the constants and use the ones from the nanopub library instead
895

896
    /**
897
     * Template for the query that fetches the status of a repository.
898
     */
899
    // Template for .fetchRepoStatus
900
    private static final String REPO_STATUS_QUERY_TEMPLATE = """
84✔
901
            SELECT * { graph <%s> {
902
              OPTIONAL { <%s> <%s> ?loadNumber . }
903
              <%s> <%s> ?count ;
904
                   <%s> ?checksum .
905
            } }
906
            """.formatted(NPA.GRAPH, "%s", NPA.HAS_LOAD_NUMBER, NPA.THIS_REPO, NPA.HAS_NANOPUB_COUNT, NPA.HAS_NANOPUB_CHECKSUM);
6✔
907
}
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