缓存索引数据库加载失败时自动尝试重建数据库文件

This commit is contained in:
GoEdgeLab
2023-04-21 17:38:31 +08:00
parent 3c353708d4
commit 755004e4d3

View File

@@ -226,6 +226,20 @@ func (this *FileListDB) Init() error {
err := this.hashMap.Load(this) err := this.hashMap.Load(this)
if err != nil { if err != nil {
remotelogs.Error("LIST_FILE_DB", "load hash map failed: "+err.Error()+"(file: "+this.dbPath+")") remotelogs.Error("LIST_FILE_DB", "load hash map failed: "+err.Error()+"(file: "+this.dbPath+")")
// 自动修复错误
// TODO 将来希望能尽可能恢复以往数据库中的内容
if strings.Contains(err.Error(), "database is closed") || strings.Contains(err.Error(), "database disk image is malformed") {
_ = this.Close()
this.deleteDB()
remotelogs.Println("LIST_FILE_DB", "recreating the database (file:"+this.dbPath+") ...")
err = this.Open(this.dbPath)
if err != nil {
remotelogs.Error("LIST_FILE_DB", "recreate the database failed: "+err.Error()+" (file:"+this.dbPath+")")
} else {
_ = this.Init()
}
}
} }
}() }()
@@ -683,3 +697,10 @@ func (this *FileListDB) shouldRecover() bool {
_ = result.Close() _ = result.Close()
return shouldRecover return shouldRecover
} }
// 删除数据库文件
func (this *FileListDB) deleteDB() {
_ = os.Remove(this.dbPath)
_ = os.Remove(this.dbPath + "-shm")
_ = os.Remove(this.dbPath + "-wal")
}