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 git_pack::cache::DecodeEntry + Send + 'static;
12pub type NewPackCacheFn = dyn Fn() -> Box<PackCache> + Send + Sync + 'static;
14
15pub type ObjectCache = dyn git_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: git_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::{io::Read, ops::DerefMut};
137
138 use git_hash::{oid, ObjectId};
139 use git_object::{Data, Kind};
140 use git_pack::cache::Object;
141
142 use crate::{find::Header, pack::data::entry::Location, Cache};
143
144 impl<S> crate::Write for Cache<S>
145 where
146 S: crate::Write,
147 {
148 type Error = S::Error;
149
150 fn write_stream(&self, kind: Kind, size: u64, from: impl Read) -> Result<ObjectId, Self::Error> {
151 self.inner.write_stream(kind, size, from)
152 }
153 }
154
155 impl<S> crate::Find for Cache<S>
156 where
157 S: git_pack::Find,
158 {
159 type Error = S::Error;
160
161 fn contains(&self, id: impl AsRef<oid>) -> bool {
162 self.inner.contains(id)
163 }
164
165 fn try_find<'a>(&self, id: impl AsRef<oid>, buffer: &'a mut Vec<u8>) -> Result<Option<Data<'a>>, Self::Error> {
166 git_pack::Find::try_find(self, id, buffer).map(|t| t.map(|t| t.0))
167 }
168 }
169
170 impl<S> crate::Header for Cache<S>
171 where
172 S: crate::Header,
173 {
174 type Error = S::Error;
175
176 fn try_header(&self, id: impl AsRef<oid>) -> Result<Option<Header>, Self::Error> {
177 self.inner.try_header(id)
178 }
179 }
180
181 impl<S> git_pack::Find for Cache<S>
182 where
183 S: git_pack::Find,
184 {
185 type Error = S::Error;
186
187 fn contains(&self, id: impl AsRef<oid>) -> bool {
188 self.inner.contains(id)
189 }
190
191 fn try_find<'a>(
192 &self,
193 id: impl AsRef<oid>,
194 buffer: &'a mut Vec<u8>,
195 ) -> Result<Option<(Data<'a>, Option<Location>)>, Self::Error> {
196 match self.pack_cache.as_ref().map(|rc| rc.borrow_mut()) {
197 Some(mut pack_cache) => self.try_find_cached(id, buffer, pack_cache.deref_mut()),
198 None => self.try_find_cached(id, buffer, &mut git_pack::cache::Never),
199 }
200 }
201
202 fn try_find_cached<'a>(
203 &self,
204 id: impl AsRef<oid>,
205 buffer: &'a mut Vec<u8>,
206 pack_cache: &mut impl git_pack::cache::DecodeEntry,
207 ) -> Result<Option<(Data<'a>, Option<git_pack::data::entry::Location>)>, Self::Error> {
208 if let Some(mut obj_cache) = self.object_cache.as_ref().map(|rc| rc.borrow_mut()) {
209 if let Some(kind) = obj_cache.get(&id.as_ref().to_owned(), buffer) {
210 return Ok(Some((Data::new(kind, buffer), None)));
211 }
212 }
213 let possibly_obj = self.inner.try_find_cached(id.as_ref(), buffer, pack_cache)?;
214 if let (Some(mut obj_cache), Some((obj, _location))) =
215 (self.object_cache.as_ref().map(|rc| rc.borrow_mut()), &possibly_obj)
216 {
217 obj_cache.put(id.as_ref().to_owned(), obj.kind, obj.data);
218 }
219 Ok(possibly_obj)
220 }
221
222 fn location_by_oid(&self, id: impl AsRef<oid>, buf: &mut Vec<u8>) -> Option<git_pack::data::entry::Location> {
223 self.inner.location_by_oid(id, buf)
224 }
225
226 fn pack_offsets_and_oid(&self, pack_id: u32) -> Option<Vec<(u64, git_hash::ObjectId)>> {
227 self.inner.pack_offsets_and_oid(pack_id)
228 }
229
230 fn entry_by_location(&self, location: &Location) -> Option<git_pack::find::Entry> {
231 self.inner.entry_by_location(location)
232 }
233 }
234}