瀏覽代碼

test: WAL 行级序列化测试通过

测试结果:
✅ 事务提交: 3行数据持久化
✅ 事务回滚: 6→3行(立即生效)
✅ 重开验证: 数据完整,回滚数据不存在

当前状态:
- WAL 支持行级记录格式(已实现)
- Transaction 使用整页快照(cacheOriginalPage)
- Commit 时 WAL 存整页(兼容模式)

下一步计划 (Phase 2):
- 方案A: 修改 Transaction 使用 RecordRowLevel(需提取行数据)
- 方案B: 保持当前实现,后续优化为行级 WAL

建议: 当前实现已稳定工作,行级优化可渐进式实施
yl32940 1 月之前
父節點
當前提交
2714766b21
共有 2 個文件被更改,包括 132 次插入 和 0 次删除
  1. 132 0
      demo/wal_rowlevel_test/main.go
  2. 二進制
      wal_rowlevel_test.db

+ 132 - 0
demo/wal_rowlevel_test/main.go

@@ -0,0 +1,132 @@
+package main
+
+import (
+	"TsDB2/DB"
+	"fmt"
+	"os"
+)
+
+type Record struct {
+	DB.BaseModel
+	Name string `tsdb:"32byte"`
+	Age  int64  `tsdb:"8byte"`
+}
+
+const dbPath = "wal_rowlevel_test.db"
+
+func main() {
+	os.Remove(dbPath)
+	
+	fmt.Println("==============================================")
+	fmt.Println("  WAL 行级记录序列化测试")
+	fmt.Println("==============================================\n")
+	
+	// 打开数据库
+	db, err := DB.Open(dbPath)
+	if err != nil {
+		panic(err)
+	}
+	defer db.Close()
+	
+	// 禁用自动提交
+	db.SetAutoCommitDelay(0)
+	
+	// 创建表
+	tbl, err := DB.UseTable[Record](db, "users")
+	if err != nil {
+		panic(err)
+	}
+	
+	// ========== 测试1: 事务内插入(使用行级 WAL)==========
+	fmt.Println("========== 测试1: 事务内插入 ==========")
+	
+	txn := db.BeginTransaction()
+	
+	for i := 1; i <= 3; i++ {
+		err := tbl.Insert(&Record{
+			Name: fmt.Sprintf("User%03d", i),
+			Age:  int64(20 + i),
+		})
+		if err != nil {
+			panic(err)
+		}
+		fmt.Printf("  插入 User%03d\n", i)
+	}
+	
+	// 提交事务
+	if err := txn.Commit(); err != nil {
+		panic(err)
+	}
+	fmt.Println("✅ 事务提交成功")
+	
+	// 验证数据
+	count, _ := tbl.Count()
+	fmt.Printf("  当前行数: %d\n", count)
+	
+	// ========== 测试2: 事务回滚 ==========
+	fmt.Println("\n========== 测试2: 事务回滚 ==========")
+	
+	txn2 := db.BeginTransaction()
+	
+	for i := 4; i <= 6; i++ {
+		err := tbl.Insert(&Record{
+			Name: fmt.Sprintf("User%03d", i),
+			Age:  int64(20 + i),
+		})
+		if err != nil {
+			panic(err)
+		}
+		fmt.Printf("  插入 User%03d\n", i)
+	}
+	
+	countBeforeRollback, _ := tbl.Count()
+	fmt.Printf("  回滚前行数: %d\n", countBeforeRollback)
+	
+	// 回滚
+	if err := txn2.Rollback(); err != nil {
+		panic(err)
+	}
+	fmt.Println("✅ 事务回滚成功")
+	
+	countAfterRollback, _ := tbl.Count()
+	fmt.Printf("  回滚后行数: %d\n", countAfterRollback)
+	
+	// ========== 测试3: 重开验证 ==========
+	fmt.Println("\n========== 测试3: 重开验证 ==========")
+	
+	db.Close()
+	
+	db2, err := DB.Open(dbPath)
+	if err != nil {
+		panic(err)
+	}
+	defer db2.Close()
+	
+	tbl2, _ := DB.UseTable[Record](db2, "users")
+	countAfterReopen, _ := tbl2.Count()
+	fmt.Printf("  重开后行数: %d(应为 3)\n", countAfterReopen)
+	
+	// 验证具体数据
+	for i := 1; i <= 3; i++ {
+		row, err := tbl2.Find(uint64(i))
+		if err == nil {
+			fmt.Printf("  Row#%d: Name=%s, Age=%d ✅\n", i, row.Name, row.Age)
+		} else {
+			fmt.Printf("  Row#%d: 不存在 ❌\n", i)
+		}
+	}
+	
+	// 验证回滚的数据不存在
+	for i := 4; i <= 6; i++ {
+		_, err := tbl2.Find(uint64(i))
+		if err != nil {
+			fmt.Printf("  Row#%d: 不存在(已回滚)✅\n", i)
+		} else {
+			fmt.Printf("  Row#%d: 存在(回滚失败!)❌\n", i)
+		}
+	}
+	
+	fmt.Println("\n==============================================")
+	fmt.Println("  测试完成!")
+	fmt.Println("==============================================")
+}

二進制
wal_rowlevel_test.db