##// END OF EJS Templates
dirstate-tree: Add tree traversal/iteration...
Simon Sapin -
r47870:caa3031c default
parent child Browse files
Show More
@@ -1,323 +1,418 b''
1 1 use std::collections::BTreeMap;
2 2 use std::path::PathBuf;
3 3 use std::time::Duration;
4 4
5 5 use super::path_with_basename::WithBasename;
6 6 use crate::dirstate::parsers::parse_dirstate_entries;
7 7 use crate::dirstate::parsers::parse_dirstate_parents;
8 8
9 9 use crate::matchers::Matcher;
10 10 use crate::revlog::node::NULL_NODE;
11 11 use crate::utils::hg_path::{HgPath, HgPathBuf};
12 12 use crate::CopyMapIter;
13 13 use crate::DirstateEntry;
14 14 use crate::DirstateError;
15 15 use crate::DirstateMapError;
16 16 use crate::DirstateParents;
17 17 use crate::DirstateStatus;
18 18 use crate::EntryState;
19 19 use crate::FastHashMap;
20 20 use crate::HgPathCow;
21 21 use crate::PatternFileWarning;
22 22 use crate::StateMapIter;
23 23 use crate::StatusError;
24 24 use crate::StatusOptions;
25 25
26 26 pub struct DirstateMap {
27 27 parents: Option<DirstateParents>,
28 28 dirty_parents: bool,
29 29 root: ChildNodes,
30 30 }
31 31
32 32 /// Using a plain `HgPathBuf` of the full path from the repository root as a
33 33 /// map key would also work: all paths in a given map have the same parent
34 34 /// path, so comparing full paths gives the same result as comparing base
35 35 /// names. However `BTreeMap` would waste time always re-comparing the same
36 36 /// string prefix.
37 37 type ChildNodes = BTreeMap<WithBasename<HgPathBuf>, Node>;
38 38
39 39 #[derive(Default)]
40 40 struct Node {
41 41 entry: Option<DirstateEntry>,
42 42 copy_source: Option<HgPathBuf>,
43 43 children: ChildNodes,
44 44 }
45 45
46 /// `(full_path, entry, copy_source)`
47 type NodeDataMut<'a> = (
48 &'a WithBasename<HgPathBuf>,
49 &'a mut Option<DirstateEntry>,
50 &'a mut Option<HgPathBuf>,
51 );
52
46 53 impl DirstateMap {
47 54 pub fn new() -> Self {
48 55 Self {
49 56 parents: None,
50 57 dirty_parents: false,
51 58 root: ChildNodes::new(),
52 59 }
53 60 }
54 61
55 62 fn get_node(&self, path: &HgPath) -> Option<&Node> {
56 63 let mut children = &self.root;
57 64 let mut components = path.components();
58 65 let mut component =
59 66 components.next().expect("expected at least one components");
60 67 loop {
61 68 let child = children.get(component)?;
62 69 if let Some(next_component) = components.next() {
63 70 component = next_component;
64 71 children = &child.children;
65 72 } else {
66 73 return Some(child);
67 74 }
68 75 }
69 76 }
70 77
71 78 fn get_or_insert_node(&mut self, path: &HgPath) -> &mut Node {
72 79 let mut child_nodes = &mut self.root;
73 80 let mut inclusive_ancestor_paths =
74 81 WithBasename::inclusive_ancestors_of(path);
75 82 let mut ancestor_path = inclusive_ancestor_paths
76 83 .next()
77 84 .expect("expected at least one inclusive ancestor");
78 85 loop {
79 86 // TODO: can we avoid double lookup in all cases without allocating
80 87 // an owned key in cases where the map already contains that key?
81 88 let child_node =
82 89 if child_nodes.contains_key(ancestor_path.base_name()) {
83 90 child_nodes.get_mut(ancestor_path.base_name()).unwrap()
84 91 } else {
85 92 // This is always a vacant entry, using `.entry()` lets us
86 93 // return a `&mut Node` of the newly-inserted node without
87 94 // yet another lookup. `BTreeMap::insert` doesn’t do this.
88 95 child_nodes.entry(ancestor_path.to_owned()).or_default()
89 96 };
90 97 if let Some(next) = inclusive_ancestor_paths.next() {
91 98 ancestor_path = next;
92 99 child_nodes = &mut child_node.children;
93 100 } else {
94 101 return child_node;
95 102 }
96 103 }
97 104 }
105
106 fn iter_nodes<'a>(
107 &'a self,
108 ) -> impl Iterator<Item = (&'a WithBasename<HgPathBuf>, &'a Node)> + 'a
109 {
110 // Depth first tree traversal.
111 //
112 // If we could afford internal iteration and recursion,
113 // this would look like:
114 //
115 // ```
116 // fn traverse_children(
117 // children: &ChildNodes,
118 // each: &mut impl FnMut(&Node),
119 // ) {
120 // for child in children.values() {
121 // traverse_children(&child.children, each);
122 // each(child);
123 // }
124 // }
125 // ```
126 //
127 // However we want an external iterator and therefore can’t use the
128 // call stack. Use an explicit stack instead:
129 let mut stack = Vec::new();
130 let mut iter = self.root.iter();
131 std::iter::from_fn(move || {
132 while let Some((key, child_node)) = iter.next() {
133 // Pseudo-recursion
134 let new_iter = child_node.children.iter();
135 let old_iter = std::mem::replace(&mut iter, new_iter);
136 stack.push((key, child_node, old_iter));
137 }
138 // Found the end of a `children.iter()` iterator.
139 if let Some((key, child_node, next_iter)) = stack.pop() {
140 // "Return" from pseudo-recursion by restoring state from the
141 // explicit stack
142 iter = next_iter;
143
144 Some((key, child_node))
145 } else {
146 // Reached the bottom of the stack, we’re done
147 None
148 }
149 })
150 }
151
152 /// Mutable iterator for the `(entry, copy source)` of each node.
153 ///
154 /// It would not be safe to yield mutable references to nodes themeselves
155 /// with `-> impl Iterator<Item = &mut Node>` since child nodes are
156 /// reachable from their ancestor nodes, potentially creating multiple
157 /// `&mut` references to a given node.
158 fn iter_node_data_mut<'a>(
159 &'a mut self,
160 ) -> impl Iterator<Item = NodeDataMut<'a>> + 'a {
161 // Explict stack for pseudo-recursion, see `iter_nodes` above.
162 let mut stack = Vec::new();
163 let mut iter = self.root.iter_mut();
164 std::iter::from_fn(move || {
165 while let Some((key, child_node)) = iter.next() {
166 // Pseudo-recursion
167 let data =
168 (key, &mut child_node.entry, &mut child_node.copy_source);
169 let new_iter = child_node.children.iter_mut();
170 let old_iter = std::mem::replace(&mut iter, new_iter);
171 stack.push((data, old_iter));
172 }
173 // Found the end of a `children.values_mut()` iterator.
174 if let Some((data, next_iter)) = stack.pop() {
175 // "Return" from pseudo-recursion by restoring state from the
176 // explicit stack
177 iter = next_iter;
178
179 Some(data)
180 } else {
181 // Reached the bottom of the stack, we’re done
182 None
183 }
184 })
185 }
98 186 }
99 187
100 188 impl super::dispatch::DirstateMapMethods for DirstateMap {
101 189 fn clear(&mut self) {
102 190 self.set_parents(&DirstateParents {
103 191 p1: NULL_NODE,
104 192 p2: NULL_NODE,
105 193 });
106 194 self.root.clear()
107 195 }
108 196
109 197 fn add_file(
110 198 &mut self,
111 199 _filename: &HgPath,
112 200 _old_state: EntryState,
113 201 _entry: DirstateEntry,
114 202 ) -> Result<(), DirstateMapError> {
115 203 todo!()
116 204 }
117 205
118 206 fn remove_file(
119 207 &mut self,
120 208 _filename: &HgPath,
121 209 _old_state: EntryState,
122 210 _size: i32,
123 211 ) -> Result<(), DirstateMapError> {
124 212 todo!()
125 213 }
126 214
127 215 fn drop_file(
128 216 &mut self,
129 217 _filename: &HgPath,
130 218 _old_state: EntryState,
131 219 ) -> Result<bool, DirstateMapError> {
132 220 todo!()
133 221 }
134 222
135 223 fn clear_ambiguous_times(
136 224 &mut self,
137 225 _filenames: Vec<HgPathBuf>,
138 226 _now: i32,
139 227 ) {
140 228 todo!()
141 229 }
142 230
143 231 fn non_normal_entries_contains(&mut self, _key: &HgPath) -> bool {
144 232 todo!()
145 233 }
146 234
147 235 fn non_normal_entries_remove(&mut self, _key: &HgPath) -> bool {
148 236 todo!()
149 237 }
150 238
151 239 fn non_normal_or_other_parent_paths(
152 240 &mut self,
153 241 ) -> Box<dyn Iterator<Item = &HgPathBuf> + '_> {
154 242 todo!()
155 243 }
156 244
157 245 fn set_non_normal_other_parent_entries(&mut self, _force: bool) {
158 246 todo!()
159 247 }
160 248
161 249 fn iter_non_normal_paths(
162 250 &mut self,
163 251 ) -> Box<dyn Iterator<Item = &HgPathBuf> + Send + '_> {
164 252 todo!()
165 253 }
166 254
167 255 fn iter_non_normal_paths_panic(
168 256 &self,
169 257 ) -> Box<dyn Iterator<Item = &HgPathBuf> + Send + '_> {
170 258 todo!()
171 259 }
172 260
173 261 fn iter_other_parent_paths(
174 262 &mut self,
175 263 ) -> Box<dyn Iterator<Item = &HgPathBuf> + Send + '_> {
176 264 todo!()
177 265 }
178 266
179 267 fn has_tracked_dir(
180 268 &mut self,
181 269 _directory: &HgPath,
182 270 ) -> Result<bool, DirstateMapError> {
183 271 todo!()
184 272 }
185 273
186 274 fn has_dir(
187 275 &mut self,
188 276 _directory: &HgPath,
189 277 ) -> Result<bool, DirstateMapError> {
190 278 todo!()
191 279 }
192 280
193 281 fn parents(
194 282 &mut self,
195 283 file_contents: &[u8],
196 284 ) -> Result<&DirstateParents, DirstateError> {
197 285 if self.parents.is_none() {
198 286 let parents = if !file_contents.is_empty() {
199 287 parse_dirstate_parents(file_contents)?.clone()
200 288 } else {
201 289 DirstateParents {
202 290 p1: NULL_NODE,
203 291 p2: NULL_NODE,
204 292 }
205 293 };
206 294 self.parents = Some(parents);
207 295 }
208 296 Ok(self.parents.as_ref().unwrap())
209 297 }
210 298
211 299 fn set_parents(&mut self, parents: &DirstateParents) {
212 300 self.parents = Some(parents.clone());
213 301 self.dirty_parents = true;
214 302 }
215 303
216 304 fn read<'a>(
217 305 &mut self,
218 306 file_contents: &'a [u8],
219 307 ) -> Result<Option<&'a DirstateParents>, DirstateError> {
220 308 if file_contents.is_empty() {
221 309 return Ok(None);
222 310 }
223 311
224 312 let parents = parse_dirstate_entries(
225 313 file_contents,
226 314 |path, entry, copy_source| {
227 315 let node = self.get_or_insert_node(path);
228 316 node.entry = Some(*entry);
229 317 node.copy_source = copy_source.map(HgPath::to_owned);
230 318 },
231 319 )?;
232 320
233 321 if !self.dirty_parents {
234 322 self.set_parents(parents);
235 323 }
236 324
237 325 Ok(Some(parents))
238 326 }
239 327
240 328 fn pack(
241 329 &mut self,
242 330 _parents: DirstateParents,
243 331 _now: Duration,
244 332 ) -> Result<Vec<u8>, DirstateError> {
333 let _ = self.iter_node_data_mut();
245 334 todo!()
246 335 }
247 336
248 337 fn build_file_fold_map(&mut self) -> &FastHashMap<HgPathBuf, HgPathBuf> {
249 338 todo!()
250 339 }
251 340
252 341 fn set_all_dirs(&mut self) -> Result<(), DirstateMapError> {
253 342 todo!()
254 343 }
255 344
256 345 fn set_dirs(&mut self) -> Result<(), DirstateMapError> {
257 346 todo!()
258 347 }
259 348
260 349 fn status<'a>(
261 350 &'a self,
262 351 _matcher: &'a (dyn Matcher + Sync),
263 352 _root_dir: PathBuf,
264 353 _ignore_files: Vec<PathBuf>,
265 354 _options: StatusOptions,
266 355 ) -> Result<
267 356 (
268 357 (Vec<HgPathCow<'a>>, DirstateStatus<'a>),
269 358 Vec<PatternFileWarning>,
270 359 ),
271 360 StatusError,
272 361 > {
273 362 todo!()
274 363 }
275 364
276 365 fn copy_map_len(&self) -> usize {
277 366 todo!()
278 367 }
279 368
280 369 fn copy_map_iter(&self) -> CopyMapIter<'_> {
281 todo!()
370 Box::new(self.iter_nodes().filter_map(|(path, node)| {
371 node.copy_source
372 .as_ref()
373 .map(|copy_source| (path.full_path(), copy_source))
374 }))
282 375 }
283 376
284 377 fn copy_map_contains_key(&self, key: &HgPath) -> bool {
285 378 if let Some(node) = self.get_node(key) {
286 379 node.copy_source.is_some()
287 380 } else {
288 381 false
289 382 }
290 383 }
291 384
292 385 fn copy_map_get(&self, key: &HgPath) -> Option<&HgPathBuf> {
293 386 self.get_node(key)?.copy_source.as_ref()
294 387 }
295 388
296 389 fn copy_map_remove(&mut self, _key: &HgPath) -> Option<HgPathBuf> {
297 390 todo!()
298 391 }
299 392
300 393 fn copy_map_insert(
301 394 &mut self,
302 395 _key: HgPathBuf,
303 396 _value: HgPathBuf,
304 397 ) -> Option<HgPathBuf> {
305 398 todo!()
306 399 }
307 400
308 401 fn len(&self) -> usize {
309 402 todo!()
310 403 }
311 404
312 405 fn contains_key(&self, key: &HgPath) -> bool {
313 406 self.get(key).is_some()
314 407 }
315 408
316 409 fn get(&self, key: &HgPath) -> Option<&DirstateEntry> {
317 410 self.get_node(key)?.entry.as_ref()
318 411 }
319 412
320 413 fn iter(&self) -> StateMapIter<'_> {
321 todo!()
414 Box::new(self.iter_nodes().filter_map(|(path, node)| {
415 node.entry.as_ref().map(|entry| (path.full_path(), entry))
416 }))
322 417 }
323 418 }
General Comments 0
You need to be logged in to leave comments. Login now