ostrya 0.14.0

Pure-Rust async reimplementation of the ostree repository library
Documentation
//! The subpaths that select the parts of a commit tree that a pull from a
//! remote fetches.
//!
//! [`PullOptions::subpaths`](crate::PullOptions::subpaths) states the rules of
//! a value. The walk descends from the root dirtree along each path, one
//! component at a time.
//!
//! The walk of each dirtree has a [`Scope`]. The scope lists each path position
//! that reaches the dirtree: the index of the path and the number of components
//! that the walk matched. If the walk reaches one dirtree checksum at two
//! positions, it walks that dirtree under the union of the two scopes.

use std::collections::HashMap;

use ostrya_core::{DirTree, ObjectName, ObjectType};

use crate::error::{Error, Result};

/// The parsed subpath values of one pull, each split into its components.
#[derive(Clone)]
pub(crate) struct Subpaths {
    paths: Vec<Vec<String>>,
}

impl Subpaths {
    /// Parses the values of [`PullOptions::subpaths`](crate::PullOptions::subpaths).
    ///
    /// If the list is empty, returns `None`, and the pull fetches the whole
    /// tree.
    ///
    /// # Errors
    ///
    /// - [`Error::Pull`] if a value does not start with `/`. The empty value is
    ///   such a value.
    /// - [`Error::Pull`] if the list holds more than `u32::MAX` values.
    pub(crate) fn parse(values: &[String]) -> Result<Option<Subpaths>> {
        if values.is_empty() {
            return Ok(None);
        }
        let mut paths = Vec::with_capacity(values.len());
        for value in values {
            let Some(rest) = value.strip_prefix('/') else {
                return Err(Error::Pull(format!(
                    "subpath '{value}' is not an absolute path"
                )));
            };
            paths.push(rest.split('/').map(str::to_owned).collect());
        }
        if u32::try_from(paths.len()).is_err() {
            return Err(Error::Pull("too many subpaths".into()));
        }
        Ok(Some(Subpaths { paths }))
    }

    /// Returns the scope of the walk of the root dirtree of each commit.
    ///
    /// The scope holds each path at its first component.
    pub(crate) fn root(&self) -> Scope {
        // `parse` bounds the count by `u32`.
        Scope::Along((0..self.paths.len() as u32).map(|i| (i, 0)).collect())
    }

    /// Returns the objects that `dirtree` references and that a walk under
    /// `scope` fetches.
    ///
    /// Each object comes with a scope. The walk of a dirtree object uses this
    /// scope. If several positions name one entry, the list holds the entry
    /// once, under the union of their scopes.
    pub(crate) fn children(&self, dirtree: &DirTree, scope: &Scope) -> Vec<(ObjectName, Scope)> {
        let Scope::Along(positions) = scope else {
            return all_children(dirtree);
        };
        let mut out: Vec<(ObjectName, Scope)> = Vec::new();
        let mut index: HashMap<ObjectName, usize> = HashMap::new();
        let mut add = |name: ObjectName, scope: Scope| match index.get(&name) {
            Some(&at) => match (&mut out[at].1, scope) {
                (Scope::All, _) => {}
                (held, Scope::All) => *held = Scope::All,
                // The loop at the end sorts and deduplicates each list once.
                (Scope::Along(held), Scope::Along(more)) => held.extend(more),
            },
            None => {
                index.insert(name, out.len());
                out.push((name, scope));
            }
        };
        for &(path, matched) in positions {
            let components = &self.paths[path as usize];
            let Some(component) = components.get(matched as usize) else {
                continue;
            };
            let last = matched as usize + 1 == components.len();
            // The dirtree parse checks that both lists are sorted by name.
            if let Ok(at) = dirtree
                .files
                .binary_search_by(|(name, _)| name.as_str().cmp(component))
            {
                if last {
                    add(
                        ObjectName::new(dirtree.files[at].1, ObjectType::File),
                        Scope::All,
                    );
                }
                continue;
            }
            if let Ok(at) = dirtree
                .dirs
                .binary_search_by(|(name, _, _)| name.as_str().cmp(component))
            {
                let (_, tree, meta) = &dirtree.dirs[at];
                let scope = if last {
                    Scope::All
                } else {
                    Scope::Along(vec![(path, matched + 1)])
                };
                add(ObjectName::new(*meta, ObjectType::DirMeta), Scope::All);
                add(ObjectName::new(*tree, ObjectType::DirTree), scope);
            }
        }
        for (_, scope) in &mut out {
            if let Scope::Along(positions) = scope {
                positions.sort_unstable();
                positions.dedup();
            }
        }
        out
    }
}

/// The progress of the walk of one dirtree along the subpaths.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) enum Scope {
    /// The walk fetches everything under the dirtree.
    #[default]
    All,
    /// The path positions that reach the dirtree.
    ///
    /// Each position is the index of a path and the number of its components
    /// that the walk matched. The list is sorted and holds no duplicate.
    Along(Vec<(u32, u32)>),
}

impl Scope {
    /// Returns `true` if a walk under `self` fetches each object that a walk
    /// under `other` fetches.
    pub(crate) fn covers(&self, other: &Scope) -> bool {
        match (self, other) {
            (Scope::All, _) => true,
            (Scope::Along(_), Scope::All) => false,
            (Scope::Along(mine), Scope::Along(theirs)) => {
                theirs.iter().all(|p| mine.binary_search(p).is_ok())
            }
        }
    }

    /// Widens `self` so that it also covers `other`.
    pub(crate) fn widen(&mut self, other: &Scope) {
        match (&mut *self, other) {
            (Scope::All, _) => {}
            (_, Scope::All) => *self = Scope::All,
            (Scope::Along(mine), Scope::Along(theirs)) => {
                // A merge of the two sorted lists, linear in their lengths.
                let mut merged = Vec::with_capacity(mine.len() + theirs.len());
                let (mut a, mut b) = (mine.iter().peekable(), theirs.iter().peekable());
                while let (Some(&&x), Some(&&y)) = (a.peek(), b.peek()) {
                    merged.push(x.min(y));
                    if x <= y {
                        a.next();
                    }
                    if y <= x {
                        b.next();
                    }
                }
                merged.extend(a);
                merged.extend(b);
                *mine = merged;
            }
        }
    }
}

/// Returns each object that a dirtree references, with the scope [`Scope::All`].
///
/// The objects are the files, and the dirmeta and the dirtree of each
/// subdirectory.
fn all_children(dirtree: &DirTree) -> Vec<(ObjectName, Scope)> {
    let mut out = Vec::with_capacity(dirtree.files.len() + 2 * dirtree.dirs.len());
    for (_, file) in &dirtree.files {
        out.push((ObjectName::new(*file, ObjectType::File), Scope::All));
    }
    for (_, subtree, submeta) in &dirtree.dirs {
        out.push((ObjectName::new(*submeta, ObjectType::DirMeta), Scope::All));
        out.push((ObjectName::new(*subtree, ObjectType::DirTree), Scope::All));
    }
    out
}

#[cfg(test)]
mod tests {
    use ostrya_core::Checksum;

    use super::*;

    fn cs(byte: u8) -> Checksum {
        Checksum::from_bytes([byte; 32])
    }

    fn parse(values: &[&str]) -> Subpaths {
        let values: Vec<String> = values.iter().map(|v| (*v).to_owned()).collect();
        Subpaths::parse(&values).unwrap().unwrap()
    }

    /// Returns a root that holds the file `a` (1), the directory `sub` (dirtree
    /// 2, dirmeta 3), and the directory `other` (dirtree 4, dirmeta 5).
    fn root() -> DirTree {
        DirTree {
            files: vec![("a".into(), cs(1))],
            dirs: vec![("other".into(), cs(4), cs(5)), ("sub".into(), cs(2), cs(3))],
        }
    }

    fn file(b: u8) -> ObjectName {
        ObjectName::new(cs(b), ObjectType::File)
    }
    fn tree(b: u8) -> ObjectName {
        ObjectName::new(cs(b), ObjectType::DirTree)
    }
    fn meta(b: u8) -> ObjectName {
        ObjectName::new(cs(b), ObjectType::DirMeta)
    }

    #[test]
    fn parse_refuses_a_relative_and_an_empty_value() {
        for value in ["sub", "", "sub/"] {
            let err = Subpaths::parse(&[value.to_owned()]).err().unwrap();
            assert!(matches!(err, Error::Pull(_)), "{value}: {err}");
        }
        assert!(Subpaths::parse(&[]).unwrap().is_none());
    }

    #[test]
    fn parse_keeps_every_component() {
        assert_eq!(parse(&["/"]).paths, vec![vec![String::new()]]);
        assert_eq!(parse(&["/sub"]).paths, vec![vec!["sub".to_owned()]]);
        assert_eq!(
            parse(&["/sub/"]).paths,
            vec![vec!["sub".to_owned(), String::new()]]
        );
        assert_eq!(
            parse(&["//sub"]).paths,
            vec![vec![String::new(), "sub".to_owned()]]
        );
    }

    #[test]
    fn a_directory_path_fetches_it_whole_and_no_sibling() {
        let paths = parse(&["/sub"]);
        assert_eq!(
            paths.children(&root(), &paths.root()),
            vec![(meta(3), Scope::All), (tree(2), Scope::All)]
        );
    }

    #[test]
    fn a_path_through_a_directory_walks_it_under_the_next_component() {
        let paths = parse(&["/sub/deeper"]);
        assert_eq!(
            paths.children(&root(), &paths.root()),
            vec![(meta(3), Scope::All), (tree(2), Scope::Along(vec![(0, 1)]))]
        );
    }

    #[test]
    fn a_file_is_fetched_only_as_the_last_component() {
        let paths = parse(&["/a"]);
        assert_eq!(
            paths.children(&root(), &paths.root()),
            vec![(file(1), Scope::All)]
        );
        for value in ["/a/", "/a/x"] {
            let paths = parse(&[value]);
            assert!(paths.children(&root(), &paths.root()).is_empty(), "{value}");
        }
    }

    #[test]
    fn names_the_dirtree_cannot_hold_match_nothing() {
        for value in ["/", "//sub", "/./sub", "/../sub", "/nonexist"] {
            let paths = parse(&[value]);
            assert!(paths.children(&root(), &paths.root()).is_empty(), "{value}");
        }
    }

    #[test]
    fn several_paths_take_the_union() {
        let paths = parse(&["/sub/x", "/a", "/sub"]);
        assert_eq!(
            paths.children(&root(), &paths.root()),
            vec![
                (meta(3), Scope::All),
                (tree(2), Scope::All),
                (file(1), Scope::All),
            ]
        );
    }

    #[test]
    fn a_walk_under_all_fetches_everything() {
        let paths = parse(&["/sub"]);
        assert_eq!(
            paths.children(&root(), &Scope::All),
            vec![
                (file(1), Scope::All),
                (meta(5), Scope::All),
                (tree(4), Scope::All),
                (meta(3), Scope::All),
                (tree(2), Scope::All),
            ]
        );
    }

    #[test]
    fn many_values_under_one_directory_merge_in_near_linear_time() {
        let count = 20_000u32;
        let values: Vec<String> = (0..count).map(|i| format!("/sub/f{i:05}")).collect();
        let paths = Subpaths::parse(&values).unwrap().unwrap();
        let children = paths.children(&root(), &paths.root());
        let expected: Vec<(u32, u32)> = (0..count).map(|i| (i, 1)).collect();
        assert_eq!(
            children,
            vec![
                (meta(3), Scope::All),
                (tree(2), Scope::Along(expected.clone()))
            ]
        );
        let sub = DirTree {
            files: (0..count)
                .map(|i| (format!("f{i:05}"), cs((i % 200) as u8)))
                .collect(),
            dirs: Vec::new(),
        };
        let files = paths.children(&sub, &Scope::Along(expected));
        // 200 distinct file objects, each named by 100 values.
        assert_eq!(files.len(), 200);
        assert!(files.iter().all(|(_, scope)| *scope == Scope::All));
        let mut a = Scope::Along((0..count).step_by(2).map(|i| (i, 0)).collect());
        a.widen(&Scope::Along((0..count).map(|i| (i, 0)).collect()));
        assert_eq!(a, Scope::Along((0..count).map(|i| (i, 0)).collect()));
    }

    #[test]
    fn covers_and_widen() {
        let mut a = Scope::Along(vec![(0, 1)]);
        let b = Scope::Along(vec![(1, 2)]);
        assert!(!a.covers(&b));
        a.widen(&b);
        assert_eq!(a, Scope::Along(vec![(0, 1), (1, 2)]));
        assert!(a.covers(&b));
        assert!(!a.covers(&Scope::All));
        a.widen(&Scope::All);
        assert_eq!(a, Scope::All);
        assert!(a.covers(&b));
    }
}