use std::collections::BTreeMap;
use relate_core::Value;
use super::super::Graph;
#[test]
fn sealed_side_table_entry_exists_only_where_a_fold_lands() {
let g = Graph::default();
let (a, b) = crate::graph::test_support::endpoints(&g);
g.begin_write().unwrap();
g.create_rel(a, b, "T".into(), BTreeMap::new()).unwrap();
g.commit_write();
assert!(
g.out_side().sealed().region(a).is_none() && g.in_side().sealed().region(b).is_none(),
"a chunk below the floor has no sealed entry"
);
let floor = crate::adjacency::COMPACTION_DELTA_FLOOR as i64;
g.begin_write().unwrap();
for i in 0..floor - 1 {
g.create_rel(
a,
b,
"T".into(),
BTreeMap::from([("ts".into(), Value::Int(i))]),
)
.unwrap();
}
g.commit_write();
g.gc_versions();
let sealed = g
.out_side()
.sealed()
.region(a)
.expect("the fold lands an entry");
assert!(sealed.chunk_count() > 0);
assert!(g.in_side().sealed().region(b).is_some());
}
#[test]
fn a_multi_node_fold_seals_every_due_chunk() {
let g = Graph::default();
let (a, b) = crate::graph::test_support::endpoints(&g);
let (c, d) = crate::graph::test_support::endpoints(&g);
let floor = crate::adjacency::COMPACTION_DELTA_FLOOR as i64;
let mut created: [Vec<_>; 2] = [Vec::new(), Vec::new()];
g.begin_write().unwrap();
for i in 0..floor {
for (pair, (from, to)) in [(a, b), (c, d)].iter().enumerate() {
created[pair].push(
g.create_rel(
*from,
*to,
"T".into(),
BTreeMap::from([("ts".into(), Value::Int(i))]),
)
.unwrap(),
);
}
}
g.commit_write();
let type_id = g.interner.get_id("T").unwrap();
g.gc_versions();
for ((from, to), ids) in [((a, b), &created[0]), ((c, d), &created[1])] {
let sealed = g
.sealed_out
.region(from)
.expect("the fold lands a sealed entry");
let mut sealed_ids: Vec<_> = sealed.decoded(type_id).iter().map(|hop| hop.id).collect();
let mut expected = ids.clone();
sealed_ids.sort_unstable();
expected.sort_unstable();
assert_eq!(sealed_ids, expected, "the out fold of {from}");
assert_eq!(
crate::graph::test_support::sealed_chunk_len(&g.in_side(), to, type_id),
crate::adjacency::COMPACTION_DELTA_FLOOR,
"the in side of {to} seals too"
);
}
}
#[test]
fn delete_of_a_sealed_hop_records_the_versioned_tombstone() {
let g = Graph::default();
let (a, _, ids, type_id) = crate::graph::test_support::folded_floor_chunk(&g);
assert_eq!(
crate::graph::test_support::sealed_chunk_len(&g.out_side(), a, type_id),
crate::adjacency::COMPACTION_DELTA_FLOOR
);
g.begin_write().unwrap();
g.delete_rel(ids[5]);
g.commit_write();
let region = g.out_side().sealed().region(a).expect("sealed region");
assert_eq!(
region.chunk_len(type_id),
crate::adjacency::COMPACTION_DELTA_FLOOR,
"the sealed hop stays physical until reclaimed"
);
let tombstones = region.tombstones(type_id);
assert_eq!(tombstones.len(), 1);
assert_eq!(tombstones[0].id, ids[5]);
assert!(tombstones[0].xmax > 0);
}
#[test]
fn delete_without_a_delta_entry_still_tombstones_the_sealed_hop() {
let g = Graph::default();
let (a, _, ids, type_id) = crate::graph::test_support::folded_floor_chunk(&g);
assert!(
g.out_side().delta().remove(&a).is_some(),
"the fold leaves a delta entry"
);
assert!(g.out_side().delta().get(&a).is_none(), "delta entry absent");
assert!(
g.out_side().sealed().region(a).is_some(),
"sealed region present"
);
g.begin_write().unwrap();
g.delete_rel(ids[5]);
g.commit_write();
let region = g.out_side().sealed().region(a).expect("sealed region");
let tombstones = region.tombstones(type_id);
assert_eq!(tombstones.len(), 1);
let tombstone = tombstones[0];
assert_eq!(tombstone.id, ids[5]);
assert!(tombstone.deleted.is_some());
let visible = |ts: u64| {
region
.visible_hops(type_id, ts)
.iter()
.any(|h| h.id == ids[5])
};
assert!(
visible(tombstone.xmax - 1),
"an older snapshot sees the hop"
);
assert!(!visible(tombstone.xmax), "the tombstone hides it at xmax");
}
#[test]
fn deleted_delta_hop_reclaims_out_of_the_sealed_stream_without_a_ghost() {
use relate_ir::Direction;
let g = Graph::default();
let (a, b) = crate::graph::test_support::endpoints(&g);
let floor = crate::adjacency::COMPACTION_DELTA_FLOOR as i64;
g.begin_write().unwrap();
for i in 0..floor {
g.create_rel(
a,
b,
"T".into(),
BTreeMap::from([("ts".into(), Value::Int(i))]),
)
.unwrap();
}
g.commit_write();
let type_id = g.interner.get_id("T").unwrap();
g.compact_adjacency_at_watermark(0);
assert_eq!(
crate::graph::test_support::sealed_chunk_len(&g.out_side(), a, type_id),
floor as usize
);
let delta = floor / 4 + 1;
let mut extra = Vec::new();
g.begin_write().unwrap();
for i in 0..delta {
extra.push(
g.create_rel(
a,
b,
"T".into(),
BTreeMap::from([("ts".into(), Value::Int(floor + i))]),
)
.unwrap(),
);
}
g.commit_write();
let deleted = extra[0];
g.begin_write().unwrap();
g.delete_rel(deleted);
g.commit_write();
g.compact_adjacency_at_watermark(0);
{
let sealed = g.out_side().sealed().region(a).unwrap();
assert_eq!(sealed.chunk_len(type_id), (floor + delta) as usize);
assert!(sealed.decoded(type_id).iter().any(|h| h.id == deleted));
}
let mut walked = Vec::new();
g.for_each_neighbor_at(
a,
Direction::Outgoing,
g.commit_ts(),
&["T".to_string()],
|rid| walked.push(rid),
);
assert!(
!walked.contains(&deleted),
"visibility hides the deleted hop"
);
g.gc_versions();
{
let adj = g.out_side().delta().get(&a).unwrap();
let sealed = g.out_side().sealed().region(a).unwrap();
assert_eq!(sealed.chunk_len(type_id), (floor + delta - 1) as usize);
assert!(!sealed.decoded(type_id).iter().any(|h| h.id == deleted));
let mut physical = Vec::new();
adj.for_each_hop_of(Some(sealed), type_id, 0, |hop| physical.push(hop.id));
assert!(!physical.contains(&deleted), "no ghost hop");
}
assert_eq!(
g.adj_len(a, Direction::Outgoing),
(floor + delta - 1) as usize
);
}
#[test]
fn a_never_sealed_chunk_never_seals_a_deleted_hop() {
use relate_ir::Direction;
let g = Graph::default();
let (a, b) = crate::graph::test_support::endpoints(&g);
let floor = crate::adjacency::COMPACTION_DELTA_FLOOR as i64;
let mut ids = Vec::new();
g.begin_write().unwrap();
for i in 0..=floor {
ids.push(
g.create_rel(
a,
b,
"T".into(),
BTreeMap::from([("ts".into(), Value::Int(i))]),
)
.unwrap(),
);
}
g.commit_write();
let type_id = g.interner.get_id("T").unwrap();
assert!(g
.sealed_out
.region(a)
.is_none_or(|region| !region.has_chunk(type_id)));
let deleted = ids[5];
g.begin_write().unwrap();
g.delete_rel(deleted);
g.commit_write();
g.gc_versions();
let adj = g.out_side().delta().get(&a).unwrap();
let sealed = g.out_side().sealed().region(a).unwrap();
assert_eq!(sealed.chunk_len(type_id), floor as usize);
assert!(
!adj.flatten(Some(sealed)).contains(&deleted),
"no ghost hop"
);
assert_eq!(g.adj_len(a, Direction::Outgoing), floor as usize);
}
#[test]
fn a_zero_hop_sealed_region_reclaims_its_tombstones_at_the_checkpoint() {
let g = Graph::default();
let (a, b) = crate::graph::test_support::endpoints(&g);
let type_id = g.interner.intern("T");
g.begin_write().unwrap();
for ts in 0..2 {
g.create_rel(
a,
b,
"T".into(),
BTreeMap::from([("ts".into(), Value::Int(ts))]),
)
.unwrap();
}
g.commit_write();
for round in 2..10i64 {
g.begin_write().unwrap();
let id = g
.create_rel(
a,
b,
"T".into(),
BTreeMap::from([("ts".into(), Value::Int(round))]),
)
.unwrap();
g.commit_write();
g.begin_write().unwrap();
g.set_property_rel(id, "ts", Value::Int(1000 + round));
g.delete_rel(id);
g.commit_write();
assert!(
g.out_side()
.sealed()
.region(a)
.is_some_and(|region| region.has_chunk(type_id)),
"round {round}: the write records the zero-hop region"
);
g.gc_versions();
let sealed_tombstones = g
.sealed_out
.region(a)
.map_or(0, |region| region.tombstones(type_id).len());
let live_delta = g
.out_adj
.get(&a)
.map_or(0, |adj| adj.chunk_hops(type_id).len());
assert_eq!(
live_delta, 2,
"round {round}: the churned hop reclaimed, the live delta stays"
);
assert_eq!(
sealed_tombstones, 1,
"round {round}: the tombstone set stays bounded by the live delta"
);
}
}
#[test]
fn delta_hop_delete_and_reposition_record_the_tombstone_without_a_decode() {
let g = Graph::default();
let (a, b) = crate::graph::test_support::endpoints(&g);
let floor = crate::adjacency::COMPACTION_DELTA_FLOOR as i64;
g.begin_write().unwrap();
for i in 0..floor {
g.create_rel(
a,
b,
"T".into(),
BTreeMap::from([("ts".into(), Value::Int(i))]),
)
.unwrap();
}
g.commit_write();
let type_id = g.interner.get_id("T").unwrap();
g.compact_adjacency_at_watermark(0);
g.begin_write().unwrap();
let deleted = g
.create_rel(
a,
b,
"T".into(),
BTreeMap::from([("ts".into(), Value::Int(floor))]),
)
.unwrap();
let moved = g
.create_rel(
a,
b,
"T".into(),
BTreeMap::from([("ts".into(), Value::Int(floor + 1))]),
)
.unwrap();
g.commit_write();
g.begin_write().unwrap();
g.delete_rel(deleted);
g.commit_write();
let tombstones = {
let sealed = g.out_side().sealed().region(a).unwrap();
assert_eq!(
sealed.chunk_len(type_id),
floor as usize,
"the write touches no sealed bytes"
);
sealed.tombstones(type_id)
};
let t = tombstones
.iter()
.find(|t| t.id == deleted)
.expect("the delete records the tombstone");
assert_eq!(t.deleted, Some(t.xmax), "a delete stamps both positions");
assert!(t.xmax > 0);
g.begin_write().unwrap();
g.set_property_rel(moved, "ts", Value::Int(floor + 5));
g.commit_write();
let tombstones = g.out_side().sealed().region(a).unwrap().tombstones(type_id);
let t = tombstones
.iter()
.find(|t| t.id == moved)
.expect("the reposition records the tombstone");
assert!(
t.deleted.is_none(),
"a reposition only supersedes the sealed position"
);
}
#[test]
fn gc_at_the_pinned_horizon_keeps_a_relocated_delta_hop() {
use relate_ir::Direction;
let g = Graph::default();
let (a, _, ids, type_id) = crate::graph::test_support::folded_floor_chunk(&g);
let id = ids[5];
g.begin_write().unwrap();
g.set_property_rel(id, "ts", Value::Int(100));
g.commit_write();
let pin = g.pin_now().unwrap();
let pinned = pin.ts();
assert_eq!(pinned, g.commit_ts());
g.begin_write().unwrap();
g.delete_rel(id);
g.commit_write();
let deleted_at = g.commit_ts();
assert!(deleted_at > pinned);
g.gc_versions();
{
let adj = g.out_side().delta().get(&a).unwrap();
let sealed = g.out_side().sealed().region(a).unwrap();
assert_eq!(
sealed.chunk_len(type_id),
crate::adjacency::COMPACTION_DELTA_FLOOR - 1,
"the sealed position is reclaimed at the pinned horizon"
);
assert_eq!(
adj.chunk_hops(type_id).len(),
1,
"the relocated hop stays delta while its delete stamp is above the horizon"
);
let t = sealed
.tombstones(type_id)
.first()
.copied()
.expect("the tombstone still names the deleted delta hop");
assert_eq!((t.xmax, t.deleted), (pinned, Some(deleted_at)));
}
let mut seen = Vec::new();
g.for_each_neighbor_at(a, Direction::Outgoing, pinned, &["T".to_string()], |rid| {
seen.push(rid)
});
assert!(
seen.contains(&id),
"the pinned reader at {pinned} still sees the rel: {seen:?}"
);
assert!(
!g.neighbors(a, Direction::Outgoing).contains(&id),
"the live reader sees the rel deleted"
);
}
#[test]
fn sealed_chunk_write_path_charges_match_the_recount_oracle() {
let g = Graph::default();
let (a, b, ids, type_id) = crate::graph::test_support::folded_floor_chunk(&g);
let floor = crate::adjacency::COMPACTION_DELTA_FLOOR as i64;
assert_eq!(
crate::graph::test_support::sealed_chunk_len(&g.out_side(), a, type_id),
floor as usize
);
assert_eq!(g.used_bytes(), g.accounted_bytes());
g.begin_write().unwrap();
g.create_rel(
a,
b,
"T".into(),
BTreeMap::from([("ts".into(), Value::Int(floor))]),
)
.unwrap();
g.commit_write();
assert_eq!(g.used_bytes(), g.accounted_bytes());
g.begin_write().unwrap();
g.delete_rel(ids[5]);
g.commit_write();
assert_eq!(g.used_bytes(), g.accounted_bytes());
g.begin_write().unwrap();
g.set_property_rel(ids[6], "ts", Value::Int(floor + 1));
g.commit_write();
assert_eq!(g.used_bytes(), g.accounted_bytes());
g.gc_versions();
assert_eq!(g.used_bytes(), g.accounted_bytes());
}
#[test]
fn sealed_vectors_hold_exactly_the_capacity_their_floor_charges() {
let g = Graph::default();
let (a, _, ids, type_id) = crate::graph::test_support::folded_floor_chunk(&g);
g.begin_write().unwrap();
g.delete_rel(ids[5]);
g.commit_write();
let mut tombstoned = 0;
for side in [&g.out_side().sealed(), &g.in_side().sealed()] {
tombstoned += side.assert_fit();
}
assert_eq!(
tombstoned, 2,
"the delete tombstoned the sealed stream on both endpoints"
);
assert!(
crate::graph::test_support::sealed_chunk_len(&g.out_side(), a, type_id) > 0,
"the fold sealed the hops"
);
}
#[test]
fn the_floor_charges_the_sealed_slot_arrays() {
let g = Graph::default();
crate::graph::test_support::folded_floor_chunk(&g);
let arrays = g.out_side().sealed().array_bytes() + g.in_side().sealed().array_bytes();
assert!(arrays > 0, "the fold created sealed entries");
let floors = g.out_side().sealed().floor_bytes() + g.in_side().sealed().floor_bytes();
let with_sealed = g.structure_bytes();
g.out_side().sealed().clear();
g.in_side().sealed().clear();
assert_eq!(
with_sealed - g.structure_bytes(),
arrays + floors,
"the span tables are charged beside the arenas' floors"
);
}