重构:将示例文件组织到子目录

修复 Go 规范问题:一个目录不能有多个 package main

文件结构调整:
- example/concurrent_example.go → example/concurrent/main.go
- example/index_example.go → example/index/main.go
- example/topic_processor_example.go → example/topic_processor/main.go

修复 API 适配:
- index/main.go: 更新为新的查询 API(移除 startIdx/endIdx 参数)
- webapp/main.go: 使用 processor.Query 方法替代 RecordQuery
  - 移除 queryCache,直接使用 processor
  - 更新查询调用,移除状态参数

文档更新:
- example/README.md: 更新所有示例的运行路径
- example/RUN_CONCURRENT.md: 更新运行命令

所有示例编译测试通过 
This commit is contained in:
2025-10-04 01:27:41 +08:00
parent 4ec153c1ac
commit 3d82a6845e
6 changed files with 34 additions and 39 deletions

View File

@@ -0,0 +1,116 @@
package main
import (
"fmt"
"log"
"log/slog"
"code.tczkiot.com/seqlog"
)
func main() {
// ===== TopicProcessor 作为聚合器使用 =====
fmt.Println("=== TopicProcessor 聚合器示例 ===\n")
// 创建 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=== 所有示例完成 ===")
}