#in-memory-database #immutability #source #update #php #read-access #mago #database-transaction #file-database #thread-safe

mago-database

Provides a high-performance, in-memory database for source code analysis, featuring distinct mutable and immutable states and transactional updates

68 releases (12 stable)

Uses new Rust 2024

new 1.5.0 Feb 4, 2026
1.4.1 Feb 3, 2026
1.3.0 Jan 29, 2026
1.0.3 Dec 24, 2025
1.0.0-beta.5 Aug 31, 2025

#474 in Filesystem

Download history 889/week @ 2025-10-15 101/week @ 2025-10-22 23/week @ 2025-10-29 37/week @ 2025-11-05 123/week @ 2025-11-12 67/week @ 2025-11-19 60/week @ 2025-11-26 47/week @ 2025-12-03 56/week @ 2025-12-10 194/week @ 2025-12-17 150/week @ 2025-12-24 76/week @ 2025-12-31 799/week @ 2026-01-07 328/week @ 2026-01-14 305/week @ 2026-01-21 420/week @ 2026-01-28

1,883 downloads per month
Used in 20 crates (18 directly)

MIT/Apache

75KB
1K SLoC

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.

Dependencies

~10–24MB
~224K SLoC