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

knowledgepixels / nanopub-query / 24516301207

16 Apr 2026 02:35PM UTC coverage: 68.052% (+1.4%) from 66.641%
24516301207

push

github

web-flow
Merge pull request #64 from knowledgepixels/feature/62-space-repositories

feat: add SpaceRegistry skeleton (step 1 of #62)

251 of 406 branches covered (61.82%)

Branch coverage included in aggregate %.

699 of 990 relevant lines covered (70.61%)

10.42 hits per line

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

82.61
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
import com.knowledgepixels.query.vocabulary.GEN;
35

36
import java.security.GeneralSecurityException;
37
import java.util.*;
38
import java.util.concurrent.ExecutionException;
39
import java.util.concurrent.Executors;
40
import java.util.concurrent.Future;
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
    private static final int MAX_RETRIES = 30;
52
    private static final int RETRY_DELAY_MS = 10000;
53
    private Nanopub np;
54
    private NanopubSignatureElement el = null;
9✔
55
    private List<Statement> metaStatements = new ArrayList<>();
15✔
56
    private List<Statement> nanopubStatements = new ArrayList<>();
15✔
57
    private List<Statement> literalStatements = new ArrayList<>();
15✔
58
    private List<Statement> invalidateStatements = new ArrayList<>();
15✔
59
    private List<Statement> textStatements, allStatements;
60
    private Calendar timestamp = null;
9✔
61
    private Statement pubkeyStatement, pubkeyStatementX;
62
    private List<String> notes = new ArrayList<>();
15✔
63
    private boolean aborted = false;
9✔
64
    private Set<String> detectedSpaceRefs = Collections.emptySet();
9✔
65
    private static final Logger log = LoggerFactory.getLogger(NanopubLoader.class);
9✔
66

67

68
    NanopubLoader(Nanopub np, long counter) {
6✔
69
        this.np = np;
9✔
70
        if (counter >= 0) {
12✔
71
            log.info("Loading {}: {}", counter, np.getUri());
24✔
72
        } else {
73
            log.info("Loading: {}", np.getUri());
15✔
74
        }
75

76
        // TODO Ensure proper synchronization and DB rollbacks
77

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

80
        String ac = TrustyUriUtils.getArtifactCode(np.getUri().toString());
15✔
81
        if (!np.getHeadUri().toString().contains(ac) || !np.getAssertionUri().toString().contains(ac) || !np.getProvenanceUri().toString().contains(ac) || !np.getPubinfoUri().toString().contains(ac)) {
72!
82
            notes.add("could not load nanopub as not all graphs contained the artifact code");
×
83
            aborted = true;
×
84
            return;
×
85
        }
86

87
        try {
88
            el = SignatureUtils.getSignatureElement(np);
12✔
89
        } catch (MalformedCryptoElementException ex) {
×
90
            notes.add("Signature error");
×
91
        }
3✔
92
        if (!hasValidSignature(el)) {
12✔
93
            aborted = true;
9✔
94
            return;
3✔
95
        }
96

97
        pubkeyStatement = vf.createStatement(np.getUri(), NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY, vf.createLiteral(el.getPublicKeyString()), NPA.GRAPH);
39✔
98
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasValidSignatureForPublicKey, FULL_PUBKEY, npa:graph, meta, full pubkey if signature is valid
99
        metaStatements.add(pubkeyStatement);
18✔
100
        pubkeyStatementX = vf.createStatement(np.getUri(), NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY_HASH, vf.createLiteral(Utils.createHash(el.getPublicKeyString())), NPA.GRAPH);
42✔
101
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasValidSignatureForPublicKeyHash, PUBKEY_HASH, npa:graph, meta, hex-encoded SHA256 hash if signature is valid
102
        metaStatements.add(pubkeyStatementX);
18✔
103

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

109
        Set<IRI> subIris = new HashSet<>();
12✔
110
        Set<IRI> otherNps = new HashSet<>();
12✔
111
        Set<IRI> invalidated = new HashSet<>();
12✔
112
        Set<IRI> retracted = new HashSet<>();
12✔
113
        Set<IRI> superseded = new HashSet<>();
12✔
114
        String combinedLiterals = "";
6✔
115
        for (Statement st : NanopubUtils.getStatements(np)) {
33✔
116
            nanopubStatements.add(st);
15✔
117

118
            if (st.getPredicate().toString().contains(ac)) {
18!
119
                subIris.add(st.getPredicate());
×
120
            } else {
121
                IRI b = getBaseTrustyUri(st.getPredicate());
12✔
122
                if (b != null) otherNps.add(b);
6!
123
            }
124
            if (st.getPredicate().equals(NPX.RETRACTS) && st.getObject() instanceof IRI) {
15!
125
                retracted.add((IRI) st.getObject());
×
126
            }
127
            if (st.getPredicate().equals(NPX.INVALIDATES) && st.getObject() instanceof IRI) {
15!
128
                invalidated.add((IRI) st.getObject());
×
129
            }
130
            if (st.getSubject().equals(np.getUri()) && st.getObject() instanceof IRI) {
30✔
131
                if (st.getPredicate().equals(NPX.SUPERSEDES)) {
15✔
132
                    superseded.add((IRI) st.getObject());
18✔
133
                }
134
                if (st.getObject().toString().matches(".*[^A-Za-z0-9\\-_]RA[A-Za-z0-9\\-_]{43}")) {
18✔
135
                    metaStatements.add(vf.createStatement(np.getUri(), st.getPredicate(), st.getObject(), NPA.NETWORK_GRAPH));
39✔
136
                    // @ADMIN-TRIPLE-TABLE@ NANOPUB1, RELATION, NANOPUB2, npa:networkGraph, meta, any inter-nanopub relation found in NANOPUB1
137
                }
138
                if (st.getContext().equals(np.getPubinfoUri())) {
18✔
139
                    if (st.getPredicate().equals(NPX.INTRODUCES) || st.getPredicate().equals(NPX.DESCRIBES) || st.getPredicate().equals(NPX.EMBEDS)) {
45!
140
                        metaStatements.add(vf.createStatement(np.getUri(), st.getPredicate(), st.getObject(), NPA.GRAPH));
39✔
141
                        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:introduces, THING, npa:graph, meta, when such a triple is present in pubinfo of NANOPUB
142
                        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:describes, THING, npa:graph, meta, when such a triple is present in pubinfo of NANOPUB
143
                        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:embeds, THING, npa:graph, meta, when such a triple is present in pubinfo of NANOPUB
144
                    }
145
                }
146
            }
147
            if (st.getSubject().toString().contains(ac)) {
18✔
148
                subIris.add((IRI) st.getSubject());
21✔
149
            } else {
150
                IRI b = getBaseTrustyUri(st.getSubject());
12✔
151
                if (b != null) otherNps.add(b);
6!
152
            }
153
            if (st.getObject() instanceof IRI) {
12✔
154
                if (st.getObject().toString().contains(ac)) {
18✔
155
                    subIris.add((IRI) st.getObject());
21✔
156
                } else {
157
                    IRI b = getBaseTrustyUri(st.getObject());
12✔
158
                    if (b != null) otherNps.add(b);
18✔
159
                }
3✔
160
            } else {
161
                combinedLiterals += st.getObject().stringValue().replaceAll("\\s+", " ") + "\n";
27✔
162
//                                if (st.getSubject().equals(np.getUri()) && !st.getSubject().equals(HAS_FILTER_LITERAL)) {
163
//                                        literalStatements.add(vf.createStatement(np.getUri(), st.getPredicate(), st.getObject(), LITERAL_GRAPH));
164
//                                } else {
165
//                                        literalStatements.add(vf.createStatement(np.getUri(), HAS_LITERAL, st.getObject(), LITERAL_GRAPH));
166
//                                }
167
            }
168
        }
3✔
169
        subIris.remove(np.getUri());
15✔
170
        subIris.remove(np.getAssertionUri());
15✔
171
        subIris.remove(np.getProvenanceUri());
15✔
172
        subIris.remove(np.getPubinfoUri());
15✔
173
        for (IRI i : subIris) {
30✔
174
            metaStatements.add(vf.createStatement(np.getUri(), NPA.HAS_SUB_IRI, i, NPA.GRAPH));
33✔
175
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasSubIri, SUB_IRI, npa:graph, meta, for any IRI minted in the namespace of the NANOPUB
176
        }
3✔
177
        for (IRI i : otherNps) {
30✔
178
            metaStatements.add(vf.createStatement(np.getUri(), NPA.REFERS_TO_NANOPUB, i, NPA.NETWORK_GRAPH));
33✔
179
            // @ADMIN-TRIPLE-TABLE@ NANOPUB1, npa:refersToNanopub, NANOPUB2, npa:networkGraph, meta, generic inter-nanopub relation
180
        }
3✔
181
        for (IRI i : invalidated) {
18!
182
            invalidateStatements.add(vf.createStatement(np.getUri(), NPX.INVALIDATES, i, NPA.GRAPH));
×
183
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:invalidates, INVALIDATED_NANOPUB, npa:graph, meta, if the NANOPUB retracts or supersedes another nanopub
184
        }
×
185
        for (IRI i : retracted) {
18!
186
            invalidateStatements.add(vf.createStatement(np.getUri(), NPX.INVALIDATES, i, NPA.GRAPH));
×
187
            metaStatements.add(vf.createStatement(np.getUri(), NPX.RETRACTS, i, NPA.GRAPH));
×
188
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:retracts, RETRACTED_NANOPUB, npa:graph, meta, if the NANOPUB retracts another nanopub
189
        }
×
190
        for (IRI i : superseded) {
30✔
191
            invalidateStatements.add(vf.createStatement(np.getUri(), NPX.INVALIDATES, i, NPA.GRAPH));
33✔
192
            metaStatements.add(vf.createStatement(np.getUri(), NPX.SUPERSEDES, i, NPA.GRAPH));
33✔
193
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:supersedes, SUPERSEDED_NANOPUB, npa:graph, meta, if the NANOPUB supersedes another nanopub
194
        }
3✔
195

196
        metaStatements.add(vf.createStatement(np.getUri(), NPA.HAS_HEAD_GRAPH, np.getHeadUri(), NPA.GRAPH));
36✔
197
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasHeadGraph, HEAD_GRAPH, npa:graph, meta, direct link to the head graph of the NANOPUB
198
        metaStatements.add(vf.createStatement(np.getUri(), NPA.HAS_GRAPH, np.getHeadUri(), NPA.GRAPH));
36✔
199
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasGraph, GRAPH, npa:graph, meta, generic link to all four graphs of the given NANOPUB
200
        metaStatements.add(vf.createStatement(np.getUri(), NP.HAS_ASSERTION, np.getAssertionUri(), NPA.GRAPH));
36✔
201
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, np:hasAssertion, ASSERTION_GRAPH, npa:graph, meta, direct link to the assertion graph of the NANOPUB
202
        metaStatements.add(vf.createStatement(np.getUri(), NPA.HAS_GRAPH, np.getAssertionUri(), NPA.GRAPH));
36✔
203
        metaStatements.add(vf.createStatement(np.getUri(), NP.HAS_PROVENANCE, np.getProvenanceUri(), NPA.GRAPH));
36✔
204
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, np:hasProvenance, PROVENANCE_GRAPH, npa:graph, meta, direct link to the provenance graph of the NANOPUB
205
        metaStatements.add(vf.createStatement(np.getUri(), NPA.HAS_GRAPH, np.getProvenanceUri(), NPA.GRAPH));
36✔
206
        metaStatements.add(vf.createStatement(np.getUri(), NP.HAS_PUBINFO, np.getPubinfoUri(), NPA.GRAPH));
36✔
207
        // @ADMIN-TRIPLE-TABLE@ NANOPUB, np:hasPublicationInfo, PUBINFO_GRAPH, npa:graph, meta, direct link to the pubinfo graph of the NANOPUB
208
        metaStatements.add(vf.createStatement(np.getUri(), NPA.HAS_GRAPH, np.getPubinfoUri(), NPA.GRAPH));
36✔
209

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

214
        if (isIntroNanopub(np)) {
9✔
215
            IntroNanopub introNp = new IntroNanopub(np);
15✔
216
            metaStatements.add(vf.createStatement(np.getUri(), NPA.IS_INTRODUCTION_OF, introNp.getUser(), NPA.GRAPH));
36✔
217
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:isIntroductionOf, AGENT, npa:graph, meta, linking intro nanopub to the agent it is introducing
218
            for (KeyDeclaration kc : introNp.getKeyDeclarations()) {
33✔
219
                metaStatements.add(vf.createStatement(np.getUri(), NPA.DECLARES_PUBKEY, vf.createLiteral(kc.getPublicKeyString()), NPA.GRAPH));
42✔
220
                // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:declaresPubkey, FULL_PUBKEY, npa:graph, meta, full pubkey declared by the given intro NANOPUB
221
            }
3✔
222
        }
223

224
        try {
225
            timestamp = SimpleTimestampPattern.getCreationTime(np);
12✔
226
        } catch (IllegalArgumentException ex) {
×
227
            notes.add("Illegal date/time");
×
228
        }
3✔
229
        if (timestamp != null) {
9!
230
            metaStatements.add(vf.createStatement(np.getUri(), DCTERMS.CREATED, vf.createLiteral(timestamp.getTime()), NPA.GRAPH));
45✔
231
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, dct:created, CREATION_DATE, npa:graph, meta, normalized creation timestamp
232
        }
233

234
        String literalFilter = "_pubkey_" + Utils.createHash(el.getPublicKeyString());
18✔
235
        for (IRI typeIri : NanopubUtils.getTypes(np)) {
33✔
236
            metaStatements.add(vf.createStatement(np.getUri(), NPX.HAS_NANOPUB_TYPE, typeIri, NPA.GRAPH));
33✔
237
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, npx:hasNanopubType, NANOPUB_TYPE, npa:graph, meta, type of NANOPUB
238
            literalFilter += " _type_" + Utils.createHash(typeIri);
15✔
239
        }
3✔
240
        detectedSpaceRefs = detectAndRegisterSpaces(np);
12✔
241
        String label = NanopubUtils.getLabel(np);
9✔
242
        if (label != null) {
6!
243
            metaStatements.add(vf.createStatement(np.getUri(), RDFS.LABEL, vf.createLiteral(label), NPA.GRAPH));
39✔
244
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, rdfs:label, LABEL, npa:graph, meta, label of NANOPUB
245
        }
246
        String description = NanopubUtils.getDescription(np);
9✔
247
        if (description != null) {
6✔
248
            metaStatements.add(vf.createStatement(np.getUri(), DCTERMS.DESCRIPTION, vf.createLiteral(description), NPA.GRAPH));
39✔
249
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, dct:description, LABEL, npa:graph, meta, description of NANOPUB
250
        }
251
        for (IRI creatorIri : SimpleCreatorPattern.getCreators(np)) {
33✔
252
            metaStatements.add(vf.createStatement(np.getUri(), DCTERMS.CREATOR, creatorIri, NPA.GRAPH));
33✔
253
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, dct:creator, CREATOR, npa:graph, meta, creator of NANOPUB (can be several)
254
        }
3✔
255
        for (IRI authorIri : SimpleCreatorPattern.getAuthors(np)) {
21!
256
            metaStatements.add(vf.createStatement(np.getUri(), PAV.AUTHORED_BY, authorIri, NPA.GRAPH));
×
257
            // @ADMIN-TRIPLE-TABLE@ NANOPUB, pav:authoredBy, AUTHOR, npa:graph, meta, author of NANOPUB (can be several)
258
        }
×
259

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

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

268
        metaStatements.addAll(invalidateStatements);
18✔
269

270
        allStatements = new ArrayList<>(nanopubStatements);
21✔
271
        allStatements.addAll(metaStatements);
18✔
272
        allStatements.addAll(invalidatingStatements);
15✔
273

274
        textStatements = new ArrayList<>(literalStatements);
21✔
275
        textStatements.addAll(metaStatements);
18✔
276
        textStatements.addAll(invalidatingStatements);
15✔
277
    }
3✔
278

279
    /**
280
     * Get the HTTP client used for fetching nanopublications.
281
     *
282
     * @return the HTTP client
283
     */
284
    static HttpClient getHttpClient() {
285
        if (httpClient == null) {
6✔
286
            httpClient = HttpClientBuilder.create().setDefaultRequestConfig(Utils.getHttpRequestConfig()).build();
15✔
287
        }
288
        return httpClient;
6✔
289
    }
290

291
    /**
292
     * Load the given nanopublication into the database.
293
     *
294
     * @param nanopubUri Nanopublication identifier (URI)
295
     */
296
    public static void load(String nanopubUri) {
297
        if (isNanopubLoaded(nanopubUri)) {
9!
298
            log.info("Already loaded: {}", nanopubUri);
×
299
        } else {
300
            Nanopub np = GetNanopub.get(nanopubUri, getHttpClient());
12✔
301
            load(np, -1);
9✔
302
        }
303
    }
3✔
304

305
    /**
306
     * Load a nanopub into the database.
307
     *
308
     * @param np      the nanopub to load
309
     * @param counter the load counter, only used for logging (or -1 if not known)
310
     * @throws RDF4JException if the loading fails
311
     */
312
    public static void load(Nanopub np, long counter) throws RDF4JException {
313
        NanopubLoader loader = new NanopubLoader(np, counter);
18✔
314
        loader.executeLoading();
6✔
315
    }
3✔
316

317
    @GeneratedFlagForDependentElements
318
    private void executeLoading() {
319
        var runningTasks = new ArrayList<Future<?>>();
320
        Consumer<Runnable> runTask = t -> runningTasks.add(loadingPool.submit(t));
×
321

322
        for (String note : notes) {
323
            loadNoteToRepo(np.getUri(), note);
324
        }
325

326
        if (!aborted) {
327
            // Submit all tasks except the "meta" task
328
            if (timestamp != null) {
329
                if (new Date().getTime() - timestamp.getTimeInMillis() < THIRTY_DAYS) {
330
                    runTask.accept(() -> loadNanopubToLatest(allStatements));
×
331
                }
332
            }
333

334
            runTask.accept(() -> loadNanopubToRepo(np.getUri(), textStatements, "text"));
×
335
            runTask.accept(() -> loadNanopubToRepo(np.getUri(), allStatements, "full"));
×
336
            // Note: "meta" task is deferred until all other tasks complete successfully
337

338
            runTask.accept(() -> loadNanopubToRepo(np.getUri(), allStatements, "pubkey_" + Utils.createHash(el.getPublicKeyString())));
×
339
            //                loadNanopubToRepo(np.getUri(), textStatements, "text-pubkey_" + Utils.createHash(el.getPublicKeyString()));
340
            for (IRI typeIri : NanopubUtils.getTypes(np)) {
341
                // Exclude locally minted IRIs:
342
                if (typeIri.stringValue().startsWith(np.getUri().stringValue())) continue;
343
                if (!typeIri.stringValue().matches("https?://.*")) continue;
344
                runTask.accept(() -> loadNanopubToRepo(np.getUri(), allStatements, "type_" + Utils.createHash(typeIri)));
×
345
                //                        loadNanopubToRepo(np.getUri(), textStatements, "text-type_" + Utils.createHash(typeIri));
346
            }
347
            for (String spaceRef : detectedSpaceRefs) {
348
                runTask.accept(() -> loadNanopubToRepo(np.getUri(), allStatements, "space_" + spaceRef));
×
349
            }
350
            //                for (IRI creatorIri : SimpleCreatorPattern.getCreators(np)) {
351
            //                        // Exclude locally minted IRIs:
352
            //                        if (creatorIri.stringValue().startsWith(np.getUri().stringValue())) continue;
353
            //                        if (!creatorIri.stringValue().matches("https?://.*")) continue;
354
            //                        loadNanopubToRepo(np.getUri(), allStatements, "user_" + Utils.createHash(creatorIri));
355
            //                        loadNanopubToRepo(np.getUri(), textStatements, "text-user_" + Utils.createHash(creatorIri));
356
            //                }
357
            //                for (IRI authorIri : SimpleCreatorPattern.getAuthors(np)) {
358
            //                        // Exclude locally minted IRIs:
359
            //                        if (authorIri.stringValue().startsWith(np.getUri().stringValue())) continue;
360
            //                        if (!authorIri.stringValue().matches("https?://.*")) continue;
361
            //                        loadNanopubToRepo(np.getUri(), allStatements, "user_" + Utils.createHash(authorIri));
362
            //                        loadNanopubToRepo(np.getUri(), textStatements, "text-user_" + Utils.createHash(authorIri));
363
            //                }
364

365
            for (Statement st : invalidateStatements) {
366
                runTask.accept(() -> loadInvalidateStatements(np, el.getPublicKeyString(), st, pubkeyStatement, pubkeyStatementX));
×
367
            }
368

369
            // Wait for all non-meta tasks to complete successfully before submitting the meta task
370
            for (var task : runningTasks) {
371
                try {
372
                    task.get();
373
                } catch (ExecutionException | InterruptedException ex) {
374
                    throw new RuntimeException("Error in nanopub loading thread", ex.getCause());
375
                }
376
            }
377

378
            // Now submit and wait for the "meta" task after all other tasks have completed successfully
379
            Future<?> metaTask = loadingPool.submit(() -> loadNanopubToRepo(np.getUri(), metaStatements, "meta"));
×
380
            try {
381
                metaTask.get();
382
            } catch (ExecutionException | InterruptedException ex) {
383
                throw new RuntimeException("Error in nanopub loading thread (meta task)", ex.getCause());
384
            }
385
        }
386
    }
387

388
    private static Long lastUpdateOfLatestRepo = null;
6✔
389
    private static long THIRTY_DAYS = 1000L * 60 * 60 * 24 * 30;
6✔
390
    private static long ONE_HOUR = 1000L * 60 * 60;
6✔
391

392
    @GeneratedFlagForDependentElements
393
    private static void loadNanopubToLatest(List<Statement> statements) {
394
        boolean success = false;
395
        int retries = 0;
396
        while (!success) {
397
            RepositoryConnection conn = TripleStore.get().getRepoConnection("last30d");
398
            try (conn) {
399
                // Read committed, because deleting old nanopubs is idempotent. Inserts do not collide
400
                // with deletes, because we are not inserting old nanopubs.
401
                conn.begin(IsolationLevels.READ_COMMITTED);
402
                conn.add(statements);
403
                if (lastUpdateOfLatestRepo == null || new Date().getTime() - lastUpdateOfLatestRepo > ONE_HOUR) {
404
                    log.trace("Remove old nanopubs...");
405
                    Literal thirtyDaysAgo = vf.createLiteral(new Date(new Date().getTime() - THIRTY_DAYS));
406
                    TupleQuery q = conn.prepareTupleQuery(QueryLanguage.SPARQL, "SELECT * { graph <" + NPA.GRAPH + "> { " + "?np <" + DCTERMS.CREATED + "> ?date . " + "filter ( ?date < ?thirtydaysago ) " + "} }");
407
                    q.setBinding("thirtydaysago", thirtyDaysAgo);
408
                    try (TupleQueryResult r = q.evaluate()) {
409
                        while (r.hasNext()) {
410
                            BindingSet b = r.next();
411
                            IRI oldNpId = (IRI) b.getBinding("np").getValue();
412
                            log.trace("Remove old nanopub: {}", oldNpId);
413
                            for (Value v : Utils.getObjectsForPattern(conn, NPA.GRAPH, oldNpId, NPA.HAS_GRAPH)) {
414
                                // Remove all four nanopub graphs:
415
                                conn.remove((Resource) null, (IRI) null, (Value) null, (IRI) v);
416
                            }
417
                            // Remove nanopubs in admin graphs:
418
                            conn.remove(oldNpId, null, null, NPA.GRAPH);
419
                            conn.remove(oldNpId, null, null, NPA.NETWORK_GRAPH);
420
                        }
421
                    }
422
                    lastUpdateOfLatestRepo = new Date().getTime();
423
                }
424
                conn.commit();
425
                success = true;
426
            } catch (Exception ex) {
427
                log.info("Could not get environment variable", ex);
428
                if (conn.isActive()) conn.rollback();
429
            }
430
            if (!success) {
431
                retries++;
432
                if (retries >= MAX_RETRIES) {
433
                    throw new RuntimeException("Failed to load nanopub to last30d repo after " + MAX_RETRIES + " retries");
434
                }
435
                log.info("Retrying in 10 seconds (attempt {}/{})...", retries, MAX_RETRIES);
436
                try {
437
                    Thread.sleep(RETRY_DELAY_MS);
438
                } catch (InterruptedException x) {
439
                }
440
            }
441
        }
442
    }
443

444
    @GeneratedFlagForDependentElements
445
    private static void loadNanopubToRepo(IRI npId, List<Statement> statements, String repoName) {
446
        boolean success = false;
447
        int retries = 0;
448
        while (!success) {
449
            RepositoryConnection conn = TripleStore.get().getRepoConnection(repoName);
450
            try (conn) {
451
                // Serializable, because write skew would cause the chain of hashes to be broken.
452
                // The inserts must be done serially.
453
                conn.begin(IsolationLevels.SERIALIZABLE);
454
                var repoStatus = fetchRepoStatus(conn, npId);
455
                if (repoStatus.isLoaded) {
456
                    log.info("Already loaded: {}", npId);
457
                } else {
458
                    // Space repos can have nanopubs removed (via invalidation/unloading), so a
459
                    // cumulative XOR checksum and count would drift after the first removal.
460
                    // Mirror the last30d approach: skip checksum/count maintenance for these
461
                    // repos. HAS_LOAD_NUMBER is still added as a presence marker so the
462
                    // isLoaded check above remains effective on re-runs.
463
                    boolean trackChecksum = !repoName.startsWith("space_");
464
                    if (trackChecksum) {
465
                        String newChecksum = NanopubUtils.updateXorChecksum(npId, repoStatus.checksum);
466
                        conn.remove(NPA.THIS_REPO, NPA.HAS_NANOPUB_COUNT, null, NPA.GRAPH);
467
                        conn.remove(NPA.THIS_REPO, NPA.HAS_NANOPUB_CHECKSUM, null, NPA.GRAPH);
468
                        conn.add(NPA.THIS_REPO, NPA.HAS_NANOPUB_COUNT, vf.createLiteral(repoStatus.count + 1), NPA.GRAPH);
469
                        // @ADMIN-TRIPLE-TABLE@ REPO, npa:hasNanopubCount, NANOPUB_COUNT, npa:graph, admin, number of nanopubs loaded
470
                        conn.add(NPA.THIS_REPO, NPA.HAS_NANOPUB_CHECKSUM, vf.createLiteral(newChecksum), NPA.GRAPH);
471
                        // @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)
472
                        conn.add(npId, NPA.HAS_LOAD_NUMBER, vf.createLiteral(repoStatus.count), NPA.GRAPH);
473
                        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasLoadNumber, LOAD_NUMBER, npa:graph, admin, the sequential number at which this NANOPUB was loaded
474
                        conn.add(npId, NPA.HAS_LOAD_CHECKSUM, vf.createLiteral(newChecksum), NPA.GRAPH);
475
                        // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasLoadChecksum, LOAD_CHECKSUM, npa:graph, admin, the checksum of all loaded nanopubs after loading the given NANOPUB
476
                    } else {
477
                        // Presence marker only — the numeric value is not meaningful for
478
                        // repos that skip checksum/count tracking.
479
                        conn.add(npId, NPA.HAS_LOAD_NUMBER, vf.createLiteral(0L), NPA.GRAPH);
480
                    }
481
                    conn.add(npId, NPA.HAS_LOAD_TIMESTAMP, vf.createLiteral(new Date()), NPA.GRAPH);
482
                    // @ADMIN-TRIPLE-TABLE@ NANOPUB, npa:hasLoadTimestamp, LOAD_TIMESTAMP, npa:graph, admin, the time point at which this NANOPUB was loaded
483
                    conn.add(statements);
484
                }
485
                conn.commit();
486
                success = true;
487
            } catch (Exception ex) {
488
                log.info("Could not load nanopub to repo. ", ex);
489
                if (conn.isActive()) conn.rollback();
490
            }
491
            if (!success) {
492
                retries++;
493
                if (retries >= MAX_RETRIES) {
494
                    throw new RuntimeException("Failed to load nanopub " + npId + " to repo " + repoName + " after " + MAX_RETRIES + " retries");
495
                }
496
                log.info("Retrying in 10 seconds (attempt {}/{})...", retries, MAX_RETRIES);
497
                try {
498
                    Thread.sleep(RETRY_DELAY_MS);
499
                } catch (InterruptedException x) {
500
                }
501
            }
502
        }
503
    }
504

505
    private record RepoStatus(boolean isLoaded, long count, String checksum) {
×
506
    }
507

508
    /**
509
     * To execute before loading a nanopub: check if the nanopub is already loaded and what is the
510
     * current load counter and checksum. This effectively batches three queries into one.
511
     * This method must be called from within a transaction.
512
     *
513
     * @param conn repo connection
514
     * @param npId nanopub ID
515
     * @return the current status
516
     */
517
    @GeneratedFlagForDependentElements
518
    private static RepoStatus fetchRepoStatus(RepositoryConnection conn, IRI npId) {
519
        var result = conn.prepareTupleQuery(QueryLanguage.SPARQL, REPO_STATUS_QUERY_TEMPLATE.formatted(npId)).evaluate();
520
        try (result) {
521
            if (!result.hasNext()) {
522
                // This may happen if the repo was created, but is completely empty.
523
                return new RepoStatus(false, 0, NanopubUtils.INIT_CHECKSUM);
524
            }
525
            var row = result.next();
526
            return new RepoStatus(row.hasBinding("loadNumber"), Long.parseLong(row.getBinding("count").getValue().stringValue()), row.getBinding("checksum").getValue().stringValue());
527
        }
528
    }
529

530
    @GeneratedFlagForDependentElements
531
    private static void loadInvalidateStatements(Nanopub thisNp, String thisPubkey, Statement invalidateStatement, Statement pubkeyStatement, Statement pubkeyStatementX) {
532
        boolean success = false;
533
        int retries = 0;
534
        while (!success) {
535
            List<RepositoryConnection> connections = new ArrayList<>();
536
            RepositoryConnection metaConn = TripleStore.get().getRepoConnection("meta");
537
            try {
538
                IRI invalidatedNpId = (IRI) invalidateStatement.getObject();
539
                // Basic isolation because here we only read append-only data.
540
                metaConn.begin(IsolationLevels.READ_COMMITTED);
541

542
                Value pubkeyValue = Utils.getObjectForPattern(metaConn, NPA.GRAPH, invalidatedNpId, NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY);
543
                if (pubkeyValue != null) {
544
                    String pubkey = pubkeyValue.stringValue();
545

546
                    if (!pubkey.equals(thisPubkey)) {
547
                        //log.info("Adding invalidation expressed in " + thisNp.getUri() + " also to repo for pubkey " + pubkey);
548
                        connections.add(loadStatements("pubkey_" + Utils.createHash(pubkey), invalidateStatement, pubkeyStatement, pubkeyStatementX));
549
//                                                connections.add(loadStatements("text-pubkey_" + Utils.createHash(pubkey), invalidateStatement, pubkeyStatement));
550
                    }
551

552
                    for (Value v : Utils.getObjectsForPattern(metaConn, NPA.GRAPH, invalidatedNpId, NPX.HAS_NANOPUB_TYPE)) {
553
                        IRI typeIri = (IRI) v;
554
                        // TODO Avoid calling getTypes and getCreators multiple times:
555
                        if (!NanopubUtils.getTypes(thisNp).contains(typeIri)) {
556
                            //log.info("Adding invalidation expressed in " + thisNp.getUri() + " also to repo for type " + typeIri);
557
                            connections.add(loadStatements("type_" + Utils.createHash(typeIri), invalidateStatement, pubkeyStatement, pubkeyStatementX));
558
//                                                        connections.add(loadStatements("text-type_" + Utils.createHash(typeIri), invalidateStatement, pubkeyStatement));
559
                        }
560
                    }
561

562
//                                        for (Value v : Utils.getObjectsForPattern(metaConn, NPA.GRAPH, invalidatedNpId, DCTERMS.CREATOR)) {
563
//                                                IRI creatorIri = (IRI) v;
564
//                                                if (!SimpleCreatorPattern.getCreators(thisNp).contains(creatorIri)) {
565
//                                                        //log.info("Adding invalidation expressed in " + thisNp.getUri() + " also to repo for user " + creatorIri);
566
//                                                        connections.add(loadStatements("user_" + Utils.createHash(creatorIri), invalidateStatement, pubkeyStatement));
567
//                                                        connections.add(loadStatements("text-user_" + Utils.createHash(creatorIri), invalidateStatement, pubkeyStatement));
568
//                                                }
569
//                                        }
570
                }
571

572
                metaConn.commit();
573
                // TODO handle case that some commits succeed and some fail
574
                for (RepositoryConnection c : connections) c.commit();
575
                success = true;
576
            } catch (Exception ex) {
577
                log.info("Could not load invalidate statements. ", ex);
578
                if (metaConn.isActive()) metaConn.rollback();
579
                for (RepositoryConnection c : connections) {
580
                    if (c.isActive()) c.rollback();
581
                }
582
            } finally {
583
                metaConn.close();
584
                for (RepositoryConnection c : connections) c.close();
585
            }
586
            if (!success) {
587
                retries++;
588
                if (retries >= MAX_RETRIES) {
589
                    throw new RuntimeException("Failed to load invalidate statements for " + thisNp.getUri() + " after " + MAX_RETRIES + " retries");
590
                }
591
                log.info("Retrying in 10 seconds (attempt {}/{})...", retries, MAX_RETRIES);
592
                try {
593
                    Thread.sleep(RETRY_DELAY_MS);
594
                } catch (InterruptedException x) {
595
                }
596
            }
597
        }
598
    }
599

600
    @GeneratedFlagForDependentElements
601
    private static RepositoryConnection loadStatements(String repoName, Statement... statements) {
602
        RepositoryConnection conn = TripleStore.get().getRepoConnection(repoName);
603
        // Basic isolation: we only append new statements
604
        conn.begin(IsolationLevels.READ_COMMITTED);
605
        for (Statement st : statements) {
606
            conn.add(st);
607
        }
608
        return conn;
609
    }
610

611
    @GeneratedFlagForDependentElements
612
    static List<Statement> getInvalidatingStatements(IRI npId) {
613
        List<Statement> invalidatingStatements = new ArrayList<>();
614
        boolean success = false;
615
        int retries = 0;
616
        while (!success) {
617
            RepositoryConnection conn = TripleStore.get().getRepoConnection("meta");
618
            try (conn) {
619
                // Basic isolation because here we only read append-only data.
620
                conn.begin(IsolationLevels.READ_COMMITTED);
621

622
                TupleQueryResult r = conn.prepareTupleQuery(QueryLanguage.SPARQL, "SELECT * { graph <" + NPA.GRAPH + "> { " + "?np <" + NPX.INVALIDATES + "> <" + npId + "> ; <" + NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY + "> ?pubkey . " + "} }").evaluate();
623
                try (r) {
624
                    while (r.hasNext()) {
625
                        BindingSet b = r.next();
626
                        invalidatingStatements.add(vf.createStatement((IRI) b.getBinding("np").getValue(), NPX.INVALIDATES, npId, NPA.GRAPH));
627
                        invalidatingStatements.add(vf.createStatement((IRI) b.getBinding("np").getValue(), NPA.HAS_VALID_SIGNATURE_FOR_PUBLIC_KEY, b.getBinding("pubkey").getValue(), NPA.GRAPH));
628
                    }
629
                }
630
                conn.commit();
631
                success = true;
632
            } catch (Exception ex) {
633
                log.info("Could not load invalidating statements. ", ex);
634
                if (conn.isActive()) conn.rollback();
635
            }
636
            if (!success) {
637
                retries++;
638
                if (retries >= MAX_RETRIES) {
639
                    throw new RuntimeException("Failed to get invalidating statements for " + npId + " after " + MAX_RETRIES + " retries");
640
                }
641
                log.info("Retrying in 10 seconds (attempt {}/{})...", retries, MAX_RETRIES);
642
                try {
643
                    Thread.sleep(RETRY_DELAY_MS);
644
                } catch (InterruptedException x) {
645
                }
646
            }
647
        }
648
        return invalidatingStatements;
649
    }
650

651
    @GeneratedFlagForDependentElements
652
    private static void loadNoteToRepo(Resource subj, String note) {
653
        boolean success = false;
654
        int retries = 0;
655
        while (!success) {
656
            RepositoryConnection conn = TripleStore.get().getAdminRepoConnection();
657
            try (conn) {
658
                List<Statement> statements = new ArrayList<>();
659
                statements.add(vf.createStatement(subj, NPA.NOTE, vf.createLiteral(note), NPA.GRAPH));
660
                conn.add(statements);
661
                success = true;
662
            } catch (Exception ex) {
663
                log.info("Could not load note to repo. ", ex);
664
            }
665
            if (!success) {
666
                retries++;
667
                if (retries >= MAX_RETRIES) {
668
                    throw new RuntimeException("Failed to load note to repo for " + subj + " after " + MAX_RETRIES + " retries");
669
                }
670
                log.info("Retrying in 10 seconds (attempt {}/{})...", retries, MAX_RETRIES);
671
                try {
672
                    Thread.sleep(RETRY_DELAY_MS);
673
                } catch (InterruptedException x) {
674
                }
675
            }
676
        }
677
    }
678

679
    static boolean hasValidSignature(NanopubSignatureElement el) {
680
        try {
681
            if (el != null && SignatureUtils.hasValidSignature(el) && el.getPublicKeyString() != null) {
24!
682
                return true;
6✔
683
            }
684
        } catch (GeneralSecurityException ex) {
3✔
685
            log.info("Error for signature element {}", el.getUri());
15✔
686
            log.info("Error", ex);
12✔
687
        }
3✔
688
        return false;
6✔
689
    }
690

691
    private static IRI getBaseTrustyUri(Value v) {
692
        if (!(v instanceof IRI)) return null;
9!
693
        String s = v.stringValue();
9✔
694
        if (!s.matches(".*[^A-Za-z0-9\\-_]RA[A-Za-z0-9\\-_]{43}([^A-Za-z0-9\\\\-_].{0,43})?")) {
12✔
695
            return null;
6✔
696
        }
697
        return vf.createIRI(s.replaceFirst("^(.*[^A-Za-z0-9\\-_]RA[A-Za-z0-9\\-_]{43})([^A-Za-z0-9\\\\-_].{0,43})?$", "$1"));
21✔
698
    }
699

700
    // TODO: Move this to nanopub library:
701
    private static boolean isIntroNanopub(Nanopub np) {
702
        for (Statement st : np.getAssertion()) {
33✔
703
            if (st.getPredicate().equals(NPX.DECLARED_BY)) return true;
21✔
704
        }
3✔
705
        return false;
6✔
706
    }
707

708
    /**
709
     * Detects whether the given nanopub is a Space-defining nanopub (typed
710
     * {@code gen:Space}) and, if so, registers each space it declares (one per
711
     * {@code <spaceIri> gen:hasRootDefinition <rootUri>} triple) in
712
     * {@link SpaceRegistry}. Nanopubs missing the {@code gen:hasRootDefinition}
713
     * triple are not recognized as space-defining — there is no transition fallback.
714
     *
715
     * @param np the nanopub to inspect
716
     * @return the set of space refs registered from this nanopub (possibly empty);
717
     *         the caller uses this to load the nanopub into the corresponding
718
     *         {@code space_<spaceRef>} repositories
719
     */
720
    static Set<String> detectAndRegisterSpaces(Nanopub np) {
721
        boolean isSpaceTyped = false;
6✔
722
        for (IRI typeIri : NanopubUtils.getTypes(np)) {
33✔
723
            if (typeIri.equals(GEN.SPACE)) {
12✔
724
                isSpaceTyped = true;
6✔
725
                break;
3✔
726
            }
727
        }
3✔
728
        if (!isSpaceTyped) return Collections.emptySet();
12✔
729
        Set<String> spaceRefs = new LinkedHashSet<>();
12✔
730
        for (Statement st : np.getAssertion()) {
33✔
731
            if (!st.getPredicate().equals(GEN.HAS_ROOT_DEFINITION)) continue;
18✔
732
            if (!(st.getSubject() instanceof IRI spaceIri)) continue;
27!
733
            if (!(st.getObject() instanceof IRI rootUri)) continue;
27!
734
            String rootNanopubId = TrustyUriUtils.getArtifactCode(rootUri.stringValue());
12✔
735
            if (rootNanopubId == null || rootNanopubId.isEmpty()) {
15!
736
                log.warn("Ignoring space {}: gen:hasRootDefinition target is not a trusty URI: {}", spaceIri, rootUri);
15✔
737
                continue;
3✔
738
            }
739
            spaceRefs.add(SpaceRegistry.get().registerSpace(rootNanopubId, spaceIri));
21✔
740
        }
3✔
741
        return spaceRefs;
6✔
742
    }
743

744
    /**
745
     * Check if a nanopub is already loaded in the admin graph.
746
     *
747
     * @param npId the nanopub ID
748
     * @return true if the nanopub is loaded, false otherwise
749
     */
750
    @GeneratedFlagForDependentElements
751
    static boolean isNanopubLoaded(String npId) {
752
        boolean loaded = false;
753
        RepositoryConnection conn = TripleStore.get().getRepoConnection("meta");
754
        try (conn) {
755
            if (Utils.getObjectForPattern(conn, NPA.GRAPH, vf.createIRI(npId), NPA.HAS_LOAD_NUMBER) != null) {
756
                loaded = true;
757
            }
758
        } catch (Exception ex) {
759
            log.info("Could no load nanopub. ", ex);
760
        }
761
        return loaded;
762
    }
763

764
    private static ValueFactory vf = SimpleValueFactory.getInstance();
6✔
765

766
    // TODO remove the constants and use the ones from the nanopub library instead
767

768
    /**
769
     * Template for the query that fetches the status of a repository.
770
     */
771
    // Template for .fetchRepoStatus
772
    private static final String REPO_STATUS_QUERY_TEMPLATE = """
84✔
773
            SELECT * { graph <%s> {
774
              OPTIONAL { <%s> <%s> ?loadNumber . }
775
              <%s> <%s> ?count ;
776
                   <%s> ?checksum .
777
            } }
778
            """.formatted(NPA.GRAPH, "%s", NPA.HAS_LOAD_NUMBER, NPA.THIS_REPO, NPA.HAS_NANOPUB_COUNT, NPA.HAS_NANOPUB_CHECKSUM);
6✔
779
}
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