torm 0.2.0

A Tokio-based async ORM for Rust with GORM-like API, supporting SQLite, MySQL and PostgreSQL via native wire protocols
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
use std::time::{Duration, Instant};
use std::sync::{Arc, Mutex};
use crate::utils::simple_lru::SimpleLruCache;

/// 查询缓存项
#[derive(Debug, Clone)]
pub struct CacheEntry<T> {
    pub data: T,
    pub created_at: Instant,
    pub expires_at: Option<Instant>,
    pub hit_count: u64,
}

impl<T> CacheEntry<T> {
    pub fn new(data: T, ttl: Option<Duration>) -> Self {
        let now = Instant::now();
        Self {
            data,
            created_at: now,
            expires_at: ttl.map(|ttl| now + ttl),
            hit_count: 0,
        }
    }

    pub fn is_expired(&self) -> bool {
        if let Some(expires_at) = self.expires_at {
            Instant::now() > expires_at
        } else {
            false
        }
    }

    pub fn increment_hit(&mut self) {
        self.hit_count += 1;
    }
}

/// 查询缓存(使用简化的 LRU 实现)
pub struct QueryCache<T: Clone> {
    cache: Arc<Mutex<SimpleLruCache<String, CacheEntry<T>>>>,
    default_ttl: Option<Duration>,
    stats: Arc<Mutex<CacheStats>>,
}

impl<T: Clone> QueryCache<T> {
    pub fn new(capacity: usize, default_ttl: Option<Duration>) -> Self {
        Self {
            cache: Arc::new(Mutex::new(SimpleLruCache::new(capacity))),
            default_ttl,
            stats: Arc::new(Mutex::new(CacheStats::default())),
        }
    }

    pub fn get(&self, key: &str) -> Option<T> {
        let mut cache = self.cache.lock().unwrap();
        let key_string = key.to_string();
        
        if let Some(entry) = cache.get(&key_string) {
            if !entry.is_expired() {
                // Create new entry with incremented hit count
                let mut updated_entry = entry.clone();
                updated_entry.increment_hit();
                cache.put(key_string.clone(), updated_entry);
                
                let mut stats = self.stats.lock().unwrap();
                stats.hits += 1;
                return Some(entry.data.clone());
            } else {
                // Remove expired entry
                cache.remove(&key_string);
            }
        }

        let mut stats = self.stats.lock().unwrap();
        stats.misses += 1;
        None
    }

    pub fn set(&self, key: &str, value: T) {
        let mut cache = self.cache.lock().unwrap();
        let ttl = self.default_ttl;
        cache.put(key.to_string(), CacheEntry::new(value, ttl));
        
        let mut stats = self.stats.lock().unwrap();
        stats.sets += 1;
    }

    pub fn set_with_ttl(&self, key: &str, value: T, ttl: Duration) {
        let mut cache = self.cache.lock().unwrap();
        cache.put(key.to_string(), CacheEntry::new(value, Some(ttl)));
        
        let mut stats = self.stats.lock().unwrap();
        stats.sets += 1;
    }

    pub fn remove(&self, key: &str) {
        let mut cache = self.cache.lock().unwrap();
        cache.remove(&key.to_string());
        
        let mut stats = self.stats.lock().unwrap();
        stats.deletes += 1;
    }

    pub fn clear(&self) {
        let mut cache = self.cache.lock().unwrap();
        cache.clear();
        
        let mut stats = self.stats.lock().unwrap();
        stats.clears += 1;
    }

    pub fn get_stats(&self) -> CacheStats {
        let stats = self.stats.lock().unwrap();
        stats.clone()
    }

    pub fn invalidate_prefix(&self, prefix: &str) {
        let mut cache = self.cache.lock().unwrap();
        let keys_to_remove: Vec<String> = cache.keys()
            .filter(|k| k.starts_with(prefix))
            .cloned()
            .collect();
        
        for key in keys_to_remove {
            cache.remove(&key);
        }
    }

    pub fn cleanup_expired(&self) {
        let mut cache = self.cache.lock().unwrap();
        let expired_keys: Vec<String> = cache.iter()
            .filter(|(_, entry)| entry.is_expired())
            .map(|(k, _)| k.clone())
            .collect();
        
        for key in expired_keys {
            cache.remove(&key);
        }
    }
}

#[derive(Debug, Clone, Default)]
pub struct CacheStats {
    pub hits: u64,
    pub misses: u64,
    pub sets: u64,
    pub deletes: u64,
    pub clears: u64,
}

impl CacheStats {
    pub fn total_requests(&self) -> u64 {
        self.hits + self.misses
    }

    pub fn hit_rate(&self) -> f64 {
        let total = self.total_requests();
        if total == 0 {
            0.0
        } else {
            self.hits as f64 / total as f64
        }
    }

    pub fn miss_rate(&self) -> f64 {
        1.0 - self.hit_rate()
    }
}

/// 批量操作
pub struct BatchOperation<T: Clone> {
    operations: Vec<BatchItem<T>>,
    batch_size: usize,
}

#[derive(Debug, Clone)]
pub enum BatchItem<T> {
    Create(T),
    Update(String, T), // ID, data
    Delete(String),   // ID
}

impl<T: Clone> BatchOperation<T> {
    pub fn new(batch_size: usize) -> Self {
        Self {
            operations: Vec::new(),
            batch_size,
        }
    }

    pub fn add_create(mut self, item: T) -> Self {
        self.operations.push(BatchItem::Create(item));
        self
    }

    pub fn add_update(mut self, id: String, item: T) -> Self {
        self.operations.push(BatchItem::Update(id, item));
        self
    }

    pub fn add_delete(mut self, id: String) -> Self {
        self.operations.push(BatchItem::Delete(id));
        self
    }

    pub fn len(&self) -> usize {
        self.operations.len()
    }

    pub fn is_empty(&self) -> bool {
        self.operations.is_empty()
    }

    pub fn is_full(&self) -> bool {
        self.operations.len() >= self.batch_size
    }

    pub fn execute_in_batches<F, Fut>(&self, _executor: F) -> impl Iterator<Item = Vec<BatchItem<T>>> + '_
    where
        F: Fn(Vec<BatchItem<T>>) -> Fut + Copy,
        Fut: std::future::Future<Output = ()>,
    {
        self.operations.chunks(self.batch_size)
            .map(|chunk| chunk.to_vec())
    }

    pub fn build_batch_sql(&self, table_name: &str) -> Vec<String> {
        let mut sqls = Vec::new();
        
        // Group operations by type for efficiency
        let mut creates = Vec::new();
        let mut updates = Vec::new();
        let mut deletes = Vec::new();

        for operation in &self.operations {
            match operation {
                BatchItem::Create(_) => creates.push(operation),
                BatchItem::Update(_, _) => updates.push(operation),
                BatchItem::Delete(_) => deletes.push(operation),
            }
        }

        // Build batch INSERT
        if !creates.is_empty() {
            // This would need to extract actual columns and values from the items
            // Simplified version
            sqls.push(format!(
                "INSERT INTO {} (column1, column2) VALUES (value1, value2), (value3, value4)",
                table_name
            ));
        }

        // Build batch UPDATE
        for update in &updates {
            if let BatchItem::Update(id, _) = update {
                sqls.push(format!(
                    "UPDATE {} SET column1 = value1 WHERE id = '{}'",
                    table_name, id
                ));
            }
        }

        // Build batch DELETE
        if !deletes.is_empty() {
            let ids: Vec<String> = deletes.iter()
                .filter_map(|item| {
                    if let BatchItem::Delete(id) = item {
                        Some(id.clone())
                    } else {
                        None
                    }
                })
                .collect();
            
            sqls.push(format!(
                "DELETE FROM {} WHERE id IN ('{}')",
                table_name,
                ids.join("', '")
            ));
        }

        sqls
    }
}

impl<T: Clone> Default for BatchOperation<T> {
    fn default() -> Self {
        Self::new(100)
    }
}

/// 预编译语句缓存(使用简化的 LRU)
pub struct PreparedStatementCache {
    cache: Arc<Mutex<SimpleLruCache<String, String>>>,
    #[allow(dead_code)]
    capacity: usize,
}

impl PreparedStatementCache {
    pub fn new(capacity: usize) -> Self {
        Self {
            cache: Arc::new(Mutex::new(SimpleLruCache::new(capacity))),
            capacity,
        }
    }

    pub fn get(&self, key: &str) -> Option<String> {
        let mut cache = self.cache.lock().unwrap();
        cache.get(&key.to_string())
    }

    pub fn set(&self, key: &str, statement: String) {
        let mut cache = self.cache.lock().unwrap();
        cache.put(key.to_string(), statement);
    }

    pub fn clear(&self) {
        let mut cache = self.cache.lock().unwrap();
        cache.clear();
    }

    pub fn len(&self) -> usize {
        let cache = self.cache.lock().unwrap();
        cache.len()
    }
}

/// 连接池监控
pub struct ConnectionPoolMonitor {
    active_connections: Arc<Mutex<usize>>,
    idle_connections: Arc<Mutex<usize>>,
    total_connections: Arc<Mutex<usize>>,
    max_connections: usize,
    min_idle: usize,
}

impl ConnectionPoolMonitor {
    pub fn new(max_connections: usize, min_idle: usize) -> Self {
        Self {
            active_connections: Arc::new(Mutex::new(0)),
            idle_connections: Arc::new(Mutex::new(0)),
            total_connections: Arc::new(Mutex::new(0)),
            max_connections,
            min_idle,
        }
    }

    pub fn record_active(&self) {
        let mut active = self.active_connections.lock().unwrap();
        let mut idle = self.idle_connections.lock().unwrap();
        let mut total = self.total_connections.lock().unwrap();
        
        *active += 1;
        *idle = idle.saturating_sub(1);
        *total = total.max(*active + *idle);
    }

    pub fn record_idle(&self) {
        let mut active = self.active_connections.lock().unwrap();
        let mut idle = self.idle_connections.lock().unwrap();
        
        *active = active.saturating_sub(1);
        *idle += 1;
    }

    pub fn get_stats(&self) -> PoolStats {
        let active = *self.active_connections.lock().unwrap();
        let idle = *self.idle_connections.lock().unwrap();
        let total = *self.total_connections.lock().unwrap();

        PoolStats {
            active_connections: active,
            idle_connections: idle,
            total_connections: total,
            max_connections: self.max_connections,
            min_idle: self.min_idle,
            utilization_rate: if self.max_connections > 0 {
                active as f64 / self.max_connections as f64
            } else {
                0.0
            },
        }
    }

    pub fn needs_scaling_up(&self) -> bool {
        let stats = self.get_stats();
        stats.utilization_rate > 0.8 || stats.idle_connections < self.min_idle
    }

    pub fn needs_scaling_down(&self) -> bool {
        let stats = self.get_stats();
        stats.utilization_rate < 0.2 && stats.idle_connections > self.min_idle * 2
    }
}

#[derive(Debug, Clone)]
pub struct PoolStats {
    pub active_connections: usize,
    pub idle_connections: usize,
    pub total_connections: usize,
    pub max_connections: usize,
    pub min_idle: usize,
    pub utilization_rate: f64,
}

/// 性能优化配置
pub struct PerformanceConfig {
    pub query_cache_enabled: bool,
    pub query_cache_size: usize,
    pub query_cache_ttl: Option<Duration>,
    pub batch_size: usize,
    pub prepared_statement_cache_size: usize,
    pub connection_pool_max_size: usize,
    pub connection_pool_min_idle: usize,
    pub slow_query_threshold: Duration,
    pub enable_query_stats: bool,
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            query_cache_enabled: true,
            query_cache_size: 1000,
            query_cache_ttl: Some(Duration::from_secs(3600)), // 1 hour
            batch_size: 100,
            prepared_statement_cache_size: 500,
            connection_pool_max_size: 10,
            connection_pool_min_idle: 2,
            slow_query_threshold: Duration::from_millis(1000),
            enable_query_stats: true,
        }
    }
}

/// 性能优化管理器
pub struct PerformanceManager {
    config: PerformanceConfig,
    query_cache_stats: Arc<Mutex<CacheStats>>,
    pool_monitor: ConnectionPoolMonitor,
}

impl PerformanceManager {
    pub fn new(config: PerformanceConfig) -> Self {
        Self {
            pool_monitor: ConnectionPoolMonitor::new(
                config.connection_pool_max_size,
                config.connection_pool_min_idle,
            ),
            query_cache_stats: Arc::new(Mutex::new(CacheStats::default())),
            config,
        }
    }

    pub fn get_config(&self) -> &PerformanceConfig {
        &self.config
    }

    pub fn update_config(&mut self, config: PerformanceConfig) {
        self.config = config;
    }

    pub fn get_pool_stats(&self) -> PoolStats {
        self.pool_monitor.get_stats()
    }

    pub fn get_query_cache_stats(&self) -> CacheStats {
        let stats = self.query_cache_stats.lock().unwrap();
        stats.clone()
    }

    pub fn should_scale_pool(&self) -> Option<PoolAction> {
        if self.pool_monitor.needs_scaling_up() {
            Some(PoolAction::ScaleUp)
        } else if self.pool_monitor.needs_scaling_down() {
            Some(PoolAction::ScaleDown)
        } else {
            None
        }
    }

    pub fn is_slow_query(&self, duration: Duration) -> bool {
        duration > self.config.slow_query_threshold
    }

    pub fn get_optimization_suggestions(&self) -> Vec<OptimizationSuggestion> {
        let mut suggestions = Vec::new();

        // Check pool utilization
        let pool_stats = self.get_pool_stats();
        if pool_stats.utilization_rate > 0.8 {
            suggestions.push(OptimizationSuggestion {
                suggestion_type: SuggestionType::PoolScaling,
                description: format!(
                    "Connection pool utilization is {:.1}%. Consider increasing max_connections.",
                    pool_stats.utilization_rate * 100.0
                ),
                priority: SuggestionPriority::High,
            });
        }

        // Check cache hit rate
        let cache_stats = self.get_query_cache_stats();
        if cache_stats.total_requests() > 100 && cache_stats.hit_rate() < 0.5 {
            suggestions.push(OptimizationSuggestion {
                suggestion_type: SuggestionType::CacheOptimization,
                description: format!(
                    "Query cache hit rate is {:.1}%. Consider adjusting cache TTL or size.",
                    cache_stats.hit_rate() * 100.0
                ),
                priority: SuggestionPriority::Medium,
            });
        }

        suggestions
    }
}

#[derive(Debug, Clone)]
pub enum PoolAction {
    ScaleUp,
    ScaleDown,
    NoAction,
}

#[derive(Debug, Clone)]
pub struct OptimizationSuggestion {
    pub suggestion_type: SuggestionType,
    pub description: String,
    pub priority: SuggestionPriority,
}

#[derive(Debug, Clone, Copy)]
pub enum SuggestionType {
    PoolScaling,
    CacheOptimization,
    QueryOptimization,
    IndexOptimization,
}

#[derive(Debug, Clone, Copy)]
pub enum SuggestionPriority {
    Low,
    Medium,
    High,
}

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

    #[test]
    fn test_cache_entry_creation() {
        let entry = CacheEntry::new("test_data", Some(Duration::from_secs(60)));
        assert_eq!(entry.hit_count, 0);
        assert!(!entry.is_expired());
    }

    #[test]
    fn test_cache_entry_expiration() {
        let entry = CacheEntry::new("test_data", Some(Duration::from_millis(1)));
        std::thread::sleep(Duration::from_millis(10));
        assert!(entry.is_expired());
    }

    #[test]
    fn test_query_cache_basic() {
        let cache = QueryCache::new(100, Some(Duration::from_secs(60)));
        
        cache.set("key1", "value1");
        assert_eq!(cache.get("key1"), Some("value1"));
        assert_eq!(cache.get("key2"), None);
    }

    #[test]
    fn test_query_cache_with_ttl() {
        let cache = QueryCache::new(100, None);
        
        cache.set_with_ttl("key1", "value1", Duration::from_millis(1));
        std::thread::sleep(Duration::from_millis(10));
        assert_eq!(cache.get("key1"), None);
    }

    #[test]
    fn test_cache_stats() {
        let mut stats = CacheStats::default();
        stats.hits = 75;
        stats.misses = 25;
        
        assert_eq!(stats.total_requests(), 100);
        assert_eq!(stats.hit_rate(), 0.75);
        assert_eq!(stats.miss_rate(), 0.25);
    }

    #[test]
    fn test_batch_operation() {
        let batch = BatchOperation::new(10)
            .add_create("item1")
            .add_create("item2")
            .add_delete("id1".to_string());
        
        assert_eq!(batch.len(), 3);
        assert!(!batch.is_empty());
        assert!(!batch.is_full());
    }

    #[test]
    fn test_connection_pool_monitor() {
        let monitor = ConnectionPoolMonitor::new(10, 2);
        
        monitor.record_active();
        monitor.record_active();
        monitor.record_idle();
        
        let stats = monitor.get_stats();
        assert_eq!(stats.active_connections, 1);
        assert_eq!(stats.idle_connections, 1);
        assert_eq!(stats.total_connections, 2);
    }

    #[test]
    fn test_performance_config_default() {
        let config = PerformanceConfig::default();
        assert!(config.query_cache_enabled);
        assert_eq!(config.query_cache_size, 1000);
        assert_eq!(config.batch_size, 100);
        assert_eq!(config.connection_pool_max_size, 10);
    }

    #[test]
    fn test_performance_manager() {
        let config = PerformanceConfig::default();
        let manager = PerformanceManager::new(config);
        
        let stats = manager.get_pool_stats();
        assert_eq!(stats.max_connections, 10);
        
        let cache_stats = manager.get_query_cache_stats();
        assert_eq!(cache_stats.total_requests(), 0);
    }

    #[test]
    fn test_pool_scaling_suggestions() {
        let monitor = ConnectionPoolMonitor::new(10, 2);
        
        // Simulate high utilization
        for _ in 0..9 {
            monitor.record_active();
        }
        
        assert!(monitor.needs_scaling_up());
        assert!(!monitor.needs_scaling_down());
    }

    #[test]
    fn test_cache_invalidation() {
        let cache = QueryCache::new(100, Some(Duration::from_secs(60)));
        
        cache.set("user:1", "data1");
        cache.set("user:2", "data2");
        cache.set("post:1", "data3");
        
        assert_eq!(cache.get("user:1"), Some("data1"));
        assert_eq!(cache.get("user:2"), Some("data2"));
        
        cache.invalidate_prefix("user:");
        
        assert_eq!(cache.get("user:1"), None);
        assert_eq!(cache.get("user:2"), None);
        assert_eq!(cache.get("post:1"), Some("data3"));
    }

    #[test]
    fn test_slow_query_detection() {
        let config = PerformanceConfig::default();
        let manager = PerformanceManager::new(config);
        
        assert!(manager.is_slow_query(Duration::from_millis(1500)));
        assert!(!manager.is_slow_query(Duration::from_millis(500)));
    }

    #[test]
    fn test_optimization_suggestions() {
        let config = PerformanceConfig::default();
        let manager = PerformanceManager::new(config);
        
        let suggestions = manager.get_optimization_suggestions();
        assert!(suggestions.is_empty()); // No suggestions under normal conditions
    }

    #[test]
    fn test_simple_lru_cache_integration() {
        let cache = QueryCache::new(3, None);
        
        cache.set("key1", "value1");
        cache.set("key2", "value2");
        cache.set("key3", "value3");
        cache.set("key4", "value4"); // Should evict key1
        
        assert_eq!(cache.get("key1"), None); // Evicted
        assert_eq!(cache.get("key2"), Some("value2"));
        assert_eq!(cache.get("key3"), Some("value3"));
        assert_eq!(cache.get("key4"), Some("value4"));
    }
}