1use std::{
2 cell::RefCell,
3 ops::{Deref, DerefMut},
4 rc::Rc,
5 sync::Arc,
6};
7
8use crate::Cache;
9
10pub type PackCache = dyn gix_pack::cache::DecodeEntry + Send + 'static;
12pub type NewPackCacheFn = dyn Fn() -> Box<PackCache> + Send + Sync + 'static;
14
15pub type ObjectCache = dyn gix_pack::cache::Object + Send + 'static;
17pub type NewObjectCacheFn = dyn Fn() -> Box<ObjectCache> + Send + Sync + 'static;
19
20impl Cache<crate::store::Handle<Rc<crate::Store>>> {
21 pub fn into_arc(self) -> std::io::Result<Cache<crate::store::Handle<Arc<crate::Store>>>> {
24 let inner = self.inner.into_arc()?;
25 Ok(Cache {
26 inner,
27 new_pack_cache: self.new_pack_cache,
28 new_object_cache: self.new_object_cache,
29 pack_cache: self.pack_cache,
30 object_cache: self.object_cache,
31 })
32 }
33}
34impl Cache<crate::store::Handle<Arc<crate::Store>>> {
35 pub fn into_arc(self) -> std::io::Result<Cache<crate::store::Handle<Arc<crate::Store>>>> {
37 Ok(self)
38 }
39}
40
41impl<S> Cache<S> {
42 pub fn into_inner(self) -> S {
44 self.inner
45 }
46 pub fn with_pack_cache(mut self, create: impl Fn() -> Box<PackCache> + Send + Sync + 'static) -> Self {
51 self.pack_cache = Some(RefCell::new(create()));
52 self.new_pack_cache = Some(Arc::new(create));
53 self
54 }
55 pub fn with_object_cache(mut self, create: impl Fn() -> Box<ObjectCache> + Send + Sync + 'static) -> Self {
60 self.object_cache = Some(RefCell::new(create()));
61 self.new_object_cache = Some(Arc::new(create));
62 self
63 }
64 pub fn set_pack_cache(&mut self, create: impl Fn() -> Box<PackCache> + Send + Sync + 'static) {
66 self.pack_cache = Some(RefCell::new(create()));
67 self.new_pack_cache = Some(Arc::new(create));
68 }
69 pub fn set_object_cache(&mut self, create: impl Fn() -> Box<ObjectCache> + Send + Sync + 'static) {
71 self.object_cache = Some(RefCell::new(create()));
72 self.new_object_cache = Some(Arc::new(create));
73 }
74 pub fn has_object_cache(&self) -> bool {
76 self.object_cache.is_some()
77 }
78 pub fn has_pack_cache(&self) -> bool {
80 self.pack_cache.is_some()
81 }
82 pub fn unset_pack_cache(&mut self) {
84 self.pack_cache = None;
85 self.new_pack_cache = None;
86 }
87 pub fn unset_object_cache(&mut self) {
89 self.object_cache = None;
90 self.new_object_cache = None;
91 }
92}
93
94impl<S> From<S> for Cache<S>
95where
96 S: gix_pack::Find,
97{
98 fn from(store: S) -> Self {
99 Self {
100 inner: store,
101 pack_cache: None,
102 new_pack_cache: None,
103 object_cache: None,
104 new_object_cache: None,
105 }
106 }
107}
108
109impl<S: Clone> Clone for Cache<S> {
110 fn clone(&self) -> Self {
111 Cache {
112 inner: self.inner.clone(),
113 new_pack_cache: self.new_pack_cache.clone(),
114 new_object_cache: self.new_object_cache.clone(),
115 pack_cache: self.new_pack_cache.as_ref().map(|create| RefCell::new(create())),
116 object_cache: self.new_object_cache.as_ref().map(|create| RefCell::new(create())),
117 }
118 }
119}
120
121impl<S> Deref for Cache<S> {
122 type Target = S;
123
124 fn deref(&self) -> &Self::Target {
125 &self.inner
126 }
127}
128
129impl<S> DerefMut for Cache<S> {
130 fn deref_mut(&mut self) -> &mut Self::Target {
131 &mut self.inner
132 }
133}
134
135mod impls {
136 use std::{cell::RefCell, io::Read, ops::DerefMut};
137
138 use gix_hash::{oid, ObjectId};
139 use gix_object::{Data, Kind};
140 use gix_pack::cache::Object;
141
142 use crate::{find::Header, pack::data::entry::Location, Cache};
143
144 impl<S> gix_object::Write for Cache<S>
145 where
146 S: gix_object::Write,
147 {
148 fn write_stream(
149 &self,
150 kind: Kind,
151 size: u64,
152 from: &mut dyn Read,
153 ) -> Result<ObjectId, gix_object::write::Error> {
154 self.inner.write_stream(kind, size, from)
155 }
156 }
157
158 impl<S> gix_object::Find for Cache<S>
159 where
160 S: gix_pack::Find,
161 {
162 fn try_find<'a>(&self, id: &oid, buffer: &'a mut Vec<u8>) -> Result<Option<Data<'a>>, gix_object::find::Error> {
163 gix_pack::Find::try_find(self, id, buffer).map(|t| t.map(|t| t.0))
164 }
165 }
166
167 impl<S> gix_object::Exists for Cache<S>
168 where
169 S: gix_pack::Find,
170 {
171 fn exists(&self, id: &oid) -> bool {
172 self.inner.contains(id)
173 }
174 }
175
176 impl<S> crate::Header for Cache<S>
177 where
178 S: crate::Header,
179 {
180 fn try_header(&self, id: &oid) -> Result<Option<Header>, gix_object::find::Error> {
181 self.inner.try_header(id)
182 }
183 }
184
185 impl<S> gix_object::FindHeader for Cache<S>
186 where
187 S: gix_object::FindHeader,
188 {
189 fn try_header(&self, id: &oid) -> Result<Option<gix_object::Header>, gix_object::find::Error> {
190 self.inner.try_header(id)
191 }
192 }
193
194 impl<S> gix_pack::Find for Cache<S>
195 where
196 S: gix_pack::Find,
197 {
198 fn contains(&self, id: &oid) -> bool {
199 self.inner.contains(id)
200 }
201
202 fn try_find<'a>(
203 &self,
204 id: &oid,
205 buffer: &'a mut Vec<u8>,
206 ) -> Result<Option<(Data<'a>, Option<Location>)>, gix_object::find::Error> {
207 match self.pack_cache.as_ref().map(RefCell::borrow_mut) {
208 Some(mut pack_cache) => self.try_find_cached(id, buffer, pack_cache.deref_mut()),
209 None => self.try_find_cached(id, buffer, &mut gix_pack::cache::Never),
210 }
211 }
212
213 fn try_find_cached<'a>(
214 &self,
215 id: &oid,
216 buffer: &'a mut Vec<u8>,
217 pack_cache: &mut dyn gix_pack::cache::DecodeEntry,
218 ) -> Result<Option<(Data<'a>, Option<gix_pack::data::entry::Location>)>, gix_object::find::Error> {
219 if let Some(mut obj_cache) = self.object_cache.as_ref().map(RefCell::borrow_mut) {
220 if let Some(kind) = obj_cache.get(&id.as_ref().to_owned(), buffer) {
221 return Ok(Some((Data::new(kind, buffer), None)));
222 }
223 }
224 let possibly_obj = self.inner.try_find_cached(id.as_ref(), buffer, pack_cache)?;
225 if let (Some(mut obj_cache), Some((obj, _location))) =
226 (self.object_cache.as_ref().map(RefCell::borrow_mut), &possibly_obj)
227 {
228 obj_cache.put(id.as_ref().to_owned(), obj.kind, obj.data);
229 }
230 Ok(possibly_obj)
231 }
232
233 fn location_by_oid(&self, id: &oid, buf: &mut Vec<u8>) -> Option<gix_pack::data::entry::Location> {
234 self.inner.location_by_oid(id, buf)
235 }
236
237 fn pack_offsets_and_oid(&self, pack_id: u32) -> Option<Vec<(u64, gix_hash::ObjectId)>> {
238 self.inner.pack_offsets_and_oid(pack_id)
239 }
240
241 fn entry_by_location(&self, location: &Location) -> Option<gix_pack::find::Entry> {
242 self.inner.entry_by_location(location)
243 }
244 }
245}