rat_quickdb 0.5.2

强大的跨数据库ODM库,支持自动索引创建、统一接口和现代异步架构
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
//! RatQuickDB MongoDB 模型操作演示
//!
//! 展示完整的模型操作:
//! - 模型定义和验证
//! - CRUD操作
//! - 并发测试
//! - 分页查询

use chrono::Utc;
use rat_logger::{LevelFilter, LoggerBuilder, handler::term::TermConfig};
use rat_quickdb::types::*;
use rat_quickdb::*;
use rat_quickdb::{
    ModelManager, ModelOperations, array_field, boolean_field, datetime_field, float_field,
    integer_field, json_field, string_field, uuid_field,
};

// 数据库别名常量
const DATABASE_ALIAS: &str = "main";
use std::collections::HashMap;
use std::time::Instant;
use tokio::join;

// 用户模型
define_model! {
    struct User {
        id: String,
        username: String,
        email: String,
        password_hash: String,
        full_name: String,
        age: Option<i32>,
        is_active: bool,
        created_at: chrono::DateTime<chrono::Utc>,
        tags: Option<Vec<String>>,
    }
    collection = "users",
    database = DATABASE_ALIAS,
    fields = {
        id: uuid_field().required().unique(),
        username: string_field(None, None, None).required().unique(),
        email: string_field(None, None, None).required().unique(),
        password_hash: string_field(None, None, None).required(),
        full_name: string_field(None, None, None).required(),
        age: integer_field(None, None),
        is_active: boolean_field().required(),
        created_at: datetime_field().required(),
        tags: array_field(field_types!(string), None, None),
    }
    indexes = [
        { fields: ["username"], unique: true, name: "idx_username" },
        { fields: ["email"], unique: true, name: "idx_email" },
        { fields: ["is_active", "created_at"], unique: false, name: "idx_active_created" },
    ],
}

// 员工模型 - 用于分页测试
define_model! {
    struct Employee {
        id: String,
        employee_id: String,
        name: String,
        department: String,
        salary: f64,
        is_active: bool,
        created_at: chrono::DateTime<chrono::Utc>,
    }
    collection = "employees",
    database = DATABASE_ALIAS,
    fields = {
        id: uuid_field().required().unique(),
        employee_id: string_field(None, None, None).required().unique(),
        name: string_field(None, None, None).required(),
        department: string_field(None, None, None).required(),
        salary: float_field(None, None).required(),
        is_active: boolean_field().required(),
        created_at: datetime_field().required(),
    }
    indexes = [
        { fields: ["employee_id"], unique: true, name: "idx_employee_id" },
        { fields: ["department"], unique: false, name: "idx_department" },
        { fields: ["salary"], unique: false, name: "idx_salary" },
    ],
}

// 简单性能统计
#[derive(Debug)]
struct SimpleStats {
    total_operations: u64,
    successful_operations: u64,
    total_time_ms: u64,
}

impl SimpleStats {
    fn new() -> Self {
        Self {
            total_operations: 0,
            successful_operations: 0,
            total_time_ms: 0,
        }
    }

    fn add_operation(&mut self, duration_ms: u64, success: bool) {
        self.total_operations += 1;
        self.total_time_ms += duration_ms;
        if success {
            self.successful_operations += 1;
        }
    }

    fn average_time(&self) -> f64 {
        if self.total_operations == 0 {
            0.0
        } else {
            self.total_time_ms as f64 / self.total_operations as f64
        }
    }

    fn success_rate(&self) -> f64 {
        if self.total_operations == 0 {
            0.0
        } else {
            self.successful_operations as f64 / self.total_operations as f64 * 100.0
        }
    }
}

// 清理测试数据
async fn cleanup_test_data() {
    println!("清理测试数据...");

    // 清理用户表
    println!("正在清理用户表...");
    match rat_quickdb::drop_table(DATABASE_ALIAS, "users").await {
        Ok(_) => println!("✅ 用户表清理成功"),
        Err(e) => println!("⚠️  清理用户表失败: {}", e),
    }

    // 清理员工表
    if let Err(e) = rat_quickdb::drop_table(DATABASE_ALIAS, "employees").await {
        println!("清理员工表失败: {}", e);
    }
}

// 演示基础CRUD操作
async fn demonstrate_crud() -> Result<SimpleStats, Box<dyn std::error::Error>> {
    println!("\n=== CRUD操作演示 ===");

    let mut stats = SimpleStats::new();

    // 测试表不存在错误识别
    println!("\n测试表不存在错误识别功能...");
    match ModelManager::<User>::find_by_id("00000000-0000-0000-0000-000000000000").await {
        Err(QuickDbError::TableNotExistError { table, message }) => {
            println!("✅ 成功识别表不存在错误:");
            println!("   表名: {}", table);
            println!("   错误信息: {}", message);
        }
        Err(e) => {
            println!("⚠️  识别到其他错误: {}", e);
        }
        Ok(_) => {
            println!("ℹ️  表已存在(可能是之前创建的)");
        }
    }

    // 创建用户
    let user = User {
        id: String::new(),
        username: format!("test_user_{}", uuid::Uuid::new_v4().simple()),
        email: format!("test_{}@example.com", uuid::Uuid::new_v4().simple()),
        password_hash: "hashed_password".to_string(),
        full_name: "测试用户".to_string(),
        age: Some(25),
        is_active: true,
        created_at: Utc::now(),
        tags: Some(vec!["测试".to_string(), "用户".to_string()]),
    };

    // 插入
    let start = Instant::now();
    let user_id = match user.save().await {
        Ok(id) => {
            println!("✅ 用户创建成功: {}", id);
            id
        }
        Err(e) => {
            println!("❌ 用户创建失败: {}", e);
            return Ok(stats);
        }
    };
    stats.add_operation(start.elapsed().as_millis() as u64, true);

    // 查询
    let start = Instant::now();
    match ModelManager::<User>::find_by_id(&user_id).await {
        Ok(Some(found_user)) => {
            println!("✅ 用户查询成功: {}", found_user.username);
            stats.add_operation(start.elapsed().as_millis() as u64, true);

            // 更新
            let start = Instant::now();
            let mut update_data = HashMap::new();
            update_data.insert("age".to_string(), DataValue::Int(26));

            match found_user.update(update_data).await {
                Ok(_) => {
                    println!("✅ 用户更新成功");
                    stats.add_operation(start.elapsed().as_millis() as u64, true);
                }
                Err(e) => {
                    println!("❌ 用户更新失败: {}", e);
                    stats.add_operation(start.elapsed().as_millis() as u64, false);
                }
            }

            // 删除
            let start = Instant::now();
            match found_user.delete().await {
                Ok(_) => {
                    println!("✅ 用户删除成功");
                    stats.add_operation(start.elapsed().as_millis() as u64, true);
                }
                Err(e) => {
                    println!("❌ 用户删除失败: {}", e);
                    stats.add_operation(start.elapsed().as_millis() as u64, false);
                }
            }
        }
        Ok(None) => {
            println!("❌ 用户未找到");
            stats.add_operation(start.elapsed().as_millis() as u64, false);
        }
        Err(e) => {
            println!("❌ 查询失败: {}", e);
            stats.add_operation(start.elapsed().as_millis() as u64, false);
        }
    }

    Ok(stats)
}

// 演示并发操作
async fn demonstrate_concurrency() -> Result<SimpleStats, Box<dyn std::error::Error>> {
    println!("\n=== 并发操作演示 ===");

    let mut stats = SimpleStats::new();
    let concurrent_count = 10;

    // 并发插入
    let mut tasks = Vec::new();
    for i in 0..concurrent_count {
        let task = tokio::spawn(async move {
            let user = User {
                id: String::new(),
                username: format!("concurrent_user_{}_{}", i, uuid::Uuid::new_v4().simple()),
                email: format!(
                    "concurrent_{}_{}@example.com",
                    i,
                    uuid::Uuid::new_v4().simple()
                ),
                password_hash: "hashed_password".to_string(),
                full_name: format!("并发用户 {}", i),
                age: Some(20 + i),
                is_active: true,
                created_at: Utc::now(),
                tags: Some(vec!["并发".to_string(), "测试".to_string()]),
            };

            let start = Instant::now();
            match user.save().await {
                Ok(id) => {
                    println!("  并发创建用户 {}: 成功 ({})", i, id);
                    (start.elapsed().as_millis() as u64, true)
                }
                Err(e) => {
                    println!("  并发创建用户 {}: 失败 - {}", i, e);
                    (start.elapsed().as_millis() as u64, false)
                }
            }
        });
        tasks.push(task);
    }

    // 等待所有任务完成
    for (i, task) in tasks.into_iter().enumerate() {
        match task.await {
            Ok((duration, success)) => {
                stats.add_operation(duration, success);
            }
            Err(e) => {
                println!("任务 {} 执行失败: {}", i, e);
                stats.add_operation(0, false);
            }
        }
    }

    println!(
        "并发操作完成 - 成功率: {:.1}%, 平均耗时: {:.1}ms",
        stats.success_rate(),
        stats.average_time()
    );

    Ok(stats)
}

// 演示分页查询
async fn demonstrate_pagination() -> Result<SimpleStats, Box<dyn std::error::Error>> {
    println!("\n=== 分页查询演示 ===");

    let mut stats = SimpleStats::new();

    // 先创建一些测试数据
    println!("创建测试员工数据...");
    let mut created_ids = Vec::new();
    for i in 0..50 {
        let employee = Employee {
            id: String::new(),
            employee_id: format!("EMP{:04}", i),
            name: format!("员工 {}", i),
            department: match i % 3 {
                0 => "技术部".to_string(),
                1 => "销售部".to_string(),
                _ => "人事部".to_string(),
            },
            salary: 5000.0 + (i as f64 * 100.0),
            is_active: i % 5 != 0, // 80%活跃
            created_at: Utc::now(),
        };

        match employee.save().await {
            Ok(id) => created_ids.push(id),
            Err(e) => println!("创建员工 {} 失败: {}", i, e),
        }
    }

    // 分页查询
    let page_size = 10;
    for page in 0..5 {
        let start = Instant::now();
        let pagination = PaginationConfig {
            limit: page_size,
            skip: page * page_size,
        };

        let options = QueryOptions {
            pagination: Some(pagination),
            ..Default::default()
        };

        match ModelManager::<Employee>::find(vec![], Some(options)).await {
            Ok(employees) => {
                stats.add_operation(start.elapsed().as_millis() as u64, true);
                println!("{} 页: {} 条记录", page + 1, employees.len());

                // 显示前几条记录
                for (i, emp) in employees.iter().take(3).enumerate() {
                    println!(
                        "  {}. {} - {} - ${:.0}",
                        (page as u64 * page_size as u64 + i as u64 + 1),
                        emp.name,
                        emp.department,
                        emp.salary
                    );
                }
                if employees.len() > 3 {
                    println!("     ... 还有 {}", employees.len() - 3);
                }
            }
            Err(e) => {
                stats.add_operation(start.elapsed().as_millis() as u64, false);
                println!("{} 页查询失败: {}", page + 1, e);
            }
        }
    }

    // 清理测试数据
    println!("清理测试数据...");
    for id in created_ids {
        if let Ok(Some(employee)) = ModelManager::<Employee>::find_by_id(&id).await {
            let _ = employee.delete().await;
        }
    }

    println!(
        "分页查询完成 - 成功率: {:.1}%, 平均耗时: {:.1}ms",
        stats.success_rate(),
        stats.average_time()
    );

    Ok(stats)
}

// 性能基准测试
async fn performance_benchmark() -> Result<(), Box<dyn std::error::Error>> {
    println!("\n=== 性能基准测试 ===");

    let test_count = 100;
    let start = Instant::now();

    // 批量创建测试
    let mut successful = 0;
    for i in 0..test_count {
        let user = User {
            id: String::new(),
            username: format!("perf_user_{}", i),
            email: format!("perf_{}@test.com", i),
            password_hash: "hash".to_string(),
            full_name: format!("性能用户 {}", i),
            age: Some(25 + i),
            is_active: true,
            created_at: Utc::now(),
            tags: Some(vec!["性能测试".to_string()]),
        };

        if user.save().await.is_ok() {
            successful += 1;
        }
    }

    let create_time = start.elapsed();
    println!(
        "创建 {} 条记录: 成功 {} 条, 耗时 {:?}, 平均 {:.1}ms/条",
        test_count,
        successful,
        create_time,
        create_time.as_millis() as f64 / test_count as f64
    );

    // 批量查询测试
    let start = Instant::now();
    let mut found = 0;

    match ModelManager::<User>::find(vec![], None).await {
        Ok(users) => {
            found = users.len();
        }
        Err(e) => println!("批量查询失败: {}", e),
    }

    let query_time = start.elapsed();
    println!(
        "查询 {} 条记录: 找到 {} 条, 耗时 {:?}",
        test_count, found, query_time
    );

    Ok(())
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("=== RatQuickDB MongoDB 模型操作演示 ===");

    // 初始化日志
    LoggerBuilder::new()
        .with_level(LevelFilter::Warn) // 减少输出
        .add_terminal_with_config(TermConfig::default())
        .init()?;

    // 初始化数据库
    // TLS配置
    let tls_config = rat_quickdb::types::TlsConfig {
        enabled: true,
        ca_cert_path: None,
        client_cert_path: None,
        client_key_path: None,
        verify_server_cert: false,
        verify_hostname: false,
        min_tls_version: Some("1.2".to_string()),
        cipher_suites: None,
    };

    // ZSTD压缩配置
    let zstd_config = rat_quickdb::types::ZstdConfig {
        enabled: true,
        compression_level: Some(3),
        compression_threshold: Some(1024),
    };

    let db_config = DatabaseConfig::builder()
        .db_type(DatabaseType::MongoDB)
        .connection(ConnectionConfig::MongoDB {
            host: "db0.0ldm0s.net".to_string(),
            port: 27017,
            database: "testdb".to_string(),
            username: Some("testdb".to_string()),
            password: Some("testdb123456".to_string()),
            auth_source: Some("testdb".to_string()),
            direct_connection: true,
            options: None,
            tls_config: Some(tls_config),
            zstd_config: Some(zstd_config),
        })
        .pool(
            PoolConfig::builder()
                .max_connections(25)
                .min_connections(5)
                .connection_timeout(10)
                .idle_timeout(30)
                .max_lifetime(1200)
                .max_retries(6)
                .retry_interval_ms(250)
                .keepalive_interval_sec(20)
                .health_check_timeout_sec(3)
                .build()?,
        )
        .alias(DATABASE_ALIAS)
        .id_strategy(IdStrategy::Uuid)
        .build()?;

    add_database(db_config).await?;
    println!("数据库连接成功");

    // 清理测试数据
    cleanup_test_data().await;
    println!("清理完成");

    // 执行演示
    let crud_stats = demonstrate_crud().await?;
    let concurrent_stats = demonstrate_concurrency().await?;
    let pagination_stats = demonstrate_pagination().await?;

    performance_benchmark().await?;

    // 输出统计
    println!("\n=== 操作统计 ===");
    println!(
        "CRUD操作: {} 次, 成功率 {:.1}%, 平均 {:.1}ms",
        crud_stats.total_operations,
        crud_stats.success_rate(),
        crud_stats.average_time()
    );
    println!(
        "并发操作: {} 次, 成功率 {:.1}%, 平均 {:.1}ms",
        concurrent_stats.total_operations,
        concurrent_stats.success_rate(),
        concurrent_stats.average_time()
    );
    println!(
        "分页操作: {} 次, 成功率 {:.1}%, 平均 {:.1}ms",
        pagination_stats.total_operations,
        pagination_stats.success_rate(),
        pagination_stats.average_time()
    );

    // 健康检查
    println!("\n=== 健康检查 ===");
    let health = health_check().await;
    for (alias, is_healthy) in health {
        let status = if is_healthy { "" } else { "" };
        println!("{}: {}", alias, status);
    }

    // 清理
    cleanup_test_data().await;
    println!("\n演示完成");

    Ok(())
}