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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
//! Version Garbage Collection
//!
//! This module provides garbage collection for old trie versions using a
//! reactive, lock-free scheduling pattern. It tracks active readers to ensure
//! versions are only reclaimed when safe.
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────────────┐
//! │                      Version GC Architecture                             │
//! ├─────────────────────────────────────────────────────────────────────────┤
//! │                                                                          │
//! │   ┌───────────────────────────────────────────────────────────────────┐ │
//! │   │                    VersionGcRegistry                               │ │
//! │   ├───────────────────────────────────────────────────────────────────┤ │
//! │   │                                                                    │ │
//! │   │  ┌─────────────────┐     ┌─────────────────┐                      │ │
//! │   │  │ active_readers  │     │ gc_candidates   │                      │ │
//! │   │  │ (DashMap)       │     │ (RwLock<Vec>)   │                      │ │
//! │   │  │ version→count   │     │                 │                      │ │
//! │   │  └────────┬────────┘     └────────┬────────┘                      │ │
//! │   │           │                        │                               │ │
//! │   │           ▼                        ▼                               │ │
//! │   │  ┌─────────────────────────────────────────┐                      │ │
//! │   │  │          GC Decision Logic              │                      │ │
//! │   │  │  - Check modifications_since_gc         │                      │ │
//! │   │  │  - Find versions with 0 readers         │                      │ │
//! │   │  │  - Respect retention policy             │                      │ │
//! │   │  └─────────────────────────────────────────┘                      │ │
//! │   │                       │                                            │ │
//! │   │                       ▼                                            │ │
//! │   │  ┌─────────────────────────────────────────┐                      │ │
//! │   │  │         GC Worker (Cron Pattern)        │                      │ │
//! │   │  │  - Runs on configurable schedule        │                      │ │
//! │   │  │  - Lock-free operation                  │                      │ │
//! │   │  │  - Graceful shutdown support            │                      │ │
//! │   │  └─────────────────────────────────────────┘                      │ │
//! │   │                                                                    │ │
//! │   └───────────────────────────────────────────────────────────────────┘ │
//! │                                                                          │
//! └─────────────────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Usage
//!
//! ```text
//! let registry = VersionGcRegistry::new(GcConfig::default());
//!
//! // Track a reader for a version
//! registry.add_reader(version_id);
//! // ... reader does work ...
//! registry.remove_reader(version_id);
//!
//! // Add versions eligible for GC
//! registry.add_gc_candidate(old_version_id, root_ptr);
//!
//! // Run GC (usually called by background worker)
//! let collected = registry.run_gc_cycle(&mut wal_writer)?;
//! ```

use std::collections::VecDeque;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use std::thread::{self, JoinHandle};
use std::time::{Duration, Instant};

use crossbeam_channel::{bounded, Receiver, Sender};
use dashmap::DashMap;
use parking_lot::RwLock;

use super::error::{PersistentARTrieError, Result};
use super::wal::{WalRecord, WalWriter};

/// Configuration for version garbage collection.
#[derive(Debug, Clone)]
pub struct GcConfig {
    /// How often to run GC cycles (default: 1 second)
    pub gc_interval: Duration,

    /// Minimum number of versions to retain (default: 5)
    pub min_retained_versions: usize,

    /// Maximum number of versions to retain (default: 100)
    pub max_retained_versions: usize,

    /// Minimum time a version must be unreferenced before GC (default: 5 seconds)
    pub grace_period: Duration,

    /// Maximum number of versions to GC in a single cycle (default: 10)
    pub max_gc_per_cycle: usize,

    /// Whether to run GC in background thread
    pub background_gc: bool,
}

impl Default for GcConfig {
    fn default() -> Self {
        Self {
            gc_interval: Duration::from_secs(1),
            min_retained_versions: 5,
            max_retained_versions: 100,
            grace_period: Duration::from_secs(5),
            max_gc_per_cycle: 10,
            background_gc: true,
        }
    }
}

impl GcConfig {
    /// Create a config for testing (faster cycles, shorter grace period).
    pub fn for_testing() -> Self {
        Self {
            gc_interval: Duration::from_millis(100),
            min_retained_versions: 2,
            max_retained_versions: 10,
            grace_period: Duration::from_millis(100),
            max_gc_per_cycle: 5,
            background_gc: false,
        }
    }

    /// Create a config for high-throughput scenarios.
    pub fn high_throughput() -> Self {
        Self {
            gc_interval: Duration::from_millis(500),
            min_retained_versions: 10,
            max_retained_versions: 1000,
            grace_period: Duration::from_secs(2),
            max_gc_per_cycle: 50,
            background_gc: true,
        }
    }
}

/// A version candidate for garbage collection.
#[derive(Debug, Clone)]
pub struct GcCandidate {
    /// The version ID to potentially collect
    pub version_id: u64,
    /// Root pointer for this version (for validation)
    pub root_ptr: u64,
    /// When this version became eligible for GC
    pub eligible_since: Instant,
    /// Number of nodes in this version
    pub node_count: u64,
}

/// Statistics for version garbage collection.
#[derive(Debug, Clone, Default)]
pub struct GcStats {
    /// Total GC cycles run
    pub cycles_run: u64,
    /// Total versions collected
    pub versions_collected: u64,
    /// Total versions skipped (had active readers)
    pub versions_skipped: u64,
    /// Total bytes reclaimed (estimated)
    pub bytes_reclaimed: u64,
    /// Last GC cycle duration
    pub last_cycle_duration: Duration,
    /// Current number of GC candidates
    pub pending_candidates: usize,
    /// Current number of versions with active readers
    pub versions_with_readers: usize,
}

/// Messages for the GC worker thread.
#[derive(Debug)]
enum GcMessage {
    /// Run a GC cycle
    RunCycle,
    /// Shutdown the worker
    Shutdown,
    /// Add a GC candidate
    AddCandidate(GcCandidate),
}

/// Registry for tracking active readers and GC candidates.
///
/// This is the central coordinator for version garbage collection. It tracks
/// which versions have active readers and which are candidates for collection.
#[derive(Debug)]
pub struct VersionGcRegistry {
    /// Active readers per version: version_id -> reader_count
    active_readers: DashMap<u64, u64>,

    /// Versions eligible for GC
    gc_candidates: RwLock<VecDeque<GcCandidate>>,

    /// Configuration
    config: GcConfig,

    /// Number of modifications since last GC (optimization: skip GC if 0)
    modifications_since_gc: AtomicU64,

    /// Whether the registry is shutting down
    terminating: AtomicBool,

    /// Statistics
    stats: RwLock<GcStats>,

    /// Channel to send messages to background worker
    worker_tx: Option<Sender<GcMessage>>,

    /// Handle to background worker thread
    worker_handle: RwLock<Option<JoinHandle<()>>>,
}

impl VersionGcRegistry {
    /// Create a new GC registry.
    pub fn new(config: GcConfig) -> Arc<Self> {
        let (worker_tx, worker_rx) = if config.background_gc {
            let (tx, rx) = bounded::<GcMessage>(1000);
            (Some(tx), Some(rx))
        } else {
            (None, None)
        };

        let registry = Arc::new(Self {
            active_readers: DashMap::new(),
            gc_candidates: RwLock::new(VecDeque::new()),
            config: config.clone(),
            modifications_since_gc: AtomicU64::new(0),
            terminating: AtomicBool::new(false),
            stats: RwLock::new(GcStats::default()),
            worker_tx,
            worker_handle: RwLock::new(None),
        });

        // Start background worker if configured
        if let Some(rx) = worker_rx {
            let registry_clone = Arc::clone(&registry);
            let interval = config.gc_interval;
            let handle = thread::Builder::new()
                .name("version-gc".to_string())
                .spawn(move || {
                    Self::gc_worker_loop(registry_clone, rx, interval);
                })
                .expect("Failed to spawn GC worker thread");

            *registry.worker_handle.write() = Some(handle);
        }

        registry
    }

    /// Background worker loop.
    fn gc_worker_loop(registry: Arc<Self>, rx: Receiver<GcMessage>, interval: Duration) {
        let mut last_cycle = Instant::now();

        loop {
            // Check for messages with timeout
            match rx.recv_timeout(interval) {
                Ok(GcMessage::Shutdown) => {
                    break;
                }
                Ok(GcMessage::RunCycle) => {
                    registry.run_gc_cycle_internal();
                    last_cycle = Instant::now();
                }
                Ok(GcMessage::AddCandidate(candidate)) => {
                    let mut candidates = registry.gc_candidates.write();
                    candidates.push_back(candidate);
                }
                Err(crossbeam_channel::RecvTimeoutError::Timeout) => {
                    // Check if it's time for a periodic GC cycle
                    if last_cycle.elapsed() >= interval {
                        registry.run_gc_cycle_internal();
                        last_cycle = Instant::now();
                    }
                }
                Err(crossbeam_channel::RecvTimeoutError::Disconnected) => {
                    break;
                }
            }

            // Check for shutdown
            if registry.terminating.load(Ordering::Acquire) {
                break;
            }
        }
    }

    /// Add a reader for a version.
    ///
    /// This increments the reference count for the version, preventing it
    /// from being garbage collected.
    pub fn add_reader(&self, version_id: u64) {
        self.active_readers
            .entry(version_id)
            .and_modify(|count| *count += 1)
            .or_insert(1);
    }

    /// Remove a reader for a version.
    ///
    /// This decrements the reference count. When the count reaches zero,
    /// the version becomes eligible for garbage collection.
    pub fn remove_reader(&self, version_id: u64) {
        if let Some(mut entry) = self.active_readers.get_mut(&version_id) {
            *entry = entry.saturating_sub(1);
            if *entry == 0 {
                drop(entry);
                self.active_readers.remove(&version_id);
            }
        }
    }

    /// Get the number of active readers for a version.
    pub fn reader_count(&self, version_id: u64) -> u64 {
        self.active_readers
            .get(&version_id)
            .map(|v| *v)
            .unwrap_or(0)
    }

    /// Check if a version has any active readers.
    pub fn has_readers(&self, version_id: u64) -> bool {
        self.reader_count(version_id) > 0
    }

    /// Add a version as a GC candidate.
    ///
    /// This marks the version as potentially eligible for garbage collection.
    /// The version will only be collected after the grace period expires and
    /// it has no active readers.
    pub fn add_gc_candidate(&self, version_id: u64, root_ptr: u64, node_count: u64) {
        let candidate = GcCandidate {
            version_id,
            root_ptr,
            eligible_since: Instant::now(),
            node_count,
        };

        if let Some(ref tx) = self.worker_tx {
            // Send to background worker
            let _ = tx.try_send(GcMessage::AddCandidate(candidate));
        } else {
            // Add directly
            let mut candidates = self.gc_candidates.write();
            candidates.push_back(candidate);
        }
    }

    /// Record a modification (for skip optimization).
    ///
    /// This increments the modification counter, which the GC uses to skip
    /// cycles when no modifications have occurred.
    pub fn record_modification(&self) {
        self.modifications_since_gc.fetch_add(1, Ordering::Release);
    }

    /// Trigger a GC cycle (asynchronous if background worker is running).
    pub fn trigger_gc(&self) {
        if let Some(ref tx) = self.worker_tx {
            let _ = tx.try_send(GcMessage::RunCycle);
        } else {
            self.run_gc_cycle_internal();
        }
    }

    /// Run a GC cycle and return versions that were collected.
    ///
    /// This should be called with a WAL writer to record VersionGc records.
    pub fn run_gc_cycle(&self, wal: &mut WalWriter) -> Result<Vec<u64>> {
        let collected = self.run_gc_cycle_internal();

        if !collected.is_empty() {
            // Write VersionGc record to WAL
            let record = WalRecord::VersionGc {
                version_ids: collected.clone(),
            };
            wal.append(record).map_err(|e| {
                PersistentARTrieError::internal(format!("Failed to write VersionGc: {}", e))
            })?;
        }

        Ok(collected)
    }

    /// Internal GC cycle implementation.
    fn run_gc_cycle_internal(&self) -> Vec<u64> {
        let start = Instant::now();

        // Early exit: no modifications = nothing to GC
        let mods = self.modifications_since_gc.load(Ordering::Acquire);
        if mods == 0 {
            return Vec::new();
        }

        let mut collected = Vec::new();
        let now = Instant::now();
        let grace_period = self.config.grace_period;
        let max_gc = self.config.max_gc_per_cycle;
        let min_retain = self.config.min_retained_versions;

        {
            let mut candidates = self.gc_candidates.write();

            // Count how many candidates we have
            let total_candidates = candidates.len();
            if total_candidates <= min_retain {
                return Vec::new(); // Not enough to GC
            }

            // Calculate how many we can GC while respecting retention
            let max_gc_for_retention = total_candidates - min_retain;

            // Process candidates from oldest to newest
            let mut remaining = VecDeque::new();
            let mut gc_count = 0;

            while let Some(candidate) = candidates.pop_front() {
                // Check if we've hit the per-cycle limit
                if gc_count >= max_gc {
                    remaining.push_back(candidate);
                    continue;
                }

                // Check if we've hit the retention limit
                if gc_count >= max_gc_for_retention {
                    remaining.push_back(candidate);
                    continue;
                }

                // Check grace period
                if now.duration_since(candidate.eligible_since) < grace_period {
                    remaining.push_back(candidate);
                    continue;
                }

                // Check for active readers
                if self.has_readers(candidate.version_id) {
                    remaining.push_back(candidate);
                    let mut stats = self.stats.write();
                    stats.versions_skipped += 1;
                    continue;
                }

                // This version can be collected
                collected.push(candidate.version_id);
                gc_count += 1;

                // Update stats
                {
                    let mut stats = self.stats.write();
                    stats.versions_collected += 1;
                    // Estimate bytes reclaimed (rough: 200 bytes per node)
                    stats.bytes_reclaimed += candidate.node_count * 200;
                }
            }

            // Put remaining candidates back
            *candidates = remaining;
        }

        // Reset modification counter
        self.modifications_since_gc.store(0, Ordering::Release);

        // Update cycle stats
        {
            let mut stats = self.stats.write();
            stats.cycles_run += 1;
            stats.last_cycle_duration = start.elapsed();
            stats.pending_candidates = self.gc_candidates.read().len();
            stats.versions_with_readers = self.active_readers.len();
        }

        collected
    }

    /// Get current GC statistics.
    pub fn stats(&self) -> GcStats {
        self.stats.read().clone()
    }

    /// Shutdown the GC worker.
    pub fn shutdown(&self) {
        self.terminating.store(true, Ordering::Release);

        if let Some(ref tx) = self.worker_tx {
            let _ = tx.send(GcMessage::Shutdown);
        }

        // Wait for worker to finish
        if let Some(handle) = self.worker_handle.write().take() {
            let _ = handle.join();
        }
    }

    /// Get the number of pending GC candidates.
    pub fn pending_count(&self) -> usize {
        self.gc_candidates.read().len()
    }

    /// Get all pending GC candidate version IDs.
    pub fn pending_versions(&self) -> Vec<u64> {
        self.gc_candidates
            .read()
            .iter()
            .map(|c| c.version_id)
            .collect()
    }

    /// Clear all GC candidates (for testing or reset).
    pub fn clear_candidates(&self) {
        self.gc_candidates.write().clear();
    }
}

impl Drop for VersionGcRegistry {
    fn drop(&mut self) {
        self.shutdown();
    }
}

/// A guard that tracks reader count for a version.
///
/// This automatically increments the reader count on creation and decrements
/// it on drop, ensuring readers are properly tracked even in the presence
/// of panics.
#[derive(Debug)]
pub struct ReaderGuard {
    version_id: u64,
    registry: Arc<VersionGcRegistry>,
}

impl ReaderGuard {
    /// Create a new reader guard for a version.
    pub fn new(version_id: u64, registry: Arc<VersionGcRegistry>) -> Self {
        registry.add_reader(version_id);
        Self {
            version_id,
            registry,
        }
    }

    /// Get the version ID this guard is protecting.
    #[inline]
    pub fn version_id(&self) -> u64 {
        self.version_id
    }
}

impl Drop for ReaderGuard {
    fn drop(&mut self) {
        self.registry.remove_reader(self.version_id);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_add_remove_reader() {
        let config = GcConfig::for_testing();
        let registry = VersionGcRegistry::new(config);

        assert_eq!(registry.reader_count(1), 0);

        registry.add_reader(1);
        assert_eq!(registry.reader_count(1), 1);

        registry.add_reader(1);
        assert_eq!(registry.reader_count(1), 2);

        registry.remove_reader(1);
        assert_eq!(registry.reader_count(1), 1);

        registry.remove_reader(1);
        assert_eq!(registry.reader_count(1), 0);
        assert!(!registry.has_readers(1));
    }

    #[test]
    fn test_reader_guard() {
        let config = GcConfig::for_testing();
        let registry = VersionGcRegistry::new(config);

        assert_eq!(registry.reader_count(1), 0);

        {
            let _guard = ReaderGuard::new(1, Arc::clone(&registry));
            assert_eq!(registry.reader_count(1), 1);
        }

        assert_eq!(registry.reader_count(1), 0);
    }

    #[test]
    fn test_gc_candidate() {
        let config = GcConfig::for_testing();
        let registry = VersionGcRegistry::new(config);

        registry.add_gc_candidate(1, 100, 50);
        registry.add_gc_candidate(2, 200, 75);

        assert_eq!(registry.pending_count(), 2);
        assert!(registry.pending_versions().contains(&1));
        assert!(registry.pending_versions().contains(&2));
    }

    #[test]
    fn test_gc_cycle_respects_readers() {
        let config = GcConfig {
            grace_period: Duration::from_millis(0), // No grace period for test
            min_retained_versions: 0,
            ..GcConfig::for_testing()
        };
        let registry = VersionGcRegistry::new(config);

        // Add candidate with a reader
        registry.add_gc_candidate(1, 100, 50);
        registry.add_reader(1);
        registry.record_modification();

        // GC should skip version 1 (has reader)
        let collected = registry.run_gc_cycle_internal();
        assert!(collected.is_empty());
        assert_eq!(registry.stats().versions_skipped, 1);

        // Remove reader and try again
        registry.remove_reader(1);
        registry.record_modification();
        let collected = registry.run_gc_cycle_internal();
        assert_eq!(collected, vec![1]);
    }

    #[test]
    fn test_gc_cycle_respects_grace_period() {
        let config = GcConfig {
            grace_period: Duration::from_secs(10), // Long grace period
            min_retained_versions: 0,
            ..GcConfig::for_testing()
        };
        let registry = VersionGcRegistry::new(config);

        registry.add_gc_candidate(1, 100, 50);
        registry.record_modification();

        // GC should skip due to grace period
        let collected = registry.run_gc_cycle_internal();
        assert!(collected.is_empty());
    }

    #[test]
    fn test_gc_cycle_respects_retention() {
        let config = GcConfig {
            grace_period: Duration::from_millis(0),
            min_retained_versions: 3,
            ..GcConfig::for_testing()
        };
        let registry = VersionGcRegistry::new(config);

        // Add 4 candidates
        for i in 1..=4 {
            registry.add_gc_candidate(i, i * 100, i * 50);
        }
        registry.record_modification();

        // Only 1 should be collected (4 - 3 = 1)
        let collected = registry.run_gc_cycle_internal();
        assert_eq!(collected.len(), 1);
        assert_eq!(registry.pending_count(), 3);
    }

    #[test]
    fn test_gc_stats() {
        let config = GcConfig {
            grace_period: Duration::from_millis(0),
            min_retained_versions: 0,
            ..GcConfig::for_testing()
        };
        let registry = VersionGcRegistry::new(config);

        registry.add_gc_candidate(1, 100, 50);
        registry.record_modification();
        registry.run_gc_cycle_internal();

        let stats = registry.stats();
        assert_eq!(stats.cycles_run, 1);
        assert_eq!(stats.versions_collected, 1);
        assert!(stats.bytes_reclaimed > 0);
    }

    #[test]
    fn test_no_gc_without_modifications() {
        let config = GcConfig::for_testing();
        let registry = VersionGcRegistry::new(config);

        registry.add_gc_candidate(1, 100, 50);
        // Don't call record_modification()

        let collected = registry.run_gc_cycle_internal();
        assert!(collected.is_empty());
    }

    #[test]
    fn test_shutdown() {
        let config = GcConfig {
            background_gc: true,
            gc_interval: Duration::from_millis(10),
            ..GcConfig::for_testing()
        };
        let registry = VersionGcRegistry::new(config);

        // Give the worker a moment to start
        thread::sleep(Duration::from_millis(50));

        // Shutdown should not hang
        registry.shutdown();
    }
}