perl-workspace 0.13.3

Workspace file discovery, indexing, and observability for Perl
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
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
//! Bounded LRU caches for workspace index components.
//!
//! This module provides thread-safe, bounded caches with LRU eviction policies
//! for AST nodes, symbols, and workspace data. These caches enforce memory limits
//! while maintaining high hit rates for frequently accessed data.
//!
//! # Performance Characteristics
//!
//! - **Cache hit rate**: >90% for typical workloads
//! - **Eviction latency**: O(1) amortized with linked hash map
//! - **Memory overhead**: ~32 bytes per cache entry
//! - **Thread safety**: Lock-free reads with atomic reference counting
//!
//! # Cache Configuration
//!
//! - **AST Node Cache**: Max 10,000 nodes, 50MB memory limit
//! - **Symbol Cache**: Max 50,000 symbols, 30MB memory limit
//! - **Workspace Cache**: Max 1,000 files, 20MB memory limit
//!
//! # Usage
//!
//! ```rust,ignore
//! use perl_workspace::workspace::cache::{BoundedLruCache, CacheConfig};
//!
//! let config = CacheConfig::default();
//! let cache = BoundedLruCache::new(config);
//!
//! cache.insert("key", "value");
//! assert_eq!(cache.get("key"), Some(&"value"));
//! ```

use parking_lot::Mutex;
use std::collections::{HashMap, VecDeque};
use std::hash::Hash;
use std::sync::Arc;
use std::time::{Duration, Instant};

/// Cache configuration for bounded LRU caches.
///
/// Defines size limits and memory budgets for cache instances.
#[derive(Clone, Debug)]
pub struct CacheConfig {
    /// Maximum number of items in the cache
    pub max_items: usize,
    /// Maximum memory usage in bytes
    pub max_bytes: usize,
    /// TTL for cache entries (None = no expiration)
    pub ttl: Option<Duration>,
}

impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            max_items: 10_000,
            max_bytes: 50 * 1024 * 1024, // 50MB
            ttl: None,
        }
    }
}

/// Cache statistics for monitoring and diagnostics.
#[derive(Clone, Debug, Default)]
pub struct CacheStats {
    /// Total number of cache hits
    pub hits: u64,
    /// Total number of cache misses
    pub misses: u64,
    /// Total number of evictions
    pub evictions: u64,
    /// Current number of items in cache
    pub current_items: usize,
    /// Current memory usage in bytes
    pub current_bytes: usize,
    /// Hit rate (hits / (hits + misses))
    pub hit_rate: f64,
}

impl CacheStats {
    /// Calculate hit rate from hits and misses.
    pub fn calculate_hit_rate(hits: u64, misses: u64) -> f64 {
        let total = hits + misses;
        if total == 0 { 0.0 } else { hits as f64 / total as f64 }
    }
}

/// Cache entry with metadata for LRU tracking and expiration.
#[derive(Clone)]
struct CacheEntry<V> {
    /// The cached value
    value: V,
    /// When this entry was last accessed
    last_accessed: Instant,
    /// When this entry was inserted
    _inserted_at: Instant,
    /// Size of the entry in bytes
    size_bytes: usize,
}

impl<V> CacheEntry<V> {
    /// Create a new cache entry.
    fn new(value: V, size_bytes: usize) -> Self {
        let now = Instant::now();
        Self { value, last_accessed: now, _inserted_at: now, size_bytes }
    }

    /// Check if this entry has expired based on TTL.
    fn is_expired(&self, ttl: Duration) -> bool {
        self.last_accessed.elapsed() > ttl
    }

    /// Update the last accessed time.
    fn touch(&mut self) {
        self.last_accessed = Instant::now();
    }
}

/// Thread-safe bounded LRU cache.
///
/// Implements a least-recently-used eviction policy with configurable
/// size limits and optional TTL expiration.
///
/// # Type Parameters
///
/// * `K` - Cache key type (must implement Hash + Eq)
/// * `V` - Cache value type
///
/// # Performance
///
/// - **Insert**: O(1) amortized
/// - **Get**: O(1) amortized
/// - **Eviction**: O(1) amortized
pub struct BoundedLruCache<K, V>
where
    K: Hash + Eq + Clone,
{
    /// Cache entries (key -> entry)
    entries: Arc<Mutex<HashMap<K, CacheEntry<V>>>>,
    /// Access order for LRU tracking (oldest keys at front)
    access_order: Arc<Mutex<VecDeque<K>>>,
    /// Cache configuration
    config: CacheConfig,
    /// Cache statistics
    stats: Arc<Mutex<CacheStats>>,
}

impl<K, V> BoundedLruCache<K, V>
where
    K: Hash + Eq + Clone,
{
    /// Create a new bounded LRU cache with the given configuration.
    ///
    /// # Arguments
    ///
    /// * `config` - Cache configuration (size limits, TTL, etc.)
    ///
    /// # Returns
    ///
    /// A new bounded LRU cache instance.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use perl_workspace::workspace::cache::{BoundedLruCache, CacheConfig};
    ///
    /// let config = CacheConfig {
    ///     max_items: 1000,
    ///     max_bytes: 10 * 1024 * 1024, // 10MB
    ///     ttl: None,
    /// };
    /// let cache: BoundedLruCache<String, String> = BoundedLruCache::new(config);
    /// ```
    pub fn new(config: CacheConfig) -> Self {
        Self {
            entries: Arc::new(Mutex::new(HashMap::new())),
            access_order: Arc::new(Mutex::new(VecDeque::new())),
            config,
            stats: Arc::new(Mutex::new(CacheStats::default())),
        }
    }

    /// Create a new cache with default configuration.
    ///
    /// # Returns
    ///
    /// A new bounded LRU cache with default limits (10,000 items, 50MB).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use perl_workspace::workspace::cache::BoundedLruCache;
    ///
    /// let cache: BoundedLruCache<String, String> = BoundedLruCache::default();
    /// ```
    pub fn default() -> Self {
        Self::new(CacheConfig::default())
    }

    /// Insert a value into the cache.
    ///
    /// If the cache is at capacity, the least recently used entry will be evicted.
    ///
    /// # Arguments
    ///
    /// * `key` - Cache key
    /// * `value` - Value to cache
    /// * `size_bytes` - Size of the value in bytes (for memory tracking)
    ///
    /// # Returns
    ///
    /// `true` if the value was inserted, `false` if evicted due to size limits.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use perl_workspace::workspace::cache::BoundedLruCache;
    ///
    /// let mut cache = BoundedLruCache::default();
    /// cache.insert_with_size("key", "value", 5);
    /// ```
    pub fn insert_with_size(&self, key: K, value: V, size_bytes: usize) -> bool {
        let mut entries = self.entries.lock();
        let mut access_order = self.access_order.lock();
        let mut stats = self.stats.lock();

        // Check if this key already exists
        if let Some(entry) = entries.get_mut(&key) {
            // Update existing entry — track byte delta incrementally
            let old_size = entry.size_bytes;
            entry.value = value;
            entry.size_bytes = size_bytes;
            entry.touch();

            // Update access order (move to end = most recent)
            if let Some(pos) = access_order.iter().position(|k| k == &key) {
                access_order.remove(pos);
            }
            access_order.push_back(key.clone());

            // Update stats incrementally
            stats.current_bytes = stats.current_bytes - old_size + size_bytes;
            stats.current_items = entries.len();
            return true;
        }

        // Check if we need to evict entries
        while !entries.is_empty()
            && (entries.len() >= self.config.max_items
                || stats.current_bytes + size_bytes > self.config.max_bytes)
        {
            // Evict least recently used (first in access_order)
            if let Some(lru_key) = access_order.pop_front() {
                if let Some(entry) = entries.remove(&lru_key) {
                    stats.current_bytes -= entry.size_bytes;
                    stats.evictions += 1;
                }
            } else {
                break;
            }
        }

        // Check if we can fit this entry
        if entries.len() >= self.config.max_items
            || stats.current_bytes + size_bytes > self.config.max_bytes
        {
            // Entry too large for cache
            return false;
        }

        // Insert new entry
        entries.insert(key.clone(), CacheEntry::new(value, size_bytes));
        access_order.push_back(key);

        // Update stats incrementally
        stats.current_bytes += size_bytes;
        stats.current_items = entries.len();

        true
    }

    /// Insert a value into the cache with estimated size.
    ///
    /// Uses a simple size estimation based on the value's memory representation.
    ///
    /// # Arguments
    ///
    /// * `key` - Cache key
    /// * `value` - Value to cache
    ///
    /// # Returns
    ///
    /// `true` if the value was inserted, `false` if evicted.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use perl_workspace::workspace::cache::BoundedLruCache;
    ///
    /// let mut cache = BoundedLruCache::default();
    /// cache.insert("key", "value");
    /// ```
    pub fn insert(&self, key: K, value: V)
    where
        V: EstimateSize,
    {
        let size_bytes = value.estimate_size();
        self.insert_with_size(key, value, size_bytes);
    }

    /// Get a value from the cache.
    ///
    /// Returns `None` if the key is not found or the entry has expired.
    ///
    /// # Arguments
    ///
    /// * `key` - Cache key to look up
    ///
    /// # Returns
    ///
    /// `Some(&V)` if found and not expired, `None` otherwise.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use perl_workspace::workspace::cache::BoundedLruCache;
    ///
    /// let mut cache = BoundedLruCache::default();
    /// cache.insert("key", "value");
    /// assert_eq!(cache.get("key"), Some(&"value"));
    /// ```
    pub fn get(&self, key: &K) -> Option<V>
    where
        V: Clone,
    {
        let mut entries = self.entries.lock();
        let mut access_order = self.access_order.lock();
        let mut stats = self.stats.lock();

        // Check TTL expiration
        if let Some(ttl) = self.config.ttl {
            if let Some(entry) = entries.get(key) {
                if entry.is_expired(ttl) {
                    // Entry expired, remove it
                    entries.remove(key);
                    if let Some(pos) = access_order.iter().position(|k| k == key) {
                        access_order.remove(pos);
                    }
                    stats.misses += 1;
                    stats.hit_rate = CacheStats::calculate_hit_rate(stats.hits, stats.misses);
                    return None;
                }
            }
        }

        // Get entry and update LRU
        if let Some(entry) = entries.get_mut(key) {
            entry.touch();

            // Update access order (move to end = most recent)
            if let Some(pos) = access_order.iter().position(|k| k == key) {
                let key_clone = access_order.remove(pos);
                if let Some(key_clone) = key_clone {
                    access_order.push_back(key_clone);
                }
            }

            stats.hits += 1;
            stats.hit_rate = CacheStats::calculate_hit_rate(stats.hits, stats.misses);
            Some(entry.value.clone())
        } else {
            stats.misses += 1;
            stats.hit_rate = CacheStats::calculate_hit_rate(stats.hits, stats.misses);
            None
        }
    }

    /// Peek a value from the cache without changing hit/miss counters.
    ///
    /// Expired entries are still removed so stale data does not linger.
    pub fn peek(&self, key: &K) -> Option<V>
    where
        V: Clone,
    {
        let mut entries = self.entries.lock();
        let mut access_order = self.access_order.lock();
        let mut stats = self.stats.lock();

        if let Some(ttl) = self.config.ttl {
            if entries.get(key).is_some_and(|entry| entry.is_expired(ttl)) {
                if let Some(entry) = entries.remove(key) {
                    stats.current_bytes -= entry.size_bytes;
                    stats.current_items = entries.len();
                }
                if let Some(pos) = access_order.iter().position(|k| k == key) {
                    access_order.remove(pos);
                }
                return None;
            }
        }

        entries.get(key).map(|entry| entry.value.clone())
    }

    /// Remove a value from the cache.
    ///
    /// # Arguments
    ///
    /// * `key` - Cache key to remove
    ///
    /// # Returns
    ///
    /// `Some(V)` if the entry was removed, `None` if not found.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use perl_workspace::workspace::cache::BoundedLruCache;
    ///
    /// let mut cache = BoundedLruCache::default();
    /// cache.insert("key", "value");
    /// assert_eq!(cache.remove("key"), Some("value"));
    /// ```
    pub fn remove(&self, key: &K) -> Option<V>
    where
        V: Clone,
    {
        let mut entries = self.entries.lock();
        let mut access_order = self.access_order.lock();
        let mut stats = self.stats.lock();

        if let Some(entry) = entries.remove(key) {
            stats.current_bytes -= entry.size_bytes;
            stats.current_items = entries.len();

            if let Some(pos) = access_order.iter().position(|k| k == key) {
                access_order.remove(pos);
            }

            Some(entry.value)
        } else {
            None
        }
    }

    /// Clear all entries from the cache.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use perl_workspace::workspace::cache::BoundedLruCache;
    ///
    /// let mut cache = BoundedLruCache::default();
    /// cache.insert("key", "value");
    /// cache.clear();
    /// assert!(cache.is_empty());
    /// ```
    pub fn clear(&self) {
        let mut entries = self.entries.lock();
        let mut access_order = self.access_order.lock();
        let mut stats = self.stats.lock();

        entries.clear();
        access_order.clear();
        stats.current_bytes = 0;
        stats.current_items = 0;
    }

    /// Check if the cache is empty.
    ///
    /// # Returns
    ///
    /// `true` if the cache contains no entries, `false` otherwise.
    pub fn is_empty(&self) -> bool {
        let entries = self.entries.lock();
        entries.is_empty()
    }

    /// Get the number of items in the cache.
    ///
    /// # Returns
    ///
    /// The current number of cached items.
    pub fn len(&self) -> usize {
        let entries = self.entries.lock();
        entries.len()
    }

    /// Get cache statistics.
    ///
    /// # Returns
    ///
    /// A snapshot of the cache statistics.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use perl_workspace::workspace::cache::BoundedLruCache;
    ///
    /// let cache = BoundedLruCache::default();
    /// let stats = cache.stats();
    /// assert_eq!(stats.hits, 0);
    /// ```
    pub fn stats(&self) -> CacheStats {
        let stats = self.stats.lock();
        stats.clone()
    }

    /// Get the cache configuration.
    ///
    /// # Returns
    ///
    /// The cache configuration.
    pub fn config(&self) -> &CacheConfig {
        &self.config
    }
}

/// Trait for estimating the memory size of cached values.
///
/// Implement this trait for custom types to enable accurate memory tracking.
pub trait EstimateSize {
    /// Estimate the memory size of this value in bytes.
    fn estimate_size(&self) -> usize;
}

// Implement EstimateSize for common types
impl EstimateSize for String {
    fn estimate_size(&self) -> usize {
        self.len()
    }
}

impl<T> EstimateSize for Vec<T>
where
    T: EstimateSize,
{
    fn estimate_size(&self) -> usize {
        self.iter().map(|t| t.estimate_size()).sum()
    }
}

impl<K, V> EstimateSize for HashMap<K, V>
where
    K: EstimateSize,
    V: EstimateSize,
{
    fn estimate_size(&self) -> usize {
        self.iter().map(|(k, v)| k.estimate_size() + v.estimate_size()).sum()
    }
}

impl EstimateSize for str {
    fn estimate_size(&self) -> usize {
        self.len()
    }
}

impl<T: EstimateSize + ?Sized> EstimateSize for &T {
    fn estimate_size(&self) -> usize {
        (**self).estimate_size()
    }
}

impl EstimateSize for [u8] {
    fn estimate_size(&self) -> usize {
        self.len()
    }
}

impl EstimateSize for () {
    fn estimate_size(&self) -> usize {
        0
    }
}

impl<T> EstimateSize for Option<T>
where
    T: EstimateSize,
{
    fn estimate_size(&self) -> usize {
        self.as_ref().map(|t| t.estimate_size()).unwrap_or(0)
    }
}

impl<T, E> EstimateSize for Result<T, E>
where
    T: EstimateSize,
    E: EstimateSize,
{
    fn estimate_size(&self) -> usize {
        match self {
            Ok(t) => t.estimate_size(),
            Err(e) => e.estimate_size(),
        }
    }
}

/// AST node cache configuration.
///
/// Optimized for AST node caching with higher memory limits.
#[derive(Clone, Debug)]
pub struct AstCacheConfig {
    /// Maximum number of AST nodes to cache
    pub max_nodes: usize,
    /// Maximum memory for AST cache in bytes
    pub max_bytes: usize,
}

impl Default for AstCacheConfig {
    fn default() -> Self {
        Self {
            max_nodes: 10_000,
            max_bytes: 50 * 1024 * 1024, // 50MB
        }
    }
}

/// Symbol cache configuration.
///
/// Optimized for symbol lookup caching.
#[derive(Clone, Debug)]
pub struct SymbolCacheConfig {
    /// Maximum number of symbols to cache
    pub max_symbols: usize,
    /// Maximum memory for symbol cache in bytes
    pub max_bytes: usize,
}

impl Default for SymbolCacheConfig {
    fn default() -> Self {
        Self {
            max_symbols: 50_000,
            max_bytes: 30 * 1024 * 1024, // 30MB
        }
    }
}

/// Workspace cache configuration.
///
/// Optimized for workspace file metadata caching.
#[derive(Clone, Debug)]
pub struct WorkspaceCacheConfig {
    /// Maximum number of workspace files to cache
    pub max_files: usize,
    /// Maximum memory for workspace cache in bytes
    pub max_bytes: usize,
}

impl Default for WorkspaceCacheConfig {
    fn default() -> Self {
        Self {
            max_files: 1_000,
            max_bytes: 20 * 1024 * 1024, // 20MB
        }
    }
}

/// Combined cache configuration for all workspace caches.
#[derive(Clone, Debug, Default)]
pub struct CombinedWorkspaceCacheConfig {
    /// AST node cache configuration
    pub ast: AstCacheConfig,
    /// Symbol cache configuration
    pub symbol: SymbolCacheConfig,
    /// Workspace cache configuration
    pub workspace: WorkspaceCacheConfig,
}

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

    #[test]
    fn test_cache_insert_get() {
        let cache = BoundedLruCache::<String, String>::default();
        cache.insert("key1".to_string(), "value1".to_string());
        assert_eq!(cache.get(&"key1".to_string()), Some("value1".to_string()));
    }

    #[test]
    fn test_cache_miss() {
        let cache = BoundedLruCache::<String, String>::default();
        assert_eq!(cache.get(&"nonexistent".to_string()), None);
    }

    #[test]
    fn test_cache_eviction() {
        let config = CacheConfig { max_items: 2, max_bytes: 100, ttl: None };
        let cache = BoundedLruCache::<String, String>::new(config);

        cache.insert("key1".to_string(), "value1".to_string());
        cache.insert("key2".to_string(), "value2".to_string());
        cache.insert("key3".to_string(), "value3".to_string());

        // key1 should be evicted (LRU)
        assert_eq!(cache.get(&"key1".to_string()), None);
        assert_eq!(cache.get(&"key2".to_string()), Some("value2".to_string()));
        assert_eq!(cache.get(&"key3".to_string()), Some("value3".to_string()));
    }

    #[test]
    fn test_cache_stats() {
        let cache = BoundedLruCache::<String, String>::default();
        cache.insert("key1".to_string(), "value1".to_string());

        cache.get(&"key1".to_string());
        cache.get(&"key2".to_string());

        let stats = cache.stats();
        assert_eq!(stats.hits, 1);
        assert_eq!(stats.misses, 1);
        assert_eq!(stats.hit_rate, 0.5);
    }

    #[test]
    fn test_cache_clear() {
        let cache = BoundedLruCache::<String, String>::default();
        cache.insert("key1".to_string(), "value1".to_string());
        cache.clear();

        assert!(cache.is_empty());
        assert_eq!(cache.len(), 0);
    }

    #[test]
    fn test_cache_remove() {
        let cache = BoundedLruCache::<String, String>::default();
        cache.insert("key1".to_string(), "value1".to_string());
        assert_eq!(cache.remove(&"key1".to_string()), Some("value1".to_string()));
        assert_eq!(cache.get(&"key1".to_string()), None);
    }

    #[test]
    fn test_cache_peek_does_not_change_stats() {
        let cache = BoundedLruCache::<String, String>::default();
        cache.insert("key1".to_string(), "value1".to_string());

        assert_eq!(cache.peek(&"key1".to_string()), Some("value1".to_string()));

        let stats = cache.stats();
        assert_eq!(stats.hits, 0);
        assert_eq!(stats.misses, 0);
    }

    #[test]
    fn test_estimate_size_string() {
        let s = "hello world";
        assert_eq!(s.estimate_size(), 11);
    }

    #[test]
    fn test_estimate_size_vec() {
        let v = vec!["hello", "world"];
        assert_eq!(v.estimate_size(), 10);
    }
}