mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-01 07:24:56 -10:00
Track which entities an op names, and project a single node or network to runtime form
174 lines
5.8 KiB
Rust
174 lines
5.8 KiB
Rust
//! Which entities a run of ops named, so a runtime mirror of the registry can be brought back into line
|
|
//! without a full rebuild.
|
|
//!
|
|
//! Over-approximate on purpose: an op is recorded whether or not it changed anything, and the mirror
|
|
//! settles the outcome by comparing each entity's stored and mirrored forms. That is cheaper than having
|
|
//! every apply arm report its outcome, and stays correct when an op revives an entity it only references.
|
|
|
|
use std::collections::BTreeSet;
|
|
|
|
use crate::{Implementation, NetworkId, NodeId, NodeInput, RegistryDelta};
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
|
pub struct Touched {
|
|
pub nodes: BTreeSet<NodeId>,
|
|
pub networks: BTreeSet<NetworkId>,
|
|
/// Some resource entry changed. Resources are mirrored as one registry rather than per entity.
|
|
pub resources: bool,
|
|
}
|
|
|
|
impl Touched {
|
|
/// Records every entity `op` names: its target and whatever it references.
|
|
pub fn record(&mut self, op: &RegistryDelta) {
|
|
match op {
|
|
RegistryDelta::AddNode { id, node } => {
|
|
self.nodes.insert(*id);
|
|
self.networks.insert(node.network);
|
|
if let Implementation::Network(network) = node.implementation {
|
|
self.networks.insert(network);
|
|
}
|
|
self.reference_inputs(node.inputs().iter().map(|slot| &slot.input));
|
|
}
|
|
RegistryDelta::SetNodeImplementation { id, implementation } => {
|
|
self.nodes.insert(*id);
|
|
if let Implementation::Network(network) = implementation {
|
|
self.networks.insert(*network);
|
|
}
|
|
}
|
|
RegistryDelta::RemoveNode { id, .. } | RegistryDelta::ChangeNodeAttribute { id, .. } | RegistryDelta::ChangeNodeInputAttribute { id, .. } => {
|
|
self.nodes.insert(*id);
|
|
}
|
|
RegistryDelta::ChangeNodeInput { id, new_input, .. } => {
|
|
self.nodes.insert(*id);
|
|
self.reference_inputs([new_input]);
|
|
}
|
|
RegistryDelta::SetNodeInputs { id, inputs } => {
|
|
self.nodes.insert(*id);
|
|
self.reference_inputs(inputs.iter().map(|slot| &slot.input));
|
|
}
|
|
RegistryDelta::SetNetworkExport { id, export, .. } => {
|
|
self.networks.insert(*id);
|
|
self.reference_inputs(export);
|
|
}
|
|
RegistryDelta::ChangeNetworkAttribute { id, .. } | RegistryDelta::AddNetwork { id, .. } | RegistryDelta::RemoveNetwork { id, .. } => {
|
|
self.networks.insert(*id);
|
|
}
|
|
RegistryDelta::AddResource { .. } | RegistryDelta::SetResourceHash { .. } | RegistryDelta::RemoveResource { .. } | RegistryDelta::AddSource { .. } | RegistryDelta::RemoveSource { .. } => {
|
|
self.resources = true;
|
|
}
|
|
RegistryDelta::RegisterPeer { .. } | RegistryDelta::ChangeDocumentAttribute { .. } | RegistryDelta::Merge { .. } | RegistryDelta::Meta | RegistryDelta::Other(_) => {}
|
|
}
|
|
}
|
|
|
|
pub fn extend(&mut self, other: Touched) {
|
|
self.nodes.extend(other.nodes);
|
|
self.networks.extend(other.networks);
|
|
self.resources |= other.resources;
|
|
}
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
self.nodes.is_empty() && self.networks.is_empty() && !self.resources
|
|
}
|
|
|
|
fn reference_inputs<'a>(&mut self, inputs: impl IntoIterator<Item = &'a NodeInput>) {
|
|
for input in inputs {
|
|
if let NodeInput::Node { id, .. } = input {
|
|
self.nodes.insert(*id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::{AttributeDelta, Network, Node};
|
|
|
|
#[test]
|
|
fn an_input_write_touches_the_node_it_references_as_well_as_its_target() {
|
|
let mut touched = Touched::default();
|
|
touched.record(&RegistryDelta::ChangeNodeInput {
|
|
id: NodeId(1),
|
|
index: 0,
|
|
new_input: NodeInput::Node { id: NodeId(2), index: 0 },
|
|
});
|
|
|
|
assert_eq!(touched.nodes, BTreeSet::from([NodeId(1), NodeId(2)]));
|
|
assert!(touched.networks.is_empty());
|
|
assert!(!touched.resources);
|
|
}
|
|
|
|
#[test]
|
|
fn an_export_touches_the_network_and_the_node_it_targets() {
|
|
let mut touched = Touched::default();
|
|
touched.record(&RegistryDelta::SetNetworkExport {
|
|
id: NetworkId(3),
|
|
index: 0,
|
|
export: Some(NodeInput::Node { id: NodeId(4), index: 0 }),
|
|
});
|
|
|
|
assert_eq!(touched.networks, BTreeSet::from([NetworkId(3)]));
|
|
assert_eq!(touched.nodes, BTreeSet::from([NodeId(4)]));
|
|
}
|
|
|
|
#[test]
|
|
fn ops_outside_the_graph_touch_nothing_or_only_the_resource_flag() {
|
|
let mut touched = Touched::default();
|
|
touched.record(&RegistryDelta::ChangeDocumentAttribute {
|
|
delta: AttributeDelta { key: "key".into(), value: None },
|
|
});
|
|
touched.record(&RegistryDelta::Merge { extra_parents: Vec::new() });
|
|
assert!(touched.is_empty());
|
|
|
|
touched.record(&RegistryDelta::SetResourceHash {
|
|
id: crate::ResourceId::new(),
|
|
hash: None,
|
|
});
|
|
assert!(touched.resources);
|
|
assert!(touched.nodes.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn a_node_add_touches_the_node_its_network_and_what_its_inputs_reference() {
|
|
let mut node = Node::dummy();
|
|
let network = node.network;
|
|
node.inputs.push(crate::InputSlot {
|
|
input: NodeInput::Node { id: NodeId(9), index: 0 },
|
|
timestamp: crate::TimeStamp::ORIGIN,
|
|
attributes: Default::default(),
|
|
});
|
|
let mut touched = Touched::default();
|
|
touched.record(&RegistryDelta::AddNode { id: NodeId(8), node });
|
|
touched.record(&RegistryDelta::AddNetwork {
|
|
id: NetworkId(5),
|
|
network: Network::default(),
|
|
});
|
|
|
|
assert_eq!(touched.nodes, BTreeSet::from([NodeId(8), NodeId(9)]));
|
|
assert_eq!(touched.networks, BTreeSet::from([network, NetworkId(5)]));
|
|
}
|
|
|
|
#[test]
|
|
fn implementing_a_node_by_a_network_touches_both() {
|
|
let mut touched = Touched::default();
|
|
touched.record(&RegistryDelta::SetNodeImplementation {
|
|
id: NodeId(1),
|
|
implementation: Implementation::Network(NetworkId(2)),
|
|
});
|
|
|
|
assert_eq!(touched.nodes, BTreeSet::from([NodeId(1)]));
|
|
assert_eq!(touched.networks, BTreeSet::from([NetworkId(2)]));
|
|
}
|
|
|
|
#[test]
|
|
fn adding_a_node_implemented_by_a_network_touches_that_network_too() {
|
|
let mut node = Node::dummy();
|
|
let network = node.network;
|
|
node.implementation = Implementation::Network(NetworkId(2));
|
|
let mut touched = Touched::default();
|
|
touched.record(&RegistryDelta::AddNode { id: NodeId(1), node });
|
|
|
|
assert_eq!(touched.networks, BTreeSet::from([network, NetworkId(2)]));
|
|
}
|
|
}
|