1use std::borrow::Cow;
64use std::path::Path;
65use std::path::PathBuf;
66use std::sync::Arc;
67
68use ahash::HashMap;
69use ahash::HashMapExt;
70use rayon::iter::IntoParallelIterator;
71use rayon::iter::ParallelIterator;
72use serde::Deserialize;
73use serde::Serialize;
74
75use crate::change::Change;
76use crate::change::ChangeLog;
77use crate::error::DatabaseError;
78use crate::exclusion::Exclusion;
79use crate::file::File;
80use crate::file::FileId;
81use crate::file::FileType;
82use crate::file::line_starts;
83use crate::operation::FilesystemOperation;
84
85mod utils;
86
87pub mod change;
88pub mod error;
89pub mod exclusion;
90pub mod file;
91pub mod loader;
92pub mod watcher;
93
94mod operation;
95
96#[derive(Debug, Clone, Serialize, Deserialize)]
98pub struct DatabaseConfiguration<'a> {
99 pub workspace: Cow<'a, Path>,
100 pub paths: Vec<Cow<'a, str>>,
103 pub includes: Vec<Cow<'a, str>>,
106 pub excludes: Vec<Exclusion<'a>>,
107 pub extensions: Vec<Cow<'a, str>>,
108}
109
110impl<'a> DatabaseConfiguration<'a> {
111 pub fn new(
112 workspace: &'a Path,
113 paths: Vec<&'a str>,
114 includes: Vec<&'a str>,
115 excludes: Vec<Exclusion<'a>>,
116 extensions: Vec<&'a str>,
117 ) -> Self {
118 let paths = paths.into_iter().map(Cow::Borrowed).collect();
119 let includes = includes.into_iter().map(Cow::Borrowed).collect();
120
121 let excludes = excludes
122 .into_iter()
123 .filter_map(|exclusion| match exclusion {
124 Exclusion::Path(p) => Some(if p.is_absolute() {
125 Exclusion::Path(p)
126 } else {
127 workspace.join(p).canonicalize().ok().map(Cow::Owned).map(Exclusion::Path)?
128 }),
129 Exclusion::Pattern(pat) => Some(Exclusion::Pattern(pat)),
130 })
131 .collect();
132
133 let extensions = extensions.into_iter().map(Cow::Borrowed).collect();
134
135 Self { workspace: Cow::Borrowed(workspace), paths, includes, excludes, extensions }
136 }
137
138 #[must_use]
139 pub fn into_static(self) -> DatabaseConfiguration<'static> {
140 DatabaseConfiguration {
141 workspace: Cow::Owned(self.workspace.into_owned()),
142 paths: self.paths.into_iter().map(|s| Cow::Owned(s.into_owned())).collect(),
143 includes: self.includes.into_iter().map(|s| Cow::Owned(s.into_owned())).collect(),
144 excludes: self
145 .excludes
146 .into_iter()
147 .map(|e| match e {
148 Exclusion::Path(p) => Exclusion::Path(Cow::Owned(p.into_owned())),
149 Exclusion::Pattern(pat) => Exclusion::Pattern(Cow::Owned(pat.into_owned())),
150 })
151 .collect(),
152 extensions: self.extensions.into_iter().map(|s| Cow::Owned(s.into_owned())).collect(),
153 }
154 }
155}
156
157#[derive(Debug, Clone, Serialize, Deserialize)]
159pub struct Database<'a> {
160 files: HashMap<Cow<'static, str>, Arc<File>>,
161 id_to_name: HashMap<FileId, Cow<'static, str>>,
162 pub(crate) configuration: DatabaseConfiguration<'a>,
163}
164
165#[derive(Debug)]
167pub struct ReadDatabase {
168 files: Vec<Arc<File>>,
169 id_to_index: HashMap<FileId, usize>,
170 name_to_index: HashMap<Cow<'static, str>, usize>,
171 path_to_index: HashMap<PathBuf, usize>,
172}
173
174impl<'a> Database<'a> {
175 #[must_use]
176 pub fn new(configuration: DatabaseConfiguration<'a>) -> Self {
177 Self { files: HashMap::default(), id_to_name: HashMap::default(), configuration }
178 }
179
180 #[must_use]
181 pub fn single(file: File, configuration: DatabaseConfiguration<'a>) -> Self {
182 let mut db = Self::new(configuration);
183 db.add(file);
184 db
185 }
186
187 pub fn add(&mut self, file: File) -> FileId {
188 let name = file.name.clone();
189 let id = file.id;
190
191 if let Some(old_file) = self.files.insert(name.clone(), Arc::new(file)) {
192 self.id_to_name.remove(&old_file.id);
193 }
194
195 self.id_to_name.insert(id, name);
196
197 id
198 }
199
200 pub fn update(&mut self, id: FileId, new_contents: Cow<'static, str>) -> bool {
205 if let Some(name) = self.id_to_name.get(&id)
206 && let Some(file) = self.files.get_mut(name)
207 && let Some(file) = Arc::get_mut(file)
208 {
209 file.contents = new_contents;
210 file.size = file.contents.len() as u32;
211 file.lines = line_starts(file.contents.as_ref());
212 return true;
213 }
214 false
215 }
216
217 pub fn delete(&mut self, id: FileId) -> bool {
221 if let Some(name) = self.id_to_name.remove(&id) { self.files.remove(&name).is_some() } else { false }
222 }
223
224 pub fn commit(&mut self, change_log: ChangeLog, write_to_disk: bool) -> Result<(), DatabaseError> {
238 let changes = change_log.into_inner()?;
239 let mut fs_operations = if write_to_disk { Vec::new() } else { Vec::with_capacity(0) };
240
241 for change in changes {
242 match change {
243 Change::Add(file) => {
244 if write_to_disk && let Some(path) = &file.path {
245 fs_operations.push(FilesystemOperation::Write(path.clone(), file.contents.clone()));
246 }
247
248 self.add(file);
249 }
250 Change::Update(id, contents) => {
251 if write_to_disk
252 && let Ok(file) = self.get(&id)
253 && let Some(path) = &file.path
254 {
255 fs_operations.push(FilesystemOperation::Write(path.clone(), contents.clone()));
256 }
257
258 self.update(id, contents);
259 }
260 Change::Delete(id) => {
261 if write_to_disk
262 && let Ok(file) = self.get(&id)
263 && let Some(path) = &file.path
264 {
265 fs_operations.push(FilesystemOperation::Delete(path.clone()));
266 }
267
268 self.delete(id);
269 }
270 }
271 }
272
273 if write_to_disk {
274 fs_operations.into_par_iter().try_for_each(|op| -> Result<(), DatabaseError> { op.execute() })?;
275 }
276
277 Ok(())
278 }
279
280 #[must_use]
287 pub fn read_only(&self) -> ReadDatabase {
288 let mut files_vec: Vec<Arc<File>> = self.files.values().cloned().collect();
289 files_vec.sort_unstable_by_key(|f| f.id);
290
291 let mut id_to_index = HashMap::with_capacity(files_vec.len());
292 let mut name_to_index = HashMap::with_capacity(files_vec.len());
293 let mut path_to_index = HashMap::with_capacity(files_vec.len());
294
295 for (index, file) in files_vec.iter().enumerate() {
296 id_to_index.insert(file.id, index);
297 name_to_index.insert(file.name.clone(), index);
298 if let Some(path) = &file.path {
299 path_to_index.insert(path.clone(), index);
300 }
301 }
302
303 ReadDatabase { files: files_vec, id_to_index, name_to_index, path_to_index }
304 }
305}
306
307impl ReadDatabase {
308 #[must_use]
309 pub fn empty() -> Self {
310 Self {
311 files: Vec::with_capacity(0),
312 id_to_index: HashMap::with_capacity(0),
313 name_to_index: HashMap::with_capacity(0),
314 path_to_index: HashMap::with_capacity(0),
315 }
316 }
317
318 #[must_use]
328 pub fn single(file: File) -> Self {
329 let mut id_to_index = HashMap::with_capacity(1);
330 let mut name_to_index = HashMap::with_capacity(1);
331 let mut path_to_index = HashMap::with_capacity(1);
332
333 id_to_index.insert(file.id, 0);
334 name_to_index.insert(file.name.clone(), 0);
335 if let Some(path) = &file.path {
336 path_to_index.insert(path.clone(), 0);
337 }
338
339 Self { files: vec![Arc::new(file)], id_to_index, name_to_index, path_to_index }
340 }
341}
342
343pub trait DatabaseReader {
349 fn get_id(&self, name: &str) -> Option<FileId>;
351
352 fn get(&self, id: &FileId) -> Result<Arc<File>, DatabaseError>;
358
359 fn get_ref(&self, id: &FileId) -> Result<&File, DatabaseError>;
365
366 fn get_by_name(&self, name: &str) -> Result<Arc<File>, DatabaseError>;
372
373 fn get_by_path(&self, path: &Path) -> Result<Arc<File>, DatabaseError>;
379
380 fn files(&self) -> impl Iterator<Item = Arc<File>>;
385
386 fn files_with_type(&self, file_type: FileType) -> impl Iterator<Item = Arc<File>> {
388 self.files().filter(move |file| file.file_type == file_type)
389 }
390
391 fn files_without_type(&self, file_type: FileType) -> impl Iterator<Item = Arc<File>> {
393 self.files().filter(move |file| file.file_type != file_type)
394 }
395
396 fn file_ids(&self) -> impl Iterator<Item = FileId> {
398 self.files().map(|file| file.id)
399 }
400
401 fn file_ids_with_type(&self, file_type: FileType) -> impl Iterator<Item = FileId> {
403 self.files_with_type(file_type).map(|file| file.id)
404 }
405
406 fn file_ids_without_type(&self, file_type: FileType) -> impl Iterator<Item = FileId> {
408 self.files_without_type(file_type).map(|file| file.id)
409 }
410
411 fn len(&self) -> usize;
413
414 fn is_empty(&self) -> bool {
416 self.len() == 0
417 }
418}
419
420impl DatabaseReader for Database<'_> {
421 fn get_id(&self, name: &str) -> Option<FileId> {
422 self.files.get(name).map(|f| f.id)
423 }
424
425 fn get(&self, id: &FileId) -> Result<Arc<File>, DatabaseError> {
426 let name = self.id_to_name.get(id).ok_or(DatabaseError::FileNotFound)?;
427 let file = self.files.get(name).ok_or(DatabaseError::FileNotFound)?;
428
429 Ok(file.clone())
430 }
431
432 fn get_ref(&self, id: &FileId) -> Result<&File, DatabaseError> {
433 let name = self.id_to_name.get(id).ok_or(DatabaseError::FileNotFound)?;
434 self.files.get(name).map(std::convert::AsRef::as_ref).ok_or(DatabaseError::FileNotFound)
435 }
436
437 fn get_by_name(&self, name: &str) -> Result<Arc<File>, DatabaseError> {
438 self.files.get(name).cloned().ok_or(DatabaseError::FileNotFound)
439 }
440
441 fn get_by_path(&self, path: &Path) -> Result<Arc<File>, DatabaseError> {
442 self.files.values().find(|file| file.path.as_deref() == Some(path)).cloned().ok_or(DatabaseError::FileNotFound)
443 }
444
445 fn files(&self) -> impl Iterator<Item = Arc<File>> {
446 self.files.values().cloned()
447 }
448
449 fn len(&self) -> usize {
450 self.files.len()
451 }
452}
453
454impl DatabaseReader for ReadDatabase {
455 fn get_id(&self, name: &str) -> Option<FileId> {
456 self.name_to_index.get(name).and_then(|&i| self.files.get(i)).map(|f| f.id)
457 }
458
459 fn get(&self, id: &FileId) -> Result<Arc<File>, DatabaseError> {
460 let index = self.id_to_index.get(id).ok_or(DatabaseError::FileNotFound)?;
461
462 self.files.get(*index).cloned().ok_or(DatabaseError::FileNotFound)
463 }
464
465 fn get_ref(&self, id: &FileId) -> Result<&File, DatabaseError> {
466 let index = self.id_to_index.get(id).ok_or(DatabaseError::FileNotFound)?;
467
468 self.files.get(*index).map(std::convert::AsRef::as_ref).ok_or(DatabaseError::FileNotFound)
469 }
470
471 fn get_by_name(&self, name: &str) -> Result<Arc<File>, DatabaseError> {
472 self.name_to_index.get(name).and_then(|&i| self.files.get(i)).cloned().ok_or(DatabaseError::FileNotFound)
473 }
474
475 fn get_by_path(&self, path: &Path) -> Result<Arc<File>, DatabaseError> {
476 self.path_to_index.get(path).and_then(|&i| self.files.get(i)).cloned().ok_or(DatabaseError::FileNotFound)
477 }
478
479 fn files(&self) -> impl Iterator<Item = Arc<File>> {
480 self.files.iter().cloned()
481 }
482
483 fn len(&self) -> usize {
484 self.files.len()
485 }
486}