git_odb/store_impls/dynamic/
write.rs

1use std::{io::Read, ops::Deref};
2
3use git_hash::ObjectId;
4use git_object::Kind;
5
6use crate::store;
7
8mod error {
9    use crate::{loose, store};
10
11    /// The error returned by the [dynamic Store's][crate::Store] [`Write`][crate::Write] implementation.
12    #[derive(Debug, thiserror::Error)]
13    #[allow(missing_docs)]
14    pub enum Error {
15        #[error(transparent)]
16        LoadIndex(#[from] store::load_index::Error),
17        #[error(transparent)]
18        LooseWrite(#[from] loose::write::Error),
19        #[error(transparent)]
20        Io(#[from] std::io::Error),
21    }
22}
23pub use error::Error;
24
25use crate::store_impls::dynamic;
26
27impl<S> crate::Write for store::Handle<S>
28where
29    S: Deref<Target = dynamic::Store> + Clone,
30{
31    type Error = Error;
32
33    fn write_stream(&self, kind: Kind, size: u64, from: impl Read) -> Result<ObjectId, Self::Error> {
34        let mut snapshot = self.snapshot.borrow_mut();
35        Ok(match snapshot.loose_dbs.first() {
36            Some(ldb) => ldb.write_stream(kind, size, from)?,
37            None => {
38                let new_snapshot = self
39                    .store
40                    .load_one_index(self.refresh, snapshot.marker)?
41                    .expect("there is always at least one ODB, and this code runs only once for initialization");
42                *snapshot = new_snapshot;
43                snapshot.loose_dbs[0].write_stream(kind, size, from)?
44            }
45        })
46    }
47}