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