linkspace/commons/
prune_replacable.rs

1use crate::prelude::*;
2use std::collections::HashMap;
3// TODO: impl prune_space
4pub fn lkc_prune_log() -> anyhow::Result<Vec<LkHash>> {
5    let mut removing = vec![];
6    let space = TryAsSpace::try_as_space("[f:prune]:[#:0]", &())?;
7
8    let at = system::lks_last();
9    let prune_point = lk_linkpoint(&space, &at.0, &[])?.with_xheader(&XFlags::REPLACABLE);
10    lks_save(&prune_point)?;
11    let mut last_pruning = Stamp::ZERO;
12
13    let mut latest: HashMap<Box<[u8]>, (Stamp, LkHash)> = HashMap::new();
14
15    lks_scan(&lkq_new(&":scan log-desc", &())?, &mut |point| {
16        if last_pruning >= point.get_recv() {
17            return true;
18        }
19        if !point.xheader_ref().xflags.contains(XFlags::REPLACABLE) {
20            return false;
21        }
22
23        // We did not process - so the new prune point should not be seen yet
24        if *point.get_domain() == space.domain
25            && *point.get_group() == space.group
26            && point.get_path() == &*space.path
27        {
28            if let Ok(stamp) = point.data().try_into() {
29                last_pruning = stamp;
30            }
31        }
32        let new_entry = (point.get_recv(), point.hash());
33        let key = point.get_pathsegm().raw_segment_bytes();
34        match latest.get_mut(key) {
35            Some(occupied) => {
36                if *occupied > new_entry {
37                    removing.push(new_entry.1);
38                } else {
39                    removing.push(occupied.1);
40                    *occupied = new_entry;
41                }
42            }
43            None => {
44                latest.insert(key.to_vec().into_boxed_slice(), new_entry);
45            }
46        }
47
48        false
49    })?;
50    Ok(removing)
51}
52
53/*
54pub fn lkc_prune_watch(space: &dyn TryAsSpace)  -> anyhow::Result<()>{
55    let mut query = crate::query::lkq_from_space(space, &())?;
56    crate::query::lkq_add_mut(&mut query,
57                r#"
58:watch all
59xflags 1 [xflag:REPLACABLE]"#,
60                &()
61)?;
62    system::lks_watch(query, move |point: &dyn Point,_watch: Option<&[u8]>| -> anyhow::Result<()>{
63        tracing::trace!(?point,"todo");
64        Ok(())
65    })
66}
67*/