Rust R-V and R-W: row identity is (schemaId, recordKey), and each hop
edge is anchored by its own direction R-V (S-1): select()'s row index and the cited incoming index were both keyed by record key alone, so two schemas sharing one key collided - which row a `cites` edge resolved to, and whether a `cited` selection counted a row as cited, depended on row order. Both indexes are now keyed by (schemaId, recordKey), matching CultCache's per-schema key uniqueness. R-W (S-3), Rust half: a selection carrying both `cites` and `cited` computed one `cited_direction` flag and applied it to every edge in the batch, so the `cites` edges vanished whenever `cited` was also present. Each `EdgeMatch` now carries its own `EdgeAnchor` (Citer/Citee) set at the point it is produced, and the page filter/sort anchor each edge by its own hop instead of one shared direction. Tests: two schemas at one record key, in both row orders, agree on which row a citation resolves to and which row counts as cited; a selection with both hops returns edges from both directions. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
99e366a36d
commit
ccea1098ce
2 changed files with 144 additions and 24 deletions
|
|
@ -1426,6 +1426,20 @@ pub struct EdgeMatch<R: Row + Clone> {
|
|||
pub role: String,
|
||||
pub to: R,
|
||||
pub payload: Option<Vec<u8>>,
|
||||
/// R-W: which end of this edge is the page row that produced it - `cites` anchors on the
|
||||
/// citer (`from`), `cited` anchors on the citee (`to`). A selection carrying both hops mixes
|
||||
/// edges from both sources in one `Vec`, so each edge must carry its own anchor rather than
|
||||
/// the whole batch being filtered by one shared direction.
|
||||
pub anchor: EdgeAnchor,
|
||||
}
|
||||
|
||||
/// Which end of an [`EdgeMatch`] is the page row it was found through (R-W).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum EdgeAnchor {
|
||||
/// Produced by `cites`: the page row is the citer, `from`.
|
||||
Citer,
|
||||
/// Produced by `cited`: the page row is the citee, `to`.
|
||||
Citee,
|
||||
}
|
||||
|
||||
/// Evaluated, ordered, paged rows plus the edges a hop traversed.
|
||||
|
|
@ -1454,7 +1468,12 @@ pub fn select<R: Row + Clone>(
|
|||
) -> Result<Evaluation<R>, SelectionRefusal> {
|
||||
validate(selection, row_set)?;
|
||||
|
||||
let by_key: HashMap<&str, &R> = all_rows.iter().map(|row| (row.record_key(), row)).collect();
|
||||
// R-V: a row's identity is `(schemaId, recordKey)`, never the key alone - CultCache keys are
|
||||
// unique per schema, so two schemas can share one record key and must not collide here.
|
||||
let by_key: HashMap<(&str, &str), &R> = all_rows
|
||||
.iter()
|
||||
.map(|row| ((row.schema_id(), row.record_key()), row))
|
||||
.collect();
|
||||
|
||||
let mut candidates: Vec<&R> = all_rows
|
||||
.iter()
|
||||
|
|
@ -1466,7 +1485,11 @@ pub fn select<R: Row + Clone>(
|
|||
if let Some(cited) = &selection.cited {
|
||||
let incoming = build_incoming_index(row_set, all_rows, &by_key, &cited.role, &mut edges)?;
|
||||
let exists = cited.exists;
|
||||
candidates.retain(|row| incoming.contains(row.record_key()) == exists);
|
||||
// R-V: membership is by (schemaId, recordKey) - a row of one schema is not "cited"
|
||||
// merely because some other row cites a different schema sharing its record key.
|
||||
candidates.retain(|row| {
|
||||
incoming.contains(&(row.schema_id().to_string(), row.record_key().to_string())) == exists
|
||||
});
|
||||
}
|
||||
|
||||
if let Some(cites) = &selection.cites {
|
||||
|
|
@ -1526,31 +1549,31 @@ pub fn select<R: Row + Clone>(
|
|||
None
|
||||
};
|
||||
|
||||
// R-B: hop edges follow the hop's direction. Under `cites`, the edges are the ones *from*
|
||||
// page rows (the citer is what is paged); under `cited`, they are the ones *into* page rows
|
||||
// (the citee is what is paged). Order is deterministic: page-row order, then (from, role, to)
|
||||
// R-B/R-W: hop edges follow the hop's own direction, per edge - not one direction for the
|
||||
// whole batch. A selection carrying both `cites` and `cited` puts edges from both functions
|
||||
// into one `Vec`, and each edge already knows which end (R-V: (schemaId, recordKey)) is the
|
||||
// page row that produced it (`EdgeAnchor`): `cites` anchors on the citer (`from`), `cited`
|
||||
// anchors on the citee (`to`). Order is deterministic: page-row order, then (from, role, to)
|
||||
// in code-point order - never a `HashMap`'s iteration order.
|
||||
let page_edges = if selection.has_hop() {
|
||||
let page_position: HashMap<&str, usize> = page
|
||||
let page_position: HashMap<(&str, &str), usize> = page
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, row)| (row.record_key(), index))
|
||||
.map(|(index, row)| ((row.schema_id(), row.record_key()), index))
|
||||
.collect();
|
||||
let cited_direction = selection.cited.is_some();
|
||||
fn owner_ref<'a, R: Row + Clone>(edge: &'a EdgeMatch<R>) -> (&'a str, &'a str) {
|
||||
match edge.anchor {
|
||||
EdgeAnchor::Citee => (edge.to.schema_id(), edge.to.record_key()),
|
||||
EdgeAnchor::Citer => (edge.from.schema_id(), edge.from.record_key()),
|
||||
}
|
||||
}
|
||||
let mut kept: Vec<EdgeMatch<R>> = edges
|
||||
.into_iter()
|
||||
.filter(|edge| {
|
||||
let owner = if cited_direction { edge.to.record_key() } else { edge.from.record_key() };
|
||||
page_position.contains_key(owner)
|
||||
})
|
||||
.filter(|edge| page_position.contains_key(&owner_ref(edge)))
|
||||
.collect();
|
||||
kept.sort_by(|a, b| {
|
||||
let owner = |edge: &EdgeMatch<R>| -> usize {
|
||||
let key = if cited_direction { edge.to.record_key() } else { edge.from.record_key() };
|
||||
page_position[key]
|
||||
};
|
||||
owner(a)
|
||||
.cmp(&owner(b))
|
||||
page_position[&owner_ref(a)]
|
||||
.cmp(&page_position[&owner_ref(b)])
|
||||
.then_with(|| a.from.schema_id().cmp(b.from.schema_id()))
|
||||
.then_with(|| a.from.record_key().cmp(b.from.record_key()))
|
||||
.then_with(|| a.role.cmp(&b.role))
|
||||
|
|
@ -1730,14 +1753,17 @@ fn ensure_within_declared_target<R: Row>(
|
|||
|
||||
fn matches_citation<R: Row + Clone>(
|
||||
row_set: &impl RowSet,
|
||||
by_key: &HashMap<&str, &R>,
|
||||
by_key: &HashMap<(&str, &str), &R>,
|
||||
citer: &R,
|
||||
citation: &Citation,
|
||||
edge_sink: &mut Vec<EdgeMatch<R>>,
|
||||
) -> Result<bool, SelectionRefusal> {
|
||||
let mut found = false;
|
||||
for (role, target, payload) in citer.references() {
|
||||
let Some(resolved) = by_key.get(target.record_key.as_str()) else {
|
||||
// R-V: the edge's own declared target is resolved by (schemaId, recordKey), not the key
|
||||
// alone, so a row of the wrong schema sharing this record key can never stand in for it.
|
||||
let Some(resolved) = by_key.get(&(target.schema_id.as_str(), target.record_key.as_str()))
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
// R-T: every resolvable edge is checked against its declared target, not only the ones
|
||||
|
|
@ -1765,6 +1791,7 @@ fn matches_citation<R: Row + Clone>(
|
|||
role,
|
||||
to: (*resolved).clone(),
|
||||
payload,
|
||||
anchor: EdgeAnchor::Citer,
|
||||
});
|
||||
found = true;
|
||||
}
|
||||
|
|
@ -1777,26 +1804,30 @@ fn matches_citation<R: Row + Clone>(
|
|||
fn build_incoming_index<R: Row + Clone>(
|
||||
row_set: &impl RowSet,
|
||||
all_rows: &[R],
|
||||
by_key: &HashMap<&str, &R>,
|
||||
by_key: &HashMap<(&str, &str), &R>,
|
||||
role: &str,
|
||||
edge_sink: &mut Vec<EdgeMatch<R>>,
|
||||
) -> Result<HashSet<String>, SelectionRefusal> {
|
||||
) -> Result<HashSet<(String, String)>, SelectionRefusal> {
|
||||
let mut incoming = HashSet::new();
|
||||
for citer in all_rows {
|
||||
for (edge_role, target, payload) in citer.references() {
|
||||
if edge_role != role {
|
||||
continue;
|
||||
}
|
||||
let Some(resolved) = by_key.get(target.record_key.as_str()) else {
|
||||
// R-V: resolved by (schemaId, recordKey) - see the matching comment in
|
||||
// `matches_citation`.
|
||||
let Some(resolved) = by_key.get(&(target.schema_id.as_str(), target.record_key.as_str()))
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
ensure_within_declared_target(row_set, &edge_role, citer, *resolved)?;
|
||||
incoming.insert(target.record_key.clone());
|
||||
incoming.insert((resolved.schema_id().to_string(), resolved.record_key().to_string()));
|
||||
edge_sink.push(EdgeMatch {
|
||||
from: citer.clone(),
|
||||
role: edge_role,
|
||||
to: (*resolved).clone(),
|
||||
payload,
|
||||
anchor: EdgeAnchor::Citee,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -290,6 +290,9 @@ impl RowSet for FixtureRowSet {
|
|||
"related".into(),
|
||||
"components".into(),
|
||||
"Design".into(),
|
||||
// R-W: a citer-to-citer role, so a both-hops selection can page a citer row that
|
||||
// is itself cited by another citer (rather than only ever by a leaf).
|
||||
"peer".into(),
|
||||
]
|
||||
} else if schema_id == NARROW_CITER {
|
||||
vec!["narrow_ref".into()]
|
||||
|
|
@ -306,6 +309,7 @@ impl RowSet for FixtureRowSet {
|
|||
vec![leaf_a().into(), leaf_b().into()]
|
||||
}
|
||||
"narrow_ref" => vec![leaf_a().into()],
|
||||
"peer" => vec![citer().into()],
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
|
@ -516,6 +520,91 @@ fn cited_exists_is_the_one_negation() {
|
|||
assert_eq!(ids, vec!["a-lo", "shared-1", "shared-3"]);
|
||||
}
|
||||
|
||||
// R-V (S-1): a row's identity is `(schemaId, recordKey)`, never the key alone. CultCache keys are
|
||||
// unique per schema, so two schemas can share one record key. Before the fix, `byKey` kept only
|
||||
// the last row seen at a key, so which schema's row a `cites` edge resolved to - and whether a
|
||||
// `cited` selection counted a row as cited at all - depended on row order.
|
||||
#[test]
|
||||
fn row_identity_is_schema_and_key_not_key_alone_in_either_row_order() {
|
||||
let leaf_a_row = FixtureRow::leaf(leaf_a(), "shared-key", 1, "weapon", "1");
|
||||
let leaf_b_row = FixtureRow::leaf(leaf_b(), "shared-key", 2, "shield", "2");
|
||||
let citer_row =
|
||||
FixtureRow::citer("citer-1", 3, vec![("Design".to_string(), rr(leaf_a(), "shared-key"), None)]);
|
||||
|
||||
let cites_selection = Selection {
|
||||
cites: Some(Citation { target: rr(leaf_a(), "shared-key"), role: Some("Design".into()) }),
|
||||
..Selection::default()
|
||||
};
|
||||
let cited_selection = Selection {
|
||||
cited: Some(Incoming { role: "Design".into(), exists: true }),
|
||||
..Selection::default()
|
||||
};
|
||||
|
||||
for rows in [
|
||||
vec![leaf_a_row.clone(), leaf_b_row.clone(), citer_row.clone()],
|
||||
vec![leaf_b_row.clone(), leaf_a_row.clone(), citer_row.clone()],
|
||||
] {
|
||||
let cites_eval = select(&FixtureRowSet, &rows, &cites_selection, 1, test_cursor_key()).unwrap();
|
||||
assert_eq!(
|
||||
cites_eval.rows.iter().map(Row::record_key).collect::<Vec<_>>(),
|
||||
vec!["citer-1"],
|
||||
"row order must not change whether the citation resolves"
|
||||
);
|
||||
assert_eq!(cites_eval.edges.len(), 1);
|
||||
assert_eq!(
|
||||
cites_eval.edges[0].to.schema_id(),
|
||||
leaf_a(),
|
||||
"the edge must resolve to leaf_a's row, never leaf_b's, regardless of row order"
|
||||
);
|
||||
|
||||
let cited_eval = select(&FixtureRowSet, &rows, &cited_selection, 1, test_cursor_key()).unwrap();
|
||||
let cited_pairs: Vec<(&str, &str)> =
|
||||
cited_eval.rows.iter().map(|r| (r.schema_id(), r.record_key())).collect();
|
||||
assert_eq!(
|
||||
cited_pairs,
|
||||
vec![(leaf_a(), "shared-key")],
|
||||
"cited membership is (schemaId, recordKey) - leaf_b's row at the same key must not \
|
||||
count as cited just because leaf_a's row at that key is"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// R-W (S-3), Rust half: a selection carrying both `cites` and `cited` anchors each edge by its
|
||||
// own hop, so it returns both sets of edges rather than one flag deciding the direction for the
|
||||
// whole batch. Shape from Soul's probe (soul-sel-final-notes.md): a page row "ca" that cites a
|
||||
// leaf via "Design" and is itself cited by another citer "cb" via "peer" - the answer must carry
|
||||
// both `cb --peer--> ca` (anchored on the citee) and `ca --Design--> leaf` (anchored on the
|
||||
// citer).
|
||||
#[test]
|
||||
fn a_selection_with_both_hops_returns_both_directions_edges() {
|
||||
let leaf = FixtureRow::leaf(leaf_a(), "leaf-row", 1, "weapon", "5");
|
||||
let ca = FixtureRow::citer("ca", 2, vec![("Design".to_string(), rr(leaf_a(), "leaf-row"), None)]);
|
||||
let cb = FixtureRow::citer("cb", 3, vec![("peer".to_string(), rr(citer(), "ca"), None)]);
|
||||
let rows = vec![leaf, ca, cb];
|
||||
|
||||
let selection = Selection {
|
||||
cites: Some(Citation { target: rr(leaf_a(), "leaf-row"), role: Some("Design".into()) }),
|
||||
cited: Some(Incoming { role: "peer".into(), exists: true }),
|
||||
..Selection::default()
|
||||
};
|
||||
|
||||
let evaluation = select(&FixtureRowSet, &rows, &selection, 1, test_cursor_key()).unwrap();
|
||||
assert_eq!(evaluation.rows.iter().map(Row::record_key).collect::<Vec<_>>(), vec!["ca"]);
|
||||
assert_eq!(
|
||||
evaluation.edges.len(),
|
||||
2,
|
||||
"both the cites edge (ca->leaf) and the cited edge (cb->ca) must survive - not just one"
|
||||
);
|
||||
|
||||
let mut seen: Vec<(&str, &str, &str)> = evaluation
|
||||
.edges
|
||||
.iter()
|
||||
.map(|edge| (edge.from.record_key(), edge.role.as_str(), edge.to.record_key()))
|
||||
.collect();
|
||||
seen.sort_unstable();
|
||||
assert_eq!(seen, vec![("ca", "Design", "leaf-row"), ("cb", "peer", "ca")]);
|
||||
}
|
||||
|
||||
// S16: the four comparisons at the boundary, including the row whose value equals the compared
|
||||
// number exactly.
|
||||
#[test]
|
||||
|
|
|
|||
Loading…
Reference in a new issue