systemd-journal-sdk-registry 0.7.8

Directory registry primitives for the pure Rust systemd journal SDK
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
//! Journal file repository
//!
//! This module provides types and data structures for representing and organizing
//! systemd journal files into a queryable repository.
//!
//! ## Key Components
//!
//! - **File**: Represents a journal file with parsed metadata (origin, status)
//! - **Origin**: Identifies where a journal file comes from (system, user, remote)
//! - **Status**: Indicates whether a file is active, archived, or disposed
//! - **Chain**: An ordered collection of journal files from the same origin
//! - **Repository**: The top-level container organizing chains by directory and origin
//! - **FileInfo**: Associates a file with its time range metadata
//! - **TimeRange**: Tracks the temporal bounds of indexed journal files
//!
//! ## Architecture
//!
//! The repository organizes files in a three-level hierarchy:
//! ```text
//! Repository
//!   └─ Directory (/var/log/journal)
//!       └─ Origin (System, User(1000), Remote("host"))
//!           └─ Chain (ordered list of files)
//! ```
//!
//! Files within a chain are kept sorted:
//! - Disposed files (corrupted) come first
//! - Archived files follow in chronological order
//! - Active file (if any) comes last

// Public modules - accessible to workspace crates via full paths
pub mod collection;
pub mod error;
pub mod file;
pub mod metadata;

// Re-export only the public API types
pub use crate::repository::file::{File, Origin, Source, Status};
pub use crate::repository::metadata::FileInfo;

// Re-export workspace-internal types (hidden from public docs)
// These are not in lib.rs exports but accessible via full paths for workspace crates
#[doc(hidden)]
pub use crate::repository::collection::{Chain, Repository};
#[doc(hidden)]
pub use crate::repository::error::RepositoryError;

// Crate-internal only
pub(crate) use crate::repository::file::scan_journal_files;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::repository::collection::Chain;
    use crate::repository::file::FileInner;
    use journal_common::Seconds;
    use journal_common::collections::VecDeque;
    use std::sync::Arc;
    use uuid::Uuid;

    const USEC_PER_SEC: u64 = std::time::Duration::from_secs(1).as_micros() as u64;

    fn create_test_origin() -> Origin {
        Origin {
            machine_id: Some(Uuid::new_v4()),
            namespace: None,
            source: Source::System,
        }
    }

    fn create_archived_file(origin: &Origin, head_realtime: u64) -> File {
        let inner = FileInner {
            path: format!("/var/log/journal/system@{}.journal", head_realtime),
            origin: origin.clone(),
            status: Status::Archived {
                seqnum_id: Uuid::new_v4(),
                head_seqnum: 1000 + head_realtime,
                head_realtime,
            },
        };

        File {
            inner: Arc::new(inner),
        }
    }

    fn create_active_file(origin: &Origin) -> File {
        let inner = FileInner {
            path: "/var/log/journal/system.journal".to_string(),
            origin: origin.clone(),
            status: Status::Active,
        };

        File {
            inner: Arc::new(inner),
        }
    }

    fn create_disposed_file(origin: &Origin, timestamp: u64, number: u64) -> File {
        let inner = FileInner {
            path: format!("/var/log/journal/system@{}-{}.journal~", timestamp, number),
            origin: origin.clone(),
            status: Status::Disposed { timestamp, number },
        };

        File {
            inner: Arc::new(inner),
        }
    }

    #[test]
    fn test_find_files_in_range_empty_chain() {
        let chain = Chain {
            files: VecDeque::new(),
        };

        let mut files = Vec::new();
        chain.find_files_in_range(Seconds(100), Seconds(200), &mut files);
        assert!(files.is_empty());
    }

    #[test]
    fn test_find_files_in_range_invalid_range() {
        let origin = create_test_origin();
        let mut chain = Chain {
            files: VecDeque::new(),
        };

        // Add some files
        chain
            .files
            .push_back(create_archived_file(&origin, 100 * USEC_PER_SEC));
        chain
            .files
            .push_back(create_archived_file(&origin, 200 * USEC_PER_SEC));

        let mut files = Vec::new();
        // Test with start >= end
        chain.find_files_in_range(Seconds(200), Seconds(200), &mut files);
        assert!(files.is_empty());

        chain.find_files_in_range(Seconds(200), Seconds(100), &mut files);
        assert!(files.is_empty());
    }

    #[test]
    fn test_find_files_in_range_single_archived() {
        let origin = create_test_origin();
        let mut chain = Chain {
            files: VecDeque::new(),
        };

        // Single archived file with head_realtime = 150
        let file = create_archived_file(&origin, 150 * USEC_PER_SEC);
        chain.files.push_back(file.clone());

        // Test range that starts before and ends after the file
        let mut files = Vec::new();
        chain.find_files_in_range(Seconds(100), Seconds(200), &mut files);
        assert_eq!(files.len(), 1);
        assert_eq!(files[0], file);

        // Test range that starts exactly at head_realtime
        files.clear();
        chain.find_files_in_range(Seconds(150), Seconds(200), &mut files);
        assert_eq!(files.len(), 1);
        assert_eq!(files[0], file);

        // Test range that ends exactly at head_realtime (should not include)
        files.clear();
        chain.find_files_in_range(Seconds(100), Seconds(150), &mut files);
        assert!(files.is_empty());

        // Test range entirely before the file
        files.clear();
        chain.find_files_in_range(Seconds(50), Seconds(100), &mut files);
        assert!(files.is_empty());

        // Test range entirely after (single archived file extends to infinity)
        files.clear();
        chain.find_files_in_range(Seconds(200), Seconds(300), &mut files);
        assert_eq!(files.len(), 1);
        assert_eq!(files[0], file);
    }

    #[test]
    fn test_find_files_in_range_multiple_archived() {
        let origin = create_test_origin();
        let mut chain = Chain {
            files: VecDeque::new(),
        };

        // Multiple archived files: 100, 200, 300, 400
        let file1 = create_archived_file(&origin, 100 * USEC_PER_SEC);
        let file2 = create_archived_file(&origin, 200 * USEC_PER_SEC);
        let file3 = create_archived_file(&origin, 300 * USEC_PER_SEC);
        let file4 = create_archived_file(&origin, 400 * USEC_PER_SEC);

        chain.files.push_back(file1.clone());
        chain.files.push_back(file2.clone());
        chain.files.push_back(file3.clone());
        chain.files.push_back(file4.clone());

        // Range [150, 350) should include files at 100, 200, and 300 in order
        let mut files = Vec::new();
        chain.find_files_in_range(Seconds(150), Seconds(350), &mut files);
        assert_eq!(files.len(), 3);
        assert_eq!(files[0], file1); // Files returned in chronological order
        assert_eq!(files[1], file2);
        assert_eq!(files[2], file3);

        // Range [200, 300) should include only file at 200
        files.clear();
        chain.find_files_in_range(Seconds(200), Seconds(300), &mut files);
        assert_eq!(files.len(), 1);
        assert_eq!(files[0], file2);

        // Range [250, 350) should include files at 200 and 300 in order
        files.clear();
        chain.find_files_in_range(Seconds(250), Seconds(350), &mut files);
        assert_eq!(files.len(), 2);
        assert_eq!(files[0], file2);
        assert_eq!(files[1], file3);

        // Range [450, 500) should include file at 400 (last file extends to infinity)
        files.clear();
        chain.find_files_in_range(Seconds(450), Seconds(500), &mut files);
        assert_eq!(files.len(), 1);
        assert_eq!(files[0], file4);
    }

    #[test]
    fn test_find_files_in_range_with_active() {
        let origin = create_test_origin();
        let mut chain = Chain {
            files: VecDeque::new(),
        };

        // Archived files at 100, 200, then active
        let file1 = create_archived_file(&origin, 100 * USEC_PER_SEC);
        let file2 = create_archived_file(&origin, 200 * USEC_PER_SEC);
        let active = create_active_file(&origin);

        chain.files.push_back(file1.clone());
        chain.files.push_back(file2.clone());
        chain.files.push_back(active.clone());

        // Range [150, 250) should include files at 100, 200, and active in order
        let mut files = Vec::new();
        chain.find_files_in_range(Seconds(150), Seconds(250), &mut files);
        assert_eq!(files.len(), 3);
        assert_eq!(files[0], file1); // Archived files in chronological order
        assert_eq!(files[1], file2);
        assert_eq!(files[2], active); // Active file comes last

        // Range [250, 350) should include only file at 200 and active in order
        files.clear();
        chain.find_files_in_range(Seconds(250), Seconds(350), &mut files);
        assert_eq!(files.len(), 2);
        assert_eq!(files[0], file2);
        assert_eq!(files[1], active);

        // Range [50, 150) should include file at 100
        files.clear();
        chain.find_files_in_range(Seconds(50), Seconds(150), &mut files);
        assert_eq!(files.len(), 1);
        assert_eq!(files[0], file1);
    }

    #[test]
    fn test_find_files_in_range_only_active() {
        let origin = create_test_origin();
        let mut chain = Chain {
            files: VecDeque::new(),
        };

        let active = create_active_file(&origin);
        chain.files.push_back(active.clone());

        // Active file with no archived files should span from u64::MIN to u64::MAX
        let mut files = Vec::new();
        chain.find_files_in_range(Seconds(0), Seconds(100), &mut files);
        assert_eq!(files.len(), 1);
        assert_eq!(files[0], active);

        files.clear();
        let start = Seconds(u32::MAX - 100);
        let end = Seconds(u32::MAX);
        chain.find_files_in_range(start, end, &mut files);
        assert_eq!(files.len(), 1);
        assert_eq!(files[0], active);
    }

    #[test]
    fn test_find_files_in_range_with_disposed() {
        let origin = create_test_origin();
        let mut chain = Chain {
            files: VecDeque::new(),
        };

        // Disposed files should be at the beginning and should be skipped
        let disposed1 = create_disposed_file(&origin, 50 * USEC_PER_SEC, 1);
        let disposed2 = create_disposed_file(&origin, 60 * USEC_PER_SEC, 2);
        let file1 = create_archived_file(&origin, 100 * USEC_PER_SEC);
        let file2 = create_archived_file(&origin, 200 * USEC_PER_SEC);

        chain.files.push_back(disposed1);
        chain.files.push_back(disposed2);
        chain.files.push_back(file1.clone());
        chain.files.push_back(file2.clone());

        // Disposed files should not appear in output, only archived files in order
        let mut files = Vec::new();
        chain.find_files_in_range(Seconds(0), Seconds(300), &mut files);
        assert_eq!(files.len(), 2);
        assert_eq!(files[0], file1); // Files in chronological order
        assert_eq!(files[1], file2);
    }

    #[test]
    fn test_drain_handles_mixed_disposed_and_archived_timestamps() {
        let origin = create_test_origin();
        let mut chain = Chain {
            files: VecDeque::new(),
        };

        let disposed_new = create_disposed_file(&origin, 200 * USEC_PER_SEC, 1);
        let archived_old = create_archived_file(&origin, 50 * USEC_PER_SEC);
        let archived_new = create_archived_file(&origin, 300 * USEC_PER_SEC);
        let active = create_active_file(&origin);

        chain.insert_file(disposed_new.clone());
        chain.insert_file(archived_old.clone());
        chain.insert_file(archived_new.clone());
        chain.insert_file(active.clone());

        let drained: Vec<_> = chain.drain(100 * USEC_PER_SEC).collect();
        assert_eq!(drained, vec![archived_old]);
        assert_eq!(
            chain.files.into_iter().collect::<Vec<_>>(),
            vec![disposed_new, archived_new, active]
        );
    }

    #[test]
    fn test_find_files_in_range_edge_cases() {
        let origin = create_test_origin();
        let mut chain = Chain {
            files: VecDeque::new(),
        };

        // Files at 100, 200, 300
        let file1 = create_archived_file(&origin, 100 * USEC_PER_SEC);
        let file2 = create_archived_file(&origin, 200 * USEC_PER_SEC);
        let file3 = create_archived_file(&origin, 300 * USEC_PER_SEC);

        chain.files.push_back(file1.clone());
        chain.files.push_back(file2.clone());
        chain.files.push_back(file3.clone());

        // Test exact boundaries
        let mut files = Vec::new();

        // Range [100, 200) should include only file at 100
        chain.find_files_in_range(Seconds(100), Seconds(200), &mut files);
        assert_eq!(files.len(), 1);
        assert_eq!(files[0], file1);

        // Range [200, 300) should include only file at 200
        files.clear();
        chain.find_files_in_range(Seconds(200), Seconds(300), &mut files);
        assert_eq!(files.len(), 1);
        assert_eq!(files[0], file2);

        // Range [300, 400) should include only file at 300
        files.clear();
        chain.find_files_in_range(Seconds(300), Seconds(400), &mut files);
        assert_eq!(files.len(), 1);
        assert_eq!(files[0], file3);

        // Range [199, 201) should include files at 100 and 200 in order
        files.clear();
        chain.find_files_in_range(Seconds(199), Seconds(201), &mut files);
        assert_eq!(files.len(), 2);
        assert_eq!(files[0], file1);
        assert_eq!(files[1], file2);
    }

    #[test]
    fn test_find_files_in_range_complex_scenario() {
        let origin = create_test_origin();
        let mut chain = Chain {
            files: VecDeque::new(),
        };

        // Complex scenario with disposed, archived, and active files
        let disposed = create_disposed_file(&origin, 10 * USEC_PER_SEC, 1);
        let file1 = create_archived_file(&origin, 1000 * USEC_PER_SEC);
        let file2 = create_archived_file(&origin, 2000 * USEC_PER_SEC);
        let file3 = create_archived_file(&origin, 3000 * USEC_PER_SEC);
        let file4 = create_archived_file(&origin, 4000 * USEC_PER_SEC);
        let active = create_active_file(&origin);

        chain.files.push_back(disposed);
        chain.files.push_back(file1.clone());
        chain.files.push_back(file2.clone());
        chain.files.push_back(file3.clone());
        chain.files.push_back(file4.clone());
        chain.files.push_back(active.clone());

        // Range [1500, 3500) should include files at 1000, 2000, 3000 in chronological order
        let mut files = Vec::new();
        chain.find_files_in_range(Seconds(1500), Seconds(3500), &mut files);
        assert_eq!(files.len(), 3);
        assert_eq!(files[0], file1); // Files returned in chronological order
        assert_eq!(files[1], file2);
        assert_eq!(files[2], file3);

        // Range [4500, 5000) should include file4 and active in order
        files.clear();
        chain.find_files_in_range(Seconds(4500), Seconds(5000), &mut files);
        assert_eq!(files.len(), 2);
        assert_eq!(files[0], file4); // Last archived file
        assert_eq!(files[1], active); // Active file comes last

        // Range [500, 1500) should include file at 1000
        files.clear();
        chain.find_files_in_range(Seconds(500), Seconds(1500), &mut files);
        assert_eq!(files.len(), 1);
        assert_eq!(files[0], file1);

        // Range covering everything - all files in chronological order
        files.clear();
        chain.find_files_in_range(Seconds(0), Seconds(u32::MAX), &mut files);
        assert_eq!(files.len(), 5); // All except disposed
        assert_eq!(files[0], file1); // Chronological order: 1000, 2000, 3000, 4000, active
        assert_eq!(files[1], file2);
        assert_eq!(files[2], file3);
        assert_eq!(files[3], file4);
        assert_eq!(files[4], active);
    }
}