ruvector_delta_graph/
node_delta.rs1use std::collections::HashMap;
6
7use ruvector_delta_core::{Delta, VectorDelta};
8
9use crate::{NodeId, PropertyOp, PropertyValue};
10
11#[derive(Debug, Clone)]
13pub struct PropertyDelta {
14 pub key: String,
16 pub operation: PropertyOp,
18}
19
20impl PropertyDelta {
21 pub fn set(key: impl Into<String>, value: PropertyValue) -> Self {
23 Self {
24 key: key.into(),
25 operation: PropertyOp::Set(value),
26 }
27 }
28
29 pub fn remove(key: impl Into<String>) -> Self {
31 Self {
32 key: key.into(),
33 operation: PropertyOp::Remove,
34 }
35 }
36
37 pub fn vector_delta(key: impl Into<String>, delta: VectorDelta) -> Self {
39 Self {
40 key: key.into(),
41 operation: PropertyOp::VectorDelta(delta),
42 }
43 }
44}
45
46#[derive(Debug, Clone)]
48pub struct NodeDelta {
49 pub property_deltas: Vec<PropertyDelta>,
51 pub label_adds: Vec<String>,
53 pub label_removes: Vec<String>,
55}
56
57impl NodeDelta {
58 pub fn new() -> Self {
60 Self {
61 property_deltas: Vec::new(),
62 label_adds: Vec::new(),
63 label_removes: Vec::new(),
64 }
65 }
66
67 pub fn is_empty(&self) -> bool {
69 self.property_deltas.is_empty()
70 && self.label_adds.is_empty()
71 && self.label_removes.is_empty()
72 }
73
74 pub fn set_property(mut self, key: impl Into<String>, value: PropertyValue) -> Self {
76 self.property_deltas.push(PropertyDelta::set(key, value));
77 self
78 }
79
80 pub fn remove_property(mut self, key: impl Into<String>) -> Self {
82 self.property_deltas.push(PropertyDelta::remove(key));
83 self
84 }
85
86 pub fn vector_delta(mut self, key: impl Into<String>, delta: VectorDelta) -> Self {
88 self.property_deltas
89 .push(PropertyDelta::vector_delta(key, delta));
90 self
91 }
92
93 pub fn add_label(mut self, label: impl Into<String>) -> Self {
95 self.label_adds.push(label.into());
96 self
97 }
98
99 pub fn remove_label(mut self, label: impl Into<String>) -> Self {
101 self.label_removes.push(label.into());
102 self
103 }
104
105 pub fn compose(mut self, other: NodeDelta) -> Self {
107 let mut prop_map: HashMap<String, PropertyDelta> = HashMap::new();
109 for pd in self.property_deltas {
110 prop_map.insert(pd.key.clone(), pd);
111 }
112 for pd in other.property_deltas {
113 if let PropertyOp::VectorDelta(vd2) = &pd.operation {
115 if let Some(existing) = prop_map.get(&pd.key) {
116 if let PropertyOp::VectorDelta(vd1) = &existing.operation {
117 let composed = vd1.clone().compose(vd2.clone());
118 prop_map.insert(
119 pd.key.clone(),
120 PropertyDelta::vector_delta(pd.key.clone(), composed),
121 );
122 continue;
123 }
124 }
125 }
126 prop_map.insert(pd.key.clone(), pd);
127 }
128 self.property_deltas = prop_map.into_values().collect();
129
130 let mut adds: std::collections::HashSet<String> = self.label_adds.into_iter().collect();
132 let mut removes: std::collections::HashSet<String> =
133 self.label_removes.into_iter().collect();
134
135 for label in other.label_adds {
136 removes.remove(&label);
137 adds.insert(label);
138 }
139 for label in other.label_removes {
140 adds.remove(&label);
141 removes.insert(label);
142 }
143
144 self.label_adds = adds.into_iter().collect();
145 self.label_removes = removes.into_iter().collect();
146
147 self
148 }
149
150 pub fn inverse(&self) -> Self {
152 Self {
155 property_deltas: Vec::new(),
156 label_adds: self.label_removes.clone(),
157 label_removes: self.label_adds.clone(),
158 }
159 }
160}
161
162impl Default for NodeDelta {
163 fn default() -> Self {
164 Self::new()
165 }
166}
167
168pub struct NodeDeltaBuilder {
170 delta: NodeDelta,
171}
172
173impl NodeDeltaBuilder {
174 pub fn new() -> Self {
176 Self {
177 delta: NodeDelta::new(),
178 }
179 }
180
181 pub fn set(mut self, key: impl Into<String>, value: PropertyValue) -> Self {
183 self.delta
184 .property_deltas
185 .push(PropertyDelta::set(key, value));
186 self
187 }
188
189 pub fn remove(mut self, key: impl Into<String>) -> Self {
191 self.delta.property_deltas.push(PropertyDelta::remove(key));
192 self
193 }
194
195 pub fn vector(mut self, key: impl Into<String>, delta: VectorDelta) -> Self {
197 self.delta
198 .property_deltas
199 .push(PropertyDelta::vector_delta(key, delta));
200 self
201 }
202
203 pub fn add_label(mut self, label: impl Into<String>) -> Self {
205 self.delta.label_adds.push(label.into());
206 self
207 }
208
209 pub fn remove_label(mut self, label: impl Into<String>) -> Self {
211 self.delta.label_removes.push(label.into());
212 self
213 }
214
215 pub fn build(self) -> NodeDelta {
217 self.delta
218 }
219}
220
221impl Default for NodeDeltaBuilder {
222 fn default() -> Self {
223 Self::new()
224 }
225}
226
227#[cfg(test)]
228mod tests {
229 use super::*;
230
231 #[test]
232 fn test_node_delta_builder() {
233 let delta = NodeDeltaBuilder::new()
234 .set("name", PropertyValue::String("Alice".into()))
235 .set("age", PropertyValue::Int(30))
236 .add_label("Person")
237 .build();
238
239 assert_eq!(delta.property_deltas.len(), 2);
240 assert_eq!(delta.label_adds.len(), 1);
241 }
242
243 #[test]
244 fn test_node_delta_compose() {
245 let d1 = NodeDelta::new()
246 .set_property("a", PropertyValue::Int(1))
247 .add_label("Label1");
248
249 let d2 = NodeDelta::new()
250 .set_property("b", PropertyValue::Int(2))
251 .remove_label("Label1")
252 .add_label("Label2");
253
254 let composed = d1.compose(d2);
255
256 assert_eq!(composed.property_deltas.len(), 2);
257 assert!(composed.label_adds.contains(&"Label2".to_string()));
258 assert!(!composed.label_adds.contains(&"Label1".to_string()));
259 }
260
261 #[test]
262 fn test_vector_delta_compose() {
263 let vd1 = VectorDelta::from_dense(vec![1.0, 0.0, 0.0]);
264 let vd2 = VectorDelta::from_dense(vec![0.0, 1.0, 0.0]);
265
266 let d1 = NodeDelta::new().vector_delta("embedding", vd1);
267 let d2 = NodeDelta::new().vector_delta("embedding", vd2);
268
269 let composed = d1.compose(d2);
270
271 assert_eq!(composed.property_deltas.len(), 1);
272
273 if let PropertyOp::VectorDelta(vd) = &composed.property_deltas[0].operation {
274 assert!(!vd.is_identity());
276 } else {
277 panic!("Expected vector delta");
278 }
279 }
280}