|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use crate::{store::load_index, types::IndexAndPacks, Store}; |
| 4 | + |
| 5 | +/// A record of a structural element of an object database. |
| 6 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 7 | +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
| 8 | +pub enum Record { |
| 9 | + /// A loose object database. |
| 10 | + LooseObjectDatabase { |
| 11 | + /// The root of the object database. |
| 12 | + objects_directory: PathBuf, |
| 13 | + /// The amount of object files. |
| 14 | + num_objects: usize, |
| 15 | + }, |
| 16 | + /// A pack index file |
| 17 | + Index { |
| 18 | + /// The location of the index file, |
| 19 | + path: PathBuf, |
| 20 | + /// Whether or not the index is mapped into memory. |
| 21 | + state: IndexState, |
| 22 | + }, |
| 23 | + /// A multi-index file |
| 24 | + MultiIndex { |
| 25 | + /// The location of the multi-index file, |
| 26 | + path: PathBuf, |
| 27 | + /// Whether or not the index is mapped into memory. |
| 28 | + state: IndexState, |
| 29 | + }, |
| 30 | + /// An empty slot was encountered, this is possibly happening as the ODB changes during query with |
| 31 | + /// a file being removed. |
| 32 | + Empty, |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 36 | +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
| 37 | +/// Possible stats of pack indices. |
| 38 | +pub enum IndexState { |
| 39 | + /// The index is active in memory because a mapping exists. |
| 40 | + Loaded, |
| 41 | + /// The index couldn't be unloaded as it was still in use, but that can happen another time. |
| 42 | + Disposable, |
| 43 | + /// The index isn't loaded/memory mapped. |
| 44 | + Unloaded, |
| 45 | +} |
| 46 | + |
| 47 | +impl Store { |
| 48 | + /// Return information about all files known to us as well as their loading state. |
| 49 | + /// |
| 50 | + /// Note that this call is expensive as it gathers additional information about loose object databases. |
| 51 | + /// Note that it may change as we collect information due to the highly volatile nature of the |
| 52 | + /// implementation. The likelihood of actual changes is low though as these still depend on something |
| 53 | + /// changing on disk and somebody reading at the same time. |
| 54 | + pub fn structure(&self) -> Result<Vec<Record>, load_index::Error> { |
| 55 | + let index = self.index.load(); |
| 56 | + if !index.is_initialized() { |
| 57 | + self.consolidate_with_disk_state(true, false /*load one new index*/)?; |
| 58 | + } |
| 59 | + let index = self.index.load(); |
| 60 | + let mut res: Vec<_> = index |
| 61 | + .loose_dbs |
| 62 | + .iter() |
| 63 | + .map(|db| Record::LooseObjectDatabase { |
| 64 | + objects_directory: db.path.clone(), |
| 65 | + num_objects: db.iter().count(), |
| 66 | + }) |
| 67 | + .collect(); |
| 68 | + |
| 69 | + for slot in index.slot_indices.iter().map(|idx| &self.files[*idx]) { |
| 70 | + let files = slot.files.load(); |
| 71 | + let record = match &**files { |
| 72 | + Some(index) => { |
| 73 | + let state = if index.is_disposable() { |
| 74 | + IndexState::Disposable |
| 75 | + } else if index.index_is_loaded() { |
| 76 | + IndexState::Loaded |
| 77 | + } else { |
| 78 | + IndexState::Unloaded |
| 79 | + }; |
| 80 | + match index { |
| 81 | + IndexAndPacks::Index(b) => Record::Index { |
| 82 | + path: b.index.path().into(), |
| 83 | + state, |
| 84 | + }, |
| 85 | + IndexAndPacks::MultiIndex(b) => Record::MultiIndex { |
| 86 | + path: b.multi_index.path().into(), |
| 87 | + state, |
| 88 | + }, |
| 89 | + } |
| 90 | + } |
| 91 | + None => Record::Empty, |
| 92 | + }; |
| 93 | + res.push(record); |
| 94 | + } |
| 95 | + Ok(res) |
| 96 | + } |
| 97 | + |
| 98 | + /// Provide a list of all `objects` directories of `alternate` object database paths. |
| 99 | + /// This list might be empty if there are no alternates. |
| 100 | + /// |
| 101 | + /// Read more about alternates in the documentation of the [`resolve`][crate::alternate::resolve()] function. |
| 102 | + pub fn alternate_db_paths(&self) -> Result<Vec<PathBuf>, load_index::Error> { |
| 103 | + let index = self.index.load(); |
| 104 | + if !index.is_initialized() { |
| 105 | + self.consolidate_with_disk_state(true, false /*load one new index*/)?; |
| 106 | + } |
| 107 | + let index = self.index.load(); |
| 108 | + Ok(index |
| 109 | + .loose_dbs |
| 110 | + .iter() |
| 111 | + .skip( |
| 112 | + 1, /* first odb is always the primary one, all the follows is alternates */ |
| 113 | + ) |
| 114 | + .map(|db| db.path.clone()) |
| 115 | + .collect()) |
| 116 | + } |
| 117 | +} |
0 commit comments