- Change `path` field to read-only `path()` method - add `hash_kind` parameter to `loose::Store::at(…, hash_kind)`
Unified · Split
| @@ -110,8 +110,10 @@ pub struct Store { · 109 unmodified lines | ||
| 110 | 110 | |
| 111 | 111 | /// The amount of times we re-read the disk state to consolidate our in-memory representation. |
| 112 | 112 | pub(crate) num_disk_state_consolidation: AtomicUsize, |
| 113 | − /// If Some(kind), we are allowed to use multi-pack indices and they must have the given hash kind or be ignored. | |
| 114 | − multi_pack_index_hash_kind: Option<git_hash::Kind>, | |
| 113 | + /// If true, we are allowed to use multi-pack indices and they must have the `hash_kind` or be ignored. | |
| 114 | + use_multi_pack_index: bool, | |
| 115 | + /// The hash kind to use for some operations | |
| 116 | + hash_kind: git_hash::Kind, | |
| 115 | 117 | } |
| 116 | 118 | |
| 117 | 119 | impl Store { |
| @@ -63,7 +63,7 @@ impl compound::Store { · 62 unmodified lines | ||
| 63 | 63 | }; |
| 64 | 64 | |
| 65 | 65 | Ok(compound::Store { |
| 66 | − loose: loose::Store::at(loose_objects), | |
| 66 | + loose: loose::Store::at(loose_objects, git_hash::Kind::Sha1), | |
| 67 | 67 | bundles: packs, |
| 68 | 68 | }) |
| 69 | 69 | } |
| @@ -10,8 +10,10 @@ use crate::store::types::{MutableIndexAndPack, SlotMapIndex}; · 9 unmodified lines | ||
| 10 | 10 | pub struct Options { |
| 11 | 11 | /// How to obtain a size for the slot map. |
| 12 | 12 | pub slots: Slots, |
| 13 | − /// The kind of hash the multi-pack index must have to be considered. If `None`, no multi-pack indices will be used. | |
| 14 | − pub multi_pack_index_hash_kind: Option<git_hash::Kind>, | |
| 13 | + /// The kind of hash we expect in our packs and would use for loose object iteration and object writing. | |
| 14 | + pub hash_kind: git_hash::Kind, | |
| 15 | + /// If false, no multi-pack indices will be used. If true, they will be used if their hash matches `hash_kind`. | |
| 16 | + pub use_multi_pack_index: bool, | |
| 15 | 17 | } |
| 16 | 18 | |
| 17 | 19 | /// Configures the amount of slots in the index slotmap, which is fixed throughout the existence of the store. |
| @@ -53,7 +55,8 @@ impl Store { · 35 unmodified lines | ||
| 53 | 55 | objects_dir: impl Into<PathBuf>, |
| 54 | 56 | Options { |
| 55 | 57 | slots, |
| 56 | − multi_pack_index_hash_kind, | |
| 58 | + hash_kind, | |
| 59 | + use_multi_pack_index, | |
| 57 | 60 | }: Options, |
| 58 | 61 | ) -> std::io::Result<Self> { |
| 59 | 62 | let objects_dir = objects_dir.into(); |
| @@ -69,10 +72,13 @@ impl Store { · 9 unmodified lines | ||
| 69 | 72 | let mut db_paths = crate::alternate::resolve(&objects_dir) |
| 70 | 73 | .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))?; |
| 71 | 74 | db_paths.insert(0, objects_dir.clone()); |
| 72 | − let num_slots = | |
| 73 | − super::Store::collect_indices_and_mtime_sorted_by_size(db_paths, None, multi_pack_index_hash_kind) | |
| 74 | − .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))? | |
| 75 | − .len(); | |
| 75 | + let num_slots = super::Store::collect_indices_and_mtime_sorted_by_size( | |
| 76 | + db_paths, | |
| 77 | + None, | |
| 78 | + use_multi_pack_index.then(|| hash_kind), | |
| 79 | + ) | |
| 80 | + .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))? | |
| 81 | + .len(); | |
| 76 | 82 | |
| 77 | 83 | ((num_slots as f32 * multiplier) as usize).max(minimum) |
| 78 | 84 | } |
| @@ -88,7 +94,8 @@ impl Store { · 9 unmodified lines | ||
| 88 | 94 | path: objects_dir, |
| 89 | 95 | files: Vec::from_iter(std::iter::repeat_with(MutableIndexAndPack::default).take(slot_count)), |
| 90 | 96 | index: ArcSwap::new(Arc::new(SlotMapIndex::default())), |
| 91 | − multi_pack_index_hash_kind, | |
| 97 | + use_multi_pack_index, | |
| 98 | + hash_kind, | |
| 92 | 99 | num_handles_stable: Default::default(), |
| 93 | 100 | num_handles_unstable: Default::default(), |
| 94 | 101 | num_disk_state_consolidation: Default::default(), |
| @@ -187,7 +187,12 @@ impl super::Store { · 186 unmodified lines | ||
| 187 | 187 | .zip(index.loose_dbs.iter().map(|ldb| &ldb.path)) |
| 188 | 188 | .any(|(lhs, rhs)| lhs != rhs) |
| 189 | 189 | { |
| 190 | − Arc::new(db_paths.iter().map(crate::loose::Store::at).collect::<Vec<_>>()) | |
| 190 | + Arc::new( | |
| 191 | + db_paths | |
| 192 | + .iter() | |
| 193 | + .map(|path| crate::loose::Store::at(path, self.hash_kind)) | |
| 194 | + .collect::<Vec<_>>(), | |
| 195 | + ) | |
| 191 | 196 | } else { |
| 192 | 197 | Arc::clone(&index.loose_dbs) |
| 193 | 198 | }; |
| @@ -195,7 +200,7 @@ impl super::Store { · 1 unmodified line | ||
| 195 | 200 | let indices_by_modification_time = Self::collect_indices_and_mtime_sorted_by_size( |
| 196 | 201 | db_paths, |
| 197 | 202 | index.slot_indices.len().into(), |
| 198 | − self.multi_pack_index_hash_kind, | |
| 203 | + self.use_multi_pack_index.then(|| self.hash_kind), | |
| 199 | 204 | )?; |
| 200 | 205 | let mut idx_by_index_path: BTreeMap<_, _> = index |
| 201 | 206 | .slot_indices |
| @@ -1,13 +1,15 @@ | ||
| 1 | 1 | //! An object database storing each object in a zlib compressed file with its hash in the path |
| 2 | 2 | const HEADER_READ_UNCOMPRESSED_BYTES: usize = 512; |
| 3 | 3 | use git_features::fs; |
| 4 | −use std::path::PathBuf; | |
| 4 | +use std::path::{Path, PathBuf}; | |
| 5 | 5 | |
| 6 | 6 | /// A database for reading and writing objects to disk, one file per object. |
| 7 | 7 | #[derive(Clone, PartialEq, Eq)] |
| 8 | 8 | pub struct Store { |
| 9 | 9 | /// The directory in which objects are stored, containing 256 folders representing the hashes first byte. |
| 10 | − pub path: PathBuf, | |
| 10 | + pub(crate) path: PathBuf, | |
| 11 | + /// The kind of hash we should assume during iteration and when writing new objects. | |
| 12 | + pub(crate) hash_kind: git_hash::Kind, | |
| 11 | 13 | } |
| 12 | 14 | |
| 13 | 15 | /// Initialization |
| @@ -16,11 +18,22 @@ impl Store { · 2 unmodified lines | ||
| 16 | 18 | /// contain all loose objects. |
| 17 | 19 | /// |
| 18 | 20 | /// In a git repository, this would be `.git/objects`. |
| 19 | − pub fn at(objects_directory: impl Into<PathBuf>) -> Store { | |
| 21 | + pub fn at(objects_directory: impl Into<PathBuf>, hash_kind: git_hash::Kind) -> Store { | |
| 20 | 22 | Store { |
| 21 | 23 | path: objects_directory.into(), |
| 24 | + hash_kind, | |
| 22 | 25 | } |
| 23 | 26 | } |
| 27 | + | |
| 28 | + /// Return the path to our `objects` directory. | |
| 29 | + pub fn path(&self) -> &Path { | |
| 30 | + &self.path | |
| 31 | + } | |
| 32 | + | |
| 33 | + /// Return the kind of hash we would iterate and write. | |
| 34 | + pub fn hash_kind(&self) -> git_hash::Kind { | |
| 35 | + self.hash_kind | |
| 36 | + } | |
| 24 | 37 | } |
| 25 | 38 | |
| 26 | 39 | fn hash_path(id: &git_hash::oid, mut root: PathBuf) -> PathBuf { |
| @@ -93,8 +93,8 @@ mod init { · 92 unmodified lines | ||
| 93 | 93 | let (object_path, linked_object_path) = alternate(tmp.path().join("a"), tmp.path().join("b"))?; |
| 94 | 94 | let db = linked::Store::try_from(object_path.clone())?; |
| 95 | 95 | assert_eq!(db.dbs.len(), 2); |
| 96 | − assert_eq!(db.dbs[0].loose.path, object_path); | |
| 97 | − assert_eq!(db.dbs[1].loose.path, linked_object_path); | |
| 96 | + assert_eq!(db.dbs[0].loose.path(), object_path); | |
| 97 | + assert_eq!(db.dbs[1].loose.path(), linked_object_path); | |
| 98 | 98 | Ok(()) |
| 99 | 99 | } |
| 100 | 100 | |
| @@ -103,7 +103,7 @@ mod init { · 2 unmodified lines | ||
| 103 | 103 | let tmp = git_testtools::tempfile::TempDir::new()?; |
| 104 | 104 | let db = linked::Store::at(tmp.path())?; |
| 105 | 105 | assert_eq!(db.dbs.len(), 1); |
| 106 | − assert_eq!(db.dbs[0].loose.path, tmp.path()); | |
| 106 | + assert_eq!(db.dbs[0].loose.path(), tmp.path()); | |
| 107 | 107 | Ok(()) |
| 108 | 108 | } |
| 109 | 109 | |
| @@ -4,7 +4,7 @@ use pretty_assertions::assert_eq; · 3 unmodified lines | ||
| 4 | 4 | use crate::{fixture_path, hex_to_id}; |
| 5 | 5 | |
| 6 | 6 | fn ldb() -> Store { |
| 7 | − Store::at(fixture_path("objects")) | |
| 7 | + Store::at(fixture_path("objects"), git_hash::Kind::Sha1) | |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | pub fn object_ids() -> Vec<git_hash::ObjectId> { |
| @@ -37,7 +37,7 @@ mod write { · 26 unmodified lines | ||
| 37 | 37 | #[test] |
| 38 | 38 | fn read_and_write() -> Result<(), Box<dyn std::error::Error>> { |
| 39 | 39 | let dir = tempfile::tempdir()?; |
| 40 | − let db = loose::Store::at(dir.path()); | |
| 40 | + let db = loose::Store::at(dir.path(), git_hash::Kind::Sha1); | |
| 41 | 41 | let mut buf = Vec::new(); |
| 42 | 42 | let mut buf2 = Vec::new(); |
| 43 | 43 | |