mago-database 1.21.0

Provides a high-performance, in-memory database for source code analysis, featuring distinct mutable and immutable states and transactional updates.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
//! High-performance file database for PHP projects.
//!
//! This crate provides an efficient in-memory database for managing collections of PHP source files.
//! It offers two complementary database types optimized for different access patterns:
//!
//! - [`Database`]: Mutable builder optimized for modifications (add, update, delete)
//! - [`ReadDatabase`]: Immutable snapshot optimized for high-performance reads
//!
//! # Architecture
//!
//! The database uses a two-phase approach:
//!
//! 1. **Build Phase**: Use [`Database`] to load files, make modifications, and track changes
//! 2. **Query Phase**: Convert to [`ReadDatabase`] via [`Database::read_only`] for fast lookups
//!
//! # Key Features
//!
//! - **Fast Lookups**: O(1) average-time access by ID, name, or filesystem path
//! - **Change Tracking**: Record and batch apply file modifications via [`ChangeLog`]
//! - **Deterministic Iteration**: [`ReadDatabase`] guarantees consistent iteration order
//! - **Parallel Operations**: Concurrent file I/O and processing support
//! - **Type Safety**: Strong typing with stable [`FileId`] handles
//!
//! # Common Workflow
//!
//! ## Loading Files
//!
//! Use [`loader::DatabaseLoader`] to scan a project directory:
//!
//! The loader handles file discovery, exclusion patterns, and parallel loading.
//!
//! ## Querying Files
//!
//! Both database types implement [`DatabaseReader`] for uniform access:
//!
//! ## Modifying Files
//!
//! Use [`ChangeLog`] to batch modifications:
//!
//! Changes can be applied to the database and optionally written to disk in parallel.
//!
//! # Performance Characteristics
//!
//! ## Database (Mutable)
//!
//! - Add/Update/Delete: O(1) average
//! - Lookup by ID/name: O(1) average
//! - Iteration: Unordered
//! - Memory: ~2x file count (maps for bidirectional lookup)
//!
//! ## `ReadDatabase` (Immutable)
//!
//! - Creation: O(n log n) for sorting
//! - Lookup by ID/name/path: O(1) average
//! - Iteration: Deterministic, sorted by `FileId`
//! - Memory: ~3x file count (vector + 3 index maps)
//!
//! # Thread Safety
//!
//! [`Database`] is not thread-safe and should be used from a single thread during construction.
//! [`ReadDatabase`] can be freely shared across threads for concurrent read access.

use std::borrow::Cow;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;

use foldhash::HashMap;
use foldhash::HashMapExt;
use rayon::iter::IntoParallelIterator;
use rayon::iter::ParallelIterator;
use serde::Deserialize;
use serde::Serialize;

use crate::change::Change;
use crate::change::ChangeLog;
use crate::error::DatabaseError;
use crate::exclusion::Exclusion;
use crate::file::File;
use crate::file::FileId;
use crate::file::FileType;
use crate::file::line_starts;
use crate::operation::FilesystemOperation;

mod utils;

pub mod change;
pub mod error;
pub mod exclusion;
pub mod file;
pub mod loader;
pub mod matcher;
pub mod watcher;

mod operation;

/// Configuration for database loading and watching.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatabaseConfiguration<'a> {
    pub workspace: Cow<'a, Path>,
    /// Paths or glob patterns for source files.
    /// Can be directory paths (e.g., "src") or glob patterns (e.g., "src/**/*.php")
    pub paths: Vec<Cow<'a, str>>,
    /// Paths or glob patterns for included files.
    /// Can be directory paths (e.g., "vendor") or glob patterns (e.g., "vendor/**/*.php")
    pub includes: Vec<Cow<'a, str>>,
    pub excludes: Vec<Exclusion<'a>>,
    pub extensions: Vec<Cow<'a, str>>,
    /// Settings for glob pattern matching behavior.
    pub glob: GlobSettings,
}

/// Settings for glob pattern matching behavior.
///
/// All defaults match the `globset` crate defaults for backwards compatibility.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct GlobSettings {
    /// Match patterns case-insensitively.
    ///
    /// Default: `false`.
    pub case_insensitive: bool,
    /// When `true`, a single `*` does not match path separators (`/`).
    /// This makes `src/*/Test` match only `src/foo/Test`, not `src/foo/bar/Test`.
    /// Use `**` for recursive matching.
    ///
    /// Default: `false`.
    pub literal_separator: bool,
    /// Whether `\` escapes special characters in patterns.
    ///
    /// Default: `true`.
    pub backslash_escape: bool,
    /// Whether an empty case in alternates is allowed (e.g., `{,a}` matches `""` and `"a"`).
    ///
    /// Default: `false`.
    pub empty_alternates: bool,
}

impl Default for GlobSettings {
    fn default() -> Self {
        Self {
            case_insensitive: false,
            literal_separator: false,
            backslash_escape: !std::path::is_separator('\\'),
            empty_alternates: false,
        }
    }
}

impl<'a> DatabaseConfiguration<'a> {
    pub fn new(
        workspace: &'a Path,
        paths: Vec<&'a str>,
        includes: Vec<&'a str>,
        excludes: Vec<Exclusion<'a>>,
        extensions: Vec<&'a str>,
    ) -> Self {
        let paths = paths.into_iter().map(Cow::Borrowed).collect();
        let includes = includes.into_iter().map(Cow::Borrowed).collect();

        let excludes = excludes
            .into_iter()
            .filter_map(|exclusion| match exclusion {
                Exclusion::Path(p) => Some(if p.is_absolute() {
                    Exclusion::Path(p)
                } else {
                    workspace.join(p).canonicalize().ok().map(Cow::Owned).map(Exclusion::Path)?
                }),
                Exclusion::Pattern(pat) => Some(Exclusion::Pattern(pat)),
            })
            .collect();

        let extensions = extensions.into_iter().map(Cow::Borrowed).collect();

        Self {
            workspace: Cow::Borrowed(workspace),
            paths,
            includes,
            excludes,
            extensions,
            glob: GlobSettings::default(),
        }
    }

    #[must_use]
    pub fn into_static(self) -> DatabaseConfiguration<'static> {
        DatabaseConfiguration {
            workspace: Cow::Owned(self.workspace.into_owned()),
            paths: self.paths.into_iter().map(|s| Cow::Owned(s.into_owned())).collect(),
            includes: self.includes.into_iter().map(|s| Cow::Owned(s.into_owned())).collect(),
            excludes: self
                .excludes
                .into_iter()
                .map(|e| match e {
                    Exclusion::Path(p) => Exclusion::Path(Cow::Owned(p.into_owned())),
                    Exclusion::Pattern(pat) => Exclusion::Pattern(Cow::Owned(pat.into_owned())),
                })
                .collect(),
            extensions: self.extensions.into_iter().map(|s| Cow::Owned(s.into_owned())).collect(),
            glob: self.glob,
        }
    }
}

/// Mutable database for managing project files with add/update/delete operations.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Database<'a> {
    files: HashMap<Cow<'static, str>, Arc<File>>,
    id_to_name: HashMap<FileId, Cow<'static, str>>,
    pub(crate) configuration: DatabaseConfiguration<'a>,
}

/// Immutable, read-optimized snapshot of the database.
#[derive(Debug)]
pub struct ReadDatabase {
    files: Vec<Arc<File>>,
    id_to_index: HashMap<FileId, usize>,
    name_to_index: HashMap<Cow<'static, str>, usize>,
    path_to_index: HashMap<PathBuf, usize>,
}

impl<'a> Database<'a> {
    #[must_use]
    pub fn new(configuration: DatabaseConfiguration<'a>) -> Self {
        Self { files: HashMap::default(), id_to_name: HashMap::default(), configuration }
    }

    #[must_use]
    pub fn single(file: File, configuration: DatabaseConfiguration<'a>) -> Self {
        let mut db = Self::new(configuration);
        db.add(file);
        db
    }

    /// Reserves capacity for at least `additional` more files.
    pub fn reserve(&mut self, additional: usize) {
        self.files.reserve(additional);
        self.id_to_name.reserve(additional);
    }

    pub fn add(&mut self, file: File) -> FileId {
        let name = file.name.clone();
        let id = file.id;

        if let Some(old_file) = self.files.insert(name.clone(), Arc::new(file)) {
            self.id_to_name.remove(&old_file.id);
        }

        self.id_to_name.insert(id, name);

        id
    }

    /// Updates a file's content using its stable `FileId`.
    ///
    /// This recalculates derived data like file size, line endings, and `FileRevision`.
    /// If another `ReadDatabase` snapshot holds a reference to the file (preventing in-place
    /// mutation), a new `Arc<File>` is created with the updated contents.
    ///
    /// Returns `true` if a file with the given ID was found and updated.
    pub fn update(&mut self, id: FileId, new_contents: Cow<'static, str>) -> bool {
        let Some(name) = self.id_to_name.get(&id) else {
            return false;
        };

        let Some(arc) = self.files.get_mut(name) else {
            return false;
        };

        if let Some(file) = Arc::get_mut(arc) {
            file.contents = new_contents;
            file.size = file.contents.len() as u32;
            file.lines = line_starts(file.contents.as_ref());
        } else {
            // other Arc clones exist (e.g., from a ReadDatabase snapshot).
            // Create a new File with updated contents and replace the Arc.
            let old = &**arc;
            *arc = Arc::new(File::new(old.name.clone(), old.file_type, old.path.clone(), new_contents));
        }

        true
    }

    /// Deletes a file from the database using its stable `FileId`.
    ///
    /// Returns `true` if a file with the given ID was found and removed.
    pub fn delete(&mut self, id: FileId) -> bool {
        if let Some(name) = self.id_to_name.remove(&id) { self.files.remove(&name).is_some() } else { false }
    }

    /// Commits a [`ChangeLog`], applying all its recorded operations to the database
    /// and optionally writing them to the filesystem.
    ///
    /// # Arguments
    ///
    /// * `change_log`: The log of changes to apply.
    /// * `write_to_disk`: If `true`, changes for files that have a filesystem
    ///   path will be written to disk in parallel.
    ///
    /// # Errors
    ///
    /// Returns a [`DatabaseError`] if the log cannot be consumed or if any
    /// filesystem operation fails.
    pub fn commit(&mut self, change_log: ChangeLog, write_to_disk: bool) -> Result<(), DatabaseError> {
        let changes = change_log.into_inner()?;
        let mut fs_operations = if write_to_disk { Vec::new() } else { Vec::with_capacity(0) };

        for change in changes {
            match change {
                Change::Add(file) => {
                    if write_to_disk && let Some(path) = &file.path {
                        fs_operations.push(FilesystemOperation::Write(path.clone(), file.contents.clone()));
                    }

                    self.add(file);
                }
                Change::Update(id, contents) => {
                    if write_to_disk
                        && let Ok(file) = self.get(&id)
                        && let Some(path) = &file.path
                    {
                        fs_operations.push(FilesystemOperation::Write(path.clone(), contents.clone()));
                    }

                    self.update(id, contents);
                }
                Change::Delete(id) => {
                    if write_to_disk
                        && let Ok(file) = self.get(&id)
                        && let Some(path) = &file.path
                    {
                        fs_operations.push(FilesystemOperation::Delete(path.clone()));
                    }

                    self.delete(id);
                }
            }
        }

        if write_to_disk {
            fs_operations.into_par_iter().try_for_each(|op| -> Result<(), DatabaseError> { op.execute() })?;
        }

        Ok(())
    }

    /// Creates an independent, immutable snapshot of the database.
    ///
    /// This is a potentially expensive one-time operation as it **clones** all file
    /// data. The resulting [`ReadDatabase`] is highly optimized for fast reads and
    /// guarantees a deterministic iteration order. The original `Database` is not
    /// consumed and can continue to be used.
    #[must_use]
    pub fn read_only(&self) -> ReadDatabase {
        let mut files_vec: Vec<Arc<File>> = self.files.values().cloned().collect();
        files_vec.sort_unstable_by_key(|f| f.id);

        let mut id_to_index = HashMap::with_capacity(files_vec.len());
        let mut name_to_index = HashMap::with_capacity(files_vec.len());
        let mut path_to_index = HashMap::with_capacity(files_vec.len());

        for (index, file) in files_vec.iter().enumerate() {
            id_to_index.insert(file.id, index);
            name_to_index.insert(file.name.clone(), index);
            if let Some(path) = &file.path {
                path_to_index.insert(path.clone(), index);
            }
        }

        ReadDatabase { files: files_vec, id_to_index, name_to_index, path_to_index }
    }
}

impl ReadDatabase {
    #[must_use]
    pub fn empty() -> Self {
        Self {
            files: Vec::with_capacity(0),
            id_to_index: HashMap::with_capacity(0),
            name_to_index: HashMap::with_capacity(0),
            path_to_index: HashMap::with_capacity(0),
        }
    }

    /// Creates a new `ReadDatabase` containing only a single file.
    ///
    /// This is a convenience constructor for situations, such as testing or
    /// single-file tools, where an operation requires a [`DatabaseReader`]
    /// implementation but only needs to be aware of one file.
    ///
    /// # Arguments
    ///
    /// * `file`: The single `File` to include in the database.
    #[must_use]
    pub fn single(file: File) -> Self {
        let mut id_to_index = HashMap::with_capacity(1);
        let mut name_to_index = HashMap::with_capacity(1);
        let mut path_to_index = HashMap::with_capacity(1);

        id_to_index.insert(file.id, 0);
        name_to_index.insert(file.name.clone(), 0);
        if let Some(path) = &file.path {
            path_to_index.insert(path.clone(), 0);
        }

        Self { files: vec![Arc::new(file)], id_to_index, name_to_index, path_to_index }
    }
}

/// A universal interface for reading data from any database implementation.
///
/// This trait provides a common API for querying file data, abstracting over
/// whether the underlying source is the mutable [`Database`] or the read-optimized
/// [`ReadDatabase`]. This allows for writing generic code that can operate on either.
pub trait DatabaseReader {
    /// Retrieves a file's stable ID using its logical name.
    fn get_id(&self, name: &str) -> Option<FileId>;

    /// Retrieves a reference to a file using its stable `FileId`.
    ///
    /// # Errors
    ///
    /// Returns `DatabaseError::FileNotFound` if no file with the given ID exists.
    fn get(&self, id: &FileId) -> Result<Arc<File>, DatabaseError>;

    /// Retrieves a reference to a file using its stable `FileId`.
    ///
    /// # Errors
    ///
    /// Returns `DatabaseError::FileNotFound` if no file with the given ID exists.
    fn get_ref(&self, id: &FileId) -> Result<&File, DatabaseError>;

    /// Retrieves a reference to a file using its logical name.
    ///
    /// # Errors
    ///
    /// Returns `DatabaseError::FileNotFound` if no file with the given name exists.
    fn get_by_name(&self, name: &str) -> Result<Arc<File>, DatabaseError>;

    /// Retrieves a reference to a file by its absolute filesystem path.
    ///
    /// # Errors
    ///
    /// Returns `DatabaseError::FileNotFound` if no file with the given path exists.
    fn get_by_path(&self, path: &Path) -> Result<Arc<File>, DatabaseError>;

    /// Returns an iterator over all files in the database.
    ///
    /// The order is not guaranteed for `Database`, but is sorted by `FileId`
    /// for `ReadDatabase`, providing deterministic iteration.
    fn files(&self) -> impl Iterator<Item = Arc<File>>;

    /// Returns an iterator over all files of a specific `FileType`.
    fn files_with_type(&self, file_type: FileType) -> impl Iterator<Item = Arc<File>> {
        self.files().filter(move |file| file.file_type == file_type)
    }

    /// Returns an iterator over all files that do not match a specific `FileType`.
    fn files_without_type(&self, file_type: FileType) -> impl Iterator<Item = Arc<File>> {
        self.files().filter(move |file| file.file_type != file_type)
    }

    /// Returns an iterator over the stable IDs of all files in the database.
    fn file_ids(&self) -> impl Iterator<Item = FileId> {
        self.files().map(|file| file.id)
    }

    /// Returns an iterator over the stable IDs of all files of a specific `FileType`.
    fn file_ids_with_type(&self, file_type: FileType) -> impl Iterator<Item = FileId> {
        self.files_with_type(file_type).map(|file| file.id)
    }

    /// Returns an iterator over the stable IDs of all files that do not match a specific `FileType`.
    fn file_ids_without_type(&self, file_type: FileType) -> impl Iterator<Item = FileId> {
        self.files_without_type(file_type).map(|file| file.id)
    }

    /// Returns the total number of files in the database.
    fn len(&self) -> usize;

    /// Returns `true` if the database contains no files.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl DatabaseReader for Database<'_> {
    fn get_id(&self, name: &str) -> Option<FileId> {
        self.files.get(name).map(|f| f.id)
    }

    fn get(&self, id: &FileId) -> Result<Arc<File>, DatabaseError> {
        let name = self.id_to_name.get(id).ok_or(DatabaseError::FileNotFound)?;
        let file = self.files.get(name).ok_or(DatabaseError::FileNotFound)?;

        Ok(file.clone())
    }

    fn get_ref(&self, id: &FileId) -> Result<&File, DatabaseError> {
        let name = self.id_to_name.get(id).ok_or(DatabaseError::FileNotFound)?;
        self.files.get(name).map(std::convert::AsRef::as_ref).ok_or(DatabaseError::FileNotFound)
    }

    fn get_by_name(&self, name: &str) -> Result<Arc<File>, DatabaseError> {
        self.files.get(name).cloned().ok_or(DatabaseError::FileNotFound)
    }

    fn get_by_path(&self, path: &Path) -> Result<Arc<File>, DatabaseError> {
        self.files.values().find(|file| file.path.as_deref() == Some(path)).cloned().ok_or(DatabaseError::FileNotFound)
    }

    fn files(&self) -> impl Iterator<Item = Arc<File>> {
        self.files.values().cloned()
    }

    fn len(&self) -> usize {
        self.files.len()
    }
}

impl DatabaseReader for ReadDatabase {
    fn get_id(&self, name: &str) -> Option<FileId> {
        self.name_to_index.get(name).and_then(|&i| self.files.get(i)).map(|f| f.id)
    }

    fn get(&self, id: &FileId) -> Result<Arc<File>, DatabaseError> {
        let index = self.id_to_index.get(id).ok_or(DatabaseError::FileNotFound)?;

        self.files.get(*index).cloned().ok_or(DatabaseError::FileNotFound)
    }

    fn get_ref(&self, id: &FileId) -> Result<&File, DatabaseError> {
        let index = self.id_to_index.get(id).ok_or(DatabaseError::FileNotFound)?;

        self.files.get(*index).map(std::convert::AsRef::as_ref).ok_or(DatabaseError::FileNotFound)
    }

    fn get_by_name(&self, name: &str) -> Result<Arc<File>, DatabaseError> {
        self.name_to_index.get(name).and_then(|&i| self.files.get(i)).cloned().ok_or(DatabaseError::FileNotFound)
    }

    fn get_by_path(&self, path: &Path) -> Result<Arc<File>, DatabaseError> {
        self.path_to_index.get(path).and_then(|&i| self.files.get(i)).cloned().ok_or(DatabaseError::FileNotFound)
    }

    fn files(&self) -> impl Iterator<Item = Arc<File>> {
        self.files.iter().cloned()
    }

    fn len(&self) -> usize {
        self.files.len()
    }
}