| 1 | # journal-registry |
| 2 | |
| 3 | This crate watches directories for journal files, parses their metadata from |
| 4 | filenames, organizes them efficiently, and keeps the collection updated as |
| 5 | files are created, rotated, or deleted. |
| 6 | |
| 7 | ## How to use it |
| 8 | |
| 9 | Start by creating a monitor and registry, then watch directories: |
| 10 | |
| 11 | ```rust |
| 12 | use journal_registry::{Registry, Monitor}; |
| 13 | |
| 14 | #[tokio::main] |
| 15 | async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 16 | let (monitor, mut event_receiver) = Monitor::new()?; |
| 17 | let registry = Registry::new(monitor); |
| 18 | |
| 19 | registry.watch_directory("/var/log/journal")?; |
| 20 | |
| 21 | // Process filesystem events in the background |
| 22 | let registry_clone = registry.clone(); |
| 23 | tokio::spawn(async move { |
| 24 | while let Some(event) = event_receiver.recv().await { |
| 25 | registry_clone.process_event(event).ok(); |
| 26 | } |
| 27 | }); |
| 28 | |
| 29 | Ok(()) |
| 30 | } |
| 31 | ``` |
| 32 | |
| 33 | Query for files in a time range: |
| 34 | |
| 35 | ```rust |
| 36 | let files = registry.find_files_in_range(start_sec, end_sec)?; |
| 37 | |
| 38 | for file_info in files { |
| 39 | println!("{}", file_info.file.path()); |
| 40 | } |
| 41 | ``` |
| 42 | |
| 43 | Update metadata after indexing: |
| 44 | |
| 45 | ```rust |
| 46 | registry.update_time_range(&file, start_time, end_time, indexed_at, online); |
| 47 | ``` |
| 48 | |
| 49 | ## How it works |
| 50 | |
| 51 | Journal files follow systemd's naming convention. Active files are named |
| 52 | like `system.journal` or `user-1000.journal`. Archived files append |
| 53 | metadata: `system@<seqnum_id>-<head_seqnum>-<head_realtime>.journal`. |
| 54 | Corrupted files end with `.journal~`. |
| 55 | |
| 56 | The registry organizes files into a three-level hierarchy: |
| 57 | directories contain origins (system, user, remote), and each origin has a |
| 58 | chain of files sorted by status and time. Disposed files come first, |
| 59 | followed by archived files in chronological order, with the active file |
| 60 | last. This ordering makes time-range queries efficient. |
| 61 | |
| 62 | Files start with unknown time ranges. After you index them and call |
| 63 | `update_time_range`, the registry uses this metadata to filter queries |
| 64 | appropriately. Files with bounded ranges are included only if they overlap |
| 65 | the requested time window, while unknown and active files are always included. |
| 66 | |
| 67 | The monitor watches directories recursively and sends create, delete, and |
| 68 | rename events through an async channel. The registry processes these to keep |
| 69 | the collection current. |