1#![expect(missing_docs)]
16
17use std::fmt::Debug;
18use std::fmt::Formatter;
19use std::pin::Pin;
20use std::sync::Arc;
21use std::sync::Mutex;
22use std::time::SystemTime;
23
24use clru::CLruCache;
25use futures::stream::BoxStream;
26use pollster::FutureExt as _;
27use tokio::io::AsyncRead;
28
29use crate::backend;
30use crate::backend::Backend;
31use crate::backend::BackendResult;
32use crate::backend::ChangeId;
33use crate::backend::CommitId;
34use crate::backend::CopyRecord;
35use crate::backend::FileId;
36use crate::backend::SigningFn;
37use crate::backend::SymlinkId;
38use crate::backend::TreeId;
39use crate::commit::Commit;
40use crate::index::Index;
41use crate::merge::Merge;
42use crate::merged_tree::MergedTree;
43use crate::repo_path::RepoPath;
44use crate::repo_path::RepoPathBuf;
45use crate::signing::Signer;
46use crate::tree::Tree;
47use crate::tree_merge::MergeOptions;
48
49pub(crate) const COMMIT_CACHE_CAPACITY: usize = 100;
52const TREE_CACHE_CAPACITY: usize = 1000;
53
54pub struct Store {
57 backend: Box<dyn Backend>,
58 signer: Signer,
59 commit_cache: Mutex<CLruCache<CommitId, Arc<backend::Commit>>>,
60 tree_cache: Mutex<CLruCache<(RepoPathBuf, TreeId), Arc<backend::Tree>>>,
61 merge_options: MergeOptions,
62}
63
64impl Debug for Store {
65 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
66 f.debug_struct("Store")
67 .field("backend", &self.backend)
68 .finish_non_exhaustive()
69 }
70}
71
72impl Store {
73 pub fn new(
74 backend: Box<dyn Backend>,
75 signer: Signer,
76 merge_options: MergeOptions,
77 ) -> Arc<Self> {
78 Arc::new(Self {
79 backend,
80 signer,
81 commit_cache: Mutex::new(CLruCache::new(COMMIT_CACHE_CAPACITY.try_into().unwrap())),
82 tree_cache: Mutex::new(CLruCache::new(TREE_CACHE_CAPACITY.try_into().unwrap())),
83 merge_options,
84 })
85 }
86
87 pub fn backend(&self) -> &dyn Backend {
88 self.backend.as_ref()
89 }
90
91 pub fn backend_impl<T: Backend>(&self) -> Option<&T> {
93 self.backend.downcast_ref()
94 }
95
96 pub fn signer(&self) -> &Signer {
97 &self.signer
98 }
99
100 pub fn merge_options(&self) -> &MergeOptions {
102 &self.merge_options
103 }
104
105 pub fn get_copy_records(
106 &self,
107 paths: Option<&[RepoPathBuf]>,
108 root: &CommitId,
109 head: &CommitId,
110 ) -> BackendResult<BoxStream<'_, BackendResult<CopyRecord>>> {
111 self.backend.get_copy_records(paths, root, head)
112 }
113
114 pub fn commit_id_length(&self) -> usize {
115 self.backend.commit_id_length()
116 }
117
118 pub fn change_id_length(&self) -> usize {
119 self.backend.change_id_length()
120 }
121
122 pub fn root_commit_id(&self) -> &CommitId {
123 self.backend.root_commit_id()
124 }
125
126 pub fn root_change_id(&self) -> &ChangeId {
127 self.backend.root_change_id()
128 }
129
130 pub fn empty_tree_id(&self) -> &TreeId {
131 self.backend.empty_tree_id()
132 }
133
134 pub fn concurrency(&self) -> usize {
135 self.backend.concurrency()
136 }
137
138 pub fn empty_merged_tree(self: &Arc<Self>) -> MergedTree {
139 let empty_tree_id = self.backend.empty_tree_id().clone();
140 MergedTree::resolved(self.clone(), empty_tree_id)
141 }
142
143 pub fn empty_merged_tree_id(&self) -> Merge<TreeId> {
144 Merge::resolved(self.backend.empty_tree_id().clone())
145 }
146
147 pub fn root_commit(self: &Arc<Self>) -> Commit {
148 self.get_commit(self.backend.root_commit_id()).unwrap()
149 }
150
151 pub fn get_commit(self: &Arc<Self>, id: &CommitId) -> BackendResult<Commit> {
152 self.get_commit_async(id).block_on()
153 }
154
155 pub async fn get_commit_async(self: &Arc<Self>, id: &CommitId) -> BackendResult<Commit> {
156 let data = self.get_backend_commit(id).await?;
157 Ok(Commit::new(self.clone(), id.clone(), data))
158 }
159
160 async fn get_backend_commit(&self, id: &CommitId) -> BackendResult<Arc<backend::Commit>> {
161 {
162 let mut locked_cache = self.commit_cache.lock().unwrap();
163 if let Some(data) = locked_cache.get(id).cloned() {
164 return Ok(data);
165 }
166 }
167 let commit = self.backend.read_commit(id).await?;
168 let data = Arc::new(commit);
169 let mut locked_cache = self.commit_cache.lock().unwrap();
170 locked_cache.put(id.clone(), data.clone());
171 Ok(data)
172 }
173
174 pub async fn write_commit(
175 self: &Arc<Self>,
176 commit: backend::Commit,
177 sign_with: Option<&mut SigningFn<'_>>,
178 ) -> BackendResult<Commit> {
179 assert!(!commit.parents.is_empty());
180
181 let (commit_id, commit) = self.backend.write_commit(commit, sign_with).await?;
182 let data = Arc::new(commit);
183 {
184 let mut locked_cache = self.commit_cache.lock().unwrap();
185 locked_cache.put(commit_id.clone(), data.clone());
186 }
187
188 Ok(Commit::new(self.clone(), commit_id, data))
189 }
190
191 pub async fn get_tree(self: &Arc<Self>, dir: RepoPathBuf, id: &TreeId) -> BackendResult<Tree> {
192 let data = self.get_backend_tree(&dir, id).await?;
193 Ok(Tree::new(self.clone(), dir, id.clone(), data))
194 }
195
196 async fn get_backend_tree(
197 &self,
198 dir: &RepoPath,
199 id: &TreeId,
200 ) -> BackendResult<Arc<backend::Tree>> {
201 let key = (dir.to_owned(), id.clone());
202 {
203 let mut locked_cache = self.tree_cache.lock().unwrap();
204 if let Some(data) = locked_cache.get(&key).cloned() {
205 return Ok(data);
206 }
207 }
208 let data = self.backend.read_tree(dir, id).await?;
209 let data = Arc::new(data);
210 let mut locked_cache = self.tree_cache.lock().unwrap();
211 locked_cache.put(key, data.clone());
212 Ok(data)
213 }
214
215 pub async fn write_tree(
216 self: &Arc<Self>,
217 path: &RepoPath,
218 tree: backend::Tree,
219 ) -> BackendResult<Tree> {
220 let tree_id = self.backend.write_tree(path, &tree).await?;
221 let data = Arc::new(tree);
222 {
223 let mut locked_cache = self.tree_cache.lock().unwrap();
224 locked_cache.put((path.to_owned(), tree_id.clone()), data.clone());
225 }
226
227 Ok(Tree::new(self.clone(), path.to_owned(), tree_id, data))
228 }
229
230 pub async fn read_file(
231 &self,
232 path: &RepoPath,
233 id: &FileId,
234 ) -> BackendResult<Pin<Box<dyn AsyncRead + Send>>> {
235 self.backend.read_file(path, id).await
236 }
237
238 pub async fn write_file(
239 &self,
240 path: &RepoPath,
241 contents: &mut (dyn AsyncRead + Send + Unpin),
242 ) -> BackendResult<FileId> {
243 self.backend.write_file(path, contents).await
244 }
245
246 pub async fn read_symlink(&self, path: &RepoPath, id: &SymlinkId) -> BackendResult<String> {
247 self.backend.read_symlink(path, id).await
248 }
249
250 pub async fn write_symlink(&self, path: &RepoPath, contents: &str) -> BackendResult<SymlinkId> {
251 self.backend.write_symlink(path, contents).await
252 }
253
254 pub fn gc(&self, index: &dyn Index, keep_newer: SystemTime) -> BackendResult<()> {
255 self.backend.gc(index, keep_newer)
256 }
257
258 pub fn clear_caches(&self) {
260 self.commit_cache.lock().unwrap().clear();
261 self.tree_cache.lock().unwrap().clear();
262 }
263}