max / audiofiles
1 file changed,
+36 insertions,
-0 deletions
| @@ -1453,9 +1453,20 @@ impl Database { | |||
| 1453 | 1453 | pub fn open(path: impl AsRef<Path>) -> Result<Self, DbError> { | |
| 1454 | 1454 | let conn = Connection::open(path)?; | |
| 1455 | 1455 | conn.execute_batch( | |
| 1456 | + | // WAL + synchronous=NORMAL is the standard durable-but-fast pairing: | |
| 1457 | + | // commits no longer fsync individually (only at checkpoint), which is | |
| 1458 | + | // what made the import path ~2 fsyncs/file. NORMAL under WAL can lose | |
| 1459 | + | // only the last few committed transactions on power loss — never | |
| 1460 | + | // corruption — acceptable for a local sample library. The cache / | |
| 1461 | + | // mmap / temp_store pragmas cut page churn on large scans and the | |
| 1462 | + | // import write batch. | |
| 1456 | 1463 | "PRAGMA journal_mode=WAL;\ | |
| 1464 | + | PRAGMA synchronous=NORMAL;\ | |
| 1457 | 1465 | PRAGMA foreign_keys=ON;\ | |
| 1458 | 1466 | PRAGMA busy_timeout=5000;\ | |
| 1467 | + | PRAGMA cache_size=-16000;\ | |
| 1468 | + | PRAGMA mmap_size=268435456;\ | |
| 1469 | + | PRAGMA temp_store=MEMORY;\ | |
| 1459 | 1470 | PRAGMA wal_checkpoint(TRUNCATE);", | |
| 1460 | 1471 | )?; | |
| 1461 | 1472 | register_hash_row_id(&conn)?; | |
| @@ -1662,6 +1673,31 @@ mod tests { | |||
| 1662 | 1673 | use super::*; | |
| 1663 | 1674 | ||
| 1664 | 1675 | #[test] | |
| 1676 | + | fn file_db_applies_performance_pragmas() { | |
| 1677 | + | let dir = tempfile::tempdir().unwrap(); | |
| 1678 | + | let db = Database::open(dir.path().join("audiofiles.db")).unwrap(); | |
| 1679 | + | ||
| 1680 | + | let journal: String = db | |
| 1681 | + | .conn() | |
| 1682 | + | .query_row("PRAGMA journal_mode", [], |r| r.get(0)) | |
| 1683 | + | .unwrap(); | |
| 1684 | + | assert_eq!(journal.to_lowercase(), "wal"); | |
| 1685 | + | ||
| 1686 | + | // synchronous: 0=OFF, 1=NORMAL, 2=FULL. We want NORMAL under WAL. | |
| 1687 | + | let synchronous: i64 = db | |
| 1688 | + | .conn() | |
| 1689 | + | .query_row("PRAGMA synchronous", [], |r| r.get(0)) | |
| 1690 | + | .unwrap(); | |
| 1691 | + | assert_eq!(synchronous, 1, "synchronous should be NORMAL"); | |
| 1692 | + | ||
| 1693 | + | let temp_store: i64 = db | |
| 1694 | + | .conn() | |
| 1695 | + | .query_row("PRAGMA temp_store", [], |r| r.get(0)) | |
| 1696 | + | .unwrap(); | |
| 1697 | + | assert_eq!(temp_store, 2, "temp_store should be MEMORY"); | |
| 1698 | + | } | |
| 1699 | + | ||
| 1700 | + | #[test] | |
| 1665 | 1701 | fn open_in_memory_creates_all_tables() { | |
| 1666 | 1702 | let db = Database::open_in_memory().unwrap(); | |
| 1667 | 1703 |