sz-orm-core 1.0.0

Core ORM engine: Model trait, ActiveRecord, QueryBuilder, Pool, Transaction, migration, and SQL dialect abstraction
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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
//! Entity Graph + @BatchSize 批量抓取
//!
//! 对应文档 6.8 节改进项 27(Entity Graph)+ 28(@BatchSize 批量抓取)。
//!
//! # 核心概念
//!
//! - **EntityGraph**:实体图,定义一组关联关系一起抓取,避免 N+1 查询
//! - **BatchSizeConfig**:批量抓取配置(大小 + 策略)
//! - **BatchLoader**:通用批量加载器,将 N 次单条查询合并为 ⌈N/batch_size⌉ 次批量查询
//! - **BatchStrategy**:批量策略(IN / JOIN / SUBQUERY)
//!
//! # 设计灵感
//!
//! - Hibernate `@NamedEntityGraph` / `@BatchSize`
//! - Doctrine `FetchMode::EAGER` / partial
//! - Django `select_related` / `prefetch_related`
//! - Sequelize `include` + `separate: true`
//!
//! # 使用示例
//!
//! ```
//! use sz_orm_core::entity_graph::{EntityGraph, BatchLoader, BatchSizeConfig, BatchStrategy};
//! use std::collections::HashMap;
//!
//! // 1. 定义 EntityGraph:抓取 user.posts.comments
//! let mut graph = EntityGraph::new();
//! graph.add_edge("user", "posts");
//! graph.add_edge_with_graph("posts", "comments", {
//!     let mut sub = EntityGraph::new();
//!     sub.add_edge("comments", "author");
//!     sub
//! });
//!
//! // 2. 使用 BatchLoader 批量加载用户
//! fn load_users(ids: &[i64]) -> HashMap<i64, String> {
//!     ids.iter().map(|id| (*id, format!("user_{}", id))).collect()
//! }
//!
//! let loader = BatchLoader::new(100, Box::new(load_users));
//! let users = loader.load_many(&[1, 2, 3, 4, 5]);
//! assert_eq!(users.len(), 5);
//! assert_eq!(users.get(&3), Some(&"user_3".to_string()));
//! ```

use std::collections::HashMap;
use std::hash::Hash;
use std::sync::RwLock;

// ============================================================================
// EntityGraph — 实体图
// ============================================================================

/// 实体图边(关联关系)
#[derive(Debug, Clone)]
pub struct GraphEdge {
    /// 父字段名
    pub parent_field: String,
    /// 关联名(如 "posts"、"comments")
    pub relation: String,
    /// 嵌套子图(用于递归抓取)
    pub sub_graph: Option<Box<EntityGraph>>,
}

/// 实体图
///
/// 描述一组关联关系的抓取计划。
///
/// # 示例
///
/// ```
/// use sz_orm_core::entity_graph::EntityGraph;
///
/// let mut graph = EntityGraph::new();
/// graph.add_edge("user", "profile");
/// graph.add_edge("user", "posts");
/// ```
#[derive(Debug, Clone, Default)]
pub struct EntityGraph {
    /// 边列表
    edges: Vec<GraphEdge>,
}

impl EntityGraph {
    /// 创建空实体图
    pub fn new() -> Self {
        Self { edges: Vec::new() }
    }

    /// 添加一条边
    pub fn add_edge(
        &mut self,
        parent_field: impl Into<String>,
        relation: impl Into<String>,
    ) -> &mut Self {
        self.edges.push(GraphEdge {
            parent_field: parent_field.into(),
            relation: relation.into(),
            sub_graph: None,
        });
        self
    }

    /// 添加一条带子图的边(嵌套抓取)
    pub fn add_edge_with_graph(
        &mut self,
        parent_field: impl Into<String>,
        relation: impl Into<String>,
        sub_graph: EntityGraph,
    ) -> &mut Self {
        self.edges.push(GraphEdge {
            parent_field: parent_field.into(),
            relation: relation.into(),
            sub_graph: Some(Box::new(sub_graph)),
        });
        self
    }

    /// 返回所有边
    pub fn edges(&self) -> &[GraphEdge] {
        &self.edges
    }

    /// 返回边的数量
    pub fn edge_count(&self) -> usize {
        self.edges.len()
    }

    /// 查询某个父字段的所有关联
    pub fn relations_of(&self, parent_field: &str) -> Vec<&GraphEdge> {
        self.edges
            .iter()
            .filter(|e| e.parent_field == parent_field)
            .collect()
    }

    /// 收集图中所有关联名(去重)
    pub fn all_relations(&self) -> Vec<String> {
        let mut rels: Vec<String> = self.edges.iter().map(|e| e.relation.clone()).collect();
        rels.sort();
        rels.dedup();
        rels
    }

    /// 收集图中所有父字段(去重)
    pub fn all_parent_fields(&self) -> Vec<String> {
        let mut fields: Vec<String> = self.edges.iter().map(|e| e.parent_field.clone()).collect();
        fields.sort();
        fields.dedup();
        fields
    }

    /// 是否为空图
    pub fn is_empty(&self) -> bool {
        self.edges.is_empty()
    }

    /// 递归收集图中所有关联(含子图)
    pub fn all_relations_recursive(&self) -> Vec<String> {
        let mut result = Vec::new();
        for edge in &self.edges {
            result.push(edge.relation.clone());
            if let Some(sub) = &edge.sub_graph {
                result.extend(sub.all_relations_recursive());
            }
        }
        result.sort();
        result.dedup();
        result
    }
}

// ============================================================================
// BatchStrategy — 批量策略
// ============================================================================

/// 批量抓取策略
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum BatchStrategy {
    /// 使用 `WHERE id IN (?, ?, ...)` 子句批量加载
    ///
    /// 适用场景:关联数量较少、目标表无索引时的备选方案
    #[default]
    In,
    /// 使用 `LEFT JOIN` 一次性加载所有关联
    ///
    /// 适用场景:关联数量较少、需要原子性读取
    Join,
    /// 使用 `WHERE id IN (SELECT ... FROM ...)` 子查询批量加载
    ///
    /// 适用场景:子查询可被数据库优化器优化时
    Subquery,
}

impl BatchStrategy {
    /// 策略名称
    pub fn name(&self) -> &'static str {
        match self {
            BatchStrategy::In => "in",
            BatchStrategy::Join => "join",
            BatchStrategy::Subquery => "subquery",
        }
    }

    /// 生成 IN 子句的 SQL 片段
    ///
    /// 返回形如 `"id IN (?, ?, ?)"` 的字符串(占位符数量与 values 一致)。
    pub fn render_in_clause(column: &str, placeholders: usize) -> String {
        if placeholders == 0 {
            return format!("{} IN ()", column);
        }
        let marks: Vec<&str> = vec!["?"; placeholders];
        format!("{} IN ({})", column, marks.join(", "))
    }
}

// ============================================================================
// BatchSizeConfig — 批量大小配置
// ============================================================================

/// 批量大小配置
///
/// 对应 Hibernate `@BatchSize(size = 100)` 注解。
#[derive(Debug, Clone, Copy)]
pub struct BatchSizeConfig {
    /// 每批数量
    pub size: usize,
    /// 抓取策略
    pub strategy: BatchStrategy,
}

impl Default for BatchSizeConfig {
    fn default() -> Self {
        Self {
            size: 100,
            strategy: BatchStrategy::In,
        }
    }
}

impl BatchSizeConfig {
    /// 创建配置
    pub fn new(size: usize, strategy: BatchStrategy) -> Self {
        Self { size, strategy }
    }

    /// 创建默认策略的配置(IN)
    pub fn with_size(size: usize) -> Self {
        Self {
            size,
            strategy: BatchStrategy::In,
        }
    }

    /// 计算给定总数需要分多少批
    ///
    /// # 示例
    ///
    /// ```
    /// use sz_orm_core::entity_graph::BatchSizeConfig;
    ///
    /// let config = BatchSizeConfig::with_size(100);
    /// assert_eq!(config.batch_count(0), 0);
    /// assert_eq!(config.batch_count(1), 1);
    /// assert_eq!(config.batch_count(100), 1);
    /// assert_eq!(config.batch_count(101), 2);
    /// assert_eq!(config.batch_count(250), 3);
    /// ```
    pub fn batch_count(&self, total: usize) -> usize {
        if total == 0 {
            0
        } else {
            total.div_ceil(self.size)
        }
    }

    /// 返回第 `batch_index` 批的范围(start..end,end 不超过 total)
    ///
    /// # 示例
    ///
    /// ```
    /// use sz_orm_core::entity_graph::BatchSizeConfig;
    ///
    /// let config = BatchSizeConfig::with_size(100);
    /// assert_eq!(config.batch_range(0, 250), 0..100);
    /// assert_eq!(config.batch_range(1, 250), 100..200);
    /// assert_eq!(config.batch_range(2, 250), 200..250);
    /// ```
    pub fn batch_range(&self, batch_index: usize, total: usize) -> std::ops::Range<usize> {
        let start = batch_index * self.size;
        let end = (start + self.size).min(total);
        start..end
    }
}

// ============================================================================
// BatchLoader — 通用批量加载器
// ============================================================================

/// 批量加载函数类型
pub type BatchLoaderFn<K, V> = Box<dyn Fn(&[K]) -> HashMap<K, V> + Send + Sync>;

/// 批量加载器
///
/// 将 N 个单条加载请求合并为 ⌈N/batch_size⌉ 次批量加载,避免 N+1 查询问题。
///
/// # 泛型参数
///
/// - `K`:主键类型(必须实现 `Hash + Eq + Clone`)
/// - `V`:值类型
///
/// # 示例
///
/// ```
/// use sz_orm_core::entity_graph::BatchLoader;
/// use std::collections::HashMap;
///
/// fn load_users(ids: &[i64]) -> HashMap<i64, String> {
///     ids.iter().map(|id| (*id, format!("user_{}", id))).collect()
/// }
///
/// let loader = BatchLoader::new(100, Box::new(load_users));
/// let users = loader.load_many(&[1, 2, 3]);
/// assert_eq!(users.len(), 3);
/// ```
pub struct BatchLoader<K, V>
where
    K: Hash + Eq + Clone + Send + Sync,
    V: Clone + Send + Sync,
{
    /// 每批数量
    batch_size: usize,
    /// 实际加载函数
    loader: BatchLoaderFn<K, V>,
    /// 缓存(避免重复加载相同的 key)
    cache: RwLock<HashMap<K, V>>,
}

impl<K, V> BatchLoader<K, V>
where
    K: Hash + Eq + Clone + Send + Sync,
    V: Clone + Send + Sync,
{
    /// 创建批量加载器
    ///
    /// # 参数
    /// - `batch_size`:每批数量
    /// - `loader`:实际加载函数,接收一批 key,返回 key→value 的 HashMap
    pub fn new(batch_size: usize, loader: BatchLoaderFn<K, V>) -> Self {
        Self {
            batch_size,
            loader,
            cache: RwLock::new(HashMap::new()),
        }
    }

    /// 批量加载多个 key
    ///
    /// - 自动跳过缓存中已有的 key
    /// - 按 batch_size 分批调用 loader
    /// - 返回所有 key 对应的 value(包含缓存与新加载的)
    pub fn load_many(&self, keys: &[K]) -> HashMap<K, V> {
        let mut result: HashMap<K, V> = HashMap::new();

        // 1. 从缓存读取
        let cached = self.cache.read().unwrap();
        let mut to_load: Vec<K> = Vec::new();
        for k in keys {
            if let Some(v) = cached.get(k) {
                result.insert(k.clone(), v.clone());
            } else {
                to_load.push(k.clone());
            }
        }
        drop(cached);

        if to_load.is_empty() {
            return result;
        }

        // 2. 分批加载
        let batch_size = self.batch_size.max(1);
        let mut all_loaded: HashMap<K, V> = HashMap::new();
        for chunk in to_load.chunks(batch_size) {
            let loaded = (self.loader)(chunk);
            all_loaded.extend(loaded);
        }

        // 3. 写入缓存
        let mut cache = self.cache.write().unwrap();
        for (k, v) in &all_loaded {
            cache.insert(k.clone(), v.clone());
        }
        drop(cache);

        // 4. 合并结果
        result.extend(all_loaded);
        result
    }

    /// 加载单个 key(便捷方法)
    pub fn load_one(&self, key: &K) -> Option<V> {
        let result = self.load_many(std::slice::from_ref(key));
        result.get(key).cloned()
    }

    /// 清空缓存
    pub fn clear_cache(&self) {
        self.cache.write().unwrap().clear();
    }

    /// 返回当前缓存大小
    pub fn cache_size(&self) -> usize {
        self.cache.read().unwrap().len()
    }

    /// 返回 batch_size
    pub fn batch_size(&self) -> usize {
        self.batch_size
    }
}

// ============================================================================
// 单元测试
// ============================================================================

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

    // ===== EntityGraph 测试 =====

    #[test]
    fn test_new_graph_is_empty() {
        let g = EntityGraph::new();
        assert!(g.is_empty());
        assert_eq!(g.edge_count(), 0);
    }

    #[test]
    fn test_add_edge() {
        let mut g = EntityGraph::new();
        g.add_edge("user", "posts");
        assert_eq!(g.edge_count(), 1);
        assert!(!g.is_empty());
    }

    #[test]
    fn test_add_multiple_edges() {
        let mut g = EntityGraph::new();
        g.add_edge("user", "posts")
            .add_edge("user", "profile")
            .add_edge("user", "comments");
        assert_eq!(g.edge_count(), 3);
    }

    #[test]
    fn test_add_edge_with_sub_graph() {
        let mut sub = EntityGraph::new();
        sub.add_edge("comments", "author");

        let mut g = EntityGraph::new();
        g.add_edge_with_graph("user", "posts", sub);

        assert_eq!(g.edge_count(), 1);
        assert!(g.edges()[0].sub_graph.is_some());
        assert_eq!(g.edges()[0].sub_graph.as_ref().unwrap().edge_count(), 1);
    }

    #[test]
    fn test_relations_of() {
        let mut g = EntityGraph::new();
        g.add_edge("user", "posts")
            .add_edge("user", "profile")
            .add_edge("post", "comments");

        let user_relations = g.relations_of("user");
        assert_eq!(user_relations.len(), 2);
        assert_eq!(user_relations[0].relation, "posts");
        assert_eq!(user_relations[1].relation, "profile");

        let post_relations = g.relations_of("post");
        assert_eq!(post_relations.len(), 1);

        let none = g.relations_of("nonexistent");
        assert!(none.is_empty());
    }

    #[test]
    fn test_all_relations() {
        let mut g = EntityGraph::new();
        g.add_edge("user", "posts")
            .add_edge("user", "profile")
            .add_edge("post", "comments");

        let rels = g.all_relations();
        assert_eq!(rels, vec!["comments", "posts", "profile"]);
    }

    #[test]
    fn test_all_parent_fields() {
        let mut g = EntityGraph::new();
        g.add_edge("user", "posts")
            .add_edge("user", "profile")
            .add_edge("post", "comments");

        let fields = g.all_parent_fields();
        assert_eq!(fields, vec!["post", "user"]);
    }

    #[test]
    fn test_all_relations_recursive() {
        let mut sub = EntityGraph::new();
        sub.add_edge("comments", "author")
            .add_edge("comments", "likes");

        let mut g = EntityGraph::new();
        g.add_edge("user", "posts")
            .add_edge_with_graph("user", "comments", sub);

        let all = g.all_relations_recursive();
        assert!(all.contains(&"posts".to_string()));
        assert!(all.contains(&"comments".to_string()));
        assert!(all.contains(&"author".to_string()));
        assert!(all.contains(&"likes".to_string()));
        assert_eq!(all.len(), 4);
    }

    #[test]
    fn test_default_graph_is_empty() {
        let g = EntityGraph::default();
        assert!(g.is_empty());
    }

    // ===== BatchStrategy 测试 =====

    #[test]
    fn test_strategy_name() {
        assert_eq!(BatchStrategy::In.name(), "in");
        assert_eq!(BatchStrategy::Join.name(), "join");
        assert_eq!(BatchStrategy::Subquery.name(), "subquery");
    }

    #[test]
    fn test_strategy_default_is_in() {
        assert_eq!(BatchStrategy::default(), BatchStrategy::In);
    }

    #[test]
    fn test_render_in_clause_empty() {
        let sql = BatchStrategy::render_in_clause("id", 0);
        assert_eq!(sql, "id IN ()");
    }

    #[test]
    fn test_render_in_clause_single() {
        let sql = BatchStrategy::render_in_clause("id", 1);
        assert_eq!(sql, "id IN (?)");
    }

    #[test]
    fn test_render_in_clause_multiple() {
        let sql = BatchStrategy::render_in_clause("user_id", 3);
        assert_eq!(sql, "user_id IN (?, ?, ?)");
    }

    // ===== BatchSizeConfig 测试 =====

    #[test]
    fn test_default_config() {
        let config = BatchSizeConfig::default();
        assert_eq!(config.size, 100);
        assert_eq!(config.strategy, BatchStrategy::In);
    }

    #[test]
    fn test_with_size() {
        let config = BatchSizeConfig::with_size(50);
        assert_eq!(config.size, 50);
        assert_eq!(config.strategy, BatchStrategy::In);
    }

    #[test]
    fn test_new_with_strategy() {
        let config = BatchSizeConfig::new(200, BatchStrategy::Join);
        assert_eq!(config.size, 200);
        assert_eq!(config.strategy, BatchStrategy::Join);
    }

    #[test]
    fn test_batch_count_zero() {
        let config = BatchSizeConfig::with_size(100);
        assert_eq!(config.batch_count(0), 0);
    }

    #[test]
    fn test_batch_count_exact_multiple() {
        let config = BatchSizeConfig::with_size(100);
        assert_eq!(config.batch_count(100), 1);
        assert_eq!(config.batch_count(200), 2);
        assert_eq!(config.batch_count(500), 5);
    }

    #[test]
    fn test_batch_count_with_remainder() {
        let config = BatchSizeConfig::with_size(100);
        assert_eq!(config.batch_count(1), 1);
        assert_eq!(config.batch_count(99), 1);
        assert_eq!(config.batch_count(101), 2);
        assert_eq!(config.batch_count(150), 2);
        assert_eq!(config.batch_count(201), 3);
    }

    #[test]
    fn test_batch_range() {
        let config = BatchSizeConfig::with_size(100);

        assert_eq!(config.batch_range(0, 250), 0..100);
        assert_eq!(config.batch_range(1, 250), 100..200);
        assert_eq!(config.batch_range(2, 250), 200..250);
    }

    #[test]
    fn test_batch_range_exact() {
        let config = BatchSizeConfig::with_size(100);

        assert_eq!(config.batch_range(0, 100), 0..100);
        assert_eq!(config.batch_range(1, 100), 100..100); // 空范围
    }

    #[test]
    fn test_batch_range_small_batch() {
        let config = BatchSizeConfig::with_size(10);

        assert_eq!(config.batch_range(0, 25), 0..10);
        assert_eq!(config.batch_range(1, 25), 10..20);
        assert_eq!(config.batch_range(2, 25), 20..25);
    }

    // ===== BatchLoader 测试 =====

    fn make_loader() -> BatchLoader<i64, String> {
        let loader = Box::new(|ids: &[i64]| -> HashMap<i64, String> {
            ids.iter().map(|id| (*id, format!("user_{}", id))).collect()
        });
        BatchLoader::new(2, loader)
    }

    #[test]
    fn test_batch_loader_load_many_single_batch() {
        let loader = make_loader();
        let result = loader.load_many(&[1, 2]);
        assert_eq!(result.len(), 2);
        assert_eq!(result.get(&1), Some(&"user_1".to_string()));
        assert_eq!(result.get(&2), Some(&"user_2".to_string()));
    }

    #[test]
    fn test_batch_loader_load_many_multiple_batches() {
        let loader = make_loader();
        // batch_size=2, 5 keys → 3 batches
        let result = loader.load_many(&[1, 2, 3, 4, 5]);
        assert_eq!(result.len(), 5);
        for id in 1..=5 {
            assert_eq!(
                result.get(&id),
                Some(&format!("user_{}", id)),
                "missing user {}",
                id
            );
        }
    }

    #[test]
    fn test_batch_loader_load_one() {
        let loader = make_loader();
        let result = loader.load_one(&42);
        assert_eq!(result, Some("user_42".to_string()));
    }

    #[test]
    fn test_batch_loader_load_one_missing() {
        // loader 返回的 map 没有 key 100
        let loader: BatchLoader<i64, String> =
            BatchLoader::new(10, Box::new(|_ids: &[i64]| HashMap::new()));
        let result = loader.load_one(&100);
        assert_eq!(result, None);
    }

    #[test]
    fn test_batch_loader_caches_results() {
        let call_count = std::sync::Arc::new(std::sync::Mutex::new(0u32));
        let call_count_clone = call_count.clone();

        let loader = Box::new(move |ids: &[i64]| -> HashMap<i64, String> {
            *call_count_clone.lock().unwrap() += 1;
            ids.iter().map(|id| (*id, format!("user_{}", id))).collect()
        });

        let batch_loader = BatchLoader::new(100, loader);

        // 第一次加载
        batch_loader.load_many(&[1, 2, 3]);
        assert_eq!(*call_count.lock().unwrap(), 1);

        // 第二次加载相同 key,应命中缓存
        batch_loader.load_many(&[1, 2, 3]);
        assert_eq!(*call_count.lock().unwrap(), 1); // 未增加

        // 加载新 key,应触发新的 loader 调用
        batch_loader.load_many(&[4, 5]);
        assert_eq!(*call_count.lock().unwrap(), 2);
    }

    #[test]
    fn test_batch_loader_partial_cache_hit() {
        let call_count = std::sync::Arc::new(std::sync::Mutex::new(0u32));
        let call_count_clone = call_count.clone();

        let loader = Box::new(move |ids: &[i64]| -> HashMap<i64, String> {
            *call_count_clone.lock().unwrap() += 1;
            ids.iter().map(|id| (*id, format!("user_{}", id))).collect()
        });

        let batch_loader = BatchLoader::new(100, loader);

        // 加载 1, 2, 3
        batch_loader.load_many(&[1, 2, 3]);
        assert_eq!(*call_count.lock().unwrap(), 1);

        // 加载 1, 2, 3, 4, 5(前 3 个命中缓存)
        let result = batch_loader.load_many(&[1, 2, 3, 4, 5]);
        assert_eq!(result.len(), 5);
        assert_eq!(*call_count.lock().unwrap(), 2); // 只为 4, 5 调用一次

        // 缓存大小应为 5
        assert_eq!(batch_loader.cache_size(), 5);
    }

    #[test]
    fn test_batch_loader_clear_cache() {
        let loader = make_loader();
        loader.load_many(&[1, 2]);
        assert_eq!(loader.cache_size(), 2);

        loader.clear_cache();
        assert_eq!(loader.cache_size(), 0);
    }

    #[test]
    fn test_batch_loader_empty_input() {
        let loader = make_loader();
        let result = loader.load_many(&[]);
        assert!(result.is_empty());
    }

    #[test]
    fn test_batch_loader_batch_size_attribute() {
        let loader = make_loader();
        assert_eq!(loader.batch_size(), 2);
    }

    #[test]
    fn test_batch_loader_with_size_1() {
        let loader = BatchLoader::new(
            1,
            Box::new(|ids: &[i64]| ids.iter().map(|id| (*id, *id * 10)).collect()),
        );
        let result = loader.load_many(&[1, 2, 3]);
        assert_eq!(result.len(), 3);
        assert_eq!(result.get(&1), Some(&10));
        assert_eq!(result.get(&2), Some(&20));
        assert_eq!(result.get(&3), Some(&30));
    }

    // ===== 集成场景测试 =====

    #[test]
    fn test_workflow_graph_and_batch_loader() {
        // 模拟 User → Posts → Comments 的批量加载场景
        let mut graph = EntityGraph::new();
        graph.add_edge_with_graph("user", "posts", {
            let mut sub = EntityGraph::new();
            sub.add_edge("posts", "comments");
            sub
        });
        assert_eq!(graph.all_relations_recursive().len(), 2);

        // 模拟批量加载用户
        let user_loader = BatchLoader::new(
            50,
            Box::new(|ids: &[i64]| ids.iter().map(|id| (*id, format!("User#{}", id))).collect()),
        );

        // 加载 123 个用户(应分 3 批)
        let user_ids: Vec<i64> = (1..=123).collect();
        let users = user_loader.load_many(&user_ids);
        assert_eq!(users.len(), 123);
        assert_eq!(user_loader.cache_size(), 123);
    }

    #[test]
    fn test_n_plus_1_problem_solved() {
        // 经典 N+1 问题演示:
        // - 错误做法:N 个用户各发 1 次查询加载 posts → N+1 次查询
        // - 正确做法:用 BatchLoader 一次批量加载 → ⌈N/batch⌉+1 次查询

        let query_count = std::sync::Arc::new(std::sync::Mutex::new(0u32));
        let query_count_clone = query_count.clone();

        let post_loader = BatchLoader::new(
            100,
            Box::new(move |user_ids: &[i64]| {
                *query_count_clone.lock().unwrap() += 1;
                // 模拟为每个 user_id 返回 posts
                user_ids
                    .iter()
                    .map(|uid| (*uid, format!("posts_for_user_{}", uid)))
                    .collect()
            }),
        );

        // 250 个用户
        let user_ids: Vec<i64> = (1..=250).collect();
        let _posts = post_loader.load_many(&user_ids);

        // 应分 3 批(100+100+50),调用 loader 3 次
        assert_eq!(*query_count.lock().unwrap(), 3);
    }
}