gix_odb/store_impls/dynamic/
init.rs

1use std::{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: gix_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 `gix_fs::current_dir(false)`.
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 number 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 number of slots to use, that is the total number 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 number 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 number 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: PathBuf,
74        replacements: &mut dyn Iterator<Item = (gix_hash::ObjectId, gix_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 _span = gix_features::trace::detail!("gix_odb::Store::at()");
83        let current_dir = current_dir.map_or_else(
84            || {
85                // It's only used for real-pathing alternate paths and there it just needs to be consistent (enough).
86                gix_fs::current_dir(false)
87            },
88            Ok,
89        )?;
90        if !objects_dir.is_dir() {
91            return Err(std::io::Error::other(format!(
92                "'{}' wasn't a directory",
93                objects_dir.display()
94            )));
95        }
96        let slot_count = match slots {
97            Slots::Given(n) => n as usize,
98            Slots::AsNeededByDiskState { multiplier, minimum } => {
99                let mut db_paths =
100                    crate::alternate::resolve(objects_dir.clone(), &current_dir).map_err(std::io::Error::other)?;
101                db_paths.insert(0, objects_dir.clone());
102                let num_slots = Store::collect_indices_and_mtime_sorted_by_size(db_paths, None, None)
103                    .map_err(std::io::Error::other)?
104                    .len();
105
106                let candidate = ((num_slots as f32 * multiplier) as usize).max(minimum);
107                if candidate > crate::store::types::PackId::max_indices() {
108                    // A chance for this to work without 10% extra allocation - this already
109                    // is an insane amount of packs.
110                    num_slots
111                } else {
112                    candidate
113                }
114            }
115        };
116        if slot_count > crate::store::types::PackId::max_indices() {
117            return Err(std::io::Error::other(format!(
118                "Cannot use more than 2^15-1 slots, got {slot_count}"
119            )));
120        }
121        let mut replacements: Vec<_> = replacements.collect();
122        replacements.sort_by(|a, b| a.0.cmp(&b.0));
123
124        Ok(Store {
125            current_dir,
126            write: Default::default(),
127            replacements,
128            path: objects_dir,
129            files: Vec::from_iter(std::iter::repeat_with(MutableIndexAndPack::default).take(slot_count)),
130            index: ArcSwap::new(Arc::new(SlotMapIndex::default())),
131            use_multi_pack_index,
132            object_hash,
133            num_handles_stable: Default::default(),
134            num_handles_unstable: Default::default(),
135            num_disk_state_consolidation: Default::default(),
136        })
137    }
138}