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

knowledgepixels / nanopub-registry / 27538729666

15 Jun 2026 10:02AM UTC coverage: 32.089% (+0.6%) from 31.456%
27538729666

push

github

ashleycaselli
chore(RegistryDB): enhance logging for database operations and initialization checks

295 of 1054 branches covered (27.99%)

Branch coverage included in aggregate %.

891 of 2642 relevant lines covered (33.72%)

5.65 hits per line

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

67.4
src/main/java/com/knowledgepixels/registry/RegistryDB.java
1
package com.knowledgepixels.registry;
2

3
import com.knowledgepixels.registry.db.IndexInitializer;
4
import com.mongodb.*;
5
import com.mongodb.client.ClientSession;
6
import com.mongodb.client.MongoCollection;
7
import com.mongodb.client.MongoCursor;
8
import com.mongodb.client.MongoDatabase;
9
import com.mongodb.client.model.CountOptions;
10
import com.mongodb.client.model.FindOneAndUpdateOptions;
11
import com.mongodb.client.model.ReturnDocument;
12
import com.mongodb.client.model.UpdateOptions;
13
import net.trustyuri.TrustyUriUtils;
14
import org.bson.Document;
15
import org.bson.conversions.Bson;
16
import org.bson.types.Binary;
17
import org.eclipse.rdf4j.common.exception.RDF4JException;
18
import org.eclipse.rdf4j.model.IRI;
19
import org.eclipse.rdf4j.rio.RDFFormat;
20
import org.nanopub.MalformedNanopubException;
21
import org.nanopub.Nanopub;
22
import org.nanopub.NanopubUtils;
23
import org.nanopub.extra.security.MalformedCryptoElementException;
24
import org.nanopub.extra.security.NanopubSignatureElement;
25
import org.nanopub.extra.security.SignatureUtils;
26
import org.nanopub.jelly.JellyUtils;
27
import org.slf4j.Logger;
28
import org.slf4j.LoggerFactory;
29

30
import java.io.IOException;
31
import java.security.GeneralSecurityException;
32
import java.util.ArrayList;
33
import java.util.Calendar;
34

35
import static com.mongodb.client.model.Indexes.ascending;
36

37
public class RegistryDB {
38

39
    private RegistryDB() {
40
    }
41

42
    private static final String REGISTRY_DB_NAME = Utils.getEnv("REGISTRY_DB_NAME", "nanopubRegistry");
12✔
43

44
    private static final Logger logger = LoggerFactory.getLogger(RegistryDB.class);
9✔
45

46
    private static MongoClient mongoClient;
47
    private static MongoDatabase mongoDB;
48

49
    /**
50
     * Returns the MongoDB database instance.
51
     *
52
     * @return the MongoDatabase instance
53
     */
54
    public static MongoDatabase getDB() {
55
        return mongoDB;
6✔
56
    }
57

58
    /**
59
     * Returns the MongoDB client instance.
60
     *
61
     * @return the MongoClient instance
62
     */
63
    public static MongoClient getClient() {
64
        return mongoClient;
6✔
65
    }
66

67
    /**
68
     * Returns the specified collection from the MongoDB database.
69
     *
70
     * @param name the name of the collection
71
     * @return the MongoCollection instance
72
     */
73
    public static MongoCollection<Document> collection(String name) {
74
        return mongoDB.getCollection(name);
12✔
75
    }
76

77
    /**
78
     * Initializes the MongoDB connection and sets up collections and indexes if not already initialized.
79
     */
80
    public static void init() {
81
        if (mongoClient != null) {
6✔
82
            logger.info("RegistryDB already initialized (database: {})", REGISTRY_DB_NAME);
12✔
83
            return;
3✔
84
        }
85
        final String REGISTRY_DB_HOST = Utils.getEnv("REGISTRY_DB_HOST", "mongodb");
12✔
86
        final int REGISTRY_DB_PORT = Integer.parseInt(Utils.getEnv("REGISTRY_DB_PORT", String.valueOf(ServerAddress.defaultPort())));
18✔
87
        logger.info("Initializing RegistryDB connection to database '{}' at {}:{}", REGISTRY_DB_NAME, REGISTRY_DB_HOST, REGISTRY_DB_PORT);
54✔
88
        mongoClient = new MongoClient(REGISTRY_DB_HOST, REGISTRY_DB_PORT);
18✔
89
        mongoDB = mongoClient.getDatabase(REGISTRY_DB_NAME);
12✔
90

91
        try (ClientSession mongoSession = mongoClient.startSession()) {
9✔
92
            logger.debug("MongoDB client session started for initialization");
9✔
93
            if (!isInitialized(mongoSession)) {
9!
94
                logger.info("Database '{}' not initialized; creating collections and indexes", REGISTRY_DB_NAME);
12✔
95
                IndexInitializer.initCollections(mongoSession);
9✔
96
            } else {
97
                logger.debug("Database '{}' already has setupId", REGISTRY_DB_NAME);
×
98
            }
99
            initCounter(mongoSession);
6✔
100
        }
101
    }
3✔
102

103
    /**
104
     * Checks if the database has been initialized.
105
     *
106
     * @param mongoSession the MongoDB client session
107
     * @return true if initialized, false otherwise
108
     */
109
    public static boolean isInitialized(ClientSession mongoSession) {
110
        boolean initialized = getValue(mongoSession, Collection.SERVER_INFO.toString(), "setupId") != null;
24!
111
        logger.debug("isInitialized check for database '{}': {}", REGISTRY_DB_NAME, initialized);
18✔
112
        return initialized;
6✔
113
    }
114

115
    /**
116
     * Renames a collection in the database. If the new collection name already exists, it will be dropped first.
117
     *
118
     * @param oldCollectionName the current name of the collection
119
     * @param newCollectionName the new name for the collection
120
     */
121
    public static void rename(String oldCollectionName, String newCollectionName) {
122
        // Designed as idempotent operation: calling multiple times has same effect as calling once
123
        if (hasCollection(oldCollectionName)) {
9✔
124
            if (hasCollection(newCollectionName)) {
9✔
125
                collection(newCollectionName).drop();
9✔
126
            }
127
            collection(oldCollectionName).renameCollection(new MongoNamespace(REGISTRY_DB_NAME, newCollectionName));
24✔
128
        }
129
    }
3✔
130

131
    /**
132
     * Checks if a collection with the given name exists in the database.
133
     *
134
     * @param collectionName the name of the collection to check
135
     * @return true if the collection exists, false otherwise
136
     */
137
    public static boolean hasCollection(String collectionName) {
138
        boolean exists = mongoDB.listCollectionNames().into(new ArrayList<>()).contains(collectionName);
30✔
139
        logger.debug("Collection existence check for '{}': {}", collectionName, exists);
18✔
140
        return exists;
6✔
141
    }
142

143
    /**
144
     * Increases the trust state counter in the server info collection.
145
     *
146
     * @param mongoSession the MongoDB client session
147
     */
148
    public static void increaseStateCounter(ClientSession mongoSession) {
149
        MongoCursor<Document> cursor = collection(Collection.SERVER_INFO.toString()).find(mongoSession, new Document("_id", "trustStateCounter")).cursor();
36✔
150
        if (cursor.hasNext()) {
9✔
151
            long counter = cursor.next().getLong("value");
21✔
152
            collection(Collection.SERVER_INFO.toString()).updateOne(mongoSession, new Document("_id", "trustStateCounter"), new Document("$set", new Document("value", counter + 1)));
69✔
153
            logger.debug("Incremented trustStateCounter from {} to {}", counter, counter + 1);
27✔
154
        } else {
3✔
155
            collection(Collection.SERVER_INFO.toString()).insertOne(mongoSession, new Document("_id", "trustStateCounter").append("value", 0L));
42✔
156
            logger.info("Initialized trustStateCounter to 0 in collection '{}'", Collection.SERVER_INFO);
12✔
157
        }
158
    }
3✔
159

160
    /**
161
     * Checks if an element with the given name exists in the specified collection.
162
     *
163
     * @param mongoSession the MongoDB client session
164
     * @param collection   the name of the collection
165
     * @param elementName  the name of the element used as the _id field
166
     * @return true if the element exists, false otherwise
167
     */
168
    public static boolean has(ClientSession mongoSession, String collection, String elementName) {
169
        return has(mongoSession, collection, new Document("_id", elementName));
27✔
170
    }
171

172
    private static final CountOptions hasCountOptions = new CountOptions().limit(1);
21✔
173

174
    /**
175
     * Checks if any document matching the given filter exists in the specified collection.
176
     *
177
     * @param mongoSession the MongoDB client session
178
     * @param collection   the name of the collection
179
     * @param find         the filter to match documents
180
     * @return true if at least one matching document exists, false otherwise
181
     */
182
    public static boolean has(ClientSession mongoSession, String collection, Bson find) {
183
        boolean found = collection(collection).countDocuments(mongoSession, find, hasCountOptions) > 0;
39✔
184
        logger.debug("Existence check in collection '{}' for filter {}: {}", collection, find, found);
54✔
185
        return found;
6✔
186
    }
187

188
    /**
189
     * Retrieves a cursor for documents matching the given filter in the specified collection.
190
     *
191
     * @param mongoSession the MongoDB client session
192
     * @param collection   the name of the collection
193
     * @param find         the filter to match documents
194
     * @return a MongoCursor for the matching documents
195
     */
196
    public static MongoCursor<Document> get(ClientSession mongoSession, String collection, Bson find) {
197
        logger.trace("Querying collection '{}' with filter {}", collection, find);
15✔
198
        return collection(collection).find(mongoSession, find).cursor();
21✔
199
    }
200

201
    /**
202
     * Retrieves the value of an element with the given name from the specified collection.
203
     *
204
     * @param mongoSession the MongoDB client session
205
     * @param collection   the name of the collection
206
     * @param elementName  the name of the element used as the _id field
207
     * @return the value of the element, or null if not found
208
     */
209
    public static Object getValue(ClientSession mongoSession, String collection, String elementName) {
210
        logger.debug("Reading value of element '{}' from collection '{}'", elementName, collection);
15✔
211
        Document d = collection(collection).find(mongoSession, new Document("_id", elementName)).first();
36✔
212
        if (d == null) {
6✔
213
            logger.trace("Element '{}' not found in collection '{}'", elementName, collection);
15✔
214
            return null;
6✔
215
        }
216
        Object value = d.get("value");
12✔
217
        logger.debug("Found element '{}' in collection '{}' with value type {}", elementName, collection, value == null ? "null" : value.getClass().getSimpleName());
63!
218
        return value;
6✔
219
    }
220

221
    /**
222
     * Retrieves the boolean value of an element with the given name from the specified collection.
223
     *
224
     * @param mongoSession the MongoDB client session
225
     * @param collection   the name of the collection
226
     * @param elementName  the name of the element used as the _id field
227
     * @return the value of the element, or null if not found
228
     */
229
    public static boolean isSet(ClientSession mongoSession, String collection, String elementName) {
230
        Document d = collection(collection).find(mongoSession, new Document("_id", elementName)).first();
36✔
231
        if (d == null) {
6✔
232
            logger.trace("isSet: element '{}' not found in collection '{}'", elementName, collection);
15✔
233
            return false;
6✔
234
        }
235
        Boolean val = d.getBoolean("value");
12✔
236
        logger.debug("isSet: element '{}' in collection '{}' has boolean value {}", elementName, collection, val);
51✔
237
        return val;
9✔
238
    }
239

240
    /**
241
     * Retrieves a single document matching the given filter from the specified collection.
242
     *
243
     * @param mongoSession the MongoDB client session
244
     * @param collection   the name of the collection
245
     * @param find         the filter to match the document
246
     * @return the matching document, or null if not found
247
     */
248
    public static Document getOne(ClientSession mongoSession, String collection, Bson find) {
249
        logger.trace("getOne from '{}' with filter {}", collection, find);
15✔
250
        return collection(collection).find(mongoSession, find).first();
24✔
251
    }
252

253
    /**
254
     * Retrieves the maximum value of a specified field from the documents in the given collection.
255
     *
256
     * @param mongoSession the MongoDB client session
257
     * @param collection   the name of the collection
258
     * @param fieldName    the field for which to find the maximum value
259
     * @return the maximum value of the specified field, or null if no documents exist
260
     */
261
    public static Object getMaxValue(ClientSession mongoSession, String collection, String fieldName) {
262
        Document doc = collection(collection).find(mongoSession).projection(new Document(fieldName, 1)).sort(new Document(fieldName, -1)).first();
63✔
263
        if (doc == null) {
6✔
264
            logger.trace("getMaxValue: no documents in collection '{}' for field '{}'", collection, fieldName);
15✔
265
            return null;
6✔
266
        }
267
        Object val = doc.get(fieldName);
12✔
268
        logger.debug("getMaxValue: collection '{}' field '{}' max = {}", collection, fieldName, val);
51✔
269
        return val;
6✔
270
    }
271

272
    /**
273
     * Retrieves the document with the maximum value of a specified field from the documents matching the given filter in the specified collection.
274
     *
275
     * @param mongoSession the MongoDB client session
276
     * @param collection   the name of the collection
277
     * @param find         the filter to match documents
278
     * @param fieldName    the field for which to find the maximum value
279
     * @return the document with the maximum value of the specified field, or null if no matching documents exist
280
     */
281
    public static Document getMaxValueDocument(ClientSession mongoSession, String collection, Bson find, String fieldName) {
282
        logger.trace("getMaxValueDocument in '{}' with filter {} for field '{}'", collection, find, fieldName);
51✔
283
        return collection(collection).find(mongoSession, find).sort(new Document(fieldName, -1)).first();
45✔
284
    }
285

286
    /**
287
     * Sets or updates a document in the specified collection.
288
     *
289
     * @param mongoSession the MongoDB client session
290
     * @param collection   the name of the collection
291
     * @param update       the document to set or update (must contain an _id field)
292
     */
293
    public static void set(ClientSession mongoSession, String collection, Document update) {
294
        Bson find = new Document("_id", update.get("_id"));
24✔
295
        MongoCursor<Document> cursor = collection(collection).find(mongoSession, find).cursor();
21✔
296
        if (cursor.hasNext()) {
9✔
297
            collection(collection).updateOne(mongoSession, find, new Document("$set", update));
33✔
298
            logger.debug("Updated document with _id={} in collection '{}'", update.get("_id"), collection);
24✔
299
        } else {
300
            logger.debug("set: no existing document with _id={} in collection '{}'; update skipped", update.get("_id"), collection);
21✔
301
        }
302
    }
3✔
303

304
    /**
305
     * Inserts a document into the specified collection.
306
     *
307
     * @param mongoSession the MongoDB client session
308
     * @param collection   the name of the collection
309
     * @param doc          the document to insert
310
     */
311
    public static void insert(ClientSession mongoSession, String collection, Document doc) {
312
        collection(collection).insertOne(mongoSession, doc);
15✔
313
        logger.debug("Inserted document into '{}' with _id={}", collection, doc.get("_id"));
21✔
314
    }
3✔
315

316
    /**
317
     * Sets the value of an element with the given name in the specified collection.
318
     * If the element does not exist, it will be created.
319
     *
320
     * @param mongoSession the MongoDB client session
321
     * @param collection   the name of the collection
322
     * @param elementId    the name of the element used as the _id field
323
     * @param value        the value to set
324
     */
325
    public static void setValue(ClientSession mongoSession, String collection, String elementId, Object value) {
326
        logger.debug("Setting value for element '{}' in collection '{}' (upsert)", elementId, collection);
15✔
327
        collection(collection).updateOne(mongoSession, new Document("_id", elementId), new Document("$set", new Document("value", value)), new UpdateOptions().upsert(true));
72✔
328
    }
3✔
329

330
    /**
331
     * Records the hash of a given value in the "hashes" collection.
332
     * Uses upsert to avoid expensive exception-based duplicate handling.
333
     *
334
     * @param mongoSession the MongoDB client session
335
     * @param value        the value to hash and record
336
     */
337
    public static void recordHash(ClientSession mongoSession, String value) {
338
        String hash = Utils.getHash(value);
9✔
339
        try {
340
            collection("hashes").updateOne(mongoSession, new Document("value", value), new Document("$setOnInsert", new Document("value", value).append("hash", hash)), new UpdateOptions().upsert(true));
81✔
341
            logger.debug("Recorded hash for value (hash={})", hash);
12✔
342
        } catch (MongoWriteException e) {
×
343
            // Concurrent upsert race: another thread inserted the same hash — safe to ignore
344
            if (e.getError().getCategory() != ErrorCategory.DUPLICATE_KEY) {
×
345
                logger.error("Failed to record hash for value (hash={}): {}", hash, e.getMessage(), e);
×
346
                throw e;
×
347
            }
348
            logger.debug("Concurrent insertion for hash {} detected; duplicate ignored", hash);
×
349
        }
3✔
350
    }
3✔
351

352
    /**
353
     * Retrieves the original value corresponding to a given hash from the "hashes" collection.
354
     *
355
     * @param hash the hash to look up
356
     * @return the original value, or null if not found
357
     */
358
    public static String unhash(String hash) {
359
        try (var c = collection("hashes").find(new Document("hash", hash)).cursor()) {
30✔
360
            if (c.hasNext()) {
9✔
361
                String value = c.next().getString("value");
18✔
362
                logger.debug("Unhash found value for hash {}", hash);
12✔
363
                return value;
12✔
364
            }
365
            logger.debug("Unhash: no value found for hash {}", hash);
12✔
366
            return null;
12✔
367
        }
12!
368
    }
369

370
    /**
371
     * Initializes the counter document to the current maximum counter value
372
     * in the nanopubs collection.
373
     * Uses $max to ensure the counter is never decreased. Safe to call on every startup.
374
     */
375
    private static void initCounter(ClientSession mongoSession) {
376
        Long maxCounter = (Long) getMaxValue(mongoSession, Collection.NANOPUBS.toString(), "counter");
21✔
377
        long effective = maxCounter != null ? maxCounter : 0L;
21✔
378
        collection("counters").updateOne(mongoSession, new Document("_id", "nanopubs"), new Document("$max", new Document("value", effective)), new UpdateOptions().upsert(true));
75✔
379
        if (maxCounter != null) {
6✔
380
            logger.info("Nanopub counter resumed at {} (max found in DB)", effective);
18✔
381
        } else {
382
            logger.info("Nanopub counter initialized to 0 (no existing nanopubs found)");
9✔
383
        }
384
    }
3✔
385

386
    /**
387
     * Returns the next counter value for a nanopub via atomic increment.
388
     */
389
    private static long getNextCounter(ClientSession mongoSession) {
390
        Document result = collection("counters").findOneAndUpdate(mongoSession, new Document("_id", "nanopubs"), new Document("$inc", new Document("value", 1L)), new FindOneAndUpdateOptions().upsert(true).returnDocument(ReturnDocument.AFTER));
84✔
391
        return result.getLong("value");
15✔
392
    }
393

394
    /**
395
     * Loads a nanopublication into the database.
396
     *
397
     * @param mongoSession the MongoDB client session
398
     * @param nanopub      the nanopublication to load
399
     */
400
    public static boolean loadNanopub(ClientSession mongoSession, Nanopub nanopub) {
401
        return loadNanopub(mongoSession, nanopub, null);
21✔
402
    }
403

404
    /**
405
     * Loads a nanopublication into the database, optionally filtering by public key hash and types.
406
     *
407
     * @param mongoSession the MongoDB client session
408
     * @param nanopub      the nanopublication to load
409
     * @param pubkeyHash   the public key hash to filter by (can be null)
410
     * @param types        the types to filter by (can be empty)
411
     * @return true if the nanopublication was loaded, false otherwise
412
     */
413
    public static boolean loadNanopub(ClientSession mongoSession, Nanopub nanopub, String pubkeyHash, String... types) {
414
        String pubkey = getPubkey(nanopub);
9✔
415
        if (pubkey == null) {
6✔
416
            logger.warn("Ignoring nanopub {}: no valid public key / signature found", nanopub.getUri());
15✔
417
            return false;
6✔
418
        }
419
        return loadNanopubVerified(mongoSession, nanopub, pubkey, pubkeyHash, types);
21✔
420
    }
421

422
    /**
423
     * Loads a nanopublication with a pre-verified public key, skipping signature verification.
424
     * Use this when the caller has already verified the signature via getPubkey().
425
     */
426
    static boolean loadNanopubVerified(ClientSession mongoSession, Nanopub nanopub, String verifiedPubkey, String pubkeyHash, String... types) {
427
        if (nanopub.getTripleCount() > 1200) {
12!
428
            logger.error("Rejecting nanopub {}: triple count {} exceeds limit of 1200", nanopub.getUri(), nanopub.getTripleCount());
×
429
            return false;
×
430
        }
431
        if (nanopub.getByteCount() > 1000000) {
15!
432
            logger.error("Rejecting nanopub {}: size {} bytes exceeds limit of 1000000", nanopub.getUri(), nanopub.getByteCount());
×
433
            return false;
×
434
        }
435
        Calendar creationTime;
436
        try {
437
            creationTime = nanopub.getCreationTime();
9✔
438
        } catch (Exception ex) {
×
439
            logger.warn("Nanopub {} has a malformed timestamp; proceeding without one", nanopub.getUri());
×
440
            creationTime = null;
×
441
        }
3✔
442
        if (creationTime != null && creationTime.getTimeInMillis() > System.currentTimeMillis() + 60000) {
27!
443
            logger.error("Rejecting nanopub {}: timestamp {} is more than 60s in the future", nanopub.getUri(), creationTime.toInstant());
×
444
            return false;
×
445
        }
446
        String nanopubUriStr = nanopub.getUri().stringValue();
12✔
447
        for (IRI graphUri : nanopub.getGraphUris()) {
33✔
448
            if (!graphUri.stringValue().startsWith(nanopubUriStr)) {
15!
449
                logger.error("Rejecting nanopub {}: graph URI {} does not start with the nanopub base URI", nanopub.getUri(), graphUri);
×
450
                return false;
×
451
            }
452
        }
3✔
453
        String ph = Utils.getHash(verifiedPubkey);
9✔
454
        if (pubkeyHash != null && !pubkeyHash.equals(ph)) {
18!
455
            logger.error("Rejecting nanopub {}: provided pubkey hash {} does not match computed hash {}", nanopub.getUri(), pubkeyHash, ph);
×
456
            return false;
×
457
        }
458
        recordHash(mongoSession, verifiedPubkey);
9✔
459

460
        String ac = TrustyUriUtils.getArtifactCode(nanopub.getUri().stringValue());
15✔
461
        if (ac == null) {
6!
462
            // I don't think this ever happens, but checking here to be sure
463
            logger.error("Rejecting nanopub {}: could not extract artifact code from Trusty URI", nanopub.getUri());
×
464
            return false;
×
465
        }
466
        if (has(mongoSession, Collection.NANOPUBS.toString(), ac)) {
18✔
467
            logger.debug("Skipping nanopub {}: already present in the database", nanopub.getUri());
18✔
468
        } else {
469
            String nanopubString;
470
            byte[] jellyContent;
471
            try {
472
                nanopubString = NanopubUtils.writeToString(nanopub, RDFFormat.TRIG);
12✔
473
                // Save the same thing in the Jelly format for faster loading
474
                jellyContent = JellyUtils.writeNanopubForDB(nanopub);
9✔
475
            } catch (IOException ex) {
×
476
                logger.error("Failed to serialize nanopub {}: {}", nanopub.getUri(), ex.getMessage(), ex);
×
477
                throw new RuntimeException(ex);
×
478
            }
3✔
479
            long counter = getNextCounter(mongoSession);
9✔
480
            boolean inserted = false;
6✔
481
            try {
482
                collection(Collection.NANOPUBS.toString()).insertOne(mongoSession, new Document("_id", ac).append("fullId", nanopub.getUri().stringValue()).append("counter", counter).append("pubkey", ph).append("content", nanopubString).append("jelly", new Binary(jellyContent)));
93✔
483
                inserted = true;
6✔
484
                logger.info("Loaded nanopub {} (counter: {}, pubkey hash: {})", nanopub.getUri(), counter, ph);
57✔
485
            } catch (MongoWriteException e) {
×
486
                if (e.getError().getCategory() != ErrorCategory.DUPLICATE_KEY) {
×
487
                    logger.error("Failed to insert nanopub {} (artifact {}): {}", nanopub.getUri(), ac, e.getMessage(), e);
×
488
                    throw e;
×
489
                }
490
                // Another thread inserted this nanopub concurrently — safe to skip
491
                logger.debug("Skipping nanopub {}: inserted concurrently by another thread", nanopub.getUri());
×
492
            }
3✔
493

494
            if (inserted) {
6!
495
                for (IRI invalidatedId : Utils.getInvalidatedNanopubIds(nanopub)) {
33✔
496
                    String invalidatedAc = TrustyUriUtils.getArtifactCode(invalidatedId.stringValue());
12✔
497
                    if (invalidatedAc == null) {
6!
498
                        logger.warn("Nanopub {} references invalidated nanopub {} with an unresolvable artifact code; skipping", nanopub.getUri(), invalidatedId);
×
499
                        continue;  // This should never happen; checking here just to be sure
×
500
                    }
501

502
                    // Add this nanopub also to all lists of invalidated nanopubs:
503
                    logger.debug("Nanopub {} invalidates {}; updating list entries and trust edges", nanopub.getUri(), invalidatedId);
18✔
504
                    collection("invalidations").insertOne(mongoSession, new Document("invalidatingNp", ac).append("invalidatingPubkey", ph).append("invalidatedNp", invalidatedAc));
45✔
505
                    MongoCursor<Document> invalidatedEntries = collection("listEntries").find(mongoSession, new Document("np", invalidatedAc).append("pubkey", ph)).cursor();
42✔
506
                    while (invalidatedEntries.hasNext()) {
9!
507
                        Document invalidatedEntry = invalidatedEntries.next();
×
508
                        addToList(mongoSession, nanopub, ph, invalidatedEntry.getString("type"));
×
509
                    }
×
510

511
                    collection("listEntries").updateMany(mongoSession, new Document("np", invalidatedAc).append("pubkey", ph), new Document("$set", new Document("invalidated", true)));
69✔
512
                    collection("trustEdges").updateMany(mongoSession, new Document("source", invalidatedAc), new Document("$set", new Document("invalidated", true)));
60✔
513
                    logger.debug("Marked invalidated entries and trust edges for invalidated artifact {}", invalidatedAc);
12✔
514
                }
3✔
515
            }
516
        }
517

518
        if (pubkeyHash != null) {
6✔
519
            for (String type : types) {
48✔
520
                // TODO Check if nanopub really has the type?
521
                addToList(mongoSession, nanopub, pubkeyHash, Utils.getTypeHash(mongoSession, type));
21✔
522
                if (type.equals("$")) {
12!
523
                    for (IRI t : NanopubUtils.getTypes(nanopub)) {
33✔
524
                        String th = Utils.getTypeHash(mongoSession, t);
12✔
525
                        if (CoverageFilter.isCoveredType(th)) {
9!
526
                            addToList(mongoSession, nanopub, pubkeyHash, th);
15✔
527
                        }
528
                    }
3✔
529
                }
530
            }
531
        }
532

533
        // Add the invalidating nanopubs also to the lists of this nanopub:
534
        try (MongoCursor<Document> invalidations = collection("invalidations").find(mongoSession, new Document("invalidatedNp", ac).append("invalidatingPubkey", ph)).cursor()) {
42✔
535
            if (invalidations.hasNext()) {
9!
536
                collection("listEntries").updateMany(mongoSession, new Document("np", ac).append("pubkey", ph), new Document("$set", new Document("invalidated", true)));
×
537
                collection("trustEdges").updateMany(mongoSession, new Document("source", ac), new Document("$set", new Document("invalidated", true)));
×
538
                logger.debug("Marked existing list entries and trust edges for nanopub {} as invalidated due to invalidations", ac);
×
539
            }
540
            while (invalidations.hasNext()) {
9!
541
                String iac = invalidations.next().getString("invalidatingNp");
×
542
                try {
543
                    Document npDoc = collection(Collection.NANOPUBS.toString()).find(mongoSession, new Document("_id", iac)).projection(new Document("jelly", 1)).first();
×
544
                    Nanopub inp = JellyUtils.readFromDB(npDoc.get("jelly", Binary.class).getData());
×
545
                    for (IRI type : NanopubUtils.getTypes(inp)) {
×
546
                        addToList(mongoSession, inp, ph, Utils.getTypeHash(mongoSession, type));
×
547
                    }
×
548
                } catch (RDF4JException | MalformedNanopubException ex) {
×
549
                    logger.error("Failed to load invalidating nanopub {} for invalidation record; skipping", iac, ex);
×
550
                }
×
551
            }
×
552

553
        }
554

555
        return true;
6✔
556
    }
557

558
    private static void addToList(ClientSession mongoSession, Nanopub nanopub, String pubkeyHash, String typeHash) {
559
        String ac = TrustyUriUtils.getArtifactCode(nanopub.getUri().stringValue());
15✔
560
        try {
561
            insert(mongoSession, "lists", new Document("pubkey", pubkeyHash).append("type", typeHash).append("maxPosition", -1L));
45✔
562
            logger.debug("Ensured list document exists for pubkey={} type={}", pubkeyHash, typeHash);
15✔
563
        } catch (MongoWriteException e) {
×
564
            // Duplicate key error -- ignore it
565
            if (e.getError().getCategory() != ErrorCategory.DUPLICATE_KEY) {
×
566
                logger.error("Failed to create list document for pubkey={} type={}: {}", pubkeyHash, typeHash, e.getMessage(), e);
×
567
                throw e;
×
568
            }
569
            logger.trace("List document already existed for pubkey={} type={}", pubkeyHash, typeHash);
×
570
        }
3✔
571

572
        if (has(mongoSession, "listEntries", new Document("pubkey", pubkeyHash).append("type", typeHash).append("np", ac))) {
45!
573
            logger.debug("Already listed: nanopub {} (artifact {}) for pubkey={} type={}", nanopub.getUri(), ac, pubkeyHash, typeHash);
×
574
        } else {
575
            initListPositionIfNeeded(mongoSession, pubkeyHash, typeHash);
12✔
576

577
            for (int attempt = 0; ; attempt++) {
6✔
578
                // Atomically claim next position
579
                Document updated = collection("lists").findOneAndUpdate(mongoSession, new Document("pubkey", pubkeyHash).append("type", typeHash), new Document("$inc", new Document("maxPosition", 1L)), new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER));
87✔
580
                long position = updated.getLong("maxPosition");
15✔
581

582
                // Get checksum from previous entry by exact position lookup (O(1) index hit)
583
                String checksum;
584
                if (position == 0) {
12!
585
                    checksum = NanopubUtils.updateXorChecksum(nanopub.getUri(), NanopubUtils.INIT_CHECKSUM);
18✔
586
                } else {
587
                    Document prevEntry = collection("listEntries").find(mongoSession, new Document("pubkey", pubkeyHash).append("type", typeHash).append("position", position - 1)).first();
×
588
                    String prevChecksum = (prevEntry != null) ? prevEntry.getString("checksum") : null;
×
589
                    if (prevChecksum == null) {
×
590
                        // Rare: previous entry not yet inserted by concurrent thread; fall back to sorted query
591
                        Document maxDoc = getMaxValueDocument(mongoSession, "listEntries", new Document("pubkey", pubkeyHash).append("type", typeHash), "position");
×
592
                        prevChecksum = (maxDoc != null) ? maxDoc.getString("checksum") : NanopubUtils.INIT_CHECKSUM;
×
593
                    }
594
                    checksum = NanopubUtils.updateXorChecksum(nanopub.getUri(), prevChecksum);
×
595
                }
596

597
                try {
598
                    collection("listEntries").insertOne(mongoSession, new Document("pubkey", pubkeyHash).append("type", typeHash).append("position", position).append("np", ac).append("checksum", checksum).append("invalidated", false));
78✔
599
                    logger.debug("Inserted list entry: pubkey={} type={} np={} position={} checksum={}", pubkeyHash, typeHash, ac, position, checksum);
78✔
600
                    break;
3✔
601
                } catch (MongoWriteException e) {
×
602
                    if (e.getError().getCategory() != ErrorCategory.DUPLICATE_KEY) {
×
603
                        logger.error("Failed to insert list entry for pubkey={} type={} np={}: {}", pubkeyHash, typeHash, ac, e.getMessage(), e);
×
604
                        throw e;
×
605
                    }
606
                    if (has(mongoSession, "listEntries", new Document("pubkey", pubkeyHash).append("type", typeHash).append("np", ac))) {
×
607
                        logger.debug("Concurrent insert detected and entry already exists for pubkey={} type={} np={}", pubkeyHash, typeHash, ac);
×
608
                        break; // Already listed by concurrent thread
×
609
                    }
610
                    if (attempt >= 100) {
×
611
                        logger.error("Failed to insert list entry after {} attempts for pubkey={} type={} np={}", attempt + 1, pubkeyHash, typeHash, ac);
×
612
                        throw new RuntimeException("Failed to insert list entry after " + (attempt + 1) + " attempts");
×
613
                    }
614
                    logger.debug("Retrying list entry insert (attempt {}) for pubkey={} type={} np={}", attempt + 1, pubkeyHash, typeHash, ac);
×
615
                }
616
            }
617
        }
618
    }
3✔
619

620
    /**
621
     * Lazily initializes the maxPosition field on a lists document for lists
622
     * created before this field existed. Uses a one-time sorted query, then
623
     * all subsequent calls use the atomic counter.
624
     */
625
    private static void initListPositionIfNeeded(ClientSession mongoSession, String pubkeyHash, String typeHash) {
626
        Document listDoc = collection("lists").find(mongoSession, new Document("pubkey", pubkeyHash).append("type", typeHash)).first();
45✔
627
        if (listDoc == null || listDoc.get("maxPosition") != null) {
18!
628
            logger.trace("initListPositionIfNeeded: no action needed for pubkey={} type={}", pubkeyHash, typeHash);
15✔
629
            return;
3✔
630
        }
631

632
        Document maxDoc = getMaxValueDocument(mongoSession, "listEntries", new Document("pubkey", pubkeyHash).append("type", typeHash), "position");
×
633
        long maxPos = (maxDoc != null) ? maxDoc.getLong("position") : -1L;
×
634

635
        // Conditional update: only set if maxPosition still doesn't exist (race-safe)
636
        collection("lists").updateOne(mongoSession, new Document("pubkey", pubkeyHash).append("type", typeHash).append("maxPosition", new Document("$exists", false)), new Document("$set", new Document("maxPosition", maxPos)));
×
637
        logger.debug("Initialized maxPosition={} for list pubkey={} type={}", maxPos, pubkeyHash, typeHash);
×
638
    }
×
639

640
    /**
641
     * Builds a comma-separated list of checksums at geometric positions for a given list,
642
     * for use with the afterChecksums parameter during peer sync.
643
     * Returns checksums at positions: max, max-10, max-100, max-1000, max-10000, ...
644
     * Returns null if the list has no entries.
645
     */
646
    public static String buildChecksumFallbacks(ClientSession mongoSession, String pubkeyHash, String typeHash) {
647
        Document maxDoc = getMaxValueDocument(mongoSession, "listEntries", new Document("pubkey", pubkeyHash).append("type", typeHash), "position");
39✔
648
        if (maxDoc == null) {
6✔
649
            logger.debug("buildChecksumFallbacks: no entries for pubkey={} type={}", pubkeyHash, typeHash);
15✔
650
            return null;
6✔
651
        }
652

653
        long maxPosition = maxDoc.getLong("position");
15✔
654
        StringBuilder sb = new StringBuilder();
12✔
655
        sb.append(maxDoc.getString("checksum"));
18✔
656

657
        for (long offset = 10; offset <= maxPosition; offset *= 10) {
33✔
658
            long targetPos = maxPosition - offset;
12✔
659
            Document entry = collection("listEntries").find(mongoSession, new Document("pubkey", pubkeyHash).append("type", typeHash).append("position", targetPos)).first();
57✔
660
            if (entry != null) {
6!
661
                sb.append(",").append(entry.getString("checksum"));
24✔
662
            }
663
        }
664
        String result = sb.toString();
9✔
665
        logger.debug("buildChecksumFallbacks for pubkey={} type={} -> {}", pubkeyHash, typeHash, result);
51✔
666
        return result;
6✔
667
    }
668

669
    /**
670
     * Returns the public key string of the Nanopub's signature, or null if not available or invalid.
671
     *
672
     * @param nanopub the nanopub to extract the public key from
673
     * @return The public key string, or null if not available or invalid.
674
     */
675
    public static String getPubkey(Nanopub nanopub) {
676
        // TODO shouldn't this be moved to a utility class in nanopub-java? there is a similar method in NanopubElement class of nanodash
677
        NanopubSignatureElement el;
678
        try {
679
            el = SignatureUtils.getSignatureElement(nanopub);
9✔
680
            if (el != null && SignatureUtils.hasValidSignature(el) && el.getPublicKeyString() != null) {
24!
681
                logger.trace("Valid signature found for nanopub {}", nanopub.getUri());
15✔
682
                return el.getPublicKeyString();
9✔
683
            }
684
            logger.debug("No valid signature element or public key present for nanopub {}", nanopub.getUri());
15✔
685
        } catch (MalformedCryptoElementException | GeneralSecurityException ex) {
×
686
            logger.error("Failed to verify signature of nanopub {}: {}", nanopub.getUri(), ex.getMessage(), ex);
×
687
        }
3✔
688
        return null;
6✔
689
    }
690

691
    /**
692
     * Calculates a hash representing the current state of the trust paths in the loading collection.
693
     *
694
     * @param mongoSession the MongoDB client session
695
     * @return the calculated trust state hash
696
     */
697
    public static String calculateTrustStateHash(ClientSession mongoSession) {
698
        MongoCursor<Document> tp = collection("trustPaths_loading").find(mongoSession).sort(ascending("_id")).cursor();
×
699
        // TODO Improve this so we don't create the full string just for calculating its hash:
700
        String s = "";
×
701
        while (tp.hasNext()) {
×
702
            Document d = tp.next();
×
703
            s += d.getString("_id") + " (" + d.getString("type") + ")\n";
×
704
        }
×
705
        String hash = Utils.getHash(s);
×
706
        logger.debug("Calculated trust state hash: {}", hash);
×
707
        return hash;
×
708
    }
709

710
}
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