libdictenstein 0.1.0

High-performance dictionary data structures (trie, DAWG, double-array trie, suffix automaton, lock-free durable persistent ART) behind one trait API; pairs with liblevenshtein for fuzzy matching
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
//! Error handling tests for PersistentARTrie.
//!
//! These tests verify graceful error handling:
//! - Corrupted WAL with various patterns
//! - Partial writes / simulated crashes
//! - File permission errors
//! - Invalid file formats
//! - Edge cases in recovery
//!
//! # Test Strategy
//!
//! We test both the error detection (does it fail?) and graceful
//! degradation (does it recover what it can?).

#![cfg(feature = "persistent-artrie")]

use libdictenstein::persistent_artrie::PersistentARTrie;
use libdictenstein::Dictionary;
use std::fs::{self, File, OpenOptions};
use std::io::{Read, Seek, SeekFrom, Write};
use tempfile::TempDir;

// =============================================================================
// Test: WAL Corruption Patterns
// =============================================================================

#[test]
fn test_corrupted_wal_truncated() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let dict_path = temp_dir.path().join("truncated_wal.part");
    // WAL uses .with_extension("wal"), so foo.part -> foo.wal
    let wal_path = dict_path.with_extension("wal");

    // Create dictionary and insert some data
    {
        let dict: PersistentARTrie<()> = PersistentARTrie::create(&dict_path).expect("create dict");
        for i in 0..10 {
            let _ = dict.insert(&format!("term{:03}", i));
        }
        dict.sync().expect("sync");
    }

    // Verify WAL exists before corruption
    assert!(wal_path.exists(), "WAL should exist after sync");

    // Corrupt the WAL by truncating it mid-record
    {
        let file = OpenOptions::new()
            .write(true)
            .open(&wal_path)
            .expect("open wal");

        let len = file.metadata().expect("metadata").len();
        if len > 50 {
            file.set_len(len - 20).expect("truncate");
        }
    }

    // Try to open - should either succeed with partial recovery or fail gracefully
    let result: Result<PersistentARTrie<()>, _> = PersistentARTrie::open(&dict_path);

    // Either we recover successfully or we get a recoverable error
    match result {
        Ok(dict) => {
            // Partial recovery is acceptable
            // We may have fewer terms than inserted
            assert!(dict.len().unwrap_or(0) <= 10);
        }
        Err(e) => {
            // Error should be a recovery error, not a panic
            let msg = format!("{:?}", e);
            assert!(
                msg.contains("Recovery")
                    || msg.contains("WAL")
                    || msg.contains("truncat")
                    || msg.contains("incomplete"),
                "Unexpected error: {}",
                msg
            );
        }
    }
}

#[test]
fn test_corrupted_wal_garbage_bytes() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let dict_path = temp_dir.path().join("garbage_wal.part");
    let wal_path = dict_path.with_extension("wal");

    // Create dictionary
    {
        let dict: PersistentARTrie<()> = PersistentARTrie::create(&dict_path).expect("create dict");
        for i in 0..5 {
            let _ = dict.insert(&format!("term{:03}", i));
        }
        dict.sync().expect("sync");
    }

    assert!(wal_path.exists(), "WAL should exist after sync");

    // Append garbage to WAL
    {
        let mut file = OpenOptions::new()
            .append(true)
            .open(&wal_path)
            .expect("open wal");

        // Write random garbage that doesn't look like a valid record
        let garbage = [0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x00, 0xFF, 0xFF];
        file.write_all(&garbage).expect("write garbage");
    }

    // Recovery should handle garbage gracefully
    let result: Result<PersistentARTrie<()>, _> = PersistentARTrie::open(&dict_path);

    match result {
        Ok(dict) => {
            // Should recover valid records before the garbage
            // Recovery may have succeeded with some or all terms
            let _ = dict.contains("term000"); // Check doesn't panic
            let _ = dict.len(); // Get length doesn't panic
        }
        Err(e) => {
            // Error is acceptable - corrupted data
            let msg = format!("{:?}", e);
            assert!(
                msg.contains("Recovery") || msg.contains("parse") || msg.contains("invalid"),
                "Unexpected error: {}",
                msg
            );
        }
    }
}

#[test]
fn test_corrupted_wal_header() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let dict_path = temp_dir.path().join("header_wal.part");
    let wal_path = dict_path.with_extension("wal");

    // Create dictionary
    {
        let dict: PersistentARTrie<()> = PersistentARTrie::create(&dict_path).expect("create dict");
        let _ = dict.insert("test");
        dict.sync().expect("sync");
    }

    assert!(wal_path.exists(), "WAL should exist after sync");

    // Corrupt the WAL header (first bytes)
    {
        let mut file = OpenOptions::new()
            .write(true)
            .open(&wal_path)
            .expect("open wal");

        // Overwrite the magic bytes
        file.seek(SeekFrom::Start(0)).expect("seek");
        file.write_all(&[0x00, 0x00, 0x00, 0x00]).expect("write");
    }

    // Should fail to open or recover with error
    let result: Result<PersistentARTrie<()>, _> = PersistentARTrie::open(&dict_path);

    match result {
        Ok(_) => {
            // Acceptable if we fall back to empty state
        }
        Err(e) => {
            let msg = format!("{:?}", e);
            assert!(
                msg.contains("header") || msg.contains("magic") || msg.contains("Recovery"),
                "Unexpected error: {}",
                msg
            );
        }
    }
}

#[test]
fn test_corrupted_wal_checksum() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let dict_path = temp_dir.path().join("checksum_wal.part");
    let wal_path = dict_path.with_extension("wal");

    // Create dictionary with checkpoint
    {
        let dict: PersistentARTrie<()> = PersistentARTrie::create(&dict_path).expect("create dict");
        for i in 0..10 {
            let _ = dict.insert(&format!("term{:03}", i));
        }
        dict.checkpoint().expect("checkpoint");
    }

    assert!(wal_path.exists(), "WAL should exist after checkpoint");

    // Flip some bits in the middle of the WAL
    {
        let mut file = OpenOptions::new()
            .read(true)
            .write(true)
            .open(&wal_path)
            .expect("open wal");

        let len = file.metadata().expect("metadata").len();
        if len > 100 {
            file.seek(SeekFrom::Start(len / 2)).expect("seek");
            let mut byte = [0u8; 1];
            file.read_exact(&mut byte).expect("read");
            byte[0] ^= 0xFF; // Flip all bits
            file.seek(SeekFrom::Start(len / 2)).expect("seek back");
            file.write_all(&byte).expect("write");
        }
    }

    // Recovery should detect corruption
    let result: Result<PersistentARTrie<()>, _> = PersistentARTrie::open(&dict_path);

    // Either fails or recovers partially
    match result {
        Ok(dict) => {
            // Partial recovery is acceptable
            assert!(dict.len().unwrap_or(0) <= 10);
        }
        Err(e) => {
            let msg = format!("{:?}", e);
            assert!(
                msg.contains("checksum")
                    || msg.contains("corrupt")
                    || msg.contains("invalid")
                    || msg.contains("Recovery"),
                "Unexpected error: {}",
                msg
            );
        }
    }
}

// =============================================================================
// Test: File System Errors
// =============================================================================

#[test]
fn test_create_in_nonexistent_directory() {
    let path = "/nonexistent/directory/path/that/does/not/exist/dict.part";

    let result: Result<PersistentARTrie<()>, _> = PersistentARTrie::create(path);

    assert!(result.is_err(), "Should fail for nonexistent directory");

    let err = result.unwrap_err();
    let msg = format!("{:?}", err);
    assert!(
        msg.contains("No such file")
            || msg.contains("not found")
            || msg.contains("directory")
            || msg.contains("Io"),
        "Unexpected error: {}",
        msg
    );
}

#[test]
fn test_open_nonexistent_file() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let path = temp_dir.path().join("does_not_exist.part");

    let result: Result<PersistentARTrie<()>, _> = PersistentARTrie::open(&path);

    assert!(result.is_err(), "Should fail for nonexistent file");

    let err = result.unwrap_err();
    let msg = format!("{:?}", err);
    assert!(
        msg.contains("not found") || msg.contains("No such") || msg.contains("Io"),
        "Unexpected error: {}",
        msg
    );
}

#[test]
fn test_create_existing_file() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let dict_path = temp_dir.path().join("existing.part");

    // Create first dictionary
    {
        let _dict: PersistentARTrie<()> =
            PersistentARTrie::create(&dict_path).expect("create dict");
    }

    // Try to create again - should fail
    let result: Result<PersistentARTrie<()>, _> = PersistentARTrie::create(&dict_path);

    assert!(result.is_err(), "Should fail for existing file");
}

#[test]
#[cfg(unix)]
fn test_readonly_directory() {
    use std::os::unix::fs::PermissionsExt;

    let temp_dir = TempDir::new().expect("create temp dir");
    let readonly_dir = temp_dir.path().join("readonly");
    fs::create_dir(&readonly_dir).expect("create dir");

    // Make directory read-only
    let mut perms = fs::metadata(&readonly_dir).expect("metadata").permissions();
    perms.set_mode(0o555); // r-xr-xr-x
    fs::set_permissions(&readonly_dir, perms.clone()).expect("set perms");

    let dict_path = readonly_dir.join("dict.part");
    let result: Result<PersistentARTrie<()>, _> = PersistentARTrie::create(&dict_path);

    // Restore permissions before checking result
    perms.set_mode(0o755);
    let _ = fs::set_permissions(&readonly_dir, perms);

    assert!(result.is_err(), "Should fail for read-only directory");
}

// =============================================================================
// Test: Empty/Minimal Files
// =============================================================================

#[test]
fn test_empty_wal_file() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let dict_path = temp_dir.path().join("empty_wal.part");
    let wal_path = dict_path.with_extension("wal");

    // Create dictionary
    {
        let _dict: PersistentARTrie<()> =
            PersistentARTrie::create(&dict_path).expect("create dict");
    }

    // Replace WAL with empty file
    {
        let _ = File::create(&wal_path).expect("create empty file");
    }

    // Should handle empty WAL gracefully
    let result: Result<PersistentARTrie<()>, _> = PersistentARTrie::open(&dict_path);

    match result {
        Ok(dict) => {
            // Empty dictionary is acceptable
            assert_eq!(dict.len().unwrap_or(0), 0);
        }
        Err(e) => {
            // Error about empty/invalid WAL is acceptable
            let msg = format!("{:?}", e);
            assert!(
                msg.contains("empty")
                    || msg.contains("header")
                    || msg.contains("too short")
                    || msg.contains("Recovery")
                    || msg.contains("IoError")
                    || msg.contains("UnexpectedEof"),
                "Unexpected error: {}",
                msg
            );
        }
    }
}

#[test]
fn test_zero_filled_wal() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let dict_path = temp_dir.path().join("zero_wal.part");
    let wal_path = dict_path.with_extension("wal");

    // Create dictionary
    {
        let dict: PersistentARTrie<()> = PersistentARTrie::create(&dict_path).expect("create dict");
        let _ = dict.insert("test");
        dict.sync().expect("sync");
    }

    assert!(wal_path.exists(), "WAL should exist after sync");

    // Replace WAL content with zeros
    {
        let mut file = OpenOptions::new()
            .write(true)
            .open(&wal_path)
            .expect("open wal");

        let zeros = vec![0u8; 1024];
        file.write_all(&zeros).expect("write zeros");
    }

    // Should handle zero-filled WAL
    let result: Result<PersistentARTrie<()>, _> = PersistentARTrie::open(&dict_path);

    // Either error or empty recovery
    match result {
        Ok(dict) => {
            assert!(dict.len().unwrap_or(0) <= 1);
        }
        Err(_) => {
            // Error is acceptable
        }
    }
}

// =============================================================================
// Test: Recovery Edge Cases
// =============================================================================

#[test]
fn test_recovery_with_only_removes() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let dict_path = temp_dir.path().join("only_removes.part");

    // Create dictionary, insert, remove, and checkpoint
    {
        let dict: PersistentARTrie<()> = PersistentARTrie::create(&dict_path).expect("create dict");

        for i in 0..5 {
            let _ = dict.insert(&format!("term{}", i));
        }
        for i in 0..5 {
            let _ = dict.remove(&format!("term{}", i));
        }

        dict.sync().expect("sync");
    }

    // Reopen
    let dict: PersistentARTrie<()> = PersistentARTrie::open(&dict_path).expect("open");

    // Should have 0 terms after removes
    assert_eq!(dict.len().unwrap_or(0), 0);
}

#[test]
fn test_recovery_interleaved_insert_remove() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let dict_path = temp_dir.path().join("interleaved.part");

    // Create with interleaved operations
    {
        let dict: PersistentARTrie<()> = PersistentARTrie::create(&dict_path).expect("create dict");

        let _ = dict.insert("keep1");
        let _ = dict.insert("remove1");
        let _ = dict.remove("remove1");
        let _ = dict.insert("keep2");
        let _ = dict.insert("remove2");
        let _ = dict.remove("remove2");
        let _ = dict.insert("keep3");

        dict.sync().expect("sync");
    }

    // Reopen and verify
    let dict: PersistentARTrie<()> = PersistentARTrie::open(&dict_path).expect("open");

    assert!(dict.contains("keep1"));
    assert!(dict.contains("keep2"));
    assert!(dict.contains("keep3"));
    assert!(!dict.contains("remove1"));
    assert!(!dict.contains("remove2"));
}

#[test]
fn test_recovery_duplicate_inserts() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let dict_path = temp_dir.path().join("duplicates.part");

    // Insert same term multiple times
    {
        let dict: PersistentARTrie<()> = PersistentARTrie::create(&dict_path).expect("create dict");

        // First insert succeeds
        assert!(dict.insert("term"));
        // Subsequent inserts should fail (term exists)
        assert!(!dict.insert("term"));
        assert!(!dict.insert("term"));

        dict.sync().expect("sync");
    }

    // Reopen
    let dict: PersistentARTrie<()> = PersistentARTrie::open(&dict_path).expect("open");

    assert!(dict.contains("term"));
    assert_eq!(dict.len().unwrap_or(0), 1);
}

#[test]
fn test_recovery_remove_nonexistent() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let dict_path = temp_dir.path().join("remove_nonexistent.part");

    {
        let dict: PersistentARTrie<()> = PersistentARTrie::create(&dict_path).expect("create dict");

        let _ = dict.insert("exists");
        // Try to remove something that doesn't exist
        assert!(!dict.remove("does_not_exist"));

        dict.sync().expect("sync");
    }

    // Reopen
    let dict: PersistentARTrie<()> = PersistentARTrie::open(&dict_path).expect("open");

    assert!(dict.contains("exists"));
    assert!(!dict.contains("does_not_exist"));
}

// =============================================================================
// Test: Checkpoint Edge Cases
// =============================================================================

#[test]
fn test_checkpoint_empty_dictionary() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let dict_path = temp_dir.path().join("empty_checkpoint.part");

    {
        let dict: PersistentARTrie<()> = PersistentARTrie::create(&dict_path).expect("create dict");

        // Checkpoint empty dictionary
        dict.checkpoint().expect("checkpoint");
    }

    // Reopen
    let dict: PersistentARTrie<()> = PersistentARTrie::open(&dict_path).expect("open");

    assert_eq!(dict.len().unwrap_or(0), 0);
}

#[test]
fn test_multiple_checkpoints_empty() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let dict_path = temp_dir.path().join("multi_empty_checkpoint.part");

    {
        let dict: PersistentARTrie<()> = PersistentARTrie::create(&dict_path).expect("create dict");

        // Multiple checkpoints on empty dictionary
        for _ in 0..5 {
            dict.checkpoint().expect("checkpoint");
        }
    }

    // Reopen
    let dict: PersistentARTrie<()> = PersistentARTrie::open(&dict_path).expect("open");

    assert_eq!(dict.len().unwrap_or(0), 0);
}

#[test]
fn test_checkpoint_after_all_removed() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let dict_path = temp_dir.path().join("all_removed_checkpoint.part");

    {
        let dict: PersistentARTrie<()> = PersistentARTrie::create(&dict_path).expect("create dict");

        // Insert and remove all
        for i in 0..5 {
            let _ = dict.insert(&format!("term{}", i));
        }
        for i in 0..5 {
            let _ = dict.remove(&format!("term{}", i));
        }

        // Checkpoint when dictionary is empty
        dict.checkpoint().expect("checkpoint");
    }

    // Reopen
    let dict: PersistentARTrie<()> = PersistentARTrie::open(&dict_path).expect("open");

    assert_eq!(dict.len().unwrap_or(0), 0);
}

// =============================================================================
// Test: Invalid Data Types
// =============================================================================

#[test]
fn test_open_wrong_file_type() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let dict_path = temp_dir.path().join("wrong_type.part");

    // Create a text file instead of a dictionary
    {
        let mut file = File::create(&dict_path).expect("create file");
        file.write_all(b"This is not a dictionary file\n")
            .expect("write");
    }

    let result: Result<PersistentARTrie<()>, _> = PersistentARTrie::open(&dict_path);

    assert!(result.is_err(), "Should fail for wrong file type");
}

#[test]
fn test_moderately_large_term() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let dict_path = temp_dir.path().join("large_term.part");

    let dict: PersistentARTrie<()> = PersistentARTrie::create(&dict_path).expect("create dict");

    // Try to insert a moderately large term (1KB - safe for stack)
    // Note: Very large terms (64KB+) can cause stack overflow due to
    // recursive bucket implementation. This is a known limitation.
    let large_term: String = "x".repeat(1_000);
    let result = dict.insert(&large_term);

    // Should succeed for 1KB terms
    assert!(result);
    assert!(dict.contains(&large_term));
}

// =============================================================================
// Test: Sync Without Writes
// =============================================================================

#[test]
fn test_sync_no_changes() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let dict_path = temp_dir.path().join("sync_no_changes.part");

    let dict: PersistentARTrie<()> = PersistentARTrie::create(&dict_path).expect("create dict");

    // Sync without any writes - should be no-op
    dict.sync().expect("sync");
    dict.sync().expect("sync again");
}

#[test]
fn test_checkpoint_no_changes() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let dict_path = temp_dir.path().join("checkpoint_no_changes.part");

    let dict: PersistentARTrie<()> = PersistentARTrie::create(&dict_path).expect("create dict");

    // Insert, checkpoint, then checkpoint again without changes
    let _ = dict.insert("term");
    dict.checkpoint().expect("checkpoint 1");
    dict.checkpoint().expect("checkpoint 2"); // No changes since last checkpoint
}

// =============================================================================
// Test: Error Message Quality
// =============================================================================

#[test]
fn test_error_messages_are_descriptive() {
    let temp_dir = TempDir::new().expect("create temp dir");
    let nonexistent = temp_dir.path().join("nonexistent.part");

    let result: Result<PersistentARTrie<()>, _> = PersistentARTrie::open(&nonexistent);

    assert!(result.is_err());
    let err = result.unwrap_err();

    // Error should be convertible to string
    let msg = format!("{}", err);
    assert!(!msg.is_empty(), "Error message should not be empty");

    // Debug format should work too
    let debug_msg = format!("{:?}", err);
    assert!(!debug_msg.is_empty(), "Debug message should not be empty");
}