An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:recycle: 重构 `文件历史` 查询 https://github.com/siyuan-note/siyuan/issues/6504
Liang Ding2fa409c2e9f2
diff --git a/kernel/model/history.go b/kernel/model/history.goindex 653ef894a3..5d5e7dd103 100644--- a/kernel/model/history.go+++ b/kernel/model/history.go@@ -310,7 +310,7 @@ type HistoryItem struct {Path string `json:"path"`}-func FullTextSearchHistory(query, box, op string, typ, page int) (ret []*History, pageCount, totalCount int) {+func FullTextSearchHistory(query, box, op string, typ, page int) (ret []string, pageCount, totalCount int) {query = gulu.Str.RemoveInvisible(query)if "" != query {query = stringQuery(query)@@ -320,7 +320,7 @@ func FullTextSearchHistory(query, box, op string, typ, page int) (ret []*Historyoffset := (page - 1) * pageSizetable := "histories_fts_case_insensitive"- stmt := "SELECT * FROM " + table + " WHERE "+ stmt := "SELECT DISTINCT created FROM " + table + " WHERE "if "" != query {stmt += table + " MATCH '{title content}:(" + query + ")'"} else {@@ -339,11 +339,16 @@ func FullTextSearchHistory(query, box, op string, typ, page int) (ret []*History} else if HistoryTypeAsset == typ {stmt += " AND path LIKE '%/assets/%'"}- countStmt := strings.ReplaceAll(stmt, "SELECT *", "SELECT COUNT(*) AS total")+ countStmt := strings.ReplaceAll(stmt, "SELECT DISTINCT created", "SELECT COUNT(DISTINCT created) AS total")stmt += " ORDER BY created DESC LIMIT " + strconv.Itoa(pageSize) + " OFFSET " + strconv.Itoa(offset)- sqlHistories := sql.SelectHistoriesRawStmt(stmt)- ret = fromSQLHistories(sqlHistories)- result, err := sql.QueryHistory(countStmt)+ result, err := sql.QueryHistory(stmt)+ if nil != err {+ return+ }+ for _, row := range result {+ ret = append(ret, row["created"].(string))+ }+ result, err = sql.QueryHistory(countStmt)if nil != err {return}@@ -651,49 +656,3 @@ func indexHistoryDir(name string, luteEngine *lute.Lute) {}return}--func fromSQLHistories(sqlHistories []*sql.History) (ret []*History) {- if 1 > len(sqlHistories) {- ret = []*History{}- return- }-- var items []*HistoryItem- tmpTime, _ := strconv.ParseInt(sqlHistories[0].Created, 10, 64)- for _, sqlHistory := range sqlHistories {- unixSec, _ := strconv.ParseInt(sqlHistory.Created, 10, 64)- if tmpTime == unixSec {- item := &HistoryItem{- Title: sqlHistory.Title,- Path: filepath.Join(util.HistoryDir, sqlHistory.Path),- }- if HistoryTypeAsset == sqlHistory.Type {- item.Path = filepath.ToSlash(strings.TrimPrefix(item.Path, util.WorkspaceDir))- }- items = append(items, item)- } else {- ret = append(ret, &History{- HCreated: time.Unix(unixSec, 0).Format("2006-01-02 15:04:05"),- Items: items,- })-- item := &HistoryItem{- Title: sqlHistory.Title,- Path: filepath.Join(util.HistoryDir, sqlHistory.Path),- }- if HistoryTypeAsset == sqlHistory.Type {- item.Path = filepath.ToSlash(strings.TrimPrefix(item.Path, util.WorkspaceDir))- }- items = []*HistoryItem{}- items = append(items, item)- }- tmpTime = unixSec- }- if 0 < len(items) {- ret = append(ret, &History{- HCreated: time.Unix(tmpTime, 0).Format("2006-01-02 15:04:05"),- Items: items,- })- }- return-}diff --git a/kernel/sql/history.go b/kernel/sql/history.goindex 593b5f851d..50e1ec6367 100644--- a/kernel/sql/history.go+++ b/kernel/sql/history.go@@ -77,31 +77,6 @@ func queryHistory(query string, args ...interface{}) (*sql.Rows, error) {return historyDB.Query(query, args...)}-func SelectHistoriesRawStmt(stmt string) (ret []*History) {- rows, err := historyDB.Query(stmt)- if nil != err {- logging.LogWarnf("sql query [%s] failed: %s", stmt, err)- return- }- defer rows.Close()- for rows.Next() {- if history := scanHistoryRows(rows); nil != history {- ret = append(ret, history)- }- }- return-}--func scanHistoryRows(rows *sql.Rows) (ret *History) {- var history History- if err := rows.Scan(&history.Type, &history.Op, &history.Title, &history.Content, &history.Path, &history.Created); nil != err {- logging.LogErrorf("query scan field failed: %s\n%s", err, logging.ShortStack())- return- }- ret = &history- return-}-func DeleteHistoriesByPathPrefix(tx *sql.Tx, pathPrefix string) (err error) {stmt := "DELETE FROM histories_fts_case_insensitive WHERE path LIKE ?"if err = execStmtTx(tx, stmt, pathPrefix+"%"); nil != err {