git_odb/
lib.rs

1//! Git stores all of its data as _Objects_, which are data along with a hash over all data. Thus it's an
2//! object store indexed by the signature of data itself with inherent deduplication: the same data will have the same hash,
3//! and thus occupy the same space within the store.
4//!
5//! There is only one all-round object store, also known as the [`Store`], as it supports ~~everything~~ most of what git has to offer.
6//!
7//! * loose object reading and writing
8//! * access to packed objects
9//! * multiple loose objects and pack locations as gathered from `alternates` files.
10//! ## Feature Flags
11#![cfg_attr(
12    feature = "document-features",
13    cfg_attr(doc, doc = ::document_features::document_features!())
14)]
15#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
16#![deny(missing_docs, rust_2018_idioms, unsafe_code)]
17
18use std::{
19    cell::RefCell,
20    path::PathBuf,
21    sync::{atomic::AtomicUsize, Arc},
22};
23
24use arc_swap::ArcSwap;
25use git_features::{threading::OwnShared, zlib::stream::deflate};
26pub use git_pack as pack;
27
28mod store_impls;
29pub use store_impls::{dynamic as store, loose};
30
31pub mod alternate;
32
33/// A way to access objects along with pre-configured thread-local caches for packed base objects as well as objects themselves.
34///
35/// By default, no cache will be used.
36pub struct Cache<S> {
37    /// The inner provider of trait implementations we use in conjunction with our caches.
38    ///
39    /// For calling methods on `inner`, prefer to make use of auto-dereferencing, i.e. `cache.inner_method()` instead of `cache.inner.inner_method()`.
40    inner: S,
41    // TODO: have single-threaded code-paths also for pack-creation (entries from counts) so that we can use OwnShared here
42    //       instead of Arc. However, it's probably not that important as these aren't called often.
43    new_pack_cache: Option<Arc<cache::NewPackCacheFn>>,
44    new_object_cache: Option<Arc<cache::NewObjectCacheFn>>,
45    pack_cache: Option<RefCell<Box<cache::PackCache>>>,
46    object_cache: Option<RefCell<Box<cache::ObjectCache>>>,
47}
48
49///
50pub mod cache;
51
52///
53/// It can optionally compress the content, similarly to what would happen when using a [`loose::Store`][crate::loose::Store].
54///
55pub struct Sink {
56    compressor: Option<RefCell<deflate::Write<std::io::Sink>>>,
57    object_hash: git_hash::Kind,
58}
59
60/// Create a new [`Sink`] with compression disabled.
61pub fn sink(object_hash: git_hash::Kind) -> Sink {
62    Sink {
63        compressor: None,
64        object_hash,
65    }
66}
67
68///
69pub mod sink;
70
71///
72pub mod find;
73
74/// An object database equivalent to `/dev/null`, dropping all objects stored into it.
75mod traits;
76
77pub use traits::{Find, FindExt, Header, HeaderExt, Write};
78
79/// A thread-local handle to access any object.
80pub type Handle = Cache<store::Handle<OwnShared<Store>>>;
81/// A thread-local handle to access any object, but thread-safe and independent of the actual type of `OwnShared` or feature toggles in `git-features`.
82pub type HandleArc = Cache<store::Handle<Arc<Store>>>;
83
84use store::types;
85
86/// The object store for use in any applications with support for auto-updates in the light of changes to the object database.
87///
88/// ### Features
89///
90/// - entirely lazy, creating an instance does no disk IO at all if [`Slots::Given`][store::init::Slots::Given] is used.
91/// - multi-threaded lazy-loading of indices and packs
92/// - per-thread pack and object caching avoiding cache trashing.
93/// - most-recently-used packs are always first for speedups if objects are stored in the same pack, typical for packs organized by
94///   commit graph and object age.
95/// - lock-free reading for perfect scaling across all cores, and changes to it don't affect readers as long as these don't want to
96///   enter the same branch.
97/// - sync with the state on disk if objects aren't found to catch up with changes if an object seems to be missing.
98///    - turn off the behaviour above for all handles if objects are expected to be missing due to spare checkouts.
99pub struct Store {
100    /// The central write lock without which the slotmap index can't be changed.
101    write: parking_lot::Mutex<()>,
102
103    /// The source directory from which all content is loaded, and the central write lock for use when a directory refresh is needed.
104    pub(crate) path: PathBuf,
105
106    /// The current working directory at the time this store was instantiated. It becomes relevant when resolving alternate paths
107    /// when re-reading the store configuration on updates when an object was missed.
108    /// Keeping it here helps to assure consistency even while a process changes its CWD.
109    pub(crate) current_dir: PathBuf,
110
111    /// A set of replacements that given a source OID return a destination OID. The vector is sorted.
112    pub(crate) replacements: Vec<(git_hash::ObjectId, git_hash::ObjectId)>,
113
114    /// A list of indices keeping track of which slots are filled with data. These are usually, but not always, consecutive.
115    pub(crate) index: ArcSwap<types::SlotMapIndex>,
116
117    /// The below state acts like a slot-map with each slot is mutable when the write lock is held, but readable independently of it.
118    /// This allows multiple file to be loaded concurrently if there is multiple handles requesting to load packs or additional indices.
119    /// The map is static and cannot typically change.
120    /// It's read often and changed rarely.
121    pub(crate) files: Vec<types::MutableIndexAndPack>,
122
123    /// The amount of handles that would prevent us from unloading packs or indices
124    pub(crate) num_handles_stable: AtomicUsize,
125    /// The amount of handles that don't affect our ability to compact our internal data structures or unload packs or indices.
126    pub(crate) num_handles_unstable: AtomicUsize,
127
128    /// The amount of times we re-read the disk state to consolidate our in-memory representation.
129    pub(crate) num_disk_state_consolidation: AtomicUsize,
130    /// If true, we are allowed to use multi-pack indices and they must have the `object_hash` or be ignored.
131    use_multi_pack_index: bool,
132    /// The hash kind to use for some operations
133    object_hash: git_hash::Kind,
134}
135
136/// Create a new cached handle to the object store with support for additional options.
137///
138/// `replacements` is an iterator over pairs of old and new object ids for replacement support.
139/// This means that when asking for object `X`, one will receive object `X-replaced` given an iterator like `Some((X, X-replaced))`.
140pub fn at_opts(
141    objects_dir: impl Into<PathBuf>,
142    replacements: impl IntoIterator<Item = (git_hash::ObjectId, git_hash::ObjectId)>,
143    options: store::init::Options,
144) -> std::io::Result<Handle> {
145    let handle = OwnShared::new(Store::at_opts(objects_dir, replacements, options)?).to_handle();
146    Ok(Cache::from(handle))
147}
148
149/// Create a new cached handle to the object store.
150pub fn at(objects_dir: impl Into<PathBuf>) -> std::io::Result<Handle> {
151    at_opts(objects_dir, Vec::new().into_iter(), Default::default())
152}