diff --git a/crates/engine/src/cmd/sharing.rs b/crates/engine/src/cmd/sharing.rs index 9ca060db3..4378aa5f2 100644 --- a/crates/engine/src/cmd/sharing.rs +++ b/crates/engine/src/cmd/sharing.rs @@ -1390,13 +1390,17 @@ fn link_chart(s: &Session, rel: Option<&str>) -> Result<(String, Option) } // The features a key relates to are counted once, however many features share it. let mut counted: HashSet = HashSet::new(); + // Where a key's destinations already in the chart sit among its destinations, found when + // the chart has no room for another one and kept while it stays that size: a full chart + // then looks at these only, not at every destination of every feature. + let mut in_chart: HashMap)> = HashMap::new(); for orow in 0..o.dataset.features.len() { let Some(key) = key_of(&o.dataset, orow, ok) else { continue }; let Some(drows) = by_key.get(&key) else { continue }; related.insert((om, o.id, orow)); // Counted in 64 bits: relations can pass `usize` in the browser (32 bits). relations += drows.len() as u64; - if counted.insert(key) { + if counted.insert(key.clone()) { related.extend(drows.iter().map(|&drow| (dm, d.id, drow))); } if links.len() >= MAX_LINKS { @@ -1407,29 +1411,49 @@ fn link_chart(s: &Session, rel: Option<&str>) -> Result<(String, Option) // out; one already in the chart may still be linked. // Up to 200 links drawn from it: destinations the chart has no room for don't count // toward them, so a later one it can draw still is. - let origin = (om, o.id, orow); - let mut a = index.get(&origin).copied(); + let mut a = index.get(&(om, o.id, orow)).copied(); + // A feature the chart has no room for can't be drawn, so none of its links can be. if a.is_none() && nodes.len() >= MAX_NODES { continue; } let mut drawn_here = 0; - for &drow in drows { + let mut i = 0; + while i < drows.len() { if drawn_here >= MAX_FEATURE_LINKS || links.len() >= MAX_LINKS { break; } - let destination = (dm, d.id, drow); - let new = usize::from(a.is_none()) + usize::from(destination != origin && !index.contains_key(&destination)); + let drow = drows[i]; + // A feature related to itself is one end, not two. + let origin_new = a.is_none(); + let dest_new = !index.contains_key(&(dm, d.id, drow)) && !(origin_new && (om, o.id, orow) == (dm, d.id, drow)); + let new = usize::from(origin_new) + usize::from(dest_new); if nodes.len() + new > MAX_NODES { + // No room for a destination the chart doesn't hold: on to the next one it + // does, or to the feature itself. + let (at, rows) = in_chart.entry(key.clone()).or_insert((usize::MAX, Vec::new())); + if *at != nodes.len() { + *rows = (0..drows.len()).filter(|&j| index.contains_key(&(dm, d.id, drows[j]))).collect(); + *at = nodes.len(); + } + let next_held = rows.get(rows.partition_point(|&j| j <= i)).copied(); + let next_self = + if origin_new && (om, o.id) == (dm, d.id) { drows.binary_search(&orow).ok().filter(|&j| j > i) } else { None }; + match next_held.into_iter().chain(next_self).min() { + Some(j) => i = j, + None => break, + } continue; } let (Some(from), Some(to)) = (a.or_else(|| node(&mut index, &mut nodes, om, o, orow, gi)), node(&mut index, &mut nodes, dm, d, drow, gi)) else { + i += 1; continue; }; a = Some(from); links.push(json!({"source": from, "target": to, "relationship": r.name})); drawn_here += 1; + i += 1; } } } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index b09e82889..5113eecbc 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -12819,6 +12819,27 @@ fn a_link_chart_says_what_its_limits_leave_out() { t.run("share.linkChart", &json!({})).unwrap(); // First draws 1 link (2 features), Fill 1,499 (2,997), and Last its one link to x (1 more). assert_eq!((drawn("Relationships").0, drawn("Relationships").1), (1501, 3000)); + // A feature related to itself is one end, not two: with the chart at 2,999, its link still + // fits. On a session of its own, with First and Fill as above. + let mut u = world(); + u.run("share.dataStore", &json!({"name": "out", "path": dir.to_str().unwrap()})).unwrap(); + add(&mut u, "self_first_a.csv", &["x".to_string()]); + add(&mut u, "self_first_b.csv", &["x".to_string()]); + add(&mut u, "past_fill_one.csv", &fill_one); + add(&mut u, "past_fill_many.csv", &fill_many); + add(&mut u, "past_self.csv", &["z".to_string()]); + for (name, origin, destination) in + [("First", "self_first_a", "self_first_b"), ("Fill", "past_fill_one", "past_fill_many"), ("Self", "past_self", "past_self")] + { + u.run( + "data.relate", + &json!({"name": name, "origin": origin, "originKey": "key", "destination": destination, "destinationKey": "key"}), + ) + .unwrap(); + } + u.run("share.linkChart", &json!({})).unwrap(); + // First draws 1 link (2 features), Fill 1,499 (2,997), and Self its feature's link to itself (1 more). + assert_eq!((drawn("Relationships").0, drawn("Relationships").1), (1501, 3000)); // A feature isn't drawn alone: one feature related to 2 (3 features), then pairs. After 1,498 // pairs the chart holds 2,999, and a pair needs 2 more: the rest are left out whole, rather // than an origin drawn without its link.