@@ -264,9 +264,23 @@ void SanityCheck(const DepGraph<SetType>& depgraph)
264
264
for (ClusterIndex j = 0 ; j < depgraph.TxCount (); ++j) {
265
265
assert (depgraph.Ancestors (i)[j] == depgraph.Descendants (j)[i]);
266
266
}
267
+ // No transaction is a parent or child of itself.
268
+ auto parents = depgraph.GetReducedParents (i);
269
+ auto children = depgraph.GetReducedChildren (i);
270
+ assert (!parents[i]);
271
+ assert (!children[i]);
272
+ // Parents of a transaction do not have ancestors inside those parents (except itself).
273
+ // Note that even the transaction itself may be missing (if it is part of a cycle).
274
+ for (auto parent : parents) {
275
+ assert ((depgraph.Ancestors (parent) & parents).IsSubsetOf (SetType::Singleton (parent)));
276
+ }
277
+ // Similar for children and descendants.
278
+ for (auto child : children) {
279
+ assert ((depgraph.Descendants (child) & children).IsSubsetOf (SetType::Singleton (child)));
280
+ }
267
281
}
268
- // If DepGraph is acyclic, serialize + deserialize must roundtrip.
269
282
if (IsAcyclic (depgraph)) {
283
+ // If DepGraph is acyclic, serialize + deserialize must roundtrip.
270
284
std::vector<unsigned char > ser;
271
285
VectorWriter writer (ser, 0 );
272
286
writer << Using<DepGraphFormatter>(depgraph);
@@ -284,6 +298,37 @@ void SanityCheck(const DepGraph<SetType>& depgraph)
284
298
reader >> Using<DepGraphFormatter>(decoded_depgraph);
285
299
assert (depgraph == decoded_depgraph);
286
300
assert (reader.empty ());
301
+
302
+ // In acyclic graphs, the union of parents with parents of parents etc. yields the
303
+ // full ancestor set (and similar for children and descendants).
304
+ std::vector<SetType> parents, children;
305
+ for (ClusterIndex i = 0 ; i < depgraph.TxCount (); ++i) {
306
+ parents.push_back (depgraph.GetReducedParents (i));
307
+ children.push_back (depgraph.GetReducedChildren (i));
308
+ }
309
+ for (ClusterIndex i = 0 ; i < depgraph.TxCount (); ++i) {
310
+ // Initialize the set of ancestors with just the current transaction itself.
311
+ SetType ancestors = SetType::Singleton (i);
312
+ // Iteratively add parents of all transactions in the ancestor set to itself.
313
+ while (true ) {
314
+ const auto old_ancestors = ancestors;
315
+ for (auto j : ancestors) ancestors |= parents[j];
316
+ // Stop when no more changes are being made.
317
+ if (old_ancestors == ancestors) break ;
318
+ }
319
+ assert (ancestors == depgraph.Ancestors (i));
320
+
321
+ // Initialize the set of descendants with just the current transaction itself.
322
+ SetType descendants = SetType::Singleton (i);
323
+ // Iteratively add children of all transactions in the descendant set to itself.
324
+ while (true ) {
325
+ const auto old_descendants = descendants;
326
+ for (auto j : descendants) descendants |= children[j];
327
+ // Stop when no more changes are being made.
328
+ if (old_descendants == descendants) break ;
329
+ }
330
+ assert (descendants == depgraph.Descendants (i));
331
+ }
287
332
}
288
333
}
289
334
0 commit comments