// 演示组管理功能 package main import ( "fmt" "log" "os" "time" "code.tczkiot.com/wlw/pipelinedb" "code.tczkiot.com/wlw/pipelinedb/examples/common" ) func main() { // 创建临时数据库文件 dbFile := "group_example.db" defer os.Remove(dbFile) // 确保文件可以创建 if _, err := os.Create(dbFile); err != nil { log.Fatalf("创建数据库文件失败: %v", err) } fmt.Println("🚀 组管理示例") fmt.Println("==============") // 配置数据库 fmt.Println("\n📂 步骤1: 配置数据库") config := &pipelinedb.Config{ CacheSize: 50, } // 创建处理器 handler := common.NewExampleHandler("组管理示例") pdb, err := pipelinedb.Open(pipelinedb.Options{ Filename: dbFile, Config: config, Handler: handler, }) if err != nil { log.Fatalf("打开数据库失败: %v", err) } defer pdb.Stop() fmt.Println("✅ 数据库已配置") // 创建多个组的数据 fmt.Println("\n📊 步骤2: 创建多组数据") groups := map[string][]string{ "订单处理": { "新订单创建: 订单号 #12345", "支付确认: 订单号 #12345", "库存检查: 商品 SKU-001", "发货准备: 订单号 #12345", "物流跟踪: 快递单号 SF123456", }, "用户管理": { "用户注册: user@example.com", "邮箱验证: user@example.com", "个人资料更新: 用户ID 1001", "密码修改: 用户ID 1001", "账户注销: 用户ID 1002", }, "系统监控": { "CPU使用率: 85%", "内存使用率: 70%", "磁盘空间: 剩余 20GB", "网络延迟: 50ms", "数据库连接数: 150", }, "安全审计": { "登录尝试: IP 192.168.1.100", "权限检查: 用户ID 1001", "异常访问: IP 10.0.0.1", "数据访问: 表 users", "API调用: /api/v1/users", }, } // 插入数据到各个组 for group, messages := range groups { fmt.Printf("\n📝 插入数据到组: %s\n", group) for i, message := range messages { metadata := fmt.Sprintf(`{"sequence": %d, "priority": "normal"}`, i+1) recordID, err := pdb.AcceptData(group, []byte(message), metadata) if err != nil { log.Fatalf("插入数据失败: %v", err) } fmt.Printf(" ✅ 记录 %d: %s\n", recordID, message) } } // 查看初始统计 fmt.Println("\n📊 步骤3: 查看初始统计") stats, err := pdb.GetStats() if err != nil { log.Fatalf("获取统计失败: %v", err) } fmt.Printf("📈 数据库统计:\n") fmt.Printf(" 总记录数: %d\n", stats.TotalRecords) fmt.Printf(" 总组数: %d\n", len(stats.GroupStats)) for group, groupStats := range stats.GroupStats { fmt.Printf(" [%s]: 总计:%d (热:%d 温:%d 冷:%d)\n", group, groupStats.TotalRecords, groupStats.HotRecords, groupStats.WarmRecords, groupStats.ColdRecords) } // 演示组查询 fmt.Println("\n🔍 步骤4: 演示组查询") for group := range groups { fmt.Printf("\n📋 查询组: %s\n", group) // 分页查询 pageReq := &pipelinedb.PageRequest{ Page: 1, PageSize: 3, // 每页3条记录 } response, err := pdb.GetRecordsByGroup(group, pageReq) if err != nil { fmt.Printf(" ❌ 查询失败: %v\n", err) continue } fmt.Printf(" 📊 总记录: %d, 当前页: %d/%d\n", response.TotalCount, response.Page, response.TotalPages) for _, record := range response.Records { fmt.Printf(" 📄 ID:%d [%s] %s\n", record.ID, record.Status, string(record.Data)) } // 如果有多页,查询第二页 if response.TotalPages > 1 { fmt.Printf(" 📄 查询第2页:\n") pageReq.Page = 2 response2, err := pdb.GetRecordsByGroup(group, pageReq) if err != nil { fmt.Printf(" ❌ 查询第2页失败: %v\n", err) continue } for _, record := range response2.Records { fmt.Printf(" 📄 ID:%d [%s] %s\n", record.ID, record.Status, string(record.Data)) } } } // 演示数据状态流转 fmt.Println("\n🔄 步骤5: 观察数据状态流转") // 等待一段时间让数据处理 fmt.Println("⏳ 等待数据处理...") time.Sleep(2 * time.Second) // 再次查看统计 fmt.Println("\n📊 处理后的统计:") finalStats, err := pdb.GetStats() if err != nil { log.Fatalf("获取最终统计失败: %v", err) } for group, groupStats := range finalStats.GroupStats { fmt.Printf(" [%s]: 总计:%d (热:%d 温:%d 冷:%d)\n", group, groupStats.TotalRecords, groupStats.HotRecords, groupStats.WarmRecords, groupStats.ColdRecords) } // 演示跨组查询 fmt.Println("\n🔍 步骤6: 跨组数据分析") totalRecords := 0 totalHot := 0 totalWarm := 0 totalCold := 0 for _, groupStats := range finalStats.GroupStats { totalRecords += groupStats.TotalRecords totalHot += groupStats.HotRecords totalWarm += groupStats.WarmRecords totalCold += groupStats.ColdRecords } fmt.Printf("📊 跨组统计汇总:\n") fmt.Printf(" 总记录数: %d\n", totalRecords) fmt.Printf(" 状态分布:\n") fmt.Printf(" 热数据: %d (%.1f%%)\n", totalHot, float64(totalHot)/float64(totalRecords)*100) fmt.Printf(" 温数据: %d (%.1f%%)\n", totalWarm, float64(totalWarm)/float64(totalRecords)*100) fmt.Printf(" 冷数据: %d (%.1f%%)\n", totalCold, float64(totalCold)/float64(totalRecords)*100) // 找出最活跃的组 maxRecords := 0 mostActiveGroup := "" for group, groupStats := range finalStats.GroupStats { if groupStats.TotalRecords > maxRecords { maxRecords = groupStats.TotalRecords mostActiveGroup = group } } fmt.Printf("\n🏆 最活跃的组: %s (%d 条记录)\n", mostActiveGroup, maxRecords) // 演示组级别的操作 fmt.Println("\n🛠️ 步骤7: 组级别操作演示") fmt.Printf("📋 所有组列表:\n") for group := range finalStats.GroupStats { fmt.Printf(" 📁 %s\n", group) } // 模拟组的生命周期管理 fmt.Printf("\n🔄 组生命周期管理:\n") fmt.Printf(" 1. 创建组 -> 接收数据 -> 处理数据\n") fmt.Printf(" 2. 监控组状态 -> 统计分析\n") fmt.Printf(" 3. 暂停/恢复组 -> 组级别控制\n") fmt.Printf(" 4. 查询组数据 -> 分页浏览\n") fmt.Printf(" 5. 跨组分析 -> 全局统计\n") fmt.Println("\n🎉 组管理示例完成!") fmt.Println("💡 提示: 组功能让你可以按业务逻辑组织和管理数据") fmt.Println("💡 每个组都有独立的统计信息和处理流程") fmt.Println("💡 支持组级别的暂停、恢复和查询操作") }