| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | | use crate::hash::ChangeHash; |
| 5 | | use crate::ids::NodeId; |
| 6 | | use crate::objects::{Mode, NodeKind, OpRef, TreeOp}; |
| 7 | | use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; |
| 8 | |
|
| 9 | |
|
| 10 | | pub type OpKey = (ChangeHash, u32); |
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | | type Map<K, V> = crate::fasthash::FastMap<K, V>; |
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 19 | | enum KeySet { |
| 20 | | #[default] |
| 21 | | Empty, |
| 22 | | One(OpKey), |
| 23 | | Many(Vec<OpKey>), |
| 24 | | } |
| 25 | |
|
| 26 | | impl KeySet { |
| 27 | 278314× | fn insert(&mut self, k: OpKey) { |
| 28 | 192× | match self { |
| 29 | 278076× | KeySet::Empty => *self = KeySet::One(k), |
| 30 | 192× | KeySet::One(a) if *a == k => {} |
| 31 | 192× | KeySet::One(a) => { |
| 32 | 192× | let a = *a; |
| 33 | 192× | *self = KeySet::Many(if a < k { vec![a, k] } else { vec![k, a] }); |
| 34 | | } |
| 35 | 46× | KeySet::Many(v) => { |
| 36 | 46× | if let Err(i) = v.binary_search(&k) { |
| 37 | 46× | v.insert(i, k); |
| 38 | 46× | } |
| 39 | | } |
| 40 | | } |
| 41 | 278314× | } |
| 42 | 497× | fn remove(&mut self, k: &OpKey) { |
| 43 | 482× | match self { |
| 44 | 482× | KeySet::One(a) if a == k => *self = KeySet::Empty, |
| 45 | 15× | KeySet::Many(v) => { |
| 46 | 15× | if let Ok(i) = v.binary_search(k) { |
| 47 | 14× | v.remove(i); |
| 48 | 14× | } |
| 49 | 15× | if v.len() == 1 { |
| 50 | 14× | *self = KeySet::One(v[0]); |
| 51 | 14× | } else if v.is_empty() { |
| 52 | 0× | *self = KeySet::Empty; |
| 53 | 1× | } |
| 54 | | } |
| 55 | 91× | _ => {} |
| 56 | | } |
| 57 | 497× | } |
| 58 | 20304739× | fn as_slice(&self) -> &[OpKey] { |
| 59 | 20304739× | match self { |
| 60 | 0× | KeySet::Empty => &[], |
| 61 | 20304695× | KeySet::One(a) => std::slice::from_ref(a), |
| 62 | 44× | KeySet::Many(v) => v, |
| 63 | | } |
| 64 | 20304739× | } |
| 65 | 20293079× | fn iter(&self) -> std::slice::Iter<'_, OpKey> { |
| 66 | 20293079× | self.as_slice().iter() |
| 67 | 20293079× | } |
| 68 | 11660× | fn len(&self) -> usize { |
| 69 | 11660× | self.as_slice().len() |
| 70 | 11660× | } |
| 71 | 497× | fn is_empty(&self) -> bool { |
| 72 | 497× | matches!(self, KeySet::Empty) |
| 73 | 497× | } |
| 74 | | } |
| 75 | |
|
| 76 | | impl<'a> IntoIterator for &'a KeySet { |
| 77 | | type Item = &'a OpKey; |
| 78 | | type IntoIter = std::slice::Iter<'a, OpKey>; |
| 79 | 6763642× | fn into_iter(self) -> Self::IntoIter { |
| 80 | 6763642× | self.iter() |
| 81 | 6763642× | } |
| 82 | | } |
| 83 | |
|
| 84 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 85 | | pub struct Placement { |
| 86 | | pub node: NodeId, |
| 87 | | pub parent: NodeId, |
| 88 | | pub name: String, |
| 89 | | } |
| 90 | |
|
| 91 | | #[derive(Clone, Debug, Default)] |
| 92 | | struct TreeMaps { |
| 93 | | kinds: Map<NodeId, NodeKind>, |
| 94 | | introduced: Map<NodeId, ChangeHash>, |
| 95 | | placements: Map<OpKey, Placement>, |
| 96 | | modes: Map<OpKey, (NodeId, Mode)>, |
| 97 | | |
| 98 | | |
| 99 | | |
| 100 | | deleted_placements: Map<OpKey, BTreeSet<ChangeHash>>, |
| 101 | | deleted_modes: Map<OpKey, BTreeSet<ChangeHash>>, |
| 102 | | placements_of: Map<NodeId, KeySet>, |
| 103 | | modes_of: Map<NodeId, KeySet>, |
| 104 | | |
| 105 | | |
| 106 | | live_placements_of: Map<NodeId, KeySet>, |
| 107 | | live_modes_of: Map<NodeId, KeySet>, |
| 108 | | } |
| 109 | |
|
| 110 | | #[derive(Clone, Debug, Default)] |
| 111 | | pub struct TreeGraph { |
| 112 | | |
| 113 | | maps: std::sync::OnceLock<TreeMaps>, |
| 114 | | |
| 115 | | |
| 116 | | raw: Option<std::sync::Arc<Vec<u8>>>, |
| 117 | | |
| 118 | | |
| 119 | | raw_introduced: Option<std::sync::Arc<Vec<u8>>>, |
| 120 | | introduced_only: std::sync::OnceLock<Map<NodeId, ChangeHash>>, |
| 121 | | |
| 122 | | |
| 123 | | |
| 124 | | view_cache: std::sync::OnceLock<CachedView>, |
| 125 | | } |
| 126 | |
|
| 127 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 128 | | pub enum TreeConflict { |
| 129 | | |
| 130 | | Name { |
| 131 | | parent: NodeId, |
| 132 | | name: String, |
| 133 | | nodes: Vec<NodeId>, |
| 134 | | }, |
| 135 | | |
| 136 | | MultiPlacement { |
| 137 | | node: NodeId, |
| 138 | | placements: Vec<OpKey>, |
| 139 | | }, |
| 140 | | |
| 141 | | Orphan { node: NodeId, parent: NodeId }, |
| 142 | | |
| 143 | | Cycle { node: NodeId }, |
| 144 | | |
| 145 | | Mode { node: NodeId, modes: Vec<Mode> }, |
| 146 | | } |
| 147 | |
|
| 148 | |
|
| 149 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 150 | | pub struct TreeEntry { |
| 151 | | pub path: String, |
| 152 | | pub node: NodeId, |
| 153 | | pub kind: NodeKind, |
| 154 | | pub mode: Mode, |
| 155 | | |
| 156 | | pub placement: OpKey, |
| 157 | | |
| 158 | | pub mode_atom: Option<OpKey>, |
| 159 | | } |
| 160 | |
|
| 161 | | #[derive(Clone, Debug, Default)] |
| 162 | | struct CachedView { |
| 163 | | view: TreeView, |
| 164 | | dirs: HashMap<NodeId, String>, |
| 165 | | } |
| 166 | |
|
| 167 | | impl CachedView { |
| 168 | 3845× | fn new(view: TreeView) -> CachedView { |
| 169 | 3845× | let mut dirs: HashMap<NodeId, String> = view |
| 170 | 3845× | .entries |
| 171 | 3845× | .values() |
| 172 | 38744× | .filter(|e| e.kind == NodeKind::Dir) |
| 173 | 3845× | .map(|e| (e.node, e.path.clone())) |
| 174 | 3845× | .collect(); |
| 175 | 3845× | dirs.insert(NodeId::ROOT, String::new()); |
| 176 | 3845× | CachedView { view, dirs } |
| 177 | 3845× | } |
| 178 | | } |
| 179 | |
|
| 180 | | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 181 | | pub struct TreeView { |
| 182 | | |
| 183 | | |
| 184 | | |
| 185 | | pub entries: std::sync::Arc<BTreeMap<String, TreeEntry>>, |
| 186 | | pub conflicts: Vec<TreeConflict>, |
| 187 | | } |
| 188 | |
|
| 189 | | impl TreeView { |
| 190 | 2379× | pub fn is_conflicted(&self) -> bool { |
| 191 | 2379× | !self.conflicts.is_empty() |
| 192 | 2379× | } |
| 193 | 1301× | pub fn by_node(&self) -> HashMap<NodeId, &TreeEntry> { |
| 194 | 9495× | self.entries.values().map(|e| (e.node, e)).collect() |
| 195 | 1301× | } |
| 196 | | } |
| 197 | |
|
| 198 | | impl TreeGraph { |
| 199 | 2765× | pub fn new() -> Self { |
| 200 | 2765× | Self::default() |
| 201 | 2765× | } |
| 202 | |
|
| 203 | | |
| 204 | 48316974× | fn m(&self) -> &TreeMaps { |
| 205 | 48316974× | self.maps.get_or_init(|| match &self.raw { |
| 206 | 2757× | Some(raw) => { |
| 207 | 2757× | TreeMaps::decode(raw).expect("a state index is checked against its hash when read") |
| 208 | | } |
| 209 | 5094× | None => TreeMaps::default(), |
| 210 | 7851× | }) |
| 211 | 48316974× | } |
| 212 | |
|
| 213 | 706961× | fn m_mut(&mut self) -> &mut TreeMaps { |
| 214 | 706961× | self.m(); |
| 215 | 706961× | self.maps.get_mut().expect("decoded just now") |
| 216 | 706961× | } |
| 217 | |
|
| 218 | | |
| 219 | 37802× | pub fn apply(&mut self, change: ChangeHash, index: u32, op: &TreeOp) { |
| 220 | 37802× | let cached = self.view_cache.take(); |
| 221 | | |
| 222 | 37802× | let was_live = match op { |
| 223 | 675× | TreeOp::RemoveNode { placement, .. } => self.placement_live(&op_key(*placement)), |
| 224 | 182× | TreeOp::Move { old, .. } => self.placement_live(&op_key(*old)), |
| 225 | 28× | TreeOp::SetMode { old, .. } => self.mode_live(&op_key(*old)), |
| 226 | 36917× | TreeOp::AddNode { .. } => false, |
| 227 | | }; |
| 228 | 37802× | self.apply_to_graph(change, index, op); |
| 229 | 37802× | if let Some(mut c) = cached { |
| 230 | 11845× | if self.update_view(&mut c, (change, index), op, was_live) { |
| 231 | 2417× | #[cfg(test)] |
| 232 | 9233× | assert_eq!( |
| 233 | 2417× | c.view, |
| 234 | 9233× | self.compute_view(), |
| 235 | 2417× | "view kept up to date by {op:?}" |
| 236 | 2417× | ); |
| 237 | 11650× | self.view_cache = std::sync::OnceLock::from(c); |
| 238 | 2417× | } |
| 239 | 25957× | } |
| 240 | 37802× | } |
| 241 | |
|
| 242 | 37802× | fn apply_to_graph(&mut self, change: ChangeHash, index: u32, op: &TreeOp) { |
| 243 | 37802× | let key = (change, index); |
| 244 | 37802× | match op { |
| 245 | | TreeOp::AddNode { |
| 246 | 36917× | node, |
| 247 | 36917× | kind, |
| 248 | 36917× | mode, |
| 249 | 36917× | parent, |
| 250 | 36917× | name, |
| 251 | | } => { |
| 252 | 36917× | self.m_mut().kinds.insert(*node, *kind); |
| 253 | 36917× | self.m_mut().introduced.insert(*node, change); |
| 254 | 36917× | self.m_mut().placements.insert( |
| 255 | 36917× | key, |
| 256 | 36917× | Placement { |
| 257 | 36917× | node: *node, |
| 258 | 36917× | parent: *parent, |
| 259 | 36917× | name: name.clone(), |
| 260 | 36917× | }, |
| 261 | | ); |
| 262 | 36917× | self.m_mut() |
| 263 | 36917× | .placements_of |
| 264 | 36917× | .entry(*node) |
| 265 | 36917× | .or_default() |
| 266 | 36917× | .insert(key); |
| 267 | 36917× | self.index_placement(key); |
| 268 | 36917× | if *kind != NodeKind::Dir { |
| 269 | 35164× | self.m_mut().modes.insert(key, (*node, *mode)); |
| 270 | 35164× | self.m_mut().modes_of.entry(*node).or_default().insert(key); |
| 271 | 35164× | self.index_mode(key); |
| 272 | 35164× | } |
| 273 | | } |
| 274 | 675× | TreeOp::RemoveNode { placement, .. } => { |
| 275 | 675× | self.m_mut() |
| 276 | 675× | .deleted_placements |
| 277 | 675× | .entry(op_key(*placement)) |
| 278 | 675× | .or_default() |
| 279 | 675× | .insert(change); |
| 280 | 675× | self.index_placement(op_key(*placement)); |
| 281 | 675× | } |
| 282 | | TreeOp::Move { |
| 283 | 182× | node, |
| 284 | 182× | old, |
| 285 | 182× | parent, |
| 286 | 182× | name, |
| 287 | 182× | } => { |
| 288 | 182× | self.m_mut() |
| 289 | 182× | .deleted_placements |
| 290 | 182× | .entry(op_key(*old)) |
| 291 | 182× | .or_default() |
| 292 | 182× | .insert(change); |
| 293 | 182× | self.index_placement(op_key(*old)); |
| 294 | 182× | self.m_mut().placements.insert( |
| 295 | 182× | key, |
| 296 | 182× | Placement { |
| 297 | 182× | node: *node, |
| 298 | 182× | parent: *parent, |
| 299 | 182× | name: name.clone(), |
| 300 | 182× | }, |
| 301 | 182× | ); |
| 302 | 182× | self.m_mut() |
| 303 | 182× | .placements_of |
| 304 | 182× | .entry(*node) |
| 305 | 182× | .or_default() |
| 306 | 182× | .insert(key); |
| 307 | 182× | self.index_placement(key); |
| 308 | 182× | } |
| 309 | 28× | TreeOp::SetMode { node, old, mode } => { |
| 310 | 28× | self.m_mut() |
| 311 | 28× | .deleted_modes |
| 312 | 28× | .entry(op_key(*old)) |
| 313 | 28× | .or_default() |
| 314 | 28× | .insert(change); |
| 315 | 28× | self.index_mode(op_key(*old)); |
| 316 | 28× | self.m_mut().modes.insert(key, (*node, *mode)); |
| 317 | 28× | self.m_mut().modes_of.entry(*node).or_default().insert(key); |
| 318 | 28× | self.index_mode(key); |
| 319 | 28× | } |
| 320 | | } |
| 321 | 37802× | } |
| 322 | |
|
| 323 | | |
| 324 | | |
| 325 | | |
| 326 | | |
| 327 | 11845× | fn update_view(&self, c: &mut CachedView, key: OpKey, op: &TreeOp, was_live: bool) -> bool { |
| 328 | 11845× | if !c.view.conflicts.is_empty() { |
| 329 | 9× | return false; |
| 330 | 11836× | } |
| 331 | 11836× | match op { |
| 332 | 11648× | TreeOp::AddNode { node, .. } => self.view_add(c, *node, key), |
| 333 | 113× | TreeOp::RemoveNode { node, placement } => { |
| 334 | 113× | !was_live || self.view_remove(c, *node, op_key(*placement)) |
| 335 | | } |
| 336 | 67× | TreeOp::Move { node, old, .. } => { |
| 337 | 67× | (!was_live || self.view_remove(c, *node, op_key(*old))) |
| 338 | 12× | && self.view_add(c, *node, key) |
| 339 | | } |
| 340 | 8× | TreeOp::SetMode { node, .. } => { |
| 341 | 8× | let live = self.live_placements(node); |
| 342 | 8× | match live.as_slice() { |
| 343 | 8× | [] => true, |
| 344 | 5× | [(k, p)] => { |
| 345 | 5× | let Some(path) = c.dirs.get(&p.parent).map(|d| join(d, &p.name)) else { |
| 346 | 0× | return false; |
| 347 | | }; |
| 348 | 5× | let Some(mode) = self.entry_mode(node) else { |
| 349 | 0× | return false; |
| 350 | | }; |
| 351 | 5× | let entries = std::sync::Arc::make_mut(&mut c.view.entries); |
| 352 | 5× | match entries.get_mut(&path) { |
| 353 | 5× | Some(e) if e.node == *node && e.placement == *k => { |
| 354 | 5× | (e.mode_atom, e.mode) = mode; |
| 355 | 5× | true |
| 356 | | } |
| 357 | 0× | _ => false, |
| 358 | | } |
| 359 | | } |
| 360 | 0× | _ => false, |
| 361 | | } |
| 362 | | } |
| 363 | | } |
| 364 | 11845× | } |
| 365 | |
|
| 366 | | |
| 367 | 11660× | fn view_add(&self, c: &mut CachedView, node: NodeId, key: OpKey) -> bool { |
| 368 | 11660× | if !self.placement_live(&key) { |
| 369 | 0× | return true; |
| 370 | 11660× | } |
| 371 | 11660× | if self |
| 372 | 11660× | .m() |
| 373 | 11660× | .live_placements_of |
| 374 | 11660× | .get(&node) |
| 375 | 11660× | .is_none_or(|s| s.len() != 1) |
| 376 | | { |
| 377 | 8× | return false; |
| 378 | 11652× | } |
| 379 | 11652× | let Some(p) = self.m().placements.get(&key) else { |
| 380 | 0× | return false; |
| 381 | | }; |
| 382 | 11652× | let Some(path) = c.dirs.get(&p.parent).map(|d| join(d, &p.name)) else { |
| 383 | 1× | return false; |
| 384 | | }; |
| 385 | 11651× | let Some((mode_atom, mode)) = self.entry_mode(&node) else { |
| 386 | 1× | return false; |
| 387 | | }; |
| 388 | 11650× | let kind = self.kind(&node).unwrap_or(NodeKind::File); |
| 389 | 11650× | let entries = std::sync::Arc::make_mut(&mut c.view.entries); |
| 390 | 11650× | if entries.contains_key(&path) || node == NodeId::ROOT { |
| 391 | 38× | return false; |
| 392 | 11612× | } |
| 393 | | |
| 394 | | |
| 395 | 11612× | if kind == NodeKind::Dir { |
| 396 | 754× | c.dirs.insert(node, path.clone()); |
| 397 | 10858× | } |
| 398 | 11612× | entries.insert( |
| 399 | 11612× | path.clone(), |
| 400 | 11612× | TreeEntry { |
| 401 | 11612× | path, |
| 402 | 11612× | node, |
| 403 | 11612× | kind, |
| 404 | 11612× | mode, |
| 405 | 11612× | placement: key, |
| 406 | 11612× | mode_atom, |
| 407 | 11612× | }, |
| 408 | | ); |
| 409 | 11612× | true |
| 410 | 11660× | } |
| 411 | |
|
| 412 | | |
| 413 | 168× | fn view_remove(&self, c: &mut CachedView, node: NodeId, key: OpKey) -> bool { |
| 414 | 168× | if self.m().live_placements_of.contains_key(&node) { |
| 415 | 55× | return false; |
| 416 | 113× | } |
| 417 | 113× | let Some(p) = self.m().placements.get(&key) else { |
| 418 | 66× | return false; |
| 419 | | }; |
| 420 | 47× | let Some(path) = c.dirs.get(&p.parent).map(|d| join(d, &p.name)) else { |
| 421 | 0× | return false; |
| 422 | | }; |
| 423 | 47× | let entries = std::sync::Arc::make_mut(&mut c.view.entries); |
| 424 | 47× | match entries.get(&path) { |
| 425 | 47× | Some(e) if e.node == node && e.placement == key => {} |
| 426 | 0× | _ => return false, |
| 427 | | } |
| 428 | 47× | if entries.get(&path).is_some_and(|e| e.kind == NodeKind::Dir) { |
| 429 | 18× | let prefix = format!("{path}/"); |
| 430 | 18× | if entries |
| 431 | 18× | .range(prefix.clone()..) |
| 432 | 18× | .next() |
| 433 | 18× | .is_some_and(|(k, _)| k.starts_with(&prefix)) |
| 434 | | { |
| 435 | 17× | return false; |
| 436 | 1× | } |
| 437 | 1× | c.dirs.remove(&node); |
| 438 | 29× | } |
| 439 | 30× | entries.remove(&path); |
| 440 | 30× | true |
| 441 | 168× | } |
| 442 | |
|
| 443 | | |
| 444 | | |
| 445 | 11656× | fn entry_mode(&self, node: &NodeId) -> Option<(Option<OpKey>, Mode)> { |
| 446 | 11656× | let lm = self.live_modes(node); |
| 447 | 11656× | let distinct: BTreeSet<Mode> = lm.iter().map(|(_, m)| *m).collect(); |
| 448 | 11656× | if distinct.len() > 1 { |
| 449 | 1× | return None; |
| 450 | 11655× | } |
| 451 | 11655× | let mode_atom = lm.iter().map(|(k, _)| *k).max(); |
| 452 | 11655× | let mode = mode_atom |
| 453 | 11655× | .and_then(|k| self.m().modes.get(&k).map(|(_, m)| *m)) |
| 454 | 11655× | .unwrap_or_default(); |
| 455 | 11655× | Some((mode_atom, mode)) |
| 456 | 11656× | } |
| 457 | |
|
| 458 | | |
| 459 | 71525× | fn index_placement(&mut self, k: OpKey) { |
| 460 | 71525× | let Some(node) = self.m_mut().placements.get(&k).map(|p| p.node) else { |
| 461 | 368× | return; |
| 462 | | }; |
| 463 | 71157× | if self.placement_live(&k) { |
| 464 | 70288× | self.m_mut() |
| 465 | 70288× | .live_placements_of |
| 466 | 70288× | .entry(node) |
| 467 | 70288× | .or_default() |
| 468 | 70288× | .insert(k); |
| 469 | 70288× | } else if let Some(set) = self.m_mut().live_placements_of.get_mut(&node) { |
| 470 | 468× | set.remove(&k); |
| 471 | 468× | if set.is_empty() { |
| 472 | 381× | self.m_mut().live_placements_of.remove(&node); |
| 473 | 381× | } |
| 474 | 401× | } |
| 475 | 71525× | } |
| 476 | |
|
| 477 | | |
| 478 | 68713× | fn index_mode(&mut self, k: OpKey) { |
| 479 | 68713× | let Some(node) = self.m_mut().modes.get(&k).map(|(n, _)| *n) else { |
| 480 | 11× | return; |
| 481 | | }; |
| 482 | 68702× | if self.mode_live(&k) { |
| 483 | 68673× | self.m_mut() |
| 484 | 68673× | .live_modes_of |
| 485 | 68673× | .entry(node) |
| 486 | 68673× | .or_default() |
| 487 | 68673× | .insert(k); |
| 488 | 68673× | } else if let Some(set) = self.m_mut().live_modes_of.get_mut(&node) { |
| 489 | 29× | set.remove(&k); |
| 490 | 29× | if set.is_empty() { |
| 491 | 10× | self.m_mut().live_modes_of.remove(&node); |
| 492 | 19× | } |
| 493 | 0× | } |
| 494 | 68713× | } |
| 495 | |
|
| 496 | | |
| 497 | | |
| 498 | | |
| 499 | 14× | pub fn dead_dirs(&self) -> HashMap<(NodeId, String), (NodeId, OpKey)> { |
| 500 | 14× | let mut out: HashMap<(NodeId, String), (NodeId, OpKey)> = HashMap::new(); |
| 501 | 15× | for (k, p) in &self.m().placements { |
| 502 | 15× | if self.kind(&p.node) != Some(NodeKind::Dir) |
| 503 | 5× | || self.placement_live(k) |
| 504 | 2× | || !self.live_placements(&p.node).is_empty() |
| 505 | | { |
| 506 | 13× | continue; |
| 507 | 2× | } |
| 508 | 2× | let slot = out |
| 509 | 2× | .entry((p.parent, p.name.clone())) |
| 510 | 2× | .or_insert((p.node, *k)); |
| 511 | | |
| 512 | 2× | if *k > slot.1 { |
| 513 | 0× | *slot = (p.node, *k); |
| 514 | 2× | } |
| 515 | | } |
| 516 | 14× | out |
| 517 | 14× | } |
| 518 | |
|
| 519 | | |
| 520 | | |
| 521 | 0× | pub fn placement_history(&self, node: &NodeId) -> Vec<(OpKey, &Placement, Vec<ChangeHash>)> { |
| 522 | 0× | self.m() |
| 523 | 0× | .placements_of |
| 524 | 0× | .get(node) |
| 525 | 0× | .into_iter() |
| 526 | 0× | .flatten() |
| 527 | 0× | .filter_map(|k| { |
| 528 | 0× | let p = self.m().placements.get(k)?; |
| 529 | 0× | let by = self |
| 530 | 0× | .m() |
| 531 | 0× | .deleted_placements |
| 532 | 0× | .get(k) |
| 533 | 0× | .map(|d| d.iter().copied().collect()) |
| 534 | 0× | .unwrap_or_default(); |
| 535 | 0× | Some((*k, p, by)) |
| 536 | 0× | }) |
| 537 | 0× | .collect() |
| 538 | 0× | } |
| 539 | |
|
| 540 | 83679× | fn placement_live(&self, k: &OpKey) -> bool { |
| 541 | 83679× | self.m() |
| 542 | 83679× | .deleted_placements |
| 543 | 83679× | .get(k) |
| 544 | 83679× | .is_none_or(|d| d.is_empty()) |
| 545 | 83679× | } |
| 546 | 68730× | fn mode_live(&self, k: &OpKey) -> bool { |
| 547 | 68730× | self.m().deleted_modes.get(k).is_none_or(|d| d.is_empty()) |
| 548 | 68730× | } |
| 549 | |
|
| 550 | 6775316× | pub fn kind(&self, node: &NodeId) -> Option<NodeKind> { |
| 551 | 6775316× | if node.is_root() { |
| 552 | 0× | Some(NodeKind::Dir) |
| 553 | | } else { |
| 554 | 6775316× | self.m().kinds.get(node).copied() |
| 555 | | } |
| 556 | 6775316× | } |
| 557 | |
|
| 558 | | |
| 559 | 14599× | pub fn introducer(&self, node: &NodeId) -> Option<ChangeHash> { |
| 560 | 14599× | if let (None, Some(raw)) = (self.maps.get(), &self.raw_introduced) { |
| 561 | 546× | let only = self.introduced_only.get_or_init(|| { |
| 562 | 58× | read_introduced(raw).expect("a state index is checked against its hash when read") |
| 563 | 58× | }); |
| 564 | | #[cfg(test)] |
| 565 | 0× | assert_eq!( |
| 566 | 0× | only.get(node), |
| 567 | 0× | TreeMaps::decode(self.raw.as_ref().unwrap()) |
| 568 | 0× | .unwrap() |
| 569 | 0× | .introduced |
| 570 | 0× | .get(node), |
| 571 | | "the introducers block is the maps'" |
| 572 | | ); |
| 573 | 546× | return only.get(node).copied(); |
| 574 | 14053× | } |
| 575 | 14053× | self.m().introduced.get(node).copied() |
| 576 | 14599× | } |
| 577 | |
|
| 578 | 161× | pub fn placement(&self, k: &OpKey) -> Option<&Placement> { |
| 579 | 161× | self.m().placements.get(k) |
| 580 | 161× | } |
| 581 | |
|
| 582 | 6763848× | pub fn live_placements(&self, node: &NodeId) -> Vec<(OpKey, &Placement)> { |
| 583 | 6763848× | self.m() |
| 584 | 6763848× | .live_placements_of |
| 585 | 6763848× | .get(node) |
| 586 | 6763848× | .into_iter() |
| 587 | 6763848× | .flat_map(|ks| ks.iter()) |
| 588 | 6763856× | .filter_map(|k| self.m().placements.get(k).map(|p| (*k, p))) |
| 589 | 6763848× | .collect() |
| 590 | 6763848× | } |
| 591 | |
|
| 592 | 6775298× | pub fn live_modes(&self, node: &NodeId) -> Vec<(OpKey, Mode)> { |
| 593 | 6775298× | self.m() |
| 594 | 6775298× | .live_modes_of |
| 595 | 6775298× | .get(node) |
| 596 | 6775298× | .into_iter() |
| 597 | 6775298× | .flat_map(|ks| ks.iter()) |
| 598 | 6775298× | .filter_map(|k| self.m().modes.get(k).map(|(_, m)| (*k, *m))) |
| 599 | 6775298× | .collect() |
| 600 | 6775298× | } |
| 601 | |
|
| 602 | | |
| 603 | 0× | pub fn placed_nodes(&self) -> impl Iterator<Item = NodeId> + '_ { |
| 604 | 0× | self.m().live_placements_of.keys().copied() |
| 605 | 0× | } |
| 606 | |
|
| 607 | | |
| 608 | | |
| 609 | | |
| 610 | 6907× | pub fn view(&self) -> TreeView { |
| 611 | 6907× | self.view_cache |
| 612 | 6907× | .get_or_init(|| CachedView::new(self.compute_view())) |
| 613 | | .view |
| 614 | 6907× | .clone() |
| 615 | 6907× | } |
| 616 | |
|
| 617 | 12367× | fn compute_view(&self) -> TreeView { |
| 618 | 12367× | let mut view = TreeView::default(); |
| 619 | 12367× | let mut entries: BTreeMap<String, TreeEntry> = BTreeMap::new(); |
| 620 | |
|
| 621 | | |
| 622 | 12367× | let mut children: HashMap<NodeId, Vec<(OpKey, &Placement)>> = HashMap::new(); |
| 623 | 6763642× | for ks in self.m().live_placements_of.values() { |
| 624 | 6763657× | for k in ks { |
| 625 | 6763657× | if let Some(p) = self.m().placements.get(k) { |
| 626 | 6763657× | children.entry(p.parent).or_default().push((*k, p)); |
| 627 | 6763657× | } |
| 628 | | } |
| 629 | | } |
| 630 | 19561× | for c in children.values_mut() { |
| 631 | 74606901× | c.sort_by(|a, b| a.1.name.cmp(&b.1.name).then(a.0.cmp(&b.0))); |
| 632 | | } |
| 633 | |
|
| 634 | | |
| 635 | | |
| 636 | 12367× | let mut nodes: Vec<NodeId> = self.m().live_placements_of.keys().copied().collect(); |
| 637 | 12367× | nodes.sort(); |
| 638 | 12367× | let mut multi: HashSet<NodeId> = HashSet::new(); |
| 639 | | |
| 640 | 12367× | let mut modes: HashMap<NodeId, (Option<OpKey>, Mode)> = HashMap::new(); |
| 641 | 6763642× | for n in &nodes { |
| 642 | 6763642× | let lp = self.live_placements(n); |
| 643 | | |
| 644 | 6763642× | if lp.len() > 1 { |
| 645 | 15× | let distinct_places: BTreeSet<(&NodeId, &str)> = lp |
| 646 | 15× | .iter() |
| 647 | 30× | .map(|(_, p)| (&p.parent, p.name.as_str())) |
| 648 | 15× | .collect(); |
| 649 | 15× | if distinct_places.len() > 1 { |
| 650 | 2× | multi.insert(*n); |
| 651 | 2× | view.conflicts.push(TreeConflict::MultiPlacement { |
| 652 | 2× | node: *n, |
| 653 | 2× | placements: lp.iter().map(|(k, _)| *k).collect(), |
| 654 | | }); |
| 655 | 13× | } |
| 656 | 6763627× | } |
| 657 | 6763642× | let lm = self.live_modes(n); |
| 658 | 6763642× | if lm.len() > 1 { |
| 659 | 2× | let distinct_modes: BTreeSet<Mode> = lm.iter().map(|(_, m)| *m).collect(); |
| 660 | 2× | if distinct_modes.len() > 1 { |
| 661 | 1× | view.conflicts.push(TreeConflict::Mode { |
| 662 | 1× | node: *n, |
| 663 | 1× | modes: distinct_modes.into_iter().collect(), |
| 664 | 1× | }); |
| 665 | 1× | } |
| 666 | 6763640× | } |
| 667 | 6763642× | let mode_atom = lm.iter().map(|(k, _)| *k).max(); |
| 668 | 6763642× | let mode = mode_atom |
| 669 | 6763642× | .and_then(|k| self.m().modes.get(&k).map(|(_, m)| *m)) |
| 670 | 6763642× | .unwrap_or_default(); |
| 671 | 6763642× | modes.insert(*n, (mode_atom, mode)); |
| 672 | | } |
| 673 | |
|
| 674 | | |
| 675 | 12367× | let mut reached: HashSet<NodeId> = HashSet::new(); |
| 676 | 12367× | reached.insert(NodeId::ROOT); |
| 677 | 12367× | let mut queue: Vec<(NodeId, String)> = vec![(NodeId::ROOT, String::new())]; |
| 678 | 12367× | let mut on_path: HashSet<NodeId> = HashSet::new(); |
| 679 | 33677× | while let Some((dir, dir_path)) = queue.pop() { |
| 680 | 21310× | let Some(kids) = children.get(&dir) else { |
| 681 | 1754× | continue; |
| 682 | | }; |
| 683 | | |
| 684 | | |
| 685 | 19556× | let mut groups: Vec<(&str, &[(OpKey, &Placement)])> = Vec::new(); |
| 686 | 19556× | let mut start = 0; |
| 687 | 6763651× | for i in 1..=kids.len() { |
| 688 | 6763651× | if i == kids.len() || kids[i].1.name != kids[start].1.name { |
| 689 | 6763410× | groups.push((kids[start].1.name.as_str(), &kids[start..i])); |
| 690 | 6763410× | start = i; |
| 691 | 6763410× | } |
| 692 | | } |
| 693 | 6763410× | for (name, group) in groups { |
| 694 | 6763410× | let distinct: BTreeSet<NodeId> = group.iter().map(|(_, p)| p.node).collect(); |
| 695 | 6763410× | if distinct.len() > 1 { |
| 696 | 189× | view.conflicts.push(TreeConflict::Name { |
| 697 | 189× | parent: dir, |
| 698 | 189× | name: name.to_string(), |
| 699 | 189× | nodes: distinct.iter().copied().collect(), |
| 700 | 189× | }); |
| 701 | 6763221× | } |
| 702 | 6763410× | let suffixed = distinct.len() > 1; |
| 703 | 6763651× | for (i, (k, p)) in group.iter().enumerate() { |
| 704 | 6763651× | let node = p.node; |
| 705 | 6763651× | if node == dir || on_path.contains(&node) { |
| 706 | 0× | view.conflicts.push(TreeConflict::Cycle { node }); |
| 707 | 0× | continue; |
| 708 | 6763651× | } |
| 709 | 6763651× | let needs_suffix = suffixed || multi.contains(&node); |
| 710 | 6763651× | let leaf = if needs_suffix { |
| 711 | 420× | format!("{name}.oxy-conflict-{}", i + 1) |
| 712 | | } else { |
| 713 | 6763231× | name.to_string() |
| 714 | | }; |
| 715 | 6763651× | let path = if dir_path.is_empty() { |
| 716 | 17813× | leaf |
| 717 | | } else { |
| 718 | 6745838× | format!("{dir_path}/{leaf}") |
| 719 | | }; |
| 720 | 6763651× | let kind = self.kind(&node).unwrap_or(NodeKind::File); |
| 721 | 6763651× | let (mode_atom, mode) = modes.get(&node).copied().unwrap_or_default(); |
| 722 | 6763651× | entries.insert( |
| 723 | 6763651× | path.clone(), |
| 724 | 6763651× | TreeEntry { |
| 725 | 6763651× | path: path.clone(), |
| 726 | 6763651× | node, |
| 727 | 6763651× | kind, |
| 728 | 6763651× | mode, |
| 729 | 6763651× | placement: *k, |
| 730 | 6763651× | mode_atom, |
| 731 | 6763651× | }, |
| 732 | | ); |
| 733 | 6763651× | if kind == NodeKind::Dir && reached.insert(node) { |
| 734 | 8943× | on_path.insert(dir); |
| 735 | 8943× | queue.push((node, path)); |
| 736 | 6754708× | } else { |
| 737 | 6754708× | reached.insert(node); |
| 738 | 6754708× | } |
| 739 | | } |
| 740 | | } |
| 741 | | } |
| 742 | |
|
| 743 | | |
| 744 | 6763642× | for n in &nodes { |
| 745 | 6763642× | if reached.contains(n) { |
| 746 | 6763637× | continue; |
| 747 | 5× | } |
| 748 | 5× | let lp = self.live_placements(n); |
| 749 | 5× | if lp.is_empty() { |
| 750 | 0× | continue; |
| 751 | 5× | } |
| 752 | 5× | let mut seen = HashSet::new(); |
| 753 | 5× | let mut cur = *n; |
| 754 | 5× | let conflict = loop { |
| 755 | 12× | if !seen.insert(cur) { |
| 756 | 2× | break TreeConflict::Cycle { node: *n }; |
| 757 | 10× | } |
| 758 | 10× | let lp = self.live_placements(&cur); |
| 759 | 10× | let Some((_, p)) = lp.first() else { |
| 760 | 3× | break TreeConflict::Orphan { |
| 761 | 3× | node: *n, |
| 762 | 3× | parent: cur, |
| 763 | 3× | }; |
| 764 | | }; |
| 765 | 7× | if reached.contains(&p.parent) { |
| 766 | | |
| 767 | | |
| 768 | 0× | break TreeConflict::Orphan { |
| 769 | 0× | node: *n, |
| 770 | 0× | parent: p.parent, |
| 771 | 0× | }; |
| 772 | 7× | } |
| 773 | 7× | cur = p.parent; |
| 774 | | }; |
| 775 | 5× | if !view.conflicts.contains(&conflict) { |
| 776 | 5× | view.conflicts.push(conflict); |
| 777 | 5× | } |
| 778 | | } |
| 779 | 12367× | view.entries = std::sync::Arc::new(entries); |
| 780 | 12367× | view |
| 781 | 12367× | } |
| 782 | | } |
| 783 | |
|
| 784 | | impl TreeGraph { |
| 785 | | |
| 786 | 400× | pub fn to_value(&self) -> crate::cbor::Value { |
| 787 | | use crate::cbor::Value; |
| 788 | 4099× | let opkey = |k: &OpKey| vec![Value::Bytes(k.0 .0.to_vec()), Value::Uint(k.1 as u64)]; |
| 789 | 800× | let deleters = |m: &Map<OpKey, BTreeSet<ChangeHash>>| { |
| 790 | 800× | let mut ks: Vec<&OpKey> = m.keys().collect(); |
| 791 | 800× | ks.sort(); |
| 792 | | Value::Array( |
| 793 | 800× | ks.into_iter() |
| 794 | 800× | .map(|k| { |
| 795 | 279× | let mut v = opkey(k); |
| 796 | 279× | v.push(Value::Array( |
| 797 | 323× | m[k].iter().map(|d| Value::Bytes(d.0.to_vec())).collect(), |
| 798 | | )); |
| 799 | 279× | Value::Array(v) |
| 800 | 279× | }) |
| 801 | 800× | .collect(), |
| 802 | | ) |
| 803 | 800× | }; |
| 804 | 400× | let mut nodes: Vec<&NodeId> = self.m().kinds.keys().collect(); |
| 805 | 400× | nodes.sort(); |
| 806 | 400× | let mut pks: Vec<&OpKey> = self.m().placements.keys().collect(); |
| 807 | 400× | pks.sort(); |
| 808 | 400× | let mut mks: Vec<&OpKey> = self.m().modes.keys().collect(); |
| 809 | 400× | mks.sort(); |
| 810 | 400× | Value::map([ |
| 811 | | ( |
| 812 | | 0, |
| 813 | | Value::Array( |
| 814 | 400× | nodes |
| 815 | 400× | .into_iter() |
| 816 | 2085× | .map(|n| { |
| 817 | 2085× | Value::Array(vec![ |
| 818 | 2085× | Value::Bytes(n.0.to_vec()), |
| 819 | 2085× | Value::Uint(self.m().kinds[n].tag()), |
| 820 | 2085× | self.m() |
| 821 | 2085× | .introduced |
| 822 | 2085× | .get(n) |
| 823 | 2085× | .map(|h| Value::Bytes(h.0.to_vec())) |
| 824 | 2085× | .unwrap_or(Value::Null), |
| 825 | | ]) |
| 826 | 2085× | }) |
| 827 | 400× | .collect(), |
| 828 | | ), |
| 829 | | ), |
| 830 | | ( |
| 831 | | 1, |
| 832 | | Value::Array( |
| 833 | 400× | pks.into_iter() |
| 834 | 2098× | .map(|k| { |
| 835 | 2098× | let p = &self.m().placements[k]; |
| 836 | 2098× | let mut v = opkey(k); |
| 837 | 2098× | v.extend([ |
| 838 | 2098× | Value::Bytes(p.node.0.to_vec()), |
| 839 | 2098× | Value::Bytes(p.parent.0.to_vec()), |
| 840 | 2098× | Value::Text(p.name.clone()), |
| 841 | 2098× | ]); |
| 842 | 2098× | Value::Array(v) |
| 843 | 2098× | }) |
| 844 | 400× | .collect(), |
| 845 | | ), |
| 846 | | ), |
| 847 | 400× | (2, deleters(&self.m().deleted_placements)), |
| 848 | | ( |
| 849 | | 3, |
| 850 | | Value::Array( |
| 851 | 400× | mks.into_iter() |
| 852 | 1722× | .map(|k| { |
| 853 | 1722× | let (n, m) = &self.m().modes[k]; |
| 854 | 1722× | let mut v = opkey(k); |
| 855 | 1722× | v.extend([Value::Bytes(n.0.to_vec()), Value::Uint(m.bits())]); |
| 856 | 1722× | Value::Array(v) |
| 857 | 1722× | }) |
| 858 | 400× | .collect(), |
| 859 | | ), |
| 860 | | ), |
| 861 | 400× | (4, deleters(&self.m().deleted_modes)), |
| 862 | | ]) |
| 863 | 400× | } |
| 864 | |
|
| 865 | 100× | pub fn from_value(v: &crate::cbor::Value) -> Result<TreeGraph, crate::objects::ObjectError> { |
| 866 | | use crate::objects::{array, bytes, field, map, oref, text, u32_, uint, ObjectError}; |
| 867 | 100× | let m = map(v, "tree graph")?; |
| 868 | 1703× | let node = |v: &crate::cbor::Value| { |
| 869 | 1703× | NodeId::from_slice(bytes(v, "node")?) |
| 870 | 1703× | .ok_or_else(|| ObjectError::Schema("node: expected 16 bytes".into())) |
| 871 | 1703× | }; |
| 872 | 862× | let opkey = |e: &[crate::cbor::Value]| -> Result<OpKey, ObjectError> { |
| 873 | 862× | Ok((oref(&e[0], "op change")?, u32_(&e[1], "op index")?)) |
| 874 | 862× | }; |
| 875 | 100× | let mut g = TreeGraph::default(); |
| 876 | 433× | for n in array(field(m, 0, "nodes")?, "nodes")? { |
| 877 | 433× | let e = array(n, "node entry")?; |
| 878 | 433× | if e.len() != 3 { |
| 879 | 0× | return Err(ObjectError::Schema("node entry: expected 3 fields".into())); |
| 880 | 433× | } |
| 881 | 433× | let id = node(&e[0])?; |
| 882 | 433× | g.m_mut() |
| 883 | 433× | .kinds |
| 884 | 433× | .insert(id, NodeKind::from_tag(uint(&e[1], "kind")?)?); |
| 885 | 433× | if !e[2].is_null() { |
| 886 | 433× | g.m_mut().introduced.insert(id, oref(&e[2], "introducer")?); |
| 887 | 0× | } |
| 888 | | } |
| 889 | 435× | for p in array(field(m, 1, "placements")?, "placements")? { |
| 890 | 435× | let e = array(p, "placement")?; |
| 891 | 435× | if e.len() != 5 { |
| 892 | 0× | return Err(ObjectError::Schema("placement: expected 5 fields".into())); |
| 893 | 435× | } |
| 894 | 435× | let k = opkey(e)?; |
| 895 | 435× | let pl = Placement { |
| 896 | 435× | node: node(&e[2])?, |
| 897 | 435× | parent: node(&e[3])?, |
| 898 | 435× | name: text(&e[4], "name")?.to_owned(), |
| 899 | | }; |
| 900 | 435× | g.m_mut() |
| 901 | 435× | .placements_of |
| 902 | 435× | .entry(pl.node) |
| 903 | 435× | .or_default() |
| 904 | 435× | .insert(k); |
| 905 | 435× | g.m_mut().placements.insert(k, pl); |
| 906 | | } |
| 907 | 100× | let deleters = |v: &crate::cbor::Value, |
| 908 | | what: &str| |
| 909 | 200× | -> Result<Map<OpKey, BTreeSet<ChangeHash>>, ObjectError> { |
| 910 | 200× | let mut out = Map::default(); |
| 911 | 200× | for d in array(v, what)? { |
| 912 | 27× | let e = array(d, what)?; |
| 913 | 27× | if e.len() != 3 { |
| 914 | 0× | return Err(ObjectError::Schema(format!("{what}: expected 3 fields"))); |
| 915 | 27× | } |
| 916 | 27× | out.insert( |
| 917 | 27× | opkey(e)?, |
| 918 | 27× | array(&e[2], "deleters")? |
| 919 | 27× | .iter() |
| 920 | 31× | .map(|x| oref(x, "deleter")) |
| 921 | 27× | .collect::<Result<BTreeSet<_>, _>>()?, |
| 922 | | ); |
| 923 | | } |
| 924 | 200× | Ok(out) |
| 925 | 200× | }; |
| 926 | 100× | g.m_mut().deleted_placements = |
| 927 | 100× | deleters(field(m, 2, "deleted placements")?, "deleted placement")?; |
| 928 | 400× | for md in array(field(m, 3, "modes")?, "modes")? { |
| 929 | 400× | let e = array(md, "mode")?; |
| 930 | 400× | if e.len() != 4 { |
| 931 | 0× | return Err(ObjectError::Schema("mode: expected 4 fields".into())); |
| 932 | 400× | } |
| 933 | 400× | let k = opkey(e)?; |
| 934 | 400× | let n = node(&e[2])?; |
| 935 | 400× | g.m_mut() |
| 936 | 400× | .modes |
| 937 | 400× | .insert(k, (n, Mode::from_bits(uint(&e[3], "mode bits")?)?)); |
| 938 | 400× | g.m_mut().modes_of.entry(n).or_default().insert(k); |
| 939 | | } |
| 940 | 100× | g.m_mut().deleted_modes = deleters(field(m, 4, "deleted modes")?, "deleted mode")?; |
| 941 | 100× | let keys: Vec<OpKey> = g.m_mut().placements.keys().copied().collect(); |
| 942 | 435× | for k in keys { |
| 943 | 435× | g.index_placement(k); |
| 944 | 435× | } |
| 945 | 100× | let keys: Vec<OpKey> = g.m_mut().modes.keys().copied().collect(); |
| 946 | 400× | for k in keys { |
| 947 | 400× | g.index_mode(k); |
| 948 | 400× | } |
| 949 | 100× | Ok(g) |
| 950 | 100× | } |
| 951 | | } |
| 952 | |
|
| 953 | |
|
| 954 | |
|
| 955 | | impl TreeGraph { |
| 956 | 910× | pub(crate) fn write_flat(&self, w: &mut crate::flat::Writer) { |
| 957 | 130992× | let key = |w: &mut crate::flat::Writer, k: &OpKey| { |
| 958 | 130992× | w.object(&k.0); |
| 959 | 130992× | w.u32(k.1); |
| 960 | 130992× | }; |
| 961 | | |
| 962 | | |
| 963 | 910× | let outer = std::mem::take(w); |
| 964 | 910× | let mut kinds: Vec<(&NodeId, &NodeKind)> = self.m().kinds.iter().collect(); |
| 965 | 910× | kinds.sort_by_key(|(n, _)| **n); |
| 966 | 910× | w.count(kinds.len()); |
| 967 | 32768× | for (n, k) in kinds { |
| 968 | 32768× | w.fixed(&n.0); |
| 969 | 32768× | w.u8(k.tag() as u8); |
| 970 | 32768× | } |
| 971 | 910× | let mut intro: Vec<(&NodeId, &ChangeHash)> = self.m().introduced.iter().collect(); |
| 972 | 910× | intro.sort(); |
| 973 | 910× | w.count(intro.len()); |
| 974 | 32768× | for (n, c) in intro { |
| 975 | 32768× | w.fixed(&n.0); |
| 976 | 32768× | w.object(c); |
| 977 | 32768× | } |
| 978 | 910× | let mut pl: Vec<(&OpKey, &Placement)> = self.m().placements.iter().collect(); |
| 979 | 910× | pl.sort_by_key(|(k, _)| **k); |
| 980 | 910× | w.count(pl.len()); |
| 981 | 32768× | for (k, p) in pl { |
| 982 | 32768× | key(w, k); |
| 983 | 32768× | w.fixed(&p.node.0); |
| 984 | 32768× | w.fixed(&p.parent.0); |
| 985 | 32768× | w.bytes(p.name.as_bytes()); |
| 986 | 32768× | } |
| 987 | 910× | let mut modes: Vec<(&OpKey, &(NodeId, Mode))> = self.m().modes.iter().collect(); |
| 988 | 910× | modes.sort_by_key(|(k, _)| **k); |
| 989 | 910× | w.count(modes.len()); |
| 990 | 32728× | for (k, (n, m)) in modes { |
| 991 | 32728× | key(w, k); |
| 992 | 32728× | w.fixed(&n.0); |
| 993 | 32728× | w.u8(m.bits() as u8); |
| 994 | 32728× | } |
| 995 | 1820× | for m in [&self.m().deleted_placements, &self.m().deleted_modes] { |
| 996 | 1820× | let mut ks: Vec<(&OpKey, &BTreeSet<ChangeHash>)> = m.iter().collect(); |
| 997 | 1820× | ks.sort_by_key(|(k, _)| **k); |
| 998 | 1820× | w.count(ks.len()); |
| 999 | 1820× | for (k, by) in ks { |
| 1000 | 0× | key(w, k); |
| 1001 | 0× | w.count(by.len()); |
| 1002 | 0× | for c in by { |
| 1003 | 0× | w.object(c); |
| 1004 | 0× | } |
| 1005 | | } |
| 1006 | | } |
| 1007 | 910× | let maps = std::mem::replace(w, outer); |
| 1008 | 910× | w.bytes(&maps.0); |
| 1009 | | |
| 1010 | 910× | let mut iw = crate::flat::Writer::default(); |
| 1011 | 910× | let mut intro: Vec<(&NodeId, &ChangeHash)> = self.m().introduced.iter().collect(); |
| 1012 | 910× | intro.sort(); |
| 1013 | 910× | iw.count(intro.len()); |
| 1014 | 32768× | for (n, c) in intro { |
| 1015 | 32768× | iw.fixed(&n.0); |
| 1016 | 32768× | iw.object(c); |
| 1017 | 32768× | } |
| 1018 | 910× | w.bytes(&iw.0); |
| 1019 | | |
| 1020 | 910× | let outer = std::mem::take(w); |
| 1021 | 910× | let view = self.view(); |
| 1022 | 910× | if view.conflicts.is_empty() { |
| 1023 | 910× | w.u8(1); |
| 1024 | 910× | w.count(view.entries.len()); |
| 1025 | 32768× | for (path, e) in view.entries.iter() { |
| 1026 | 32768× | w.bytes(path.as_bytes()); |
| 1027 | 32768× | w.fixed(&e.node.0); |
| 1028 | 32768× | w.u8(e.kind.tag() as u8); |
| 1029 | 32768× | w.u8(e.mode.bits() as u8); |
| 1030 | 32768× | key(w, &e.placement); |
| 1031 | 32768× | match &e.mode_atom { |
| 1032 | 40× | None => w.u8(0), |
| 1033 | 32728× | Some(k) => { |
| 1034 | 32728× | w.u8(1); |
| 1035 | 32728× | key(w, k); |
| 1036 | 32728× | } |
| 1037 | | } |
| 1038 | | } |
| 1039 | 0× | } else { |
| 1040 | 0× | w.u8(0); |
| 1041 | 0× | } |
| 1042 | 910× | let view_bytes = std::mem::replace(w, outer); |
| 1043 | 910× | w.bytes(&view_bytes.0); |
| 1044 | 910× | } |
| 1045 | |
|
| 1046 | 1767× | pub(crate) fn read_flat( |
| 1047 | 1767× | r: &mut crate::flat::Reader, |
| 1048 | 1767× | ) -> Result<TreeGraph, crate::objects::ObjectError> { |
| 1049 | 1767× | let maps = r.bytes()?; |
| 1050 | 1767× | let introduced = r.bytes()?; |
| 1051 | 1767× | let view = Self::read_view(r.bytes()?)?; |
| 1052 | 1767× | let mut g = TreeGraph { |
| 1053 | 1767× | raw: Some(std::sync::Arc::new(maps.to_vec())), |
| 1054 | 1767× | raw_introduced: Some(std::sync::Arc::new(introduced.to_vec())), |
| 1055 | 1767× | ..TreeGraph::default() |
| 1056 | 1767× | }; |
| 1057 | 1767× | if let Some(view) = view { |
| 1058 | 718× | |
| 1059 | 718× | #[cfg(test)] |
| 1060 | 1049× | assert_eq!( |
| 1061 | 718× | view, |
| 1062 | 1049× | TreeGraph { |
| 1063 | 1049× | raw: g.raw.clone(), |
| 1064 | 1049× | ..TreeGraph::default() |
| 1065 | 1049× | } |
| 1066 | 1049× | .compute_view(), |
| 1067 | 718× | "an index's view is the tree's" |
| 1068 | 718× | ); |
| 1069 | 1767× | g.view_cache = std::sync::OnceLock::from(CachedView::new(view)); |
| 1070 | 718× | } |
| 1071 | 1767× | Ok(g) |
| 1072 | 1767× | } |
| 1073 | | } |
| 1074 | |
|
| 1075 | | impl TreeMaps { |
| 1076 | | |
| 1077 | 2757× | fn decode(bytes: &[u8]) -> Result<TreeMaps, crate::objects::ObjectError> { |
| 1078 | | use crate::objects::ObjectError; |
| 1079 | 2757× | let _t = crate::timing::phase("index: tree maps"); |
| 1080 | 165629× | let node = |r: &mut crate::flat::Reader| -> Result<NodeId, ObjectError> { |
| 1081 | 165629× | Ok(NodeId(r.fixed::<16>()?)) |
| 1082 | 165629× | }; |
| 1083 | 66227× | let key = |r: &mut crate::flat::Reader| -> Result<OpKey, ObjectError> { |
| 1084 | 66227× | Ok((r.object()?, r.u32()?)) |
| 1085 | 66227× | }; |
| 1086 | 2757× | let r = &mut crate::flat::Reader::new(bytes); |
| 1087 | 2757× | let mut g = TreeGraph::new(); |
| 1088 | 2757× | for _ in 0..r.count()? { |
| 1089 | 33134× | let n = node(r)?; |
| 1090 | 33134× | g.m_mut() |
| 1091 | 33134× | .kinds |
| 1092 | 33134× | .insert(n, NodeKind::from_tag(r.u8()? as u64)?); |
| 1093 | | } |
| 1094 | 2757× | for _ in 0..r.count()? { |
| 1095 | 33134× | let n = node(r)?; |
| 1096 | 33134× | g.m_mut().introduced.insert(n, r.object()?); |
| 1097 | | } |
| 1098 | 2757× | for _ in 0..r.count()? { |
| 1099 | 33134× | let k = key(r)?; |
| 1100 | 33134× | let p = Placement { |
| 1101 | 33134× | node: node(r)?, |
| 1102 | 33134× | parent: node(r)?, |
| 1103 | 33134× | name: r.text()?, |
| 1104 | | }; |
| 1105 | 33134× | g.m_mut().placements_of.entry(p.node).or_default().insert(k); |
| 1106 | 33134× | g.m_mut().placements.insert(k, p); |
| 1107 | | } |
| 1108 | 2757× | for _ in 0..r.count()? { |
| 1109 | 33093× | let k = key(r)?; |
| 1110 | 33093× | let n = node(r)?; |
| 1111 | 33093× | g.m_mut() |
| 1112 | 33093× | .modes |
| 1113 | 33093× | .insert(k, (n, Mode::from_bits(r.u8()? as u64)?)); |
| 1114 | 33093× | g.m_mut().modes_of.entry(n).or_default().insert(k); |
| 1115 | | } |
| 1116 | 5514× | for i in 0..2 { |
| 1117 | 5514× | let mut m: Map<OpKey, BTreeSet<ChangeHash>> = Map::default(); |
| 1118 | 5514× | for _ in 0..r.count()? { |
| 1119 | 0× | let k = key(r)?; |
| 1120 | 0× | let mut by = BTreeSet::new(); |
| 1121 | 0× | for _ in 0..r.count()? { |
| 1122 | 0× | by.insert(r.object()?); |
| 1123 | | } |
| 1124 | 0× | m.insert(k, by); |
| 1125 | | } |
| 1126 | 5514× | if i == 0 { |
| 1127 | 2757× | g.m_mut().deleted_placements = m; |
| 1128 | 2757× | } else { |
| 1129 | 2757× | g.m_mut().deleted_modes = m; |
| 1130 | 2757× | } |
| 1131 | | } |
| 1132 | 2757× | if !r.at_end() { |
| 1133 | 0× | return Err(ObjectError::Schema("state index: tree maps".into())); |
| 1134 | 2757× | } |
| 1135 | 2757× | let keys: Vec<OpKey> = g.m().placements.keys().copied().collect(); |
| 1136 | 33134× | for k in keys { |
| 1137 | 33134× | g.index_placement(k); |
| 1138 | 33134× | } |
| 1139 | 2757× | let keys: Vec<OpKey> = g.m().modes.keys().copied().collect(); |
| 1140 | 33093× | for k in keys { |
| 1141 | 33093× | g.index_mode(k); |
| 1142 | 33093× | } |
| 1143 | 2757× | Ok(g.maps.into_inner().unwrap_or_default()) |
| 1144 | 2757× | } |
| 1145 | | } |
| 1146 | |
|
| 1147 | | impl TreeGraph { |
| 1148 | | |
| 1149 | 1767× | fn read_view(bytes: &[u8]) -> Result<Option<TreeView>, crate::objects::ObjectError> { |
| 1150 | 1767× | let _t = crate::timing::phase("index: view"); |
| 1151 | 1767× | let mut r = crate::flat::Reader::new(bytes); |
| 1152 | 1767× | if r.u8()? != 1 { |
| 1153 | 0× | return Ok(None); |
| 1154 | 1767× | } |
| 1155 | 33421× | let key = |r: &mut crate::flat::Reader| -> Result<OpKey, crate::objects::ObjectError> { |
| 1156 | 33421× | Ok((r.object()?, r.u32()?)) |
| 1157 | 33421× | }; |
| 1158 | 1767× | let n = r.count()?; |
| 1159 | | |
| 1160 | | |
| 1161 | 1767× | let mut entries = Vec::with_capacity(n); |
| 1162 | 1767× | for _ in 0..n { |
| 1163 | 16721× | let path = r.text()?; |
| 1164 | 16721× | let e = TreeEntry { |
| 1165 | 16721× | path: path.clone(), |
| 1166 | 16721× | node: NodeId(r.fixed::<16>()?), |
| 1167 | 16721× | kind: NodeKind::from_tag(r.u8()? as u64)?, |
| 1168 | 16721× | mode: Mode::from_bits(r.u8()? as u64)?, |
| 1169 | 16721× | placement: key(&mut r)?, |
| 1170 | 16721× | mode_atom: match r.u8()? { |
| 1171 | 21× | 0 => None, |
| 1172 | 16700× | _ => Some(key(&mut r)?), |
| 1173 | | }, |
| 1174 | | }; |
| 1175 | 16721× | entries.push((path, e)); |
| 1176 | | } |
| 1177 | 1767× | Ok(Some(TreeView { |
| 1178 | 1767× | entries: std::sync::Arc::new(entries.into_iter().collect()), |
| 1179 | 1767× | conflicts: Vec::new(), |
| 1180 | 1767× | })) |
| 1181 | 1767× | } |
| 1182 | | } |
| 1183 | |
|
| 1184 | |
|
| 1185 | 58× | fn read_introduced(bytes: &[u8]) -> Result<Map<NodeId, ChangeHash>, crate::objects::ObjectError> { |
| 1186 | 58× | let r = &mut crate::flat::Reader::new(bytes); |
| 1187 | 58× | let n = r.count()?; |
| 1188 | 58× | let mut m = Map::default(); |
| 1189 | 58× | m.reserve(n); |
| 1190 | 58× | for _ in 0..n { |
| 1191 | 0× | let node = NodeId(r.fixed::<16>()?); |
| 1192 | 0× | m.insert(node, r.object()?); |
| 1193 | | } |
| 1194 | 58× | Ok(m) |
| 1195 | 58× | } |
| 1196 | |
|
| 1197 | 11703× | fn join(dir: &str, name: &str) -> String { |
| 1198 | 11703× | if dir.is_empty() { |
| 1199 | 3819× | name.to_string() |
| 1200 | | } else { |
| 1201 | 7884× | format!("{dir}/{name}") |
| 1202 | | } |
| 1203 | 11703× | } |
| 1204 | |
|
| 1205 | 2823× | pub fn op_key(r: OpRef) -> OpKey { |
| 1206 | 2823× | (r.change, r.index) |
| 1207 | 2823× | } |
| 1208 | |
|
| 1209 | | #[cfg(test)] |
| 1210 | | mod tests { |
| 1211 | | use super::*; |
| 1212 | | use crate::hash::ObjectRef; |
| 1213 | |
|
| 1214 | 54× | fn h(n: u8) -> ChangeHash { |
| 1215 | 54× | ObjectRef([n; 32]) |
| 1216 | 54× | } |
| 1217 | 51× | fn nid(n: u8) -> NodeId { |
| 1218 | 51× | NodeId([n; 16]) |
| 1219 | 51× | } |
| 1220 | 14× | fn add(g: &mut TreeGraph, c: u8, i: u32, node: u8, kind: NodeKind, parent: NodeId, name: &str) { |
| 1221 | 14× | g.apply( |
| 1222 | 14× | h(c), |
| 1223 | 14× | i, |
| 1224 | 14× | &TreeOp::AddNode { |
| 1225 | 14× | node: nid(node), |
| 1226 | 14× | kind, |
| 1227 | 14× | mode: Mode::TEXT, |
| 1228 | 14× | parent, |
| 1229 | 14× | name: name.into(), |
| 1230 | 14× | }, |
| 1231 | | ); |
| 1232 | 14× | } |
| 1233 | 7× | fn paths(v: &TreeView) -> Vec<String> { |
| 1234 | 7× | v.entries.keys().cloned().collect() |
| 1235 | 7× | } |
| 1236 | |
|
| 1237 | | |
| 1238 | | |
| 1239 | | #[test] |
| 1240 | 1× | fn cached_view_follows_ops() { |
| 1241 | 1× | let at = |c: u8, i: u32| OpRef { |
| 1242 | 4× | change: h(c), |
| 1243 | 4× | index: i, |
| 1244 | 4× | }; |
| 1245 | 1× | let mut g = TreeGraph::new(); |
| 1246 | 1× | let ops = [ |
| 1247 | 1× | TreeOp::AddNode { |
| 1248 | 1× | node: nid(1), |
| 1249 | 1× | kind: NodeKind::Dir, |
| 1250 | 1× | mode: Mode::TEXT, |
| 1251 | 1× | parent: NodeId::ROOT, |
| 1252 | 1× | name: "src".into(), |
| 1253 | 1× | }, |
| 1254 | 1× | TreeOp::AddNode { |
| 1255 | 1× | node: nid(2), |
| 1256 | 1× | kind: NodeKind::File, |
| 1257 | 1× | mode: Mode::TEXT, |
| 1258 | 1× | parent: nid(1), |
| 1259 | 1× | name: "a.rs".into(), |
| 1260 | 1× | }, |
| 1261 | 1× | TreeOp::SetMode { |
| 1262 | 1× | node: nid(2), |
| 1263 | 1× | old: at(1, 1), |
| 1264 | 1× | mode: Mode { |
| 1265 | 1× | exec: true, |
| 1266 | 1× | opaque: false, |
| 1267 | 1× | }, |
| 1268 | 1× | }, |
| 1269 | 1× | TreeOp::Move { |
| 1270 | 1× | node: nid(2), |
| 1271 | 1× | old: at(1, 1), |
| 1272 | 1× | parent: NodeId::ROOT, |
| 1273 | 1× | name: "b.rs".into(), |
| 1274 | 1× | }, |
| 1275 | 1× | |
| 1276 | 1× | TreeOp::Move { |
| 1277 | 1× | node: nid(1), |
| 1278 | 1× | old: at(1, 0), |
| 1279 | 1× | parent: NodeId::ROOT, |
| 1280 | 1× | name: "lib".into(), |
| 1281 | 1× | }, |
| 1282 | 1× | TreeOp::RemoveNode { |
| 1283 | 1× | node: nid(1), |
| 1284 | 1× | placement: at(1, 4), |
| 1285 | 1× | }, |
| 1286 | 1× | |
| 1287 | 1× | TreeOp::AddNode { |
| 1288 | 1× | node: nid(3), |
| 1289 | 1× | kind: NodeKind::File, |
| 1290 | 1× | mode: Mode::TEXT, |
| 1291 | 1× | parent: NodeId::ROOT, |
| 1292 | 1× | name: "b.rs".into(), |
| 1293 | 1× | }, |
| 1294 | 1× | ]; |
| 1295 | 7× | for (i, op) in ops.iter().enumerate() { |
| 1296 | 7× | let _ = g.view(); |
| 1297 | 7× | g.apply(h(1), i as u32, op); |
| 1298 | 7× | assert_eq!(g.view(), g.compute_view()); |
| 1299 | | } |
| 1300 | 1× | let v = g.view(); |
| 1301 | 1× | assert_eq!(paths(&v), ["b.rs.oxy-conflict-1", "b.rs.oxy-conflict-2"]); |
| 1302 | 1× | assert!(v.entries.values().any(|e| e.mode.exec)); |
| 1303 | 1× | } |
| 1304 | |
|
| 1305 | | #[test] |
| 1306 | 1× | fn nested_paths_and_rename() { |
| 1307 | 1× | let mut g = TreeGraph::new(); |
| 1308 | 1× | add(&mut g, 1, 0, 1, NodeKind::Dir, NodeId::ROOT, "src"); |
| 1309 | 1× | add(&mut g, 1, 1, 2, NodeKind::File, nid(1), "main.rs"); |
| 1310 | 1× | add(&mut g, 1, 2, 3, NodeKind::File, NodeId::ROOT, "README"); |
| 1311 | 1× | let v = g.view(); |
| 1312 | 1× | assert_eq!(paths(&v), ["README", "src", "src/main.rs"]); |
| 1313 | 1× | assert!(!v.is_conflicted()); |
| 1314 | |
|
| 1315 | | |
| 1316 | 1× | g.apply( |
| 1317 | 1× | h(2), |
| 1318 | | 0, |
| 1319 | 1× | &TreeOp::Move { |
| 1320 | 1× | node: nid(2), |
| 1321 | 1× | old: OpRef { |
| 1322 | 1× | change: h(1), |
| 1323 | 1× | index: 1, |
| 1324 | 1× | }, |
| 1325 | 1× | parent: nid(1), |
| 1326 | 1× | name: "lib.rs".into(), |
| 1327 | 1× | }, |
| 1328 | | ); |
| 1329 | 1× | g.apply( |
| 1330 | 1× | h(2), |
| 1331 | | 1, |
| 1332 | 1× | &TreeOp::Move { |
| 1333 | 1× | node: nid(1), |
| 1334 | 1× | old: OpRef { |
| 1335 | 1× | change: h(1), |
| 1336 | 1× | index: 0, |
| 1337 | 1× | }, |
| 1338 | 1× | parent: NodeId::ROOT, |
| 1339 | 1× | name: "lib".into(), |
| 1340 | 1× | }, |
| 1341 | | ); |
| 1342 | 1× | let v = g.view(); |
| 1343 | 1× | assert_eq!(paths(&v), ["README", "lib", "lib/lib.rs"]); |
| 1344 | 1× | assert_eq!(v.entries["lib/lib.rs"].placement, (h(2), 0)); |
| 1345 | |
|
| 1346 | 1× | g.apply( |
| 1347 | 1× | h(3), |
| 1348 | | 0, |
| 1349 | 1× | &TreeOp::RemoveNode { |
| 1350 | 1× | node: nid(3), |
| 1351 | 1× | placement: OpRef { |
| 1352 | 1× | change: h(1), |
| 1353 | 1× | index: 2, |
| 1354 | 1× | }, |
| 1355 | 1× | }, |
| 1356 | | ); |
| 1357 | 1× | assert_eq!(paths(&g.view()), ["lib", "lib/lib.rs"]); |
| 1358 | 1× | } |
| 1359 | |
|
| 1360 | | #[test] |
| 1361 | 1× | fn name_conflict_gets_suffixes() { |
| 1362 | 1× | let mut g = TreeGraph::new(); |
| 1363 | 1× | add(&mut g, 1, 0, 1, NodeKind::File, NodeId::ROOT, "a.txt"); |
| 1364 | 1× | add(&mut g, 2, 0, 2, NodeKind::File, NodeId::ROOT, "a.txt"); |
| 1365 | 1× | let v = g.view(); |
| 1366 | 1× | assert_eq!(paths(&v), ["a.txt.oxy-conflict-1", "a.txt.oxy-conflict-2"]); |
| 1367 | 1× | assert_eq!( |
| 1368 | | v.conflicts, |
| 1369 | 1× | vec![TreeConflict::Name { |
| 1370 | 1× | parent: NodeId::ROOT, |
| 1371 | 1× | name: "a.txt".into(), |
| 1372 | 1× | nodes: vec![nid(1), nid(2)] |
| 1373 | 1× | }] |
| 1374 | | ); |
| 1375 | 1× | } |
| 1376 | |
|
| 1377 | | #[test] |
| 1378 | 1× | fn concurrent_renames_are_multi_placement() { |
| 1379 | 1× | let mut g = TreeGraph::new(); |
| 1380 | 1× | add(&mut g, 1, 0, 1, NodeKind::File, NodeId::ROOT, "a"); |
| 1381 | 1× | g.apply( |
| 1382 | 1× | h(2), |
| 1383 | | 0, |
| 1384 | 1× | &TreeOp::Move { |
| 1385 | 1× | node: nid(1), |
| 1386 | 1× | old: OpRef { |
| 1387 | 1× | change: h(1), |
| 1388 | 1× | index: 0, |
| 1389 | 1× | }, |
| 1390 | 1× | parent: NodeId::ROOT, |
| 1391 | 1× | name: "b".into(), |
| 1392 | 1× | }, |
| 1393 | | ); |
| 1394 | 1× | g.apply( |
| 1395 | 1× | h(3), |
| 1396 | | 0, |
| 1397 | 1× | &TreeOp::Move { |
| 1398 | 1× | node: nid(1), |
| 1399 | 1× | old: OpRef { |
| 1400 | 1× | change: h(1), |
| 1401 | 1× | index: 0, |
| 1402 | 1× | }, |
| 1403 | 1× | parent: NodeId::ROOT, |
| 1404 | 1× | name: "c".into(), |
| 1405 | 1× | }, |
| 1406 | | ); |
| 1407 | 1× | let v = g.view(); |
| 1408 | 1× | assert_eq!(paths(&v), ["b.oxy-conflict-1", "c.oxy-conflict-1"]); |
| 1409 | 1× | assert!( |
| 1410 | 1× | matches!(v.conflicts[0], TreeConflict::MultiPlacement { node, .. } if node == nid(1)) |
| 1411 | | ); |
| 1412 | 1× | } |
| 1413 | |
|
| 1414 | | #[test] |
| 1415 | 1× | fn orphan_and_cycle() { |
| 1416 | 1× | let mut g = TreeGraph::new(); |
| 1417 | 1× | add(&mut g, 1, 0, 1, NodeKind::Dir, NodeId::ROOT, "d"); |
| 1418 | 1× | add(&mut g, 1, 1, 2, NodeKind::File, nid(1), "f"); |
| 1419 | | |
| 1420 | 1× | g.apply( |
| 1421 | 1× | h(2), |
| 1422 | | 0, |
| 1423 | 1× | &TreeOp::RemoveNode { |
| 1424 | 1× | node: nid(1), |
| 1425 | 1× | placement: OpRef { |
| 1426 | 1× | change: h(1), |
| 1427 | 1× | index: 0, |
| 1428 | 1× | }, |
| 1429 | 1× | }, |
| 1430 | | ); |
| 1431 | 1× | add(&mut g, 3, 0, 3, NodeKind::File, nid(1), "g"); |
| 1432 | 1× | let v = g.view(); |
| 1433 | 1× | assert_eq!(paths(&v), Vec::<String>::new()); |
| 1434 | 1× | assert!(v.conflicts.contains(&TreeConflict::Orphan { |
| 1435 | 1× | node: nid(2), |
| 1436 | 1× | parent: nid(1) |
| 1437 | 1× | })); |
| 1438 | 1× | assert!(v.conflicts.contains(&TreeConflict::Orphan { |
| 1439 | 1× | node: nid(3), |
| 1440 | 1× | parent: nid(1) |
| 1441 | 1× | })); |
| 1442 | |
|
| 1443 | | |
| 1444 | 1× | let mut g = TreeGraph::new(); |
| 1445 | 1× | add(&mut g, 1, 0, 1, NodeKind::Dir, NodeId::ROOT, "a"); |
| 1446 | 1× | add(&mut g, 1, 1, 2, NodeKind::Dir, nid(1), "b"); |
| 1447 | 1× | g.apply( |
| 1448 | 1× | h(2), |
| 1449 | | 0, |
| 1450 | 1× | &TreeOp::Move { |
| 1451 | 1× | node: nid(1), |
| 1452 | 1× | old: OpRef { |
| 1453 | 1× | change: h(1), |
| 1454 | 1× | index: 0, |
| 1455 | 1× | }, |
| 1456 | 1× | parent: nid(2), |
| 1457 | 1× | name: "a".into(), |
| 1458 | 1× | }, |
| 1459 | | ); |
| 1460 | 1× | let v = g.view(); |
| 1461 | 1× | assert!( |
| 1462 | 1× | v.conflicts |
| 1463 | 1× | .iter() |
| 1464 | 1× | .any(|c| matches!(c, TreeConflict::Cycle { .. })), |
| 1465 | | "{:?}", |
| 1466 | | v.conflicts |
| 1467 | | ); |
| 1468 | 1× | } |
| 1469 | |
|
| 1470 | | #[test] |
| 1471 | 1× | fn serialization_round_trip() { |
| 1472 | 1× | let mut g = TreeGraph::new(); |
| 1473 | 1× | add(&mut g, 1, 0, 1, NodeKind::Dir, NodeId::ROOT, "src"); |
| 1474 | 1× | add(&mut g, 1, 1, 2, NodeKind::File, nid(1), "main.rs"); |
| 1475 | 1× | g.apply( |
| 1476 | 1× | h(2), |
| 1477 | | 0, |
| 1478 | 1× | &TreeOp::Move { |
| 1479 | 1× | node: nid(2), |
| 1480 | 1× | old: OpRef { |
| 1481 | 1× | change: h(1), |
| 1482 | 1× | index: 1, |
| 1483 | 1× | }, |
| 1484 | 1× | parent: nid(1), |
| 1485 | 1× | name: "lib.rs".into(), |
| 1486 | 1× | }, |
| 1487 | | ); |
| 1488 | 1× | g.apply( |
| 1489 | 1× | h(3), |
| 1490 | | 0, |
| 1491 | 1× | &TreeOp::SetMode { |
| 1492 | 1× | node: nid(2), |
| 1493 | 1× | old: OpRef { |
| 1494 | 1× | change: h(1), |
| 1495 | 1× | index: 1, |
| 1496 | 1× | }, |
| 1497 | 1× | mode: Mode { |
| 1498 | 1× | exec: true, |
| 1499 | 1× | opaque: false, |
| 1500 | 1× | }, |
| 1501 | 1× | }, |
| 1502 | | ); |
| 1503 | 1× | let bytes = crate::cbor::encode(&g.to_value()); |
| 1504 | 1× | let back = TreeGraph::from_value(&crate::cbor::decode(&bytes).unwrap()).unwrap(); |
| 1505 | 1× | assert_eq!(back.view(), g.view()); |
| 1506 | 1× | assert_eq!(back.introducer(&nid(2)), Some(h(1))); |
| 1507 | 1× | assert_eq!(crate::cbor::encode(&back.to_value()), bytes); |
| 1508 | 1× | } |
| 1509 | |
|
| 1510 | | #[test] |
| 1511 | 1× | fn mode_conflict_and_resolution() { |
| 1512 | 1× | let mut g = TreeGraph::new(); |
| 1513 | 1× | add(&mut g, 1, 0, 1, NodeKind::File, NodeId::ROOT, "run.sh"); |
| 1514 | 1× | g.apply( |
| 1515 | 1× | h(2), |
| 1516 | | 0, |
| 1517 | 1× | &TreeOp::SetMode { |
| 1518 | 1× | node: nid(1), |
| 1519 | 1× | old: OpRef { |
| 1520 | 1× | change: h(1), |
| 1521 | 1× | index: 0, |
| 1522 | 1× | }, |
| 1523 | 1× | mode: Mode { |
| 1524 | 1× | exec: true, |
| 1525 | 1× | opaque: false, |
| 1526 | 1× | }, |
| 1527 | 1× | }, |
| 1528 | | ); |
| 1529 | 1× | g.apply( |
| 1530 | 1× | h(3), |
| 1531 | | 0, |
| 1532 | 1× | &TreeOp::SetMode { |
| 1533 | 1× | node: nid(1), |
| 1534 | 1× | old: OpRef { |
| 1535 | 1× | change: h(1), |
| 1536 | 1× | index: 0, |
| 1537 | 1× | }, |
| 1538 | 1× | mode: Mode { |
| 1539 | 1× | exec: false, |
| 1540 | 1× | opaque: true, |
| 1541 | 1× | }, |
| 1542 | 1× | }, |
| 1543 | | ); |
| 1544 | 1× | let v = g.view(); |
| 1545 | 1× | assert!(matches!(v.conflicts[0], TreeConflict::Mode { .. })); |
| 1546 | | |
| 1547 | 1× | assert_eq!( |
| 1548 | 1× | v.entries["run.sh"].mode, |
| 1549 | | Mode { |
| 1550 | | exec: false, |
| 1551 | | opaque: true |
| 1552 | | } |
| 1553 | | ); |
| 1554 | | |
| 1555 | 1× | assert_eq!(v.entries["run.sh"].placement, (h(1), 0)); |
| 1556 | | |
| 1557 | 1× | g.apply( |
| 1558 | 1× | h(4), |
| 1559 | | 0, |
| 1560 | 1× | &TreeOp::SetMode { |
| 1561 | 1× | node: nid(1), |
| 1562 | 1× | old: OpRef { |
| 1563 | 1× | change: h(2), |
| 1564 | 1× | index: 0, |
| 1565 | 1× | }, |
| 1566 | 1× | mode: Mode { |
| 1567 | 1× | exec: true, |
| 1568 | 1× | opaque: false, |
| 1569 | 1× | }, |
| 1570 | 1× | }, |
| 1571 | | ); |
| 1572 | 1× | g.apply( |
| 1573 | 1× | h(4), |
| 1574 | | 1, |
| 1575 | 1× | &TreeOp::SetMode { |
| 1576 | 1× | node: nid(1), |
| 1577 | 1× | old: OpRef { |
| 1578 | 1× | change: h(3), |
| 1579 | 1× | index: 0, |
| 1580 | 1× | }, |
| 1581 | 1× | mode: Mode { |
| 1582 | 1× | exec: true, |
| 1583 | 1× | opaque: false, |
| 1584 | 1× | }, |
| 1585 | 1× | }, |
| 1586 | | ); |
| 1587 | 1× | let v = g.view(); |
| 1588 | 1× | assert!(!v.is_conflicted(), "{:?}", v.conflicts); |
| 1589 | 1× | assert_eq!( |
| 1590 | 1× | v.entries["run.sh"].mode, |
| 1591 | | Mode { |
| 1592 | | exec: true, |
| 1593 | | opaque: false |
| 1594 | | } |
| 1595 | | ); |
| 1596 | 1× | } |
| 1597 | | } |