@@ -322,23 +322,25 @@ TEST_F(DeltaTriplesTest, rewriteLocalVocabEntriesAndBlankNodes) {
322
322
323
323
// _____________________________________________________________________________
324
324
TEST_F (DeltaTriplesTest, DeltaTriplesManager) {
325
+ // Preparation.
325
326
DeltaTriplesManager deltaTriplesManager (testQec->getIndex ().getImpl ());
326
327
auto & vocab = testQec->getIndex ().getVocab ();
327
328
auto cancellationHandle =
328
329
std::make_shared<ad_utility::CancellationHandle<>>();
329
-
330
330
std::vector<ad_utility::JThread> threads;
331
331
static constexpr size_t numThreads = 18 ;
332
332
static constexpr size_t numIterations = 21 ;
333
- // The following lambda inserts and deletes some triples and checks the state
334
- // of the `DeltaTriples` for consistency.
333
+
334
+ // Insert and delete a well-defined set of triples, some independent and some
335
+ // dependent on the thread index. Check that the snapshot before in the
336
+ // middle of these updates is as expected.
335
337
auto insertAndDelete = [&](size_t threadIdx) {
336
338
LocalVocab localVocab;
337
339
SharedLocatedTriplesSnapshot beforeUpdate =
338
340
deltaTriplesManager.getCurrentSnapshot ();
339
341
for (size_t i = 0 ; i < numIterations; ++i) {
340
- // The first triple in both vectors is shared between all threads, the
341
- // other triples are exclusive to this thread via the `threadIdx`.
342
+ // The first triple in both vectors is the same for all threads, the
343
+ // others are exclusive to this thread via the `threadIdx`.
342
344
auto triplesToInsert = makeIdTriples (
343
345
vocab, localVocab,
344
346
{" <A> <B> <C>" , absl::StrCat (" <A> <B> <D" , threadIdx, " >" ),
@@ -347,25 +349,26 @@ TEST_F(DeltaTriplesTest, DeltaTriplesManager) {
347
349
vocab, localVocab,
348
350
{" <A> <C> <E>" , absl::StrCat (" <A> <B> <E" , threadIdx, " >" ),
349
351
absl::StrCat (" <A> <B> <F" , threadIdx, " >" )});
350
-
352
+ // Insert the `triplesToInsert`.
351
353
deltaTriplesManager.modify ([&](DeltaTriples& deltaTriples) {
352
354
deltaTriples.insertTriples (cancellationHandle, triplesToInsert);
353
355
});
354
-
355
- // We have successfully complete an update, so we expect the snapshot
356
- // pointer to change.
356
+ // We should have successfully completed an update, so the snapshot
357
+ // pointer should have changed.
357
358
EXPECT_NE (beforeUpdate, deltaTriplesManager.getCurrentSnapshot ());
358
-
359
+ // Delete the `triplesToDelete`.
359
360
deltaTriplesManager.modify ([&](DeltaTriples& deltaTriples) {
360
361
deltaTriples.deleteTriples (cancellationHandle, triplesToDelete);
361
362
});
362
363
364
+ // Make some checks in the middle of these updates (while the other
365
+ // threads are likely to be in the middle of their updates as well).
363
366
if (i == numIterations / 2 ) {
364
367
{
365
- // Before the first iteration, none of the thread-exclusive triples
366
- // are contained in the snapshot returned by the
367
- // `locatedTriplesSnapshot_`. As the snapshot is persistent over time,
368
- // this doesn't change in further iterations .
368
+ // None of the thread-exclusive triples should be contained in the
369
+ // original snapshot and this should not change over time. The
370
+ // Boolean argument specifies whether the triple was inserted (`true`)
371
+ // or deleted (`false`) .
369
372
const auto & locatedSPO =
370
373
beforeUpdate->getLocatedTriplesForPermutation (Permutation::SPO);
371
374
EXPECT_FALSE (locatedSPO.containsTriple (triplesToInsert.at (1 ), true ));
@@ -376,6 +379,11 @@ TEST_F(DeltaTriplesTest, DeltaTriplesManager) {
376
379
EXPECT_FALSE (locatedSPO.containsTriple (triplesToDelete.at (2 ), false ));
377
380
}
378
381
{
382
+ // Check for several of the thread-exclusive triples that they are
383
+ // properly contained in the current snapshot.
384
+ //
385
+ // TODO(Hannah): I don't understand the `false` for the second
386
+ // `containsTriple`.
379
387
auto p = deltaTriplesManager.getCurrentSnapshot ();
380
388
const auto & locatedSPO =
381
389
p->getLocatedTriplesForPermutation (Permutation::SPO);
@@ -386,17 +394,26 @@ TEST_F(DeltaTriplesTest, DeltaTriplesManager) {
386
394
}
387
395
}
388
396
};
389
- // Run the above lambda in multiple threads to detect
397
+
398
+ // Run the above for each of `numThreads` threads, where each thread knows
399
+ // its index (used to create the thread-exclusive triples).
390
400
for (size_t i = 0 ; i < numThreads; ++i) {
391
401
threads.emplace_back (insertAndDelete, i);
392
402
}
393
403
threads.clear ();
394
404
395
- // If there are no updates, then the snapshot pointer doesn't change.
405
+ // Check that without updates, the snapshot pointer does not change.
396
406
auto p1 = deltaTriplesManager.getCurrentSnapshot ();
397
407
auto p2 = deltaTriplesManager.getCurrentSnapshot ();
398
408
EXPECT_EQ (p1, p2);
399
409
410
+ // Each of the threads above inserts on thread-exclusive triple, deletes one
411
+ // thread-exclusive triple and inserts one thread-exclusive triple that is
412
+ // deleted right after. Additionally, there is one common triple inserted by
413
+ // all the threads and one common triple that is deleted by all the threads.
414
+ //
415
+ // TODO(Hannah): I don't understand why the number of thread-exclusive delted
416
+ // triples is twice that of the thread-exclusive inserted triples.
400
417
auto deltaImpl = deltaTriplesManager.deltaTriples_ .rlock ();
401
418
EXPECT_THAT (*deltaImpl, NumTriples (numThreads + 1 , 2 * numThreads + 1 ,
402
419
3 * numThreads + 2 ));
0 commit comments