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

修复 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

@@ -4,7 +4,7 @@
## 示例列表 ## 示例列表
### 1. concurrent_example.go - 高并发示例 ### 1. concurrent/ - 高并发示例
展示 seqlog 在高并发场景下的性能表现。 展示 seqlog 在高并发场景下的性能表现。
@@ -19,7 +19,8 @@
**运行方式:** **运行方式:**
```bash ```bash
go run concurrent_example.go cd concurrent
go run main.go
``` ```
**注意:** 这个示例需要运行约 5 分钟,请耐心等待。 **注意:** 这个示例需要运行约 5 分钟,请耐心等待。
@@ -97,7 +98,7 @@ go run concurrent_example.go
- 多个 goroutine 可以并发查询同一或不同的 topic - 多个 goroutine 可以并发查询同一或不同的 topic
- 查询时可能遇到少量 EOF 错误(因为 tailer 正在处理文件) - 查询时可能遇到少量 EOF 错误(因为 tailer 正在处理文件)
### 2. topic_processor_example.go - TopicProcessor 基础示例 ### 2. topic_processor/ - TopicProcessor 基础示例
展示如何使用 TopicProcessor 作为日志聚合器。 展示如何使用 TopicProcessor 作为日志聚合器。
@@ -109,16 +110,18 @@ go run concurrent_example.go
**运行方式:** **运行方式:**
```bash ```bash
go run topic_processor_example.go cd topic_processor
go run main.go
``` ```
### 3. index_example.go - 索引功能示例 ### 3. index/ - 索引功能示例
展示索引文件的使用和管理。 展示索引文件的使用和管理。
**运行方式:** **运行方式:**
```bash ```bash
go run index_example.go cd index
go run main.go
``` ```
### 4. webapp/ - Web 应用示例 ### 4. webapp/ - Web 应用示例

View File

@@ -3,8 +3,8 @@
## 快速开始 ## 快速开始
```bash ```bash
cd example cd example/concurrent
go run concurrent_example.go go run main.go
``` ```
## 预计运行时间 ## 预计运行时间
@@ -22,7 +22,8 @@ go run concurrent_example.go
如果想在后台运行并保存日志: 如果想在后台运行并保存日志:
```bash ```bash
go run concurrent_example.go > output.log 2>&1 & cd example/concurrent
go run main.go > output.log 2>&1 &
echo $! > pid.txt echo $! > pid.txt
# 查看实时输出 # 查看实时输出
@@ -74,7 +75,7 @@ rm pid.txt
## 自定义测试 ## 自定义测试
如果想调整测试参数,编辑 `concurrent_example.go` 如果想调整测试参数,编辑 `concurrent/main.go`
```go ```go
// 场景 1每个 topic 写入的消息数 // 场景 1每个 topic 写入的消息数

View File

@@ -27,12 +27,14 @@ func main() {
} }
// 写入日志时,索引会自动更新 // 写入日志时,索引会自动更新
var lastOffset int64
for i := 1; i <= 10; i++ { for i := 1; i <= 10; i++ {
data := fmt.Sprintf("日志记录 #%d", i) data := fmt.Sprintf("日志记录 #%d", i)
offset, err := writer.Append([]byte(data)) offset, err := writer.Append([]byte(data))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
lastOffset = offset
fmt.Printf("写入: offset=%d, data=%s\n", offset, data) fmt.Printf("写入: offset=%d, data=%s\n", offset, data)
} }
@@ -68,23 +70,23 @@ func main() {
fmt.Printf("从第 %d 条记录开始查询\n", startIndex) fmt.Printf("从第 %d 条记录开始查询\n", startIndex)
// 向后查询(查询更早的记录) // 向后查询(查询更早的记录)
backward, err := query.QueryNewest(startIndex-1, 3, 0, startIndex) backward, err := query.QueryNewest(startIndex-1, 3)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
fmt.Printf("向后查询 3 条记录:\n") fmt.Printf("向后查询 3 条记录:\n")
for i, rws := range backward { for i, rec := range backward {
fmt.Printf(" [%d] 状态=%s, 数据=%s\n", i, rws.Status, string(rws.Record.Data)) fmt.Printf(" [%d] 数据=%s\n", i, string(rec.Data))
} }
// 向前查询(查询更新的记录) // 向前查询(查询更新的记录)
forward, err := query.QueryOldest(startIndex, 3, 0, startIndex) forward, err := query.QueryOldest(startIndex, 3)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
fmt.Printf("向前查询 3 条记录:\n") fmt.Printf("向前查询 3 条记录:\n")
for i, rws := range forward { for i, rec := range forward {
fmt.Printf(" [%d] 状态=%s, 数据=%s\n", i, rws.Status, string(rws.Record.Data)) fmt.Printf(" [%d] 数据=%s\n", i, string(rec.Data))
} }
fmt.Println() fmt.Println()
@@ -103,8 +105,8 @@ func main() {
fmt.Printf("最后一条记录偏移: %d\n", index3.LastOffset()) fmt.Printf("最后一条记录偏移: %d\n", index3.LastOffset())
// 二分查找:根据偏移量查找索引位置 // 二分查找:根据偏移量查找索引位置
idx := index3.FindIndex(offset) idx := index3.FindIndex(lastOffset)
fmt.Printf("偏移量 %d 对应的索引位置: %d\n\n", offset, idx) fmt.Printf("偏移量 %d 对应的索引位置: %d\n\n", lastOffset, idx)
index3.Close() index3.Close()
// ===== 示例 4追加写入索引自动更新===== // ===== 示例 4追加写入索引自动更新=====

View File

@@ -8,7 +8,6 @@ import (
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
"sync"
"time" "time"
"code.tczkiot.com/seqlog" "code.tczkiot.com/seqlog"
@@ -17,8 +16,6 @@ import (
var ( var (
seq *seqlog.Seqlog seq *seqlog.Seqlog
logger *slog.Logger logger *slog.Logger
queryCache = make(map[string]*seqlog.RecordQuery)
queryCacheMu sync.RWMutex
) )
func main() { func main() {
@@ -522,20 +519,12 @@ func handleQuery(w http.ResponseWriter, r *http.Request) {
forward = 10 forward = 10
} }
// 从缓存中获取或创建 query 对象 // 获取 processor
queryCacheMu.Lock() processor, err := seq.GetProcessor(topic)
query, exists := queryCache[topic]
if !exists {
var err error
query, err = seq.NewTopicQuery(topic)
if err != nil { if err != nil {
queryCacheMu.Unlock()
http.Error(w, err.Error(), http.StatusNotFound) http.Error(w, err.Error(), http.StatusNotFound)
return return
} }
queryCache[topic] = query
}
queryCacheMu.Unlock()
// 获取当前处理索引和读取索引 // 获取当前处理索引和读取索引
startIdx := seq.GetProcessingIndex(topic) startIdx := seq.GetProcessingIndex(topic)
@@ -546,7 +535,7 @@ func handleQuery(w http.ResponseWriter, r *http.Request) {
// 向后查询 // 向后查询
if backward > 0 && startIdx > 0 { if backward > 0 && startIdx > 0 {
backResults, err := query.QueryNewest(startIdx-1, backward, startIdx, endIdx) backResults, err := processor.QueryNewest(startIdx-1, backward)
if err == nil { if err == nil {
results = append(results, backResults...) results = append(results, backResults...)
} }
@@ -554,7 +543,7 @@ func handleQuery(w http.ResponseWriter, r *http.Request) {
// 当前位置 // 当前位置
if startIdx < endIdx { if startIdx < endIdx {
currentResults, err := query.QueryOldest(startIdx, 1, startIdx, endIdx) currentResults, err := processor.QueryOldest(startIdx, 1)
if err == nil { if err == nil {
results = append(results, currentResults...) results = append(results, currentResults...)
} }
@@ -562,7 +551,7 @@ func handleQuery(w http.ResponseWriter, r *http.Request) {
// 向前查询 // 向前查询
if forward > 0 { if forward > 0 {
forwardResults, err := query.QueryOldest(endIdx, forward, startIdx, endIdx) forwardResults, err := processor.QueryOldest(endIdx, forward)
if err == nil { if err == nil {
results = append(results, forwardResults...) results = append(results, forwardResults...)
} }