swh_graph/utils/mod.rs
1/*
2 * Copyright (C) 2023-2024 The Software Heritage developers
3 * See the AUTHORS file at the top-level directory of this distribution
4 * License: GNU General Public License version 3, or any later version
5 * See top-level LICENSE file for more information
6 */
7
8use std::path::{Path, PathBuf};
9
10use anyhow::{anyhow, Context, Result};
11
12use crate::{NodeConstraint, NodeType};
13
14pub mod mmap;
15pub mod shuffle;
16pub mod sort;
17
18pub fn dir_size(path: &Path) -> Result<usize> {
19 Ok(std::fs::read_dir(path)
20 .with_context(|| format!("Could not list {}", path.display()))?
21 .map(|entry| {
22 entry
23 .unwrap_or_else(|e| panic!("Could not read {} entry: {:?}", path.display(), e))
24 .metadata()
25 .as_ref()
26 .unwrap_or_else(|e| panic!("Could not read {} entry: {:?}", path.display(), e))
27 .len() as usize
28 })
29 .sum::<usize>())
30}
31
32/// Appends a string to a path
33///
34/// ```
35/// # use std::path::{Path, PathBuf};
36/// # use swh_graph::utils::suffix_path;
37///
38/// assert_eq!(
39/// suffix_path(Path::new("/tmp/graph"), "-transposed"),
40/// Path::new("/tmp/graph-transposed").to_owned()
41/// );
42/// ```
43#[inline(always)]
44pub fn suffix_path<P: AsRef<Path>, S: AsRef<std::ffi::OsStr>>(path: P, suffix: S) -> PathBuf {
45 let mut path = path.as_ref().as_os_str().to_owned();
46 path.push(suffix);
47 path.into()
48}
49
50/// Given a string like `*` or `cnt,dir,rev,rel,snp,ori`, returns a list of `NodeType`
51/// matching the string.
52///
53/// # Examples
54///
55/// ```
56/// # use swh_graph::NodeType;
57/// use swh_graph::utils::parse_allowed_node_types;
58///
59/// assert_eq!(parse_allowed_node_types("*").unwrap(), NodeType::all());
60/// assert_eq!(parse_allowed_node_types("cnt").unwrap(), vec![NodeType::Content]);
61/// assert_eq!(
62/// parse_allowed_node_types("rel,rev").unwrap(),
63/// vec![NodeType::Release, NodeType::Revision]
64/// );
65/// ```
66//
67// TODO make this return a NodeConstraint instead
68pub fn parse_allowed_node_types(s: &str) -> Result<Vec<NodeType>> {
69 s.parse::<NodeConstraint>()
70 .map_err(|s| anyhow!("Could not parse --allowed-node-types {s}"))
71 .map(|constr| constr.to_vec())
72}
73
74#[allow(clippy::len_without_is_empty)]
75pub trait GetIndex {
76 type Output;
77
78 /// Returns the total number of items in the collections
79 fn len(&self) -> usize;
80
81 /// Returns an item of the collection
82 fn get(&self, index: usize) -> Option<Self::Output>;
83
84 /// Returns an item of the collection
85 ///
86 /// # Safety
87 ///
88 /// Undefined behavior if the index is past the end of the collection.
89 unsafe fn get_unchecked(&self, index: usize) -> Self::Output;
90}
91
92impl<Item: Clone, T: std::ops::Deref<Target = [Item]>> GetIndex for T {
93 type Output = Item;
94
95 fn len(&self) -> usize {
96 <[Item]>::len(self)
97 }
98
99 fn get(&self, index: usize) -> Option<Self::Output> {
100 <[Item]>::get(self, index).cloned()
101 }
102
103 unsafe fn get_unchecked(&self, index: usize) -> Self::Output {
104 <[Item]>::get_unchecked(self, index).clone()
105 }
106}