Skip to main content

mago_database/
lib.rs

1//! High-performance file database for PHP projects.
2//!
3//! This crate provides an efficient in-memory database for managing collections of PHP source files.
4//! It offers two complementary database types optimized for different access patterns:
5//!
6//! - [`Database`]: Mutable builder optimized for modifications (add, update, delete)
7//! - [`ReadDatabase`]: Immutable snapshot optimized for high-performance reads
8//!
9//! # Architecture
10//!
11//! The database uses a two-phase approach:
12//!
13//! 1. **Build Phase**: Use [`Database`] to load files, make modifications, and track changes
14//! 2. **Query Phase**: Convert to [`ReadDatabase`] via [`Database::read_only`] for fast lookups
15//!
16//! # Key Features
17//!
18//! - **Fast Lookups**: O(1) average-time access by ID, name, or filesystem path
19//! - **Change Tracking**: Record and batch apply file modifications via [`ChangeLog`]
20//! - **Deterministic Iteration**: [`ReadDatabase`] guarantees consistent iteration order
21//! - **Parallel Operations**: Concurrent file I/O and processing support
22//! - **Type Safety**: Strong typing with stable [`FileId`] handles
23//!
24//! # Common Workflow
25//!
26//! ## Loading Files
27//!
28//! Use [`loader::DatabaseLoader`] to scan a project directory:
29//!
30//! The loader handles file discovery, exclusion patterns, and parallel loading.
31//!
32//! ## Querying Files
33//!
34//! Both database types implement [`DatabaseReader`] for uniform access:
35//!
36//! ## Modifying Files
37//!
38//! Use [`ChangeLog`] to batch modifications:
39//!
40//! Changes can be applied to the database and optionally written to disk in parallel.
41//!
42//! # Performance Characteristics
43//!
44//! ## Database (Mutable)
45//!
46//! - Add/Update/Delete: O(1) average
47//! - Lookup by ID/name: O(1) average
48//! - Iteration: Unordered
49//! - Memory: ~2x file count (maps for bidirectional lookup)
50//!
51//! ## `ReadDatabase` (Immutable)
52//!
53//! - Creation: O(n log n) for sorting
54//! - Lookup by ID/name/path: O(1) average
55//! - Iteration: Deterministic, sorted by `FileId`
56//! - Memory: ~3x file count (vector + 3 index maps)
57//!
58//! # Thread Safety
59//!
60//! [`Database`] is not thread-safe and should be used from a single thread during construction.
61//! [`ReadDatabase`] can be freely shared across threads for concurrent read access.
62
63use std::borrow::Cow;
64use std::path::Path;
65use std::path::PathBuf;
66use std::sync::Arc;
67
68use foldhash::HashMap;
69use foldhash::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/// Configuration for database loading and watching.
97#[derive(Debug, Clone, Serialize, Deserialize)]
98pub struct DatabaseConfiguration<'a> {
99    pub workspace: Cow<'a, Path>,
100    /// Paths or glob patterns for source files.
101    /// Can be directory paths (e.g., "src") or glob patterns (e.g., "src/**/*.php")
102    pub paths: Vec<Cow<'a, str>>,
103    /// Paths or glob patterns for included files.
104    /// Can be directory paths (e.g., "vendor") or glob patterns (e.g., "vendor/**/*.php")
105    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/// Mutable database for managing project files with add/update/delete operations.
158#[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/// Immutable, read-optimized snapshot of the database.
166#[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    /// Updates a file's content using its stable `FileId`.
201    ///
202    /// This recalculates derived data like file size, line endings, and `FileRevision`.
203    /// If another `ReadDatabase` snapshot holds a reference to the file (preventing in-place
204    /// mutation), a new `Arc<File>` is created with the updated contents.
205    ///
206    /// Returns `true` if a file with the given ID was found and updated.
207    pub fn update(&mut self, id: FileId, new_contents: Cow<'static, str>) -> bool {
208        let Some(name) = self.id_to_name.get(&id) else {
209            return false;
210        };
211
212        let Some(arc) = self.files.get_mut(name) else {
213            return false;
214        };
215
216        if let Some(file) = Arc::get_mut(arc) {
217            file.contents = new_contents;
218            file.size = file.contents.len() as u32;
219            file.lines = line_starts(file.contents.as_ref());
220        } else {
221            // other Arc clones exist (e.g., from a ReadDatabase snapshot).
222            // Create a new File with updated contents and replace the Arc.
223            let old = &**arc;
224            *arc = Arc::new(File::new(old.name.clone(), old.file_type, old.path.clone(), new_contents));
225        }
226
227        true
228    }
229
230    /// Deletes a file from the database using its stable `FileId`.
231    ///
232    /// Returns `true` if a file with the given ID was found and removed.
233    pub fn delete(&mut self, id: FileId) -> bool {
234        if let Some(name) = self.id_to_name.remove(&id) { self.files.remove(&name).is_some() } else { false }
235    }
236
237    /// Commits a [`ChangeLog`], applying all its recorded operations to the database
238    /// and optionally writing them to the filesystem.
239    ///
240    /// # Arguments
241    ///
242    /// * `change_log`: The log of changes to apply.
243    /// * `write_to_disk`: If `true`, changes for files that have a filesystem
244    ///   path will be written to disk in parallel.
245    ///
246    /// # Errors
247    ///
248    /// Returns a [`DatabaseError`] if the log cannot be consumed or if any
249    /// filesystem operation fails.
250    pub fn commit(&mut self, change_log: ChangeLog, write_to_disk: bool) -> Result<(), DatabaseError> {
251        let changes = change_log.into_inner()?;
252        let mut fs_operations = if write_to_disk { Vec::new() } else { Vec::with_capacity(0) };
253
254        for change in changes {
255            match change {
256                Change::Add(file) => {
257                    if write_to_disk && let Some(path) = &file.path {
258                        fs_operations.push(FilesystemOperation::Write(path.clone(), file.contents.clone()));
259                    }
260
261                    self.add(file);
262                }
263                Change::Update(id, contents) => {
264                    if write_to_disk
265                        && let Ok(file) = self.get(&id)
266                        && let Some(path) = &file.path
267                    {
268                        fs_operations.push(FilesystemOperation::Write(path.clone(), contents.clone()));
269                    }
270
271                    self.update(id, contents);
272                }
273                Change::Delete(id) => {
274                    if write_to_disk
275                        && let Ok(file) = self.get(&id)
276                        && let Some(path) = &file.path
277                    {
278                        fs_operations.push(FilesystemOperation::Delete(path.clone()));
279                    }
280
281                    self.delete(id);
282                }
283            }
284        }
285
286        if write_to_disk {
287            fs_operations.into_par_iter().try_for_each(|op| -> Result<(), DatabaseError> { op.execute() })?;
288        }
289
290        Ok(())
291    }
292
293    /// Creates an independent, immutable snapshot of the database.
294    ///
295    /// This is a potentially expensive one-time operation as it **clones** all file
296    /// data. The resulting [`ReadDatabase`] is highly optimized for fast reads and
297    /// guarantees a deterministic iteration order. The original `Database` is not
298    /// consumed and can continue to be used.
299    #[must_use]
300    pub fn read_only(&self) -> ReadDatabase {
301        let mut files_vec: Vec<Arc<File>> = self.files.values().cloned().collect();
302        files_vec.sort_unstable_by_key(|f| f.id);
303
304        let mut id_to_index = HashMap::with_capacity(files_vec.len());
305        let mut name_to_index = HashMap::with_capacity(files_vec.len());
306        let mut path_to_index = HashMap::with_capacity(files_vec.len());
307
308        for (index, file) in files_vec.iter().enumerate() {
309            id_to_index.insert(file.id, index);
310            name_to_index.insert(file.name.clone(), index);
311            if let Some(path) = &file.path {
312                path_to_index.insert(path.clone(), index);
313            }
314        }
315
316        ReadDatabase { files: files_vec, id_to_index, name_to_index, path_to_index }
317    }
318}
319
320impl ReadDatabase {
321    #[must_use]
322    pub fn empty() -> Self {
323        Self {
324            files: Vec::with_capacity(0),
325            id_to_index: HashMap::with_capacity(0),
326            name_to_index: HashMap::with_capacity(0),
327            path_to_index: HashMap::with_capacity(0),
328        }
329    }
330
331    /// Creates a new `ReadDatabase` containing only a single file.
332    ///
333    /// This is a convenience constructor for situations, such as testing or
334    /// single-file tools, where an operation requires a [`DatabaseReader`]
335    /// implementation but only needs to be aware of one file.
336    ///
337    /// # Arguments
338    ///
339    /// * `file`: The single `File` to include in the database.
340    #[must_use]
341    pub fn single(file: File) -> Self {
342        let mut id_to_index = HashMap::with_capacity(1);
343        let mut name_to_index = HashMap::with_capacity(1);
344        let mut path_to_index = HashMap::with_capacity(1);
345
346        id_to_index.insert(file.id, 0);
347        name_to_index.insert(file.name.clone(), 0);
348        if let Some(path) = &file.path {
349            path_to_index.insert(path.clone(), 0);
350        }
351
352        Self { files: vec![Arc::new(file)], id_to_index, name_to_index, path_to_index }
353    }
354}
355
356/// A universal interface for reading data from any database implementation.
357///
358/// This trait provides a common API for querying file data, abstracting over
359/// whether the underlying source is the mutable [`Database`] or the read-optimized
360/// [`ReadDatabase`]. This allows for writing generic code that can operate on either.
361pub trait DatabaseReader {
362    /// Retrieves a file's stable ID using its logical name.
363    fn get_id(&self, name: &str) -> Option<FileId>;
364
365    /// Retrieves a reference to a file using its stable `FileId`.
366    ///
367    /// # Errors
368    ///
369    /// Returns `DatabaseError::FileNotFound` if no file with the given ID exists.
370    fn get(&self, id: &FileId) -> Result<Arc<File>, DatabaseError>;
371
372    /// Retrieves a reference to a file using its stable `FileId`.
373    ///
374    /// # Errors
375    ///
376    /// Returns `DatabaseError::FileNotFound` if no file with the given ID exists.
377    fn get_ref(&self, id: &FileId) -> Result<&File, DatabaseError>;
378
379    /// Retrieves a reference to a file using its logical name.
380    ///
381    /// # Errors
382    ///
383    /// Returns `DatabaseError::FileNotFound` if no file with the given name exists.
384    fn get_by_name(&self, name: &str) -> Result<Arc<File>, DatabaseError>;
385
386    /// Retrieves a reference to a file by its absolute filesystem path.
387    ///
388    /// # Errors
389    ///
390    /// Returns `DatabaseError::FileNotFound` if no file with the given path exists.
391    fn get_by_path(&self, path: &Path) -> Result<Arc<File>, DatabaseError>;
392
393    /// Returns an iterator over all files in the database.
394    ///
395    /// The order is not guaranteed for `Database`, but is sorted by `FileId`
396    /// for `ReadDatabase`, providing deterministic iteration.
397    fn files(&self) -> impl Iterator<Item = Arc<File>>;
398
399    /// Returns an iterator over all files of a specific `FileType`.
400    fn files_with_type(&self, file_type: FileType) -> impl Iterator<Item = Arc<File>> {
401        self.files().filter(move |file| file.file_type == file_type)
402    }
403
404    /// Returns an iterator over all files that do not match a specific `FileType`.
405    fn files_without_type(&self, file_type: FileType) -> impl Iterator<Item = Arc<File>> {
406        self.files().filter(move |file| file.file_type != file_type)
407    }
408
409    /// Returns an iterator over the stable IDs of all files in the database.
410    fn file_ids(&self) -> impl Iterator<Item = FileId> {
411        self.files().map(|file| file.id)
412    }
413
414    /// Returns an iterator over the stable IDs of all files of a specific `FileType`.
415    fn file_ids_with_type(&self, file_type: FileType) -> impl Iterator<Item = FileId> {
416        self.files_with_type(file_type).map(|file| file.id)
417    }
418
419    /// Returns an iterator over the stable IDs of all files that do not match a specific `FileType`.
420    fn file_ids_without_type(&self, file_type: FileType) -> impl Iterator<Item = FileId> {
421        self.files_without_type(file_type).map(|file| file.id)
422    }
423
424    /// Returns the total number of files in the database.
425    fn len(&self) -> usize;
426
427    /// Returns `true` if the database contains no files.
428    fn is_empty(&self) -> bool {
429        self.len() == 0
430    }
431}
432
433impl DatabaseReader for Database<'_> {
434    fn get_id(&self, name: &str) -> Option<FileId> {
435        self.files.get(name).map(|f| f.id)
436    }
437
438    fn get(&self, id: &FileId) -> Result<Arc<File>, DatabaseError> {
439        let name = self.id_to_name.get(id).ok_or(DatabaseError::FileNotFound)?;
440        let file = self.files.get(name).ok_or(DatabaseError::FileNotFound)?;
441
442        Ok(file.clone())
443    }
444
445    fn get_ref(&self, id: &FileId) -> Result<&File, DatabaseError> {
446        let name = self.id_to_name.get(id).ok_or(DatabaseError::FileNotFound)?;
447        self.files.get(name).map(std::convert::AsRef::as_ref).ok_or(DatabaseError::FileNotFound)
448    }
449
450    fn get_by_name(&self, name: &str) -> Result<Arc<File>, DatabaseError> {
451        self.files.get(name).cloned().ok_or(DatabaseError::FileNotFound)
452    }
453
454    fn get_by_path(&self, path: &Path) -> Result<Arc<File>, DatabaseError> {
455        self.files.values().find(|file| file.path.as_deref() == Some(path)).cloned().ok_or(DatabaseError::FileNotFound)
456    }
457
458    fn files(&self) -> impl Iterator<Item = Arc<File>> {
459        self.files.values().cloned()
460    }
461
462    fn len(&self) -> usize {
463        self.files.len()
464    }
465}
466
467impl DatabaseReader for ReadDatabase {
468    fn get_id(&self, name: &str) -> Option<FileId> {
469        self.name_to_index.get(name).and_then(|&i| self.files.get(i)).map(|f| f.id)
470    }
471
472    fn get(&self, id: &FileId) -> Result<Arc<File>, DatabaseError> {
473        let index = self.id_to_index.get(id).ok_or(DatabaseError::FileNotFound)?;
474
475        self.files.get(*index).cloned().ok_or(DatabaseError::FileNotFound)
476    }
477
478    fn get_ref(&self, id: &FileId) -> Result<&File, DatabaseError> {
479        let index = self.id_to_index.get(id).ok_or(DatabaseError::FileNotFound)?;
480
481        self.files.get(*index).map(std::convert::AsRef::as_ref).ok_or(DatabaseError::FileNotFound)
482    }
483
484    fn get_by_name(&self, name: &str) -> Result<Arc<File>, DatabaseError> {
485        self.name_to_index.get(name).and_then(|&i| self.files.get(i)).cloned().ok_or(DatabaseError::FileNotFound)
486    }
487
488    fn get_by_path(&self, path: &Path) -> Result<Arc<File>, DatabaseError> {
489        self.path_to_index.get(path).and_then(|&i| self.files.get(i)).cloned().ok_or(DatabaseError::FileNotFound)
490    }
491
492    fn files(&self) -> impl Iterator<Item = Arc<File>> {
493        self.files.iter().cloned()
494    }
495
496    fn len(&self) -> usize {
497        self.files.len()
498    }
499}