1use std::{
2 cell::RefCell,
3 ops::{Deref, DerefMut},
4 rc::Rc,
5 sync::Arc,
6};
7
8use gix_object::Data;
9
10use crate::{find::Header, Cache};
11
12pub struct Proxy<T> {
19 inner: T,
21 object_hash: gix_hash::Kind,
23 memory: Option<RefCell<Storage>>,
26}
27
28impl<T> Proxy<T> {
30 pub fn new(odb: T, object_hash: gix_hash::Kind) -> Proxy<T> {
34 Proxy {
35 inner: odb,
36 object_hash,
37 memory: Some(Default::default()),
38 }
39 }
40
41 pub fn into_inner(self) -> T {
43 self.inner
44 }
45
46 pub fn with_write_passthrough(mut self) -> Self {
50 self.memory.take();
51 self
52 }
53}
54
55impl Proxy<Cache<crate::store::Handle<Arc<crate::Store>>>> {
56 pub fn into_arc(self) -> std::io::Result<Proxy<Cache<crate::store::Handle<Arc<crate::Store>>>>> {
58 Ok(self)
59 }
60}
61
62impl Proxy<Cache<crate::store::Handle<Rc<crate::Store>>>> {
63 pub fn into_arc(self) -> std::io::Result<Proxy<Cache<crate::store::Handle<Arc<crate::Store>>>>> {
65 Ok(Proxy {
66 inner: self.inner.into_arc()?,
67 object_hash: self.object_hash,
68 memory: self.memory,
69 })
70 }
71}
72
73impl From<crate::Handle> for Proxy<crate::Handle> {
74 fn from(odb: crate::Handle) -> Self {
75 let object_hash = odb.store.object_hash;
76 Proxy::new(odb, object_hash)
77 }
78}
79
80impl<T> Proxy<T> {
82 pub fn take_object_memory(&mut self) -> Option<Storage> {
91 self.memory.take().map(RefCell::into_inner)
92 }
93
94 pub fn set_object_memory(&mut self, new: Storage) -> Option<Storage> {
96 let previous = self.take_object_memory();
97 self.memory = Some(RefCell::new(new));
98 previous
99 }
100
101 pub fn enable_object_memory(&mut self) -> &mut Self {
105 if self.memory.is_none() {
106 self.memory = Some(Default::default());
107 }
108 self
109 }
110
111 pub fn reset_object_memory(&self) -> Option<Storage> {
117 self.memory.as_ref().map(|m| std::mem::take(&mut *m.borrow_mut()))
118 }
119
120 pub fn num_objects_in_memory(&self) -> usize {
122 self.memory.as_ref().map_or(0, |m| m.borrow().len())
123 }
124}
125
126impl<T> Clone for Proxy<T>
127where
128 T: Clone,
129{
130 fn clone(&self) -> Self {
131 Proxy {
132 inner: self.inner.clone(),
133 object_hash: self.object_hash,
134 memory: self.memory.clone(),
135 }
136 }
137}
138
139impl<T> gix_object::Find for Proxy<T>
140where
141 T: gix_object::Find,
142{
143 fn try_find<'a>(
144 &self,
145 id: &gix_hash::oid,
146 buffer: &'a mut Vec<u8>,
147 ) -> Result<Option<Data<'a>>, gix_object::find::Error> {
148 if let Some(map) = self.memory.as_ref() {
149 let map = map.borrow();
150 if let Some((kind, data)) = map.get(id) {
151 buffer.clear();
152 buffer.extend_from_slice(data);
153 return Ok(Some(Data {
154 kind: *kind,
155 data: &*buffer,
156 }));
157 }
158 }
159 self.inner.try_find(id, buffer)
160 }
161}
162
163impl<T> gix_object::Exists for Proxy<T>
164where
165 T: gix_object::Exists,
166{
167 fn exists(&self, id: &gix_hash::oid) -> bool {
168 self.memory.as_ref().is_some_and(|map| map.borrow().contains_key(id)) || self.inner.exists(id)
169 }
170}
171
172impl<T> crate::Header for Proxy<T>
173where
174 T: crate::Header,
175{
176 fn try_header(&self, id: &gix_hash::oid) -> Result<Option<Header>, gix_object::find::Error> {
177 if let Some(map) = self.memory.as_ref() {
178 let map = map.borrow();
179 if let Some((kind, data)) = map.get(id) {
180 return Ok(Some(Header::Loose {
181 kind: *kind,
182 size: data.len() as u64,
183 }));
184 }
185 }
186 self.inner.try_header(id)
187 }
188}
189
190impl<T> gix_object::FindHeader for Proxy<T>
191where
192 T: gix_object::FindHeader,
193{
194 fn try_header(&self, id: &gix_hash::oid) -> Result<Option<gix_object::Header>, gix_object::find::Error> {
195 if let Some(map) = self.memory.as_ref() {
196 let map = map.borrow();
197 if let Some((kind, data)) = map.get(id) {
198 return Ok(Some(gix_object::Header {
199 kind: *kind,
200 size: data.len() as u64,
201 }));
202 }
203 }
204 self.inner.try_header(id)
205 }
206}
207
208impl<T> gix_object::Write for Proxy<T>
209where
210 T: gix_object::Write,
211{
212 fn write_stream(
213 &self,
214 kind: gix_object::Kind,
215 size: u64,
216 from: &mut dyn std::io::Read,
217 ) -> Result<gix_hash::ObjectId, gix_object::write::Error> {
218 let Some(map) = self.memory.as_ref() else {
219 return self.inner.write_stream(kind, size, from);
220 };
221
222 let mut buf = Vec::new();
223 from.read_to_end(&mut buf)?;
224
225 let id = gix_object::compute_hash(self.object_hash, kind, &buf)?;
226 map.borrow_mut().insert(id, (kind, buf));
227 Ok(id)
228 }
229}
230
231impl<T> Deref for Proxy<T> {
232 type Target = T;
233
234 fn deref(&self) -> &Self::Target {
235 &self.inner
236 }
237}
238
239impl<T> DerefMut for Proxy<T> {
240 fn deref_mut(&mut self) -> &mut Self::Target {
241 &mut self.inner
242 }
243}
244
245#[derive(Default, Debug, Clone, Eq, PartialEq)]
247pub struct Storage(gix_hashtable::HashMap<gix_hash::ObjectId, (gix_object::Kind, Vec<u8>)>);
248
249impl Deref for Storage {
250 type Target = gix_hashtable::HashMap<gix_hash::ObjectId, (gix_object::Kind, Vec<u8>)>;
251
252 fn deref(&self) -> &Self::Target {
253 &self.0
254 }
255}
256
257impl DerefMut for Storage {
258 fn deref_mut(&mut self) -> &mut Self::Target {
259 &mut self.0
260 }
261}