History

change!: various changes to the `loose::Store` (#279)

- Change `path` field to read-only `path()` method - add `hash_kind` parameter to `loose::Store::at(…, hash_kind)`

Sebastian Thiel committed 4 years ago

Commit ad1b9ea1 parent b71ea6a7 files · +48 −21Browse files

Unified · Split

7 files
  1. git-odb/src/lib.rs +4 −2
  2. git-odb/src/store_impls/compound/init.rs +1 −1
  3. git-odb/src/store_impls/dynamic/init.rs +15 −8
  4. git-odb/src/store_impls/dynamic/load_index.rs +7 −2
  5. git-odb/src/store_impls/loose/mod.rs +16 −3
  6. git-odb/tests/odb/store/linked.rs +3 −3
  7. git-odb/tests/odb/store/loose/backend.rs +2 −2
git-odb/src/lib.rs+4−2
@@ -110,8 +110,10 @@ pub struct Store { · 109 unmodified lines
110110
111111 /// The amount of times we re-read the disk state to consolidate our in-memory representation.
112112 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,
115117 }
116118
117119 impl Store {
git-odb/src/store_impls/compound/init.rs+1−1
@@ -63,7 +63,7 @@ impl compound::Store { · 62 unmodified lines
6363 };
6464
6565 Ok(compound::Store {
66− loose: loose::Store::at(loose_objects),
66+ loose: loose::Store::at(loose_objects, git_hash::Kind::Sha1),
6767 bundles: packs,
6868 })
6969 }
git-odb/src/store_impls/dynamic/init.rs+15−8
@@ -10,8 +10,10 @@ use crate::store::types::{MutableIndexAndPack, SlotMapIndex}; · 9 unmodified lines
1010 pub struct Options {
1111 /// How to obtain a size for the slot map.
1212 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,
1517 }
1618
1719 /// 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
5355 objects_dir: impl Into<PathBuf>,
5456 Options {
5557 slots,
56− multi_pack_index_hash_kind,
58+ hash_kind,
59+ use_multi_pack_index,
5760 }: Options,
5861 ) -> std::io::Result<Self> {
5962 let objects_dir = objects_dir.into();
@@ -69,10 +72,13 @@ impl Store { · 9 unmodified lines
6972 let mut db_paths = crate::alternate::resolve(&objects_dir)
7073 .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))?;
7174 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();
7682
7783 ((num_slots as f32 * multiplier) as usize).max(minimum)
7884 }
@@ -88,7 +94,8 @@ impl Store { · 9 unmodified lines
8894 path: objects_dir,
8995 files: Vec::from_iter(std::iter::repeat_with(MutableIndexAndPack::default).take(slot_count)),
9096 index: ArcSwap::new(Arc::new(SlotMapIndex::default())),
91− multi_pack_index_hash_kind,
97+ use_multi_pack_index,
98+ hash_kind,
9299 num_handles_stable: Default::default(),
93100 num_handles_unstable: Default::default(),
94101 num_disk_state_consolidation: Default::default(),
git-odb/src/store_impls/dynamic/load_index.rs+7−2
@@ -187,7 +187,12 @@ impl super::Store { · 186 unmodified lines
187187 .zip(index.loose_dbs.iter().map(|ldb| &ldb.path))
188188 .any(|(lhs, rhs)| lhs != rhs)
189189 {
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+ )
191196 } else {
192197 Arc::clone(&index.loose_dbs)
193198 };
@@ -195,7 +200,7 @@ impl super::Store { · 1 unmodified line
195200 let indices_by_modification_time = Self::collect_indices_and_mtime_sorted_by_size(
196201 db_paths,
197202 index.slot_indices.len().into(),
198− self.multi_pack_index_hash_kind,
203+ self.use_multi_pack_index.then(|| self.hash_kind),
199204 )?;
200205 let mut idx_by_index_path: BTreeMap<_, _> = index
201206 .slot_indices
git-odb/src/store_impls/loose/mod.rs+16−3
@@ -1,13 +1,15 @@
11 //! An object database storing each object in a zlib compressed file with its hash in the path
22 const HEADER_READ_UNCOMPRESSED_BYTES: usize = 512;
33 use git_features::fs;
4−use std::path::PathBuf;
4+use std::path::{Path, PathBuf};
55
66 /// A database for reading and writing objects to disk, one file per object.
77 #[derive(Clone, PartialEq, Eq)]
88 pub struct Store {
99 /// 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,
1113 }
1214
1315 /// Initialization
@@ -16,11 +18,22 @@ impl Store { · 2 unmodified lines
1618 /// contain all loose objects.
1719 ///
1820 /// 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 {
2022 Store {
2123 path: objects_directory.into(),
24+ hash_kind,
2225 }
2326 }
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+ }
2437 }
2538
2639 fn hash_path(id: &git_hash::oid, mut root: PathBuf) -> PathBuf {
git-odb/tests/odb/store/linked.rs+3−3
@@ -93,8 +93,8 @@ mod init { · 92 unmodified lines
9393 let (object_path, linked_object_path) = alternate(tmp.path().join("a"), tmp.path().join("b"))?;
9494 let db = linked::Store::try_from(object_path.clone())?;
9595 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);
9898 Ok(())
9999 }
100100
@@ -103,7 +103,7 @@ mod init { · 2 unmodified lines
103103 let tmp = git_testtools::tempfile::TempDir::new()?;
104104 let db = linked::Store::at(tmp.path())?;
105105 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());
107107 Ok(())
108108 }
109109
git-odb/tests/odb/store/loose/backend.rs+2−2
@@ -4,7 +4,7 @@ use pretty_assertions::assert_eq; · 3 unmodified lines
44 use crate::{fixture_path, hex_to_id};
55
66 fn ldb() -> Store {
7− Store::at(fixture_path("objects"))
7+ Store::at(fixture_path("objects"), git_hash::Kind::Sha1)
88 }
99
1010 pub fn object_ids() -> Vec<git_hash::ObjectId> {
@@ -37,7 +37,7 @@ mod write { · 26 unmodified lines
3737 #[test]
3838 fn read_and_write() -> Result<(), Box<dyn std::error::Error>> {
3939 let dir = tempfile::tempdir()?;
40− let db = loose::Store::at(dir.path());
40+ let db = loose::Store::at(dir.path(), git_hash::Kind::Sha1);
4141 let mut buf = Vec::new();
4242 let mut buf2 = Vec::new();
4343

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t