|
| 1 | +use crate::find::Header; |
| 2 | +use gix_object::Data; |
| 3 | +use std::cell::RefCell; |
| 4 | +use std::ops::{Deref, DerefMut}; |
| 5 | + |
| 6 | +/// An object database to read from any implementation but write to memory. |
| 7 | +/// Previously written objects can be returned from memory upon query which |
| 8 | +/// makes the view of objects consistent, but it's impact temporary unless |
| 9 | +/// [`memory objects`](Proxy::memory) are persisted in a separate step. |
| 10 | +/// |
| 11 | +/// It's possible to turn off the memory by removing it from the instance. |
| 12 | +pub struct Proxy<T> { |
| 13 | + /// The actual odb implementation |
| 14 | + inner: T, |
| 15 | + /// The kind of hash to produce when writing new objects. |
| 16 | + object_hash: gix_hash::Kind, |
| 17 | + /// The storage for in-memory objects. |
| 18 | + /// If `None`, the proxy will always read from and write-through to `inner`. |
| 19 | + memory: Option<RefCell<Storage>>, |
| 20 | +} |
| 21 | + |
| 22 | +/// Lifecycle |
| 23 | +impl<T> Proxy<T> { |
| 24 | + /// Create a new instance using `odb` as actual object provider, with an empty in-memory store for |
| 25 | + /// objects that are to be written. |
| 26 | + /// Use `object_hash` to determine the kind of hash to produce when writing new objects. |
| 27 | + pub fn new(odb: T, object_hash: gix_hash::Kind) -> Proxy<T> { |
| 28 | + Proxy { |
| 29 | + inner: odb, |
| 30 | + object_hash, |
| 31 | + memory: Some(Default::default()), |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/// Lifecycle |
| 37 | +impl<T> Proxy<T> { |
| 38 | + /// Take all the objects in memory so far, with the memory storage itself and return it. |
| 39 | + /// |
| 40 | + /// The instance will remain in a state where it won't be able to store objects in memory at all, |
| 41 | + /// they will now be stored in the underlying object database. |
| 42 | + /// |
| 43 | + /// To avoid that, use [`reset_object_memory()`](Self::reset_object_memory()) or return the storage |
| 44 | + /// using [`set_object_memory()`](Self::set_object_memory()). |
| 45 | + pub fn take_object_memory(&mut self) -> Option<Storage> { |
| 46 | + self.memory.take().map(|mem| mem.into_inner()) |
| 47 | + } |
| 48 | + |
| 49 | + /// Set the object storage to contain only `new` objects, and return whichever objects were there previously. |
| 50 | + pub fn set_object_memory(&mut self, new: Storage) -> Option<Storage> { |
| 51 | + let previous = self.take_object_memory(); |
| 52 | + self.memory = Some(RefCell::new(new)); |
| 53 | + previous |
| 54 | + } |
| 55 | + |
| 56 | + /// Reset the internal storage to be empty, and return the previous storage, with all objects |
| 57 | + /// it contained. |
| 58 | + /// |
| 59 | + /// Note that this does nothing if this instance didn't contain object memory in the first place. |
| 60 | + /// In that case, set it explicitly. |
| 61 | + pub fn reset_object_memory(&self) -> Option<Storage> { |
| 62 | + self.memory.as_ref().map(|m| std::mem::take(&mut *m.borrow_mut())) |
| 63 | + } |
| 64 | + |
| 65 | + /// Return the amount of objects currently stored in memory. |
| 66 | + pub fn num_objects_in_memory(&self) -> usize { |
| 67 | + self.memory.as_ref().map_or(0, |m| m.borrow().len()) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl<T> gix_object::Find for Proxy<T> |
| 72 | +where |
| 73 | + T: gix_object::Find, |
| 74 | +{ |
| 75 | + fn try_find<'a>( |
| 76 | + &self, |
| 77 | + id: &gix_hash::oid, |
| 78 | + buffer: &'a mut Vec<u8>, |
| 79 | + ) -> Result<Option<Data<'a>>, gix_object::find::Error> { |
| 80 | + if let Some(map) = self.memory.as_ref() { |
| 81 | + let map = map.borrow(); |
| 82 | + if let Some((kind, data)) = map.get(id) { |
| 83 | + buffer.clear(); |
| 84 | + buffer.extend_from_slice(data); |
| 85 | + return Ok(Some(Data { |
| 86 | + kind: *kind, |
| 87 | + data: &*buffer, |
| 88 | + })); |
| 89 | + } |
| 90 | + } |
| 91 | + self.inner.try_find(id, buffer) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl<T> crate::Header for Proxy<T> |
| 96 | +where |
| 97 | + T: crate::Header, |
| 98 | +{ |
| 99 | + fn try_header(&self, id: &gix_hash::oid) -> Result<Option<Header>, gix_object::find::Error> { |
| 100 | + if let Some(map) = self.memory.as_ref() { |
| 101 | + let map = map.borrow(); |
| 102 | + if let Some((kind, data)) = map.get(id) { |
| 103 | + return Ok(Some(Header::Loose { |
| 104 | + kind: *kind, |
| 105 | + size: data.len() as u64, |
| 106 | + })); |
| 107 | + } |
| 108 | + } |
| 109 | + self.inner.try_header(id) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +impl<T> crate::Write for Proxy<T> |
| 114 | +where |
| 115 | + T: crate::Write, |
| 116 | +{ |
| 117 | + fn write_stream( |
| 118 | + &self, |
| 119 | + kind: gix_object::Kind, |
| 120 | + size: u64, |
| 121 | + from: &mut dyn std::io::Read, |
| 122 | + ) -> Result<gix_hash::ObjectId, crate::write::Error> { |
| 123 | + let Some(map) = self.memory.as_ref() else { |
| 124 | + return self.inner.write_stream(kind, size, from); |
| 125 | + }; |
| 126 | + |
| 127 | + let mut buf = Vec::new(); |
| 128 | + from.read_to_end(&mut buf)?; |
| 129 | + |
| 130 | + let id = gix_object::compute_hash(self.object_hash, kind, &buf); |
| 131 | + map.borrow_mut().insert(id, (kind, buf)); |
| 132 | + Ok(id) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +impl<T> Deref for Proxy<T> { |
| 137 | + type Target = T; |
| 138 | + |
| 139 | + fn deref(&self) -> &Self::Target { |
| 140 | + &self.inner |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +impl<T> DerefMut for Proxy<T> { |
| 145 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 146 | + &mut self.inner |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +/// A mapping between an object id and all data corresponding to an object, acting like a `HashMap<ObjectID, (Kind, Data)>`. |
| 151 | +#[derive(Default, Debug, Clone, Eq, PartialEq)] |
| 152 | +pub struct Storage(gix_hashtable::HashMap<gix_hash::ObjectId, (gix_object::Kind, Vec<u8>)>); |
| 153 | + |
| 154 | +impl Deref for Storage { |
| 155 | + type Target = gix_hashtable::HashMap<gix_hash::ObjectId, (gix_object::Kind, Vec<u8>)>; |
| 156 | + |
| 157 | + fn deref(&self) -> &Self::Target { |
| 158 | + &self.0 |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +impl DerefMut for Storage { |
| 163 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 164 | + &mut self.0 |
| 165 | + } |
| 166 | +} |
0 commit comments