use relate_core::{NodeId, RelId, Value};
use super::super::accounting::adjacency::AdjDelta;
use super::super::Graph;
use super::chain::{props, step_matches_charge};
use crate::adjacency::{with_arena_wall, Hop, SealedArena, SealedStream, RESTORE_SEAL_HOPS};
use crate::redo::RedoOp;
#[test]
fn a_wholesale_install_leaves_fit_nothing_to_copy() {
let hops: Vec<Hop> = (0..9).rev().map(|id| Hop { ts: Some(id), id }).collect();
let arena = SealedArena::default();
let mut streams = vec![SealedStream::default(), SealedStream::default()];
for node in 0..40 {
let stream = &mut streams[node as usize % 2];
for type_id in [3, 1, 2] {
assert!(stream.seal_chunk(node, type_id, &hops));
}
stream.end_node();
}
arena.install(streams);
let (buffers, capacity, len) = arena.buffer_probe();
assert_eq!(
capacity, len,
"the byte buffer is exactly its encoded length"
);
arena.fit();
assert_eq!(
arena.buffer_probe(),
(buffers, capacity, len),
"fit copied nothing"
);
assert_eq!(arena.assert_fit(), 0);
for node in [0, 7, 39] {
let region = arena.region(node).expect("every node sealed");
assert_eq!(region.type_ids(), vec![1, 2, 3]);
for type_id in 1..=3 {
assert_eq!(region.decoded(type_id), hops, "node {node} type {type_id}");
}
}
}
#[test]
fn reposition_sealed_charge_prices_the_price_side_arms() {
let g = Graph::default();
let (a, b, ids, _) = crate::graph::test_support::folded_floor_chunk(&g);
g.begin_write().unwrap();
g.set_property_rel(ids[0], "ts", Value::Int(1000));
g.commit_write();
g.begin_write().unwrap();
g.set_property_rel(ids[0], "ts", Value::Int(1001));
g.commit_write();
assert_eq!(g.used_bytes(), g.accounted_bytes());
let lone = crate::graph::test_support::doomed_rel(&g, a, b, "V");
g.begin_write().unwrap();
g.set_property_rel(lone, "ts", Value::Int(7));
g.commit_write();
assert_eq!(g.used_bytes(), g.accounted_bytes());
}
#[test]
fn a_fully_reclaimed_chunk_marks_its_stream_dead_before_the_entry_goes() {
let g = Graph::default();
let (_, arena) = crate::graph::test_support::reclaimed_folded_chunk(&g);
let (_, dead) = g.out_side().sealed().reclaim_pair();
assert_eq!(
dead, arena,
"the dead range is exactly the reclaimed stream's length"
);
g.out_side().sealed().compact();
assert_eq!(g.out_side().sealed().reclaim_pair(), (0, 0));
}
#[test]
fn a_wipe_drops_the_dead_ranges_too() {
let g = Graph::default();
let (_, arena) = crate::graph::test_support::reclaimed_folded_chunk(&g);
let (_, dead) = g.out_side().sealed().reclaim_pair();
assert_eq!(dead, arena, "the reclaimed stream is dead, not compacted");
g.out_side().sealed().clear();
assert_eq!(
g.out_side().sealed().reclaim_pair(),
(0, 0),
"the wipe drops the arena, its entries and the dead ranges"
);
assert_eq!(g.out_side().sealed().floor_bytes(), 0);
assert_eq!(g.out_side().sealed().array_bytes(), 0);
}
fn created_pair(g: &Graph) {
let create = |id: NodeId| RedoOp::create_node(id, Vec::new(), props(1));
step_matches_charge(g, vec![create(0), create(1)], false);
step_matches_charge(
g,
vec![RedoOp::create_rel(2, 0, 1, "U".into(), props(1))],
false,
);
}
fn founded_region() -> Graph {
let g = Graph::default();
created_pair(&g);
step_matches_charge(&g, vec![RedoOp::delete_rel(2)], true);
g
}
fn delete_delta(g: &Graph, rel: RelId) -> (AdjDelta, Vec<RedoOp>) {
let ops = vec![RedoOp::delete_rel(rel)];
(g.estimate_adj_delta(&ops, &g.dense_spans()).0, ops)
}
#[test]
fn sealed_delete_founds_the_on_demand_region() {
let g = Graph::default();
created_pair(&g);
step_matches_charge(&g, vec![RedoOp::delete_rel(2)], true);
}
#[test]
fn sealed_delete_of_a_rel_created_in_the_same_batch() {
let g = founded_region();
step_matches_charge(
&g,
vec![
RedoOp::create_rel(3, 0, 1, "U".into(), props(1)),
RedoOp::delete_rel(3),
],
true,
);
}
#[test]
fn sealed_delete_in_a_folded_chunk_is_all_spill() {
let g = Graph::default();
let (_, _, ids, _) = crate::graph::test_support::folded_floor_chunk(&g);
let (AdjDelta { refusable, spill }, sealed) = delete_delta(&g, ids[0]);
let stride = crate::adjacency::tombstone_stride() as i64;
assert_eq!(refusable, 0);
assert_eq!(spill, 2 * stride, "one tombstone slot per endpoint");
step_matches_charge(&g, sealed, true);
}
#[test]
fn sealed_delete_of_a_type_joining_a_held_region() {
let g = Graph::default();
let (a, b, _, _) = crate::graph::test_support::folded_floor_chunk(&g);
let joiner = crate::graph::test_support::doomed_rel(&g, a, b, "V");
step_matches_charge(&g, vec![RedoOp::delete_rel(joiner)], true);
}
#[test]
fn sealed_delete_of_two_types_joining_one_region() {
let g = Graph::default();
let (a, b, _, _) = crate::graph::test_support::folded_floor_chunk(&g);
let pair = [
crate::graph::test_support::doomed_rel(&g, a, b, "W"),
crate::graph::test_support::doomed_rel(&g, a, b, "X"),
];
step_matches_charge(
&g,
vec![RedoOp::delete_rel(pair[0]), RedoOp::delete_rel(pair[1])],
true,
);
}
#[test]
fn sealed_delete_founding_slot_growth_is_refusable() {
let g = Graph::default();
crate::graph::test_support::folded_floor_chunk(&g);
let (edge, other) = crate::graph::test_support::endpoints(&g);
let founder = crate::graph::test_support::doomed_rel(&g, edge, other, "Y");
let (
AdjDelta {
refusable: found_refusable,
spill: found_spill,
},
founder_ops,
) = delete_delta(&g, founder);
assert!(
found_refusable > 0,
"a founding delete's slot growth is refusable"
);
assert!(found_spill > 0, "its chunk and tombstone terms spill");
step_matches_charge(&g, founder_ops, true);
}
#[test]
fn sealed_delete_founding_at_a_side_map_id_refuses_nothing() {
let g = founded_region();
let sparse = crate::graph::test_support::first_side_map_id(crate::graph::sealed_slot());
g.apply_redo(
vec![RedoOp::create_node(sparse, Vec::new(), props(1))],
g.commit_ts() + 1,
)
.unwrap();
let doomed = crate::graph::test_support::doomed_rel(&g, sparse, 0, "Z");
let (
AdjDelta {
refusable: sparse_refusable,
spill: sparse_spill,
},
doomed_ops,
) = delete_delta(&g, doomed);
assert_eq!(
sparse_refusable, 0,
"a founding delete at a side-map id refuses nothing"
);
assert!(
sparse_spill > 0,
"its side-map entry is reclaimed, so spill"
);
step_matches_charge(&g, doomed_ops, true);
}
fn three_sealable_runs(g: &Graph, from: NodeId, to: NodeId) {
g.begin_write().unwrap();
for type_name in ["T0", "T1", "T2"] {
for _ in 0..RESTORE_SEAL_HOPS + 2 {
g.create_rel(
from,
to,
type_name.into(),
std::collections::BTreeMap::new(),
)
.unwrap();
}
}
g.commit_write();
}
fn sealed_chunks(g: &Graph, node: NodeId) -> usize {
g.out_side()
.sealed()
.region(node)
.map_or(0, |region| region.chunk_count())
}
#[test]
fn a_wholesale_build_past_the_wall_keeps_the_overflow_in_delta() {
let g = Graph::default();
let (a, b) = crate::graph::test_support::endpoints(&g);
three_sealable_runs(&g, a, b);
g.rebuild_adj();
assert_eq!(sealed_chunks(&g, a), 3);
let whole = g.out_side().sealed().arena_bytes();
let degree = g.degree_out(a);
let wall = whole - 1;
with_arena_wall(wall, || g.rebuild_adj());
assert!(g.out_side().sealed().arena_bytes() <= wall);
assert_eq!(
sealed_chunks(&g, a),
2,
"the overflowing run stays in the delta tier"
);
assert_eq!(
g.degree_out(a),
degree,
"the build reads back the same rels"
);
}
#[test]
fn shards_that_overflow_the_wall_together_build_all_delta() {
let g = Graph::default();
let (a, b) = crate::graph::test_support::endpoints(&g);
let (c, d) = crate::graph::test_support::endpoints(&g);
assert_ne!(a % 2, d % 2, "the two starts land in different shards");
three_sealable_runs(&g, a, b);
three_sealable_runs(&g, d, c);
let whole = crate::graph::shard::with_workers(2, || {
g.rebuild_adj();
g.out_side().sealed().arena_bytes()
});
let degree = (g.degree_out(a), g.degree_out(d));
crate::graph::shard::with_workers(2, || {
with_arena_wall(whole - 1, || g.rebuild_adj());
});
assert_eq!(g.out_side().sealed().arena_bytes(), 0, "all delta");
assert_eq!(sealed_chunks(&g, a), 0);
assert_eq!((g.degree_out(a), g.degree_out(d)), degree);
}