Files
seqlog/example/topic_processor/main.go
bourdon 48b0b56ce7 功能:实现 webapp 基于索引的日志导航
主要改进:
- 添加索引导航支持:前进/后退按钮现在基于记录索引加载数据
- 后端 API 支持可选的 index 参数,返回包含 centerIndex 的响应
- 前端追踪 currentCenterIndex,实现精确的页面跳转
- 在状态徽章中显示记录索引号 [#索引]
- 修复日志显示逻辑:从追加模式改为完全重新渲染

代码优化:
- concurrent: 使用 Go 1.25 range 语法和 min 函数
- concurrent: 使用 WaitGroup.Go 方法简化 goroutine 启动
- topic_processor: 修正格式化输出

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-04 01:43:48 +08:00

117 lines
3.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"fmt"
"log"
"log/slog"
"code.tczkiot.com/seqlog"
)
func main() {
// ===== TopicProcessor 作为聚合器使用 =====
fmt.Println("=== TopicProcessor 聚合器示例 ===")
// 创建 TopicProcessor提供空 handler
logger := slog.Default()
tp, err := seqlog.NewTopicProcessor("test_seqlog", "app", logger, &seqlog.TopicConfig{
Handler: func(rec *seqlog.Record) error {
return nil // 示例中不需要处理
},
})
if err != nil {
log.Fatalf("创建 TopicProcessor 失败: %v", err)
}
// ===== 1. 写入数据 =====
fmt.Println("1. 写入数据:")
for i := 1; i <= 5; i++ {
data := fmt.Sprintf("消息 #%d", i)
offset, err := tp.Write([]byte(data))
if err != nil {
log.Fatal(err)
}
fmt.Printf(" 写入成功: offset=%d, data=%s\n", offset, data)
}
fmt.Println()
// ===== 2. 获取记录总数 =====
fmt.Println("2. 查询记录总数:")
count := tp.GetRecordCount()
fmt.Printf(" 总共 %d 条记录\n\n", count)
// ===== 3. 获取索引 =====
fmt.Println("3. 使用索引:")
index := tp.Index()
fmt.Printf(" 索引记录数: %d\n", index.Count())
fmt.Printf(" 最后偏移: %d\n\n", index.LastOffset())
// ===== 4. 使用查询器查询 =====
fmt.Println("4. 查询记录:")
// 查询最老的 3 条记录(从索引 0 开始)
oldest, err := tp.QueryOldest(0, 3)
if err != nil {
log.Fatal(err)
}
fmt.Println(" 查询最老的 3 条:")
for i, rws := range oldest {
fmt.Printf(" [%d] 状态=%s, 数据=%s\n", i, rws.Status, string(rws.Record.Data))
}
// 查询最新的 2 条记录(从最后一条开始)
totalCount := tp.GetRecordCount()
newest, err := tp.QueryNewest(totalCount-1, 2)
if err != nil {
log.Fatal(err)
}
fmt.Println(" 查询最新的 2 条:")
for i, rws := range newest {
fmt.Printf(" [%d] 状态=%s, 数据=%s\n", i, rws.Status, string(rws.Record.Data))
}
fmt.Println()
// ===== 5. 使用游标读取 =====
fmt.Println("5. 使用游标读取:")
cursor, err := tp.Cursor()
if err != nil {
log.Fatal(err)
}
defer cursor.Close()
// 读取 3 条记录
records, err := cursor.NextRange(3)
if err != nil {
log.Fatal(err)
}
fmt.Printf(" 读取了 %d 条记录:\n", len(records))
for i, rec := range records {
fmt.Printf(" [%d] %s\n", i, string(rec.Data))
}
// 提交游标位置
cursor.Commit()
fmt.Printf(" 游标位置: start=%d, end=%d\n\n", cursor.StartIndex(), cursor.EndIndex())
// ===== 6. 继续写入 =====
fmt.Println("6. 继续写入:")
for i := 6; i <= 8; i++ {
data := fmt.Sprintf("消息 #%d", i)
offset, _ := tp.Write([]byte(data))
fmt.Printf(" 写入成功: offset=%d, data=%s\n", offset, data)
}
fmt.Println()
// ===== 7. 再次查询总数 =====
fmt.Println("7. 更新后的记录总数:")
count = tp.GetRecordCount()
fmt.Printf(" 总共 %d 条记录\n\n", count)
// ===== 8. 获取统计信息 =====
fmt.Println("8. 统计信息:")
stats := tp.GetStats()
fmt.Printf(" 写入: %d 条, %d 字节\n", stats.WriteCount, stats.WriteBytes)
fmt.Println("\n=== 所有示例完成 ===")
}