🎨 List file/asset history following the limit of editor history retention days https://github.com/siyuan-note/siyuan/issues/9723

This commit is contained in:
Daniel 2023-11-22 22:11:56 +08:00
parent e0d0c5c4d5
commit f5205d846c
No known key found for this signature in database
GPG Key ID: 86211BA83DF03017
3 changed files with 15 additions and 14 deletions

View File

@ -500,6 +500,7 @@ func clearOutdatedHistoryDir(historyDir string) {
} }
now := time.Now() now := time.Now()
ago := now.Add(-24 * time.Hour * time.Duration(Conf.Editor.HistoryRetentionDays)).Unix()
var removes []string var removes []string
for _, dir := range dirs { for _, dir := range dirs {
dirInfo, err := dir.Info() dirInfo, err := dir.Info()
@ -507,7 +508,7 @@ func clearOutdatedHistoryDir(historyDir string) {
logging.LogErrorf("read history dir [%s] failed: %s", dir.Name(), err) logging.LogErrorf("read history dir [%s] failed: %s", dir.Name(), err)
continue continue
} }
if Conf.Editor.HistoryRetentionDays < int(now.Sub(dirInfo.ModTime()).Hours()/24) { if dirInfo.ModTime().Unix() < ago {
removes = append(removes, filepath.Join(historyDir, dir.Name())) removes = append(removes, filepath.Join(historyDir, dir.Name()))
} }
} }
@ -517,10 +518,10 @@ func clearOutdatedHistoryDir(historyDir string) {
continue continue
} }
//logging.LogInfof("auto removed history dir [%s]", dir) //logging.LogInfof("auto removed history dir [%s]", dir)
// 清理历史库
sql.DeleteHistoriesByPathPrefixQueue(dir)
} }
// 清理历史库
sql.DeleteOutdatedHistories(fmt.Sprintf("%d", ago))
} }
var boxLatestHistoryTime = map[string]time.Time{} var boxLatestHistoryTime = map[string]time.Time{}

View File

@ -104,9 +104,9 @@ func queryHistory(query string, args ...interface{}) (*sql.Rows, error) {
return historyDB.Query(query, args...) return historyDB.Query(query, args...)
} }
func deleteHistoriesByPathPrefix(tx *sql.Tx, pathPrefix string, context map[string]interface{}) (err error) { func deleteOutdatedHistories(tx *sql.Tx, before string, context map[string]interface{}) (err error) {
stmt := "DELETE FROM histories_fts_case_insensitive WHERE path LIKE ?" stmt := "DELETE FROM histories_fts_case_insensitive WHERE created < ?"
if err = execStmtTx(tx, stmt, pathPrefix+"%"); nil != err { if err = execStmtTx(tx, stmt, before); nil != err {
return return
} }
return return

View File

@ -39,10 +39,10 @@ var (
type historyDBQueueOperation struct { type historyDBQueueOperation struct {
inQueueTime time.Time inQueueTime time.Time
action string // index/deletePathPrefix action string // index/deleteOutdated
histories []*History // index histories []*History // index
pathPrefix string // deletePathPrefix before string // deleteOutdated
} }
func FlushHistoryTxJob() { func FlushHistoryTxJob() {
@ -111,8 +111,8 @@ func execHistoryOp(op *historyDBQueueOperation, tx *sql.Tx, context map[string]i
switch op.action { switch op.action {
case "index": case "index":
err = insertHistories(tx, op.histories, context) err = insertHistories(tx, op.histories, context)
case "deletePathPrefix": case "deleteOutdated":
err = deleteHistoriesByPathPrefix(tx, op.pathPrefix, context) err = deleteOutdatedHistories(tx, op.before, context)
default: default:
msg := fmt.Sprintf("unknown history operation [%s]", op.action) msg := fmt.Sprintf("unknown history operation [%s]", op.action)
logging.LogErrorf(msg) logging.LogErrorf(msg)
@ -121,11 +121,11 @@ func execHistoryOp(op *historyDBQueueOperation, tx *sql.Tx, context map[string]i
return return
} }
func DeleteHistoriesByPathPrefixQueue(pathPrefix string) { func DeleteOutdatedHistories(before string) {
historyDBQueueLock.Lock() historyDBQueueLock.Lock()
defer historyDBQueueLock.Unlock() defer historyDBQueueLock.Unlock()
newOp := &historyDBQueueOperation{inQueueTime: time.Now(), action: "deletePathPrefix", pathPrefix: pathPrefix} newOp := &historyDBQueueOperation{inQueueTime: time.Now(), action: "deleteOutdated", before: before}
historyOperationQueue = append(historyOperationQueue, newOp) historyOperationQueue = append(historyOperationQueue, newOp)
} }