diff --git a/internal/caches/list_file_db.go b/internal/caches/list_file_db.go index 6541af4..dd5a04d 100644 --- a/internal/caches/list_file_db.go +++ b/internal/caches/list_file_db.go @@ -13,6 +13,7 @@ import ( "github.com/iwind/TeaGo/logs" "github.com/iwind/TeaGo/types" timeutil "github.com/iwind/TeaGo/utils/time" + "os" "runtime" "strings" "time" @@ -87,13 +88,6 @@ func (this *FileListDB) Open(dbPath string) error { this.writeDB = dbs.NewDB(writeDB) - // 检查 - if this.shouldRecover() { - for _, indexName := range []string{"staleAt", "hash"} { - _, _ = this.writeDB.Exec(`REINDEX "` + indexName + `"`) - } - } - // TODO 耗时过长,暂时不整理数据库 // TODO 需要根据行数来判断是否VACUUM // TODO 注意VACUUM反而可能让数据库文件变大 @@ -102,6 +96,16 @@ func (this *FileListDB) Open(dbPath string) error { return err }**/ + // 检查是否损坏 + // TODO 暂时屏蔽,因为用时过长 + + var recoverEnv, _ = os.LookupEnv("EdgeRecover") + if len(recoverEnv) > 0 && this.shouldRecover() { + for _, indexName := range []string{"staleAt", "hash"} { + _, _ = this.writeDB.Exec(`REINDEX "` + indexName + `"`) + } + } + this.writeBatch = dbs.NewBatch(writeDB, 4) this.writeBatch.OnFail(func(err error) { remotelogs.Warn("LIST_FILE_DB", "run batch failed: "+err.Error()) diff --git a/internal/iplibrary/ip_list_db.go b/internal/iplibrary/ip_list_db.go index 2626138..119f6f1 100644 --- a/internal/iplibrary/ip_list_db.go +++ b/internal/iplibrary/ip_list_db.go @@ -71,6 +71,14 @@ func (this *IPListDB) init() error { this.db = db + // 恢复数据库 + var recoverEnv, _ = os.LookupEnv("EdgeRecover") + if len(recoverEnv) > 0 { + for _, indexName := range []string{"ip_list_itemId", "ip_list_expiredAt"} { + _, _ = db.Exec(`REINDEX "` + indexName + `"`) + } + } + // 初始化数据库 _, err = db.Exec(`CREATE TABLE IF NOT EXISTS "` + this.itemTableName + `" ( "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, diff --git a/internal/metrics/task.go b/internal/metrics/task.go index be3281f..da30916 100644 --- a/internal/metrics/task.go +++ b/internal/metrics/task.go @@ -101,6 +101,14 @@ func (this *Task) Init() error { db.SetMaxOpenConns(1) this.db = dbs.NewDB(db) + // 恢复数据库 + var recoverEnv, _ = os.LookupEnv("EdgeRecover") + if len(recoverEnv) > 0 { + for _, indexName := range []string{"serverId", "hash"} { + _, _ = db.Exec(`REINDEX "` + indexName + `"`) + } + } + if teaconst.EnableDBStat { this.db.EnableStat(true) }