| 1 |
// dirs_multiset.rs |
| 2 |
// |
| 3 |
// Copyright 2019 Raphaël Gomès <[email protected]> |
| 4 |
// |
| 5 |
// This software may be used and distributed according to the terms of the |
| 6 |
// GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
//! A multiset of directory names. |
| 9 |
//! |
| 10 |
//! Used to counts the references to directories in a manifest or dirstate. |
| 11 |
use std::collections::hash_map; |
| 12 |
use std::collections::hash_map::Entry; |
| 13 |
use std::collections::HashMap; |
| 14 |
use std::collections::HashSet; |
| 15 |
|
| 16 |
use super::entry::DirstateEntry; |
| 17 |
use super::DirstateError; |
| 18 |
use super::DirstateMapError; |
| 19 |
use crate::dirstate::on_disk::DirstateV2ParseError; |
| 20 |
use crate::utils::files; |
| 21 |
use crate::utils::hg_path::HgPath; |
| 22 |
use crate::utils::hg_path::HgPathBuf; |
| 23 |
use crate::utils::hg_path::HgPathError; |
| 24 |
use crate::utils::hg_path::HgPathErrorKind; |
| 25 |
use crate::FastHashMap; |
| 26 |
|
| 27 |
// could be encapsulated if we care API stability more seriously |
| 28 |
pub type DirsMultisetIter<'a> = hash_map::Keys<'a, HgPathBuf, u32>; |
| 29 |
|
| 30 |
#[derive(PartialEq, Debug)] |
| 31 |
pub struct DirsMultiset { |
| 32 |
inner: FastHashMap<HgPathBuf, u32>, |
| 33 |
} |
| 34 |
|
| 35 |
impl DirsMultiset { |
| 36 |
/// Initializes the multiset from a dirstate. |
| 37 |
/// |
| 38 |
/// If `skip_state` is provided, skips dirstate entries with equal state. |
| 39 |
pub fn from_dirstate<I, P>( |
| 40 |
dirstate: I, |
| 41 |
only_tracked: bool, |
| 42 |
) -> Result<Self, DirstateError> |
| 43 |
where |
| 44 |
I: IntoIterator< |
| 45 |
Item = Result<(P, DirstateEntry), DirstateV2ParseError>, |
| 46 |
>, |
| 47 |
P: AsRef<HgPath>, |
| 48 |
{ |
| 49 |
let mut multiset = DirsMultiset { inner: FastHashMap::default() }; |
| 50 |
for item in dirstate { |
| 51 |
let (filename, entry) = item?; |
| 52 |
let filename = filename.as_ref(); |
| 53 |
// This `if` is optimized out of the loop |
| 54 |
if only_tracked { |
| 55 |
if !entry.removed() { |
| 56 |
multiset.add_path(filename)?; |
| 57 |
} |
| 58 |
} else { |
| 59 |
multiset.add_path(filename)?; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
Ok(multiset) |
| 64 |
} |
| 65 |
|
| 66 |
/// Initializes the multiset from a manifest. |
| 67 |
pub fn from_manifest( |
| 68 |
manifest: &[impl AsRef<HgPath>], |
| 69 |
) -> Result<Self, HgPathError> { |
| 70 |
let mut multiset = DirsMultiset { inner: FastHashMap::default() }; |
| 71 |
|
| 72 |
for filename in manifest { |
| 73 |
multiset.add_path(filename.as_ref())?; |
| 74 |
} |
| 75 |
|
| 76 |
Ok(multiset) |
| 77 |
} |
| 78 |
|
| 79 |
/// Increases the count of deepest directory contained in the path. |
| 80 |
/// |
| 81 |
/// If the directory is not yet in the map, adds its parents. |
| 82 |
pub fn add_path( |
| 83 |
&mut self, |
| 84 |
path: impl AsRef<HgPath>, |
| 85 |
) -> Result<(), HgPathError> { |
| 86 |
for subpath in files::find_dirs(path.as_ref()) { |
| 87 |
if subpath.as_bytes().last() == Some(&b'/') { |
| 88 |
// TODO Remove this once PathAuditor is certified |
| 89 |
// as the only entrypoint for path data |
| 90 |
let second_slash_index = subpath.len() - 1; |
| 91 |
|
| 92 |
return Err(HgPathErrorKind::ConsecutiveSlashes { |
| 93 |
bytes: path.as_ref().as_bytes().to_owned(), |
| 94 |
second_slash_index, |
| 95 |
} |
| 96 |
.into()); |
| 97 |
} |
| 98 |
if let Some(val) = self.inner.get_mut(subpath) { |
| 99 |
*val += 1; |
| 100 |
break; |
| 101 |
} |
| 102 |
self.inner.insert(subpath.to_owned(), 1); |
| 103 |
} |
| 104 |
Ok(()) |
| 105 |
} |
| 106 |
|
| 107 |
/// Decreases the count of deepest directory contained in the path. |
| 108 |
/// |
| 109 |
/// If it is the only reference, decreases all parents until one is |
| 110 |
/// removed. |
| 111 |
/// If the directory is not in the map, something horrible has happened. |
| 112 |
pub fn delete_path( |
| 113 |
&mut self, |
| 114 |
path: impl AsRef<HgPath>, |
| 115 |
) -> Result<(), DirstateMapError> { |
| 116 |
for subpath in files::find_dirs(path.as_ref()) { |
| 117 |
match self.inner.entry(subpath.to_owned()) { |
| 118 |
Entry::Occupied(mut entry) => { |
| 119 |
let val = *entry.get(); |
| 120 |
if val > 1 { |
| 121 |
entry.insert(val - 1); |
| 122 |
break; |
| 123 |
} |
| 124 |
entry.remove(); |
| 125 |
} |
| 126 |
Entry::Vacant(_) => { |
| 127 |
return Err(DirstateMapError::PathNotFound( |
| 128 |
path.as_ref().to_owned(), |
| 129 |
)) |
| 130 |
} |
| 131 |
}; |
| 132 |
} |
| 133 |
|
| 134 |
Ok(()) |
| 135 |
} |
| 136 |
|
| 137 |
pub fn contains(&self, key: impl AsRef<HgPath>) -> bool { |
| 138 |
self.inner.contains_key(key.as_ref()) |
| 139 |
} |
| 140 |
|
| 141 |
pub fn iter(&self) -> DirsMultisetIter { |
| 142 |
self.inner.keys() |
| 143 |
} |
| 144 |
|
| 145 |
pub fn len(&self) -> usize { |
| 146 |
self.inner.len() |
| 147 |
} |
| 148 |
|
| 149 |
pub fn is_empty(&self) -> bool { |
| 150 |
self.len() == 0 |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/// This is basically a reimplementation of `DirsMultiset` that stores the |
| 155 |
/// children instead of just a count of them, plus a small optional |
| 156 |
/// optimization to avoid some directories we don't need. |
| 157 |
#[derive(PartialEq, Debug)] |
| 158 |
pub struct DirsChildrenMultiset<'a> { |
| 159 |
inner: FastHashMap<&'a HgPath, HashSet<&'a HgPath>>, |
| 160 |
only_include: Option<HashSet<&'a HgPath>>, |
| 161 |
} |
| 162 |
|
| 163 |
impl<'a> DirsChildrenMultiset<'a> { |
| 164 |
pub fn new<I: Iterator<Item = &'a HgPathBuf>>( |
| 165 |
paths: impl Iterator<Item = &'a HgPathBuf>, |
| 166 |
only_include: Option<I>, |
| 167 |
) -> Self { |
| 168 |
let mut new = Self { |
| 169 |
inner: HashMap::default(), |
| 170 |
only_include: only_include.map(|s| s.map(AsRef::as_ref).collect()), |
| 171 |
}; |
| 172 |
|
| 173 |
for path in paths { |
| 174 |
new.add_path(path) |
| 175 |
} |
| 176 |
|
| 177 |
new |
| 178 |
} |
| 179 |
fn add_path(&mut self, path: &'a (impl AsRef<HgPath> + 'a)) { |
| 180 |
if path.as_ref().is_empty() { |
| 181 |
return; |
| 182 |
} |
| 183 |
for (directory, basename) in files::find_dirs_with_base(path.as_ref()) { |
| 184 |
if !self.is_dir_included(directory) { |
| 185 |
continue; |
| 186 |
} |
| 187 |
self.inner |
| 188 |
.entry(directory) |
| 189 |
.and_modify(|e| { |
| 190 |
e.insert(basename); |
| 191 |
}) |
| 192 |
.or_insert_with(|| { |
| 193 |
let mut set = HashSet::new(); |
| 194 |
set.insert(basename); |
| 195 |
set |
| 196 |
}); |
| 197 |
} |
| 198 |
} |
| 199 |
fn is_dir_included(&self, dir: impl AsRef<HgPath>) -> bool { |
| 200 |
match &self.only_include { |
| 201 |
None => false, |
| 202 |
Some(i) => i.contains(dir.as_ref()), |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
pub fn get( |
| 207 |
&self, |
| 208 |
path: impl AsRef<HgPath>, |
| 209 |
) -> Option<&HashSet<&'a HgPath>> { |
| 210 |
self.inner.get(path.as_ref()) |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
#[cfg(test)] |
| 215 |
mod tests { |
| 216 |
use super::*; |
| 217 |
use crate::dirstate::entry::EntryState; |
| 218 |
|
| 219 |
#[test] |
| 220 |
fn test_delete_path_path_not_found() { |
| 221 |
let manifest: Vec<HgPathBuf> = vec![]; |
| 222 |
let mut map = DirsMultiset::from_manifest(&manifest).unwrap(); |
| 223 |
let path = HgPathBuf::from_bytes(b"doesnotexist/"); |
| 224 |
assert_eq!( |
| 225 |
Err(DirstateMapError::PathNotFound(path.to_owned())), |
| 226 |
map.delete_path(&path) |
| 227 |
); |
| 228 |
} |
| 229 |
|
| 230 |
#[test] |
| 231 |
fn test_delete_path_empty_path() { |
| 232 |
let mut map = DirsMultiset::from_manifest(&[HgPathBuf::new()]).unwrap(); |
| 233 |
let path = HgPath::new(b""); |
| 234 |
assert_eq!(Ok(()), map.delete_path(path)); |
| 235 |
assert_eq!( |
| 236 |
Err(DirstateMapError::PathNotFound(path.to_owned())), |
| 237 |
map.delete_path(path) |
| 238 |
); |
| 239 |
} |
| 240 |
|
| 241 |
#[test] |
| 242 |
fn test_delete_path_successful() { |
| 243 |
let mut map = DirsMultiset { |
| 244 |
inner: [("", 5), ("a", 3), ("a/b", 2), ("a/c", 1)] |
| 245 |
.iter() |
| 246 |
.map(|(k, v)| (HgPathBuf::from_bytes(k.as_bytes()), *v)) |
| 247 |
.collect(), |
| 248 |
}; |
| 249 |
|
| 250 |
assert_eq!(Ok(()), map.delete_path(HgPath::new(b"a/b/"))); |
| 251 |
eprintln!("{:?}", map); |
| 252 |
assert_eq!(Ok(()), map.delete_path(HgPath::new(b"a/b/"))); |
| 253 |
eprintln!("{:?}", map); |
| 254 |
assert_eq!( |
| 255 |
Err(DirstateMapError::PathNotFound(HgPathBuf::from_bytes(b"a/b/"))), |
| 256 |
map.delete_path(HgPath::new(b"a/b/")) |
| 257 |
); |
| 258 |
|
| 259 |
assert_eq!(2, *map.inner.get(HgPath::new(b"a")).unwrap()); |
| 260 |
assert_eq!(1, *map.inner.get(HgPath::new(b"a/c")).unwrap()); |
| 261 |
eprintln!("{:?}", map); |
| 262 |
assert_eq!(Ok(()), map.delete_path(HgPath::new(b"a/"))); |
| 263 |
eprintln!("{:?}", map); |
| 264 |
|
| 265 |
assert_eq!(Ok(()), map.delete_path(HgPath::new(b"a/c/"))); |
| 266 |
assert_eq!( |
| 267 |
Err(DirstateMapError::PathNotFound(HgPathBuf::from_bytes(b"a/c/"))), |
| 268 |
map.delete_path(HgPath::new(b"a/c/")) |
| 269 |
); |
| 270 |
} |
| 271 |
|
| 272 |
#[test] |
| 273 |
fn test_add_path_empty_path() { |
| 274 |
let manifest: Vec<HgPathBuf> = vec![]; |
| 275 |
let mut map = DirsMultiset::from_manifest(&manifest).unwrap(); |
| 276 |
let path = HgPath::new(b""); |
| 277 |
map.add_path(path).unwrap(); |
| 278 |
|
| 279 |
assert_eq!(1, map.len()); |
| 280 |
} |
| 281 |
|
| 282 |
#[test] |
| 283 |
fn test_add_path_successful() { |
| 284 |
let manifest: Vec<HgPathBuf> = vec![]; |
| 285 |
let mut map = DirsMultiset::from_manifest(&manifest).unwrap(); |
| 286 |
|
| 287 |
map.add_path(HgPath::new(b"a/")).unwrap(); |
| 288 |
assert_eq!(1, *map.inner.get(HgPath::new(b"a")).unwrap()); |
| 289 |
assert_eq!(1, *map.inner.get(HgPath::new(b"")).unwrap()); |
| 290 |
assert_eq!(2, map.len()); |
| 291 |
|
| 292 |
// Non directory should be ignored |
| 293 |
map.add_path(HgPath::new(b"a")).unwrap(); |
| 294 |
assert_eq!(1, *map.inner.get(HgPath::new(b"a")).unwrap()); |
| 295 |
assert_eq!(2, map.len()); |
| 296 |
|
| 297 |
// Non directory will still add its base |
| 298 |
map.add_path(HgPath::new(b"a/b")).unwrap(); |
| 299 |
assert_eq!(2, *map.inner.get(HgPath::new(b"a")).unwrap()); |
| 300 |
assert_eq!(2, map.len()); |
| 301 |
|
| 302 |
// Duplicate path works |
| 303 |
map.add_path(HgPath::new(b"a/")).unwrap(); |
| 304 |
assert_eq!(3, *map.inner.get(HgPath::new(b"a")).unwrap()); |
| 305 |
|
| 306 |
// Nested dir adds to its base |
| 307 |
map.add_path(HgPath::new(b"a/b/")).unwrap(); |
| 308 |
assert_eq!(4, *map.inner.get(HgPath::new(b"a")).unwrap()); |
| 309 |
assert_eq!(1, *map.inner.get(HgPath::new(b"a/b")).unwrap()); |
| 310 |
|
| 311 |
// but not its base's base, because it already existed |
| 312 |
map.add_path(HgPath::new(b"a/b/c/")).unwrap(); |
| 313 |
assert_eq!(4, *map.inner.get(HgPath::new(b"a")).unwrap()); |
| 314 |
assert_eq!(2, *map.inner.get(HgPath::new(b"a/b")).unwrap()); |
| 315 |
|
| 316 |
map.add_path(HgPath::new(b"a/c/")).unwrap(); |
| 317 |
assert_eq!(1, *map.inner.get(HgPath::new(b"a/c")).unwrap()); |
| 318 |
|
| 319 |
let expected = DirsMultiset { |
| 320 |
inner: [("", 2), ("a", 5), ("a/b", 2), ("a/b/c", 1), ("a/c", 1)] |
| 321 |
.iter() |
| 322 |
.map(|(k, v)| (HgPathBuf::from_bytes(k.as_bytes()), *v)) |
| 323 |
.collect(), |
| 324 |
}; |
| 325 |
assert_eq!(map, expected); |
| 326 |
} |
| 327 |
|
| 328 |
#[test] |
| 329 |
fn test_dirsmultiset_new_empty() { |
| 330 |
let manifest: Vec<HgPathBuf> = vec![]; |
| 331 |
let new = DirsMultiset::from_manifest(&manifest).unwrap(); |
| 332 |
let expected = DirsMultiset { inner: FastHashMap::default() }; |
| 333 |
assert_eq!(expected, new); |
| 334 |
|
| 335 |
let new = DirsMultiset::from_dirstate::<_, HgPathBuf>( |
| 336 |
std::iter::empty(), |
| 337 |
false, |
| 338 |
) |
| 339 |
.unwrap(); |
| 340 |
let expected = DirsMultiset { inner: FastHashMap::default() }; |
| 341 |
assert_eq!(expected, new); |
| 342 |
} |
| 343 |
|
| 344 |
#[test] |
| 345 |
fn test_dirsmultiset_new_no_skip() { |
| 346 |
let input_vec: Vec<HgPathBuf> = ["a/", "b/", "a/c", "a/d/"] |
| 347 |
.iter() |
| 348 |
.map(|e| HgPathBuf::from_bytes(e.as_bytes())) |
| 349 |
.collect(); |
| 350 |
let expected_inner = [("", 2), ("a", 3), ("b", 1), ("a/d", 1)] |
| 351 |
.iter() |
| 352 |
.map(|(k, v)| (HgPathBuf::from_bytes(k.as_bytes()), *v)) |
| 353 |
.collect(); |
| 354 |
|
| 355 |
let new = DirsMultiset::from_manifest(&input_vec).unwrap(); |
| 356 |
let expected = DirsMultiset { inner: expected_inner }; |
| 357 |
assert_eq!(expected, new); |
| 358 |
|
| 359 |
let input_map = ["b/x", "a/c", "a/d/x"].iter().map(|f| { |
| 360 |
Ok(( |
| 361 |
HgPathBuf::from_bytes(f.as_bytes()), |
| 362 |
DirstateEntry::from_v1_data(EntryState::Normal, 0, 0, 0), |
| 363 |
)) |
| 364 |
}); |
| 365 |
let expected_inner = [("", 2), ("a", 2), ("b", 1), ("a/d", 1)] |
| 366 |
.iter() |
| 367 |
.map(|(k, v)| (HgPathBuf::from_bytes(k.as_bytes()), *v)) |
| 368 |
.collect(); |
| 369 |
|
| 370 |
let new = DirsMultiset::from_dirstate(input_map, false).unwrap(); |
| 371 |
let expected = DirsMultiset { inner: expected_inner }; |
| 372 |
assert_eq!(expected, new); |
| 373 |
} |
| 374 |
|
| 375 |
#[test] |
| 376 |
fn test_dirsmultiset_new_skip() { |
| 377 |
let input_map = [ |
| 378 |
("a/", EntryState::Normal), |
| 379 |
("a/b", EntryState::Normal), |
| 380 |
("a/c", EntryState::Removed), |
| 381 |
("a/d", EntryState::Merged), |
| 382 |
] |
| 383 |
.iter() |
| 384 |
.map(|(f, state)| { |
| 385 |
Ok(( |
| 386 |
HgPathBuf::from_bytes(f.as_bytes()), |
| 387 |
DirstateEntry::from_v1_data(*state, 0, 0, 0), |
| 388 |
)) |
| 389 |
}); |
| 390 |
|
| 391 |
// "a" incremented with "a/c" and "a/d/" |
| 392 |
let expected_inner = [("", 1), ("a", 3)] |
| 393 |
.iter() |
| 394 |
.map(|(k, v)| (HgPathBuf::from_bytes(k.as_bytes()), *v)) |
| 395 |
.collect(); |
| 396 |
|
| 397 |
let new = DirsMultiset::from_dirstate(input_map, true).unwrap(); |
| 398 |
let expected = DirsMultiset { inner: expected_inner }; |
| 399 |
assert_eq!(expected, new); |
| 400 |
} |
| 401 |
} |