git_odb/store_impls/dynamic/
init.rs

1use std::{iter::FromIterator, path::PathBuf, sync::Arc};
2
3use arc_swap::ArcSwap;
4
5use crate::{
6    store::types::{MutableIndexAndPack, SlotMapIndex},
7    Store,
8};
9
10/// Options for use in [`Store::at_opts()`].
11#[derive(Clone, Debug)]
12pub struct Options {
13    /// How to obtain a size for the slot map.
14    pub slots: Slots,
15    /// The kind of hash we expect in our packs and would use for loose object iteration and object writing.
16    pub object_hash: git_hash::Kind,
17    /// If false, no multi-pack indices will be used. If true, they will be used if their hash matches `object_hash`.
18    pub use_multi_pack_index: bool,
19    /// The current directory of the process at the time of instantiation.
20    /// If unset, it will be retrieved using `std::env::current_dir()`.
21    pub current_dir: Option<std::path::PathBuf>,
22}
23
24impl Default for Options {
25    fn default() -> Self {
26        Options {
27            slots: Default::default(),
28            object_hash: Default::default(),
29            use_multi_pack_index: true,
30            current_dir: None,
31        }
32    }
33}
34
35/// Configures the amount of slots in the index slotmap, which is fixed throughout the existence of the store.
36#[derive(Copy, Clone, Debug)]
37pub enum Slots {
38    /// The amount of slots to use, that is the total amount of indices we can hold at a time.
39    /// Using this has the advantage of avoiding an initial directory listing of the repository, and is recommended
40    /// on the server side where the repository setup is controlled.
41    ///
42    /// Note that this won't affect their packs, as each index can have one or more packs associated with it.
43    Given(u16),
44    /// Compute the amount of slots needed, as probably best used on the client side where a variety of repositories is encountered.
45    AsNeededByDiskState {
46        /// 1.0 means no safety, 1.1 means 10% more slots than needed
47        multiplier: f32,
48        /// The minimum amount of slots to assume
49        minimum: usize,
50    },
51}
52
53impl Default for Slots {
54    fn default() -> Self {
55        Slots::AsNeededByDiskState {
56            multiplier: 1.1,
57            minimum: 32,
58        }
59    }
60}
61
62impl Store {
63    /// Open the store at `objects_dir` (containing loose objects and `packs/`), which must only be a directory for
64    /// the store to be created without any additional work being done.
65    /// `slots` defines how many multi-pack-indices as well as indices we can know about at a time, which includes
66    /// the allowance for all additional object databases coming in via `alternates` as well.
67    /// Note that the `slots` isn't used for packs, these are included with their multi-index or index respectively.
68    /// For example, In a repository with 250m objects and geometric packing one would expect 27 index/pack pairs,
69    /// or a single multi-pack index.
70    /// `replacements` is an iterator over pairs of old and new object ids for replacement support.
71    /// This means that when asking for object `X`, one will receive object `X-replaced` given an iterator like `Some((X, X-replaced))`.
72    pub fn at_opts(
73        objects_dir: impl Into<PathBuf>,
74        replacements: impl IntoIterator<Item = (git_hash::ObjectId, git_hash::ObjectId)>,
75        Options {
76            slots,
77            object_hash,
78            use_multi_pack_index,
79            current_dir,
80        }: Options,
81    ) -> std::io::Result<Self> {
82        let objects_dir = objects_dir.into();
83        let current_dir = current_dir.map(Ok).unwrap_or_else(std::env::current_dir)?;
84        if !objects_dir.is_dir() {
85            return Err(std::io::Error::new(
86                std::io::ErrorKind::Other, // TODO: use NotADirectory when stabilized
87                format!("'{}' wasn't a directory", objects_dir.display()),
88            ));
89        }
90        let slot_count = match slots {
91            Slots::Given(n) => n as usize,
92            Slots::AsNeededByDiskState { multiplier, minimum } => {
93                let mut db_paths = crate::alternate::resolve(&objects_dir, &current_dir)
94                    .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))?;
95                db_paths.insert(0, objects_dir.clone());
96                let num_slots = super::Store::collect_indices_and_mtime_sorted_by_size(db_paths, None, None)
97                    .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err))?
98                    .len();
99
100                ((num_slots as f32 * multiplier) as usize).max(minimum)
101            }
102        };
103        if slot_count > crate::store::types::PackId::max_indices() {
104            return Err(std::io::Error::new(
105                std::io::ErrorKind::Other,
106                "Cannot use more than 1^15 slots",
107            ));
108        }
109        let mut replacements: Vec<_> = replacements.into_iter().collect();
110        replacements.sort_by(|a, b| a.0.cmp(&b.0));
111
112        Ok(Store {
113            current_dir,
114            write: Default::default(),
115            replacements,
116            path: objects_dir,
117            files: Vec::from_iter(std::iter::repeat_with(MutableIndexAndPack::default).take(slot_count)),
118            index: ArcSwap::new(Arc::new(SlotMapIndex::default())),
119            use_multi_pack_index,
120            object_hash,
121            num_handles_stable: Default::default(),
122            num_handles_unstable: Default::default(),
123            num_disk_state_consolidation: Default::default(),
124        })
125    }
126}