| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | | use super::*; |
| 11 | | use crate::objects::Checkpoint; |
| 12 | |
|
| 13 | | impl Repo { |
| 14 | 2418× | fn checkpoint_pointer(&self, bookmark: &str) -> PathBuf { |
| 15 | 2418× | self.oxy.join("checkpoints").join(name_file(bookmark)) |
| 16 | 2418× | } |
| 17 | |
|
| 18 | | |
| 19 | 2407× | pub fn latest_checkpoint(&self, bookmark: &str) -> Result<Option<(ObjectRef, Checkpoint)>> { |
| 20 | 2407× | match fs::read_to_string(self.checkpoint_pointer(bookmark)) { |
| 21 | 59× | Ok(h) => { |
| 22 | 59× | let r = ObjectRef::from_hex(h.trim()) |
| 23 | 59× | .ok_or_else(|| RepoError::Msg("corrupt checkpoint pointer".into()))?; |
| 24 | 59× | match self.store.get_typed(&r, ObjectType::Checkpoint) { |
| 25 | 59× | Ok(bytes) => Ok(Some((r, Checkpoint::decode(&bytes)?))), |
| 26 | 0× | Err(StoreError::NotFound(_)) => Ok(None), |
| 27 | 0× | Err(e) => Err(e.into()), |
| 28 | | } |
| 29 | | } |
| 30 | 2348× | Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None), |
| 31 | 0× | Err(e) => Err(e.into()), |
| 32 | | } |
| 33 | 2407× | } |
| 34 | |
|
| 35 | | |
| 36 | 87× | pub(crate) fn checkpoint_refs(&self) -> Result<Vec<ObjectRef>> { |
| 37 | 87× | let dir = self.oxy.join("checkpoints"); |
| 38 | 87× | let mut out = Vec::new(); |
| 39 | 87× | if let Ok(rd) = fs::read_dir(&dir) { |
| 40 | 11× | for e in rd.flatten() { |
| 41 | 11× | if let Ok(h) = fs::read_to_string(e.path()) { |
| 42 | 11× | if let Some(r) = ObjectRef::from_hex(h.trim()) { |
| 43 | 11× | out.push(r); |
| 44 | 11× | } |
| 45 | 0× | } |
| 46 | | } |
| 47 | 76× | } |
| 48 | 87× | Ok(out) |
| 49 | 87× | } |
| 50 | |
|
| 51 | | |
| 52 | 9× | pub(crate) fn write_checkpoint( |
| 53 | 9× | &self, |
| 54 | 9× | bookmark: &str, |
| 55 | 9× | state: &State, |
| 56 | 9× | heads: &[ChangeHash], |
| 57 | 9× | ) -> Result<(ObjectRef, usize)> { |
| 58 | 9× | let batch = self.store.begin_batch()?; |
| 59 | 9× | let (blob, payload_len) = self.store_checkpoint_payload(state)?; |
| 60 | 9× | let cp = Checkpoint { |
| 61 | 9× | heads: StateObject::new(heads.to_vec()).heads, |
| 62 | 9× | count: state.len() as u64, |
| 63 | 9× | payload: blob, |
| 64 | 9× | }; |
| 65 | 9× | let r = self.store.put(ObjectType::Checkpoint, &cp.encode())?; |
| 66 | 9× | batch.finish()?; |
| 67 | 9× | fs::create_dir_all(self.oxy.join("checkpoints"))?; |
| 68 | 9× | write_atomic(&self.checkpoint_pointer(bookmark), r.to_hex().as_bytes())?; |
| 69 | 9× | Ok((r, payload_len)) |
| 70 | 9× | } |
| 71 | |
|
| 72 | | |
| 73 | | |
| 74 | | |
| 75 | 9× | pub(crate) fn store_checkpoint_payload(&self, state: &State) -> Result<(ObjectRef, usize)> { |
| 76 | 9× | let graphs = state.checkpoint_file_graphs(); |
| 77 | 39× | for (_, enc) in &graphs { |
| 78 | 39× | if enc.len() >= crate::state::INLINE_GRAPH_LIMIT { |
| 79 | 3× | self.store_blob(enc)?; |
| 80 | 36× | } |
| 81 | | } |
| 82 | 9× | let index = state.checkpoint_index(&graphs, Repo::blob_ref); |
| 83 | 9× | Ok((self.store_blob(&index)?, index.len())) |
| 84 | 9× | } |
| 85 | |
|
| 86 | | |
| 87 | 7× | pub(crate) fn checkpoint_graph_blobs(&self, cp: &Checkpoint) -> Result<Vec<ObjectRef>> { |
| 88 | 7× | let payload = self.read_blob(&cp.payload)?; |
| 89 | 7× | Ok(State::checkpoint_graph_refs(&payload)?) |
| 90 | 7× | } |
| 91 | |
|
| 92 | | |
| 93 | 19× | pub(crate) fn state_from_checkpoint(&self, payload: &[u8]) -> Result<State> { |
| 94 | 19× | Ok(State::from_checkpoint(payload, |refs| { |
| 95 | 4× | self.read_blobs(refs).map_err(|e| { |
| 96 | 2× | crate::objects::ObjectError::Schema(format!("checkpoint graph blobs: {e}")) |
| 97 | 2× | }) |
| 98 | 4× | })?) |
| 99 | 19× | } |
| 100 | |
|
| 101 | | |
| 102 | 6× | pub fn checkpoint(&self) -> Result<(ObjectRef, usize, usize)> { |
| 103 | 6× | let heads = self.bookmark_heads()?; |
| 104 | 6× | let state = self.load_state_full(&heads)?; |
| 105 | 6× | let (r, bytes) = self.write_checkpoint(&self.config.bookmark, &state, &heads)?; |
| 106 | 6× | Ok((r, state.len(), bytes)) |
| 107 | 6× | } |
| 108 | |
|
| 109 | | |
| 110 | 2× | pub(crate) fn install_checkpoint(&self, bookmark: &str, r: &ObjectRef) -> Result<()> { |
| 111 | 2× | Checkpoint::decode(&self.store.get_typed(r, ObjectType::Checkpoint)?)?; |
| 112 | 2× | fs::create_dir_all(self.oxy.join("checkpoints"))?; |
| 113 | 2× | write_atomic(&self.checkpoint_pointer(bookmark), r.to_hex().as_bytes()) |
| 114 | 2× | } |
| 115 | |
|
| 116 | | |
| 117 | | |
| 118 | | |
| 119 | | |
| 120 | 2× | pub fn checkpoint_bundle_for(&self, heads: &[ChangeHash]) -> Result<Bundle> { |
| 121 | 2× | let wanted = StateObject::new(heads.to_vec()).heads; |
| 122 | 2× | let bookmark = self.config.bookmark.clone(); |
| 123 | 2× | let current = self.bookmark_heads()?; |
| 124 | 2× | let r = match self.latest_checkpoint(&bookmark)? { |
| 125 | 1× | Some((r, cp)) if cp.heads == wanted => r, |
| 126 | | _ => { |
| 127 | 1× | let state = self.load_state(&wanted)?; |
| 128 | 1× | if current == wanted { |
| 129 | 1× | self.write_checkpoint(&bookmark, &state, &wanted)?.0 |
| 130 | | } else { |
| 131 | 0× | let (blob, _) = self.store_checkpoint_payload(&state)?; |
| 132 | 0× | let cp = Checkpoint { |
| 133 | 0× | heads: wanted.clone(), |
| 134 | 0× | count: state.len() as u64, |
| 135 | 0× | payload: blob, |
| 136 | 0× | }; |
| 137 | 0× | self.store.put(ObjectType::Checkpoint, &cp.encode())? |
| 138 | | } |
| 139 | | } |
| 140 | | }; |
| 141 | 2× | let cp = Checkpoint::decode(&self.store.get_typed(&r, ObjectType::Checkpoint)?)?; |
| 142 | 2× | let mut bundle = Bundle::default(); |
| 143 | 2× | for c in Blob::decode(&self.store.get_typed(&cp.payload, ObjectType::Blob)?)?.chunks { |
| 144 | 2× | bundle.objects.push(self.store.get(&c)?); |
| 145 | | } |
| 146 | 2× | bundle.objects.push(self.store.get(&cp.payload)?); |
| 147 | 2× | for g in self.checkpoint_graph_blobs(&cp)? { |
| 148 | 1× | for c in Blob::decode(&self.store.get_typed(&g, ObjectType::Blob)?)?.chunks { |
| 149 | 1× | bundle.objects.push(self.store.get(&c)?); |
| 150 | | } |
| 151 | 1× | bundle.objects.push(self.store.get(&g)?); |
| 152 | | } |
| 153 | 2× | bundle.objects.push((ObjectType::Checkpoint, cp.encode())); |
| 154 | 2× | Ok(bundle) |
| 155 | 2× | } |
| 156 | |
|
| 157 | | |
| 158 | 194× | pub(crate) fn load_state_checkpointed(&self, heads: &[ChangeHash]) -> Result<Option<State>> { |
| 159 | 194× | self.load_state_checkpointed_as(heads, false) |
| 160 | 194× | } |
| 161 | |
|
| 162 | | |
| 163 | 1778× | pub(crate) fn load_state_checkpointed_as( |
| 164 | 1778× | &self, |
| 165 | 1778× | heads: &[ChangeHash], |
| 166 | 1778× | history: bool, |
| 167 | 1778× | ) -> Result<Option<State>> { |
| 168 | 1778× | let mut bookmarks: Vec<String> = vec![self.config.bookmark.clone()]; |
| 169 | 1778× | if let Ok(rd) = fs::read_dir(self.oxy.join("checkpoints")) { |
| 170 | 43× | for e in rd.flatten() { |
| 171 | 43× | let name = file_name_to_name(&e.file_name().to_string_lossy()); |
| 172 | 43× | if !bookmarks.contains(&name) { |
| 173 | 0× | bookmarks.push(name); |
| 174 | 43× | } |
| 175 | | } |
| 176 | 1735× | } |
| 177 | 1778× | for b in bookmarks { |
| 178 | 1778× | let Some((_, cp)) = self.latest_checkpoint(&b)? else { |
| 179 | 1735× | continue; |
| 180 | | }; |
| 181 | 43× | let payload = { |
| 182 | 43× | let _t = crate::timing::phase("load: read checkpoint"); |
| 183 | 43× | self.read_blob(&cp.payload)? |
| 184 | | }; |
| 185 | 42× | let base = { |
| 186 | 43× | let _t = crate::timing::phase("load: decode checkpoint"); |
| 187 | 43× | if history { |
| 188 | 33× | State::from_checkpoint_history(&payload)? |
| 189 | | } else { |
| 190 | 10× | self.state_from_checkpoint(&payload)? |
| 191 | | } |
| 192 | | }; |
| 193 | 42× | let cp_heads: BTreeSet<ChangeHash> = cp.heads.iter().copied().collect(); |
| 194 | 42× | let mut found: BTreeSet<ChangeHash> = BTreeSet::new(); |
| 195 | 42× | let mut delta: Vec<(ChangeHash, Change, Vec<crate::objects::TextRef>)> = Vec::new(); |
| 196 | 42× | let mut seen: BTreeSet<ChangeHash> = BTreeSet::new(); |
| 197 | 42× | let mut stack: Vec<ChangeHash> = heads.to_vec(); |
| 198 | 185× | while let Some(h) = stack.pop() { |
| 199 | 143× | if !seen.insert(h) { |
| 200 | 21× | continue; |
| 201 | 122× | } |
| 202 | 122× | if base.contains(&h) { |
| 203 | 80× | if cp_heads.contains(&h) { |
| 204 | 39× | found.insert(h); |
| 205 | 41× | } |
| 206 | 80× | continue; |
| 207 | 42× | } |
| 208 | | |
| 209 | | |
| 210 | | |
| 211 | 42× | let (c, texts) = if history { |
| 212 | 35× | self.store.get_change_lazy(&h)? |
| 213 | | } else { |
| 214 | 7× | (self.store.get_change(&h)?, Vec::new()) |
| 215 | | }; |
| 216 | 42× | stack.extend(c.deps.iter().copied()); |
| 217 | 42× | delta.push((h, c, texts)); |
| 218 | | } |
| 219 | 42× | if found.len() != cp_heads.len() { |
| 220 | 4× | continue; |
| 221 | 38× | } |
| 222 | 38× | let _t = crate::timing::phase("load: apply delta and validate"); |
| 223 | 38× | let mut state = base; |
| 224 | 38× | for (h, c, texts) in delta { |
| 225 | 33× | if history { |
| 226 | 26× | state.add_known(h, c, texts); |
| 227 | 26× | } else { |
| 228 | 7× | state.add_unchecked(c); |
| 229 | 7× | } |
| 230 | | } |
| 231 | 38× | state.validate()?; |
| 232 | 38× | return Ok(Some(state)); |
| 233 | | } |
| 234 | 1739× | Ok(None) |
| 235 | 1778× | } |
| 236 | | } |