添加 Clean 和 Destroy 功能
主要改动: - Engine: 添加 Clean() 和 Destroy() 方法 - Table: 添加 Clean() 和 Destroy() 方法(不持有 Database 引用) - Database: 添加 Clean()、CleanTable()、DestroyTable()、Destroy() 方法 - 自动 flush: 添加长时间无写入自动 flush 策略(默认 30 秒) - WebUI 优化: 优化分页查询性能 新增功能: - Clean(): 清除数据但保留结构,Engine/Table/Database 仍可用 - Destroy(): 销毁并删除所有文件,对象不可用 - CleanTable(name): 清除指定表的数据 - DestroyTable(name): 销毁指定表并从 Database 中删除 - 自动 flush 监控: 后台定期检查,空闲时自动持久化 代码优化: - Engine.Close(): 支持 Destroy 后调用,不会 panic - 二级索引持久化: 在 flush 时自动持久化索引 - WebUI 分页: 预构建字段类型 map,减少 Schema 查询 - 职责分离: Table 不再持有 Database 引用 测试覆盖: - engine_clean_test.go: Engine Clean/Destroy 测试 - table_clean_test.go: Table Clean/Destroy 测试 - database_clean_test.go: Database Clean/Destroy 测试 - database_table_ops_test.go: Database CleanTable/DestroyTable 测试
This commit is contained in:
37
table.go
37
table.go
@@ -8,12 +8,11 @@ import (
|
||||
|
||||
// Table 表
|
||||
type Table struct {
|
||||
name string // 表名
|
||||
dir string // 表目录
|
||||
schema *Schema // Schema
|
||||
engine *Engine // Engine 实例
|
||||
database *Database // 所属数据库
|
||||
createdAt int64 // 创建时间
|
||||
name string // 表名
|
||||
dir string // 表目录
|
||||
schema *Schema // Schema
|
||||
engine *Engine // Engine 实例
|
||||
createdAt int64 // 创建时间
|
||||
}
|
||||
|
||||
// createTable 创建新表
|
||||
@@ -42,7 +41,6 @@ func createTable(name string, schema *Schema, db *Database) (*Table, error) {
|
||||
dir: tableDir,
|
||||
schema: schema,
|
||||
engine: engine,
|
||||
database: db,
|
||||
createdAt: time.Now().Unix(),
|
||||
}
|
||||
|
||||
@@ -67,11 +65,10 @@ func openTable(name string, db *Database) (*Table, error) {
|
||||
sch := eng.GetSchema()
|
||||
|
||||
table := &Table{
|
||||
name: name,
|
||||
dir: tableDir,
|
||||
schema: sch,
|
||||
engine: eng,
|
||||
database: db,
|
||||
name: name,
|
||||
dir: tableDir,
|
||||
schema: sch,
|
||||
engine: eng,
|
||||
}
|
||||
|
||||
return table, nil
|
||||
@@ -139,3 +136,19 @@ func (t *Table) Close() error {
|
||||
func (t *Table) GetCreatedAt() int64 {
|
||||
return t.createdAt
|
||||
}
|
||||
|
||||
// Clean 清除表的所有数据(保留表结构和 Table 可用)
|
||||
func (t *Table) Clean() error {
|
||||
if t.engine != nil {
|
||||
return t.engine.Clean()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Destroy 销毁表并删除所有数据文件(不从 Database 中删除)
|
||||
func (t *Table) Destroy() error {
|
||||
if t.engine != nil {
|
||||
return t.engine.Destroy()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user