Skip to content

Commit 2647790

Browse files
author
Hannah Bast
committed
Review Hannah, some more changes + two more TODOs
1 parent 15edb4d commit 2647790

File tree

7 files changed

+79
-66
lines changed

7 files changed

+79
-66
lines changed

src/engine/QueryExecutionContext.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// Copyright 2011, University of Freiburg,
2-
// Chair of Algorithms and Data Structures.
3-
// Author:
4-
// 2011-2017 Björn Buchhold ([email protected])
5-
// 2018- Johannes Kalmbach ([email protected])
1+
// Copyright 2011 - 2024, University of Freiburg
2+
// Chair of Algorithms and Data Structures
3+
// Authors: Björn Buchhold <[email protected]> [2011 - 2017]
4+
// Johannes Kalmbach <[email protected]> [2017 - 2024]
65

76
#pragma once
87

@@ -127,9 +126,10 @@ class QueryExecutionContext {
127126
private:
128127
const Index& _index;
129128

130-
// When the `QueryExecutionContext` is constructed, get a stable snapshot of
131-
// the current UPDATE status from the `DeltaTriplesManager`, which can then by
132-
// used by the query without interfering with concurrent UPDATEs.
129+
// When the `QueryExecutionContext` is constructed, get a stable read-only
130+
// snapshot of the current (located) delta triples. These can then be used
131+
// by the respective query without interfering with further incoming
132+
// update operations.
133133
SharedLocatedTriplesSnapshot sharedLocatedTriplesSnapshot{
134134
_index.deltaTriplesManager().getCurrentSnapshot()};
135135
QueryResultCache* const _subtreeCache;

src/engine/QueryExecutionTree.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// Copyright 2015, University of Freiburg,
2-
// Chair of Algorithms and Data Structures.
3-
// Author:
4-
// 2015-2017 Björn Buchhold ([email protected])
5-
// 2018- Johannes Kalmbach ([email protected])
1+
// Copyright 2015 - 2024, University of Freiburg
2+
// Chair of Algorithms and Data Structures
3+
// Authors: Björn Buchhold <[email protected]> [2015 - 2017]
4+
// Johannes Kalmbach <[email protected]> [2017 - 2024]
65

76
#include "./QueryExecutionTree.h"
87

src/engine/QueryExecutionTree.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// Copyright 2015, University of Freiburg,
2-
// Chair of Algorithms and Data Structures.
3-
// Author: Björn Buchhold ([email protected])
1+
// Copyright 2015 - 2024, University of Freiburg
2+
// Chair of Algorithms and Data Structures
3+
// Authors: Björn Buchhold <[email protected]>
4+
// Johannes Kalmbach <[email protected]>
5+
46
#pragma once
57

68
#include <memory>

src/index/DeltaTriples.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright 2023 - 2024, University of Freiburg
2-
// Chair of Algorithms and Data Structures.
3-
// Authors:
4-
// 2023 Hannah Bast <bast@cs.uni-freiburg.de>
5-
// 2024 Julian Mundhahs <mundhahj@tf.uni-freiburg.de>
2+
// Chair of Algorithms and Data Structures
3+
// Authors: Hannah Bast <[email protected]>
4+
// Julian Mundhahs <mundhahj@tf.uni-freiburg.de>
5+
// Johannes Kalmbach <kalmbach@cs.uni-freiburg.de>
66

77
#include "index/DeltaTriples.h"
88

@@ -179,8 +179,9 @@ LocatedTriplesSnapshot::getLocatedTriplesForPermutation(
179179

180180
// ____________________________________________________________________________
181181
SharedLocatedTriplesSnapshot DeltaTriples::getSnapshot() const {
182-
// Note: Semantically, both the members are copied, but the `localVocab_` has
183-
// no explicit copy constructor, hence the explicit `clone`.
182+
// NOTE: Both members of the `LocatedTriplesSnapshot` are copied, but the
183+
// `localVocab_` has no copy constructor (in order to avoid accidental
184+
// copies), hence the explicit `clone`.
184185
return SharedLocatedTriplesSnapshot{std::make_shared<LocatedTriplesSnapshot>(
185186
locatedTriples(), localVocab_.clone())};
186187
}

src/index/DeltaTriples.h

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright 2023 - 2024, University of Freiburg
2-
// Chair of Algorithms and Data Structures.
3-
// Authors:
4-
// 2023 Hannah Bast <bast@cs.uni-freiburg.de>
5-
// 2024 Julian Mundhahs <mundhahj@tf.uni-freiburg.de>
2+
// Chair of Algorithms and Data Structures
3+
// Authors: Hannah Bast <[email protected]>
4+
// Julian Mundhahs <mundhahj@tf.uni-freiburg.de>
5+
// Johannes Kalmbach <kalmbach@cs.uni-freiburg.de>
66

77
#pragma once
88

@@ -31,8 +31,8 @@ struct LocatedTriplesSnapshot {
3131
Permutation::Enum permutation) const;
3232
};
3333

34-
// A `shared_ptr` to a const `LocatedTriplesSnapshot`, but as an explicit class,
35-
// s.t. it can be forward-declared.
34+
// A shared pointer to a constant `LocatedTriplesSnapshot`, but as an explicit
35+
// class, such that it can be forward-declared.
3636
class SharedLocatedTriplesSnapshot
3737
: public std::shared_ptr<const LocatedTriplesSnapshot> {};
3838

@@ -194,21 +194,13 @@ class DeltaTriplesManager {
194194
explicit DeltaTriplesManager(const IndexImpl& index);
195195
FRIEND_TEST(DeltaTriplesTest, DeltaTriplesManager);
196196

197-
// Modify the underlying `DeltaTriples` by applying the `function` to them.
198-
// Then update the current snapshot, s.t. subsequent calls to
199-
// `getCurrentSnapshot` will observe the modifications. All this is done in a
200-
// thread-safe way, meaning that there can be only one call to `modify` at the
201-
// same time.
197+
// Modify the underlying `DeltaTriples` by applying `function` and then update
198+
// the current snapshot. Concurrent calls to `modify` will be serialized, and
199+
// each call to `getCurrentSnapshot` will either return the snapshot before or
200+
// after a modification, but never one of an ongoing modification.
202201
void modify(std::function<void(DeltaTriples&)> function);
203202

204-
// Return a `SharedLocatedTriplesSnapshot` that contains a deep copy of the
205-
// state of the underlying `DeltaTriples` after the last completed UPDATE, and
206-
// thus is not affected by future UPDATE requests. It can therefore be used to
207-
// execute a query in a consistent way.
203+
// Return a shared pointer to a deep copy of the current snapshot. This can
204+
// be safely used to execute a query without interfering with future updates.
208205
SharedLocatedTriplesSnapshot getCurrentSnapshot() const;
209206
};
210-
211-
// DELTA TRIPLES AND THE CACHE
212-
//
213-
// Changes to the DeltaTriples invalidate all cache results that have an index
214-
// scan in their subtree, which is almost all entries in practice.

src/index/Permutation.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// Copyright 2018, University of Freiburg,
2-
// Chair of Algorithms and Data Structures.
3-
// Author: Johannes Kalmbach<joka921> ([email protected])
1+
// Copyright 2018 - 2024, University of Freiburg
2+
// Chair of Algorithms and Data Structures
3+
// Author: Johannes Kalmbach <[email protected]>
4+
45
#pragma once
56

67
#include <array>
@@ -148,20 +149,21 @@ class Permutation {
148149
const Permutation& getActualPermutation(const ScanSpecification& spec) const;
149150
const Permutation& getActualPermutation(Id id) const;
150151

152+
// From the given snapshot, get the located triples for this permutation.
151153
const LocatedTriplesPerBlock& getLocatedTriplesForPermutation(
152154
const LocatedTriplesSnapshot& locatedTriplesSnapshot) const;
153155

154156
const CompressedRelationReader& reader() const { return reader_.value(); }
155157

156158
private:
157-
// for Log output, e.g. "POS"
159+
// Readable name for this permutation, e.g., `POS`.
158160
std::string readableName_;
159-
// e.g. ".pos"
161+
// File name suffix for this permutation, e.g., `.pos`.
160162
std::string fileSuffix_;
161-
// order of the 3 keys S(0), P(1), and O(2) for which this permutation is
162-
// sorted, for example {1, 0, 2} for PSO.
163+
// The order of the three components (S=0, P=1, O=2) in this permutation,
164+
// e.g., `{1, 0, 2}` for `PSO`.
163165
array<size_t, 3> keyOrder_;
164-
166+
// The metadata for this permutation.
165167
MetaData meta_;
166168

167169
// This member is `optional` because we initialize it in a deferred way in the

test/DeltaTriplesTest.cpp

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -322,23 +322,25 @@ TEST_F(DeltaTriplesTest, rewriteLocalVocabEntriesAndBlankNodes) {
322322

323323
// _____________________________________________________________________________
324324
TEST_F(DeltaTriplesTest, DeltaTriplesManager) {
325+
// Preparation.
325326
DeltaTriplesManager deltaTriplesManager(testQec->getIndex().getImpl());
326327
auto& vocab = testQec->getIndex().getVocab();
327328
auto cancellationHandle =
328329
std::make_shared<ad_utility::CancellationHandle<>>();
329-
330330
std::vector<ad_utility::JThread> threads;
331331
static constexpr size_t numThreads = 18;
332332
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.
335337
auto insertAndDelete = [&](size_t threadIdx) {
336338
LocalVocab localVocab;
337339
SharedLocatedTriplesSnapshot beforeUpdate =
338340
deltaTriplesManager.getCurrentSnapshot();
339341
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`.
342344
auto triplesToInsert = makeIdTriples(
343345
vocab, localVocab,
344346
{"<A> <B> <C>", absl::StrCat("<A> <B> <D", threadIdx, ">"),
@@ -347,25 +349,26 @@ TEST_F(DeltaTriplesTest, DeltaTriplesManager) {
347349
vocab, localVocab,
348350
{"<A> <C> <E>", absl::StrCat("<A> <B> <E", threadIdx, ">"),
349351
absl::StrCat("<A> <B> <F", threadIdx, ">")});
350-
352+
// Insert the `triplesToInsert`.
351353
deltaTriplesManager.modify([&](DeltaTriples& deltaTriples) {
352354
deltaTriples.insertTriples(cancellationHandle, triplesToInsert);
353355
});
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.
357358
EXPECT_NE(beforeUpdate, deltaTriplesManager.getCurrentSnapshot());
358-
359+
// Delete the `triplesToDelete`.
359360
deltaTriplesManager.modify([&](DeltaTriples& deltaTriples) {
360361
deltaTriples.deleteTriples(cancellationHandle, triplesToDelete);
361362
});
362363

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).
363366
if (i == numIterations / 2) {
364367
{
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`).
369372
const auto& locatedSPO =
370373
beforeUpdate->getLocatedTriplesForPermutation(Permutation::SPO);
371374
EXPECT_FALSE(locatedSPO.containsTriple(triplesToInsert.at(1), true));
@@ -376,6 +379,11 @@ TEST_F(DeltaTriplesTest, DeltaTriplesManager) {
376379
EXPECT_FALSE(locatedSPO.containsTriple(triplesToDelete.at(2), false));
377380
}
378381
{
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`.
379387
auto p = deltaTriplesManager.getCurrentSnapshot();
380388
const auto& locatedSPO =
381389
p->getLocatedTriplesForPermutation(Permutation::SPO);
@@ -386,17 +394,26 @@ TEST_F(DeltaTriplesTest, DeltaTriplesManager) {
386394
}
387395
}
388396
};
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).
390400
for (size_t i = 0; i < numThreads; ++i) {
391401
threads.emplace_back(insertAndDelete, i);
392402
}
393403
threads.clear();
394404

395-
// If there are no updates, then the snapshot pointer doesn't change.
405+
// Check that without updates, the snapshot pointer does not change.
396406
auto p1 = deltaTriplesManager.getCurrentSnapshot();
397407
auto p2 = deltaTriplesManager.getCurrentSnapshot();
398408
EXPECT_EQ(p1, p2);
399409

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.
400417
auto deltaImpl = deltaTriplesManager.deltaTriples_.rlock();
401418
EXPECT_THAT(*deltaImpl, NumTriples(numThreads + 1, 2 * numThreads + 1,
402419
3 * numThreads + 2));

0 commit comments

Comments
 (0)