| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | | use super::*; |
| 11 | | use crate::fasthash::FastMap; |
| 12 | | use crate::ids::NodeId; |
| 13 | |
|
| 14 | |
|
| 15 | | pub(crate) struct Touched { |
| 16 | | nodes: FastMap<ChangeHash, Box<[NodeId]>>, |
| 17 | | |
| 18 | | |
| 19 | | transient: FastMap<ChangeHash, Box<[NodeId]>>, |
| 20 | | added: usize, |
| 21 | | path: PathBuf, |
| 22 | | } |
| 23 | |
|
| 24 | | impl Touched { |
| 25 | | |
| 26 | | |
| 27 | 49× | pub(crate) fn of(&mut self, repo: &Repo, h: &ChangeHash) -> Option<&[NodeId]> { |
| 28 | 49× | if !self.nodes.contains_key(h) && !self.transient.contains_key(h) { |
| 29 | | |
| 30 | 0× | let (c, _) = repo.store.get_change_lazy(h).ok()?; |
| 31 | 0× | self.keep(*h, &c); |
| 32 | 49× | } |
| 33 | 49× | self.nodes |
| 34 | 49× | .get(h) |
| 35 | 49× | .or_else(|| self.transient.get(h)) |
| 36 | 49× | .map(|n| &n[..]) |
| 37 | 49× | } |
| 38 | |
|
| 39 | 29× | fn keep(&mut self, h: ChangeHash, c: &Change) { |
| 40 | 29× | if c.kind == ChangeKind::WorkingCopy { |
| 41 | 8× | self.transient.insert(h, nodes_of(c)); |
| 42 | 21× | } else { |
| 43 | 21× | self.nodes.insert(h, nodes_of(c)); |
| 44 | 21× | self.added += 1; |
| 45 | 21× | } |
| 46 | 29× | } |
| 47 | |
|
| 48 | | |
| 49 | 12× | pub(crate) fn fill(&mut self, repo: &Repo, hashes: &[ChangeHash]) { |
| 50 | | use rayon::prelude::*; |
| 51 | 12× | let missing: Vec<ChangeHash> = hashes |
| 52 | 12× | .iter() |
| 53 | 47× | .filter(|h| !self.nodes.contains_key(*h) && !self.transient.contains_key(*h)) |
| 54 | 12× | .copied() |
| 55 | 12× | .collect(); |
| 56 | | |
| 57 | | |
| 58 | 12× | for batch in missing.chunks(8192) { |
| 59 | 10× | let read: Vec<(ChangeHash, Change)> = repo |
| 60 | 10× | .store |
| 61 | 10× | .get_many(batch) |
| 62 | 10× | .into_par_iter() |
| 63 | 10× | .zip(batch.par_iter()) |
| 64 | 29× | .filter_map(|(got, h)| match got { |
| 65 | 29× | Ok((ObjectType::Change, bytes)) => { |
| 66 | 29× | Some((*h, Change::decode_skeleton(&bytes, *h).ok()?.0)) |
| 67 | | } |
| 68 | 0× | _ => None, |
| 69 | 29× | }) |
| 70 | 10× | .collect(); |
| 71 | 29× | for (h, c) in &read { |
| 72 | 29× | self.keep(*h, c); |
| 73 | 29× | } |
| 74 | | } |
| 75 | 12× | } |
| 76 | |
|
| 77 | | |
| 78 | 49× | pub(crate) fn touches(&mut self, repo: &Repo, h: &ChangeHash, nodes: &HashSet<NodeId>) -> bool { |
| 79 | 49× | self.of(repo, h) |
| 80 | 62× | .is_some_and(|n| n.iter().any(|n| nodes.contains(n))) |
| 81 | 49× | } |
| 82 | |
|
| 83 | | |
| 84 | 13× | pub(crate) fn save(self) { |
| 85 | 13× | if self.added == 0 { |
| 86 | 6× | return; |
| 87 | 7× | } |
| 88 | 7× | let _t = crate::timing::phase("write: touched nodes"); |
| 89 | 7× | let mut w = crate::flat::Writer::default(); |
| 90 | 7× | w.count(self.nodes.len()); |
| 91 | 21× | for (h, nodes) in &self.nodes { |
| 92 | 21× | w.object(h); |
| 93 | 21× | w.count(nodes.len()); |
| 94 | 34× | for n in nodes.iter() { |
| 95 | 34× | w.fixed(&n.0); |
| 96 | 34× | } |
| 97 | | } |
| 98 | | |
| 99 | 7× | let sum = *blake3::hash(&w.0).as_bytes(); |
| 100 | 7× | w.fixed(&sum); |
| 101 | 7× | let _ = fs::create_dir_all(self.path.parent().unwrap_or(&self.path)); |
| 102 | 7× | let _ = write_cache(&self.path, &w.0); |
| 103 | 13× | } |
| 104 | | } |
| 105 | |
|
| 106 | |
|
| 107 | 29× | fn nodes_of(c: &Change) -> Box<[NodeId]> { |
| 108 | 29× | let mut nodes: Vec<NodeId> = c |
| 109 | 29× | .tree_ops |
| 110 | 29× | .iter() |
| 111 | 29× | .map(|o| o.node()) |
| 112 | 36× | .chain(c.edits.iter().map(|e| e.node())) |
| 113 | 29× | .collect(); |
| 114 | 29× | nodes.sort(); |
| 115 | 29× | nodes.dedup(); |
| 116 | 29× | nodes.into_boxed_slice() |
| 117 | 29× | } |
| 118 | |
|
| 119 | | impl Repo { |
| 120 | | |
| 121 | | |
| 122 | 13× | pub(crate) fn touched(&self) -> Touched { |
| 123 | 13× | let _t = crate::timing::phase("load: touched nodes"); |
| 124 | 13× | let path = self.oxy.join("index").join("touched"); |
| 125 | 13× | let nodes = fs::read(&path) |
| 126 | 13× | .ok() |
| 127 | 13× | .and_then(|b| read_touched(&b)) |
| 128 | 13× | .unwrap_or_default(); |
| 129 | 13× | Touched { |
| 130 | 13× | nodes, |
| 131 | 13× | transient: FastMap::default(), |
| 132 | 13× | added: 0, |
| 133 | 13× | path, |
| 134 | 13× | } |
| 135 | 13× | } |
| 136 | | } |
| 137 | |
|
| 138 | 8× | fn read_touched(bytes: &[u8]) -> Option<FastMap<ChangeHash, Box<[NodeId]>>> { |
| 139 | 8× | let (body, sum) = bytes.split_at_checked(bytes.len().checked_sub(32)?)?; |
| 140 | 8× | if blake3::hash(body).as_bytes() != sum { |
| 141 | 1× | return None; |
| 142 | 7× | } |
| 143 | 7× | let mut r = crate::flat::Reader::new(body); |
| 144 | 7× | let n = r.count().ok()?; |
| 145 | 7× | let mut out = FastMap::default(); |
| 146 | 7× | out.reserve(n); |
| 147 | 7× | for _ in 0..n { |
| 148 | 25× | let h = r.object().ok()?; |
| 149 | 25× | let k = r.count().ok()?; |
| 150 | 25× | let mut nodes = Vec::with_capacity(k); |
| 151 | 25× | for _ in 0..k { |
| 152 | 46× | nodes.push(NodeId(r.fixed::<16>().ok()?)); |
| 153 | | } |
| 154 | 25× | out.insert(h, nodes.into_boxed_slice()); |
| 155 | | } |
| 156 | 7× | r.at_end().then_some(out) |
| 157 | 8× | } |
| 158 | |
|
| 159 | |
|
| 160 | | #[cfg(test)] |
| 161 | 1× | pub(crate) fn read_touched_for_test(bytes: &[u8]) -> Option<HashSet<ChangeHash>> { |
| 162 | 1× | read_touched(bytes).map(|m| m.into_keys().collect()) |
| 163 | 1× | } |