From 299abb7b04627d5cf35b5b5a2b47ca2009313e84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Sun, 14 Aug 2022 15:17:07 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=9C=AC=E5=9C=B0=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E5=8F=91=E7=94=9F=E9=94=99=E8=AF=AF=E6=97=B6?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E6=8F=90=E7=A4=BA=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/caches/list_file.go | 6 +++--- internal/caches/list_file_db.go | 27 +++++++++++++++++++-------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/internal/caches/list_file.go b/internal/caches/list_file.go index 58f480d..a6669ac 100644 --- a/internal/caches/list_file.go +++ b/internal/caches/list_file.go @@ -227,7 +227,7 @@ func (this *FileList) PurgeLFU(count int, callback func(hash string) error) erro if notFound { _, err = db.deleteHitByHashStmt.Exec(hash) if err != nil { - return err + return db.WrapError(err) } } @@ -359,14 +359,14 @@ func (this *FileList) remove(hash string) (notFound bool, err error) { _, err = db.deleteByHashStmt.Exec(hash) if err != nil { - return false, err + return false, db.WrapError(err) } atomic.AddInt64(&this.total, -1) _, err = db.deleteHitByHashStmt.Exec(hash) if err != nil { - return false, err + return false, db.WrapError(err) } if this.onRemove != nil { diff --git a/internal/caches/list_file_db.go b/internal/caches/list_file_db.go index 7c80e7b..abdc625 100644 --- a/internal/caches/list_file_db.go +++ b/internal/caches/list_file_db.go @@ -16,6 +16,8 @@ import ( ) type FileListDB struct { + dbPath string + readDB *dbs.DB writeDB *dbs.DB @@ -49,6 +51,8 @@ func NewFileListDB() *FileListDB { } func (this *FileListDB) Open(dbPath string) error { + this.dbPath = dbPath + // write db writeDB, err := sql.Open("sqlite3", "file:"+dbPath+"?cache=private&mode=rwc&_journal_mode=WAL&_sync=OFF&_cache_size=32000&_secure_delete=FAST") if err != nil { @@ -185,7 +189,7 @@ func (this *FileListDB) Add(hash string, item *Item) error { // 放入队列 _, err := this.insertStmt.Exec(hash, item.Key, item.HeaderSize, item.BodySize, item.MetaSize, item.ExpiredAt, item.StaleAt, item.Host, item.ServerId, utils.UnixTime()) if err != nil { - return err + return this.WrapError(err) } return nil @@ -249,7 +253,7 @@ func (this *FileListDB) ListLFUItems(count int) (hashList []string, err error) { func (this *FileListDB) IncreaseHit(hash string) error { var week = timeutil.Format("YW") _, err := this.increaseHitStmt.Exec(hash, week, week, week, week) - return err + return this.WrapError(err) } func (this *FileListDB) CleanPrefix(prefix string) error { @@ -262,7 +266,7 @@ func (this *FileListDB) CleanPrefix(prefix string) error { for { result, err := this.writeDB.Exec(`UPDATE "`+this.itemsTableName+`" SET expiredAt=0,staleAt=? WHERE id IN (SELECT id FROM "`+this.itemsTableName+`" WHERE expiredAt>0 AND createdAt<=? AND INSTR("key", ?)=1 LIMIT `+types.String(count)+`)`, unixTime+int64(staleLife), unixTime, prefix) if err != nil { - return err + return this.WrapError(err) } affectedRows, err := result.RowsAffected() if err != nil { @@ -281,7 +285,7 @@ func (this *FileListDB) CleanAll() error { _, err := this.deleteAllStmt.Exec() if err != nil { - return err + return this.WrapError(err) } return nil @@ -351,6 +355,13 @@ func (this *FileListDB) Close() error { return errors.New("close database failed: " + strings.Join(errStrings, ", ")) } +func (this *FileListDB) WrapError(err error) error { + if err == nil { + return nil + } + return errors.New(err.Error() + "(file: " + this.dbPath + ")") +} + // 初始化 func (this *FileListDB) initTables(times int) error { { @@ -393,10 +404,10 @@ ON "` + this.itemsTableName + `" ( if dropErr == nil { return this.initTables(times + 1) } - return err + return this.WrapError(err) } - return err + return this.WrapError(err) } } @@ -421,10 +432,10 @@ ON "` + this.hitsTableName + `" ( if dropErr == nil { return this.initTables(times + 1) } - return err + return this.WrapError(err) } - return err + return this.WrapError(err) } }