1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::path::PathBuf;
use crate::{
alternate,
store_impls::{compound, linked},
};
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error(transparent)]
CompoundDbInit(#[from] compound::init::Error),
#[error(transparent)]
AlternateResolve(#[from] alternate::Error),
}
impl linked::Store {
pub fn at(objects_directory: impl Into<PathBuf>) -> Result<Self, Error> {
let mut dbs = vec![compound::Store::at(objects_directory.into(), 0)?];
let compute_ofs = |db: &compound::Store| db.bundles.iter().map(|p| p.pack.id).max().map(|ofs| ofs + 1);
let mut ofs = compute_ofs(&dbs[0]).unwrap_or(0);
for object_path in alternate::resolve(dbs[0].loose.path.clone())?.into_iter() {
let store = compound::Store::at(object_path, ofs)?;
ofs = compute_ofs(&store).unwrap_or(ofs);
dbs.push(store);
}
assert!(
!dbs.is_empty(),
"we can rely on at least one compound database to be present"
);
Ok(linked::Store { dbs })
}
pub fn refresh(&mut self) -> Result<&mut Self, Error> {
let first_db = self.dbs.remove(0);
let base_path = first_db.loose.path;
*self = Self::at(base_path)?;
Ok(self)
}
}
impl std::convert::TryFrom<PathBuf> for linked::Store {
type Error = Error;
fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
linked::Store::at(value)
}
}