45 /// Open a revlog index file. |
45 /// Open a revlog index file. |
46 /// |
46 /// |
47 /// It will also open the associated data file if index and data are not |
47 /// It will also open the associated data file if index and data are not |
48 /// interleaved. |
48 /// interleaved. |
49 #[timed] |
49 #[timed] |
50 pub fn open(index_path: &Path) -> Result<Self, RevlogError> { |
50 pub fn open( |
|
51 index_path: &Path, |
|
52 data_path: Option<&Path>, |
|
53 ) -> Result<Self, RevlogError> { |
51 let index_mmap = |
54 let index_mmap = |
52 mmap_open(&index_path).map_err(RevlogError::IoError)?; |
55 mmap_open(&index_path).map_err(RevlogError::IoError)?; |
53 |
56 |
54 let version = get_version(&index_mmap); |
57 let version = get_version(&index_mmap); |
55 if version != 1 { |
58 if version != 1 { |
56 return Err(RevlogError::UnsuportedVersion(version)); |
59 return Err(RevlogError::UnsuportedVersion(version)); |
57 } |
60 } |
58 |
61 |
59 let index = Index::new(Box::new(index_mmap))?; |
62 let index = Index::new(Box::new(index_mmap))?; |
60 |
63 |
61 // TODO load data only when needed // |
64 let default_data_path = index_path.with_extension("d"); |
|
65 |
62 // type annotation required |
66 // type annotation required |
63 // won't recognize Mmap as Deref<Target = [u8]> |
67 // won't recognize Mmap as Deref<Target = [u8]> |
64 let data_bytes: Option<Box<dyn Deref<Target = [u8]> + Send>> = |
68 let data_bytes: Option<Box<dyn Deref<Target = [u8]> + Send>> = |
65 if index.is_inline() { |
69 if index.is_inline() { |
66 None |
70 None |
67 } else { |
71 } else { |
68 let data_path = index_path.with_extension("d"); |
72 let data_path = data_path.unwrap_or(&default_data_path); |
69 let data_mmap = |
73 let data_mmap = |
70 mmap_open(&data_path).map_err(RevlogError::IoError)?; |
74 mmap_open(data_path).map_err(RevlogError::IoError)?; |
71 Some(Box::new(data_mmap)) |
75 Some(Box::new(data_mmap)) |
72 }; |
76 }; |
73 |
77 |
74 Ok(Revlog { index, data_bytes }) |
78 Ok(Revlog { index, data_bytes }) |
75 } |
79 } |