mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-20 19:10:49 +08:00
🎨 数据历史文档和资源文件支持分页和搜索 https://github.com/siyuan-note/siyuan/issues/4901
This commit is contained in:
parent
d31f23e2da
commit
f3b495aff5
@ -91,6 +91,8 @@ func ClearWorkspaceHistory() (err error) {
|
|||||||
logging.LogInfof("removed workspace history dir [%s]", historyDir)
|
logging.LogInfof("removed workspace history dir [%s]", historyDir)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sql.InitHistoryDatabase(true)
|
||||||
|
|
||||||
// 以下部分是老版本的清理逻辑,暂时保留
|
// 以下部分是老版本的清理逻辑,暂时保留
|
||||||
|
|
||||||
notebooks, err := ListNotebooks()
|
notebooks, err := ListNotebooks()
|
||||||
@ -534,6 +536,26 @@ func clearOutdatedHistoryDir(historyDir string) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
//logging.LogInfof("auto removed history dir [%s]", dir)
|
//logging.LogInfof("auto removed history dir [%s]", dir)
|
||||||
|
|
||||||
|
// 清理历史库
|
||||||
|
|
||||||
|
tx, txErr := sql.BeginHistoryTx()
|
||||||
|
if nil != txErr {
|
||||||
|
logging.LogErrorf("begin history tx failed: %s", txErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
p := strings.TrimPrefix(dir, util.HistoryDir)
|
||||||
|
p = filepath.ToSlash(p[1:])
|
||||||
|
if txErr = sql.DeleteHistoriesByPathPrefix(tx, dir); nil != txErr {
|
||||||
|
sql.RollbackTx(tx)
|
||||||
|
logging.LogErrorf("delete history [%s] failed: %s", dir, txErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if txErr = sql.CommitTx(tx); nil != txErr {
|
||||||
|
logging.LogErrorf("commit history tx failed: %s", txErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -614,7 +636,7 @@ func indexHistory() {
|
|||||||
filepath.Walk(entryPath, func(path string, info os.FileInfo, err error) error {
|
filepath.Walk(entryPath, func(path string, info os.FileInfo, err error) error {
|
||||||
if strings.HasSuffix(info.Name(), ".sy") {
|
if strings.HasSuffix(info.Name(), ".sy") {
|
||||||
docs = append(docs, path)
|
docs = append(docs, path)
|
||||||
} else if strings.Contains(path, "assets"+string(os.PathSeparator)) {
|
} else if strings.Contains(path, "assets/") {
|
||||||
assets = append(assets, path)
|
assets = append(assets, path)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -623,29 +645,33 @@ func indexHistory() {
|
|||||||
var histories []*sql.History
|
var histories []*sql.History
|
||||||
for _, doc := range docs {
|
for _, doc := range docs {
|
||||||
tree, loadErr := loadTree(doc, lutEngine)
|
tree, loadErr := loadTree(doc, lutEngine)
|
||||||
if nil != err {
|
if nil != loadErr {
|
||||||
logging.LogErrorf("load tree [%s] failed: %s", doc, loadErr)
|
logging.LogErrorf("load tree [%s] failed: %s", doc, loadErr)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
title := tree.Root.IALAttr("title")
|
title := tree.Root.IALAttr("title")
|
||||||
content := tree.Root.Content()
|
content := tree.Root.Content()
|
||||||
|
p := strings.TrimPrefix(doc, util.HistoryDir)
|
||||||
|
p = filepath.ToSlash(p[1:])
|
||||||
histories = append(histories, &sql.History{
|
histories = append(histories, &sql.History{
|
||||||
Type: 0,
|
Type: 0,
|
||||||
Op: op,
|
Op: op,
|
||||||
Title: title,
|
Title: title,
|
||||||
Content: content,
|
Content: content,
|
||||||
Path: doc,
|
Path: p,
|
||||||
Created: created,
|
Created: created,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, asset := range assets {
|
for _, asset := range assets {
|
||||||
|
p := strings.TrimPrefix(asset, util.HistoryDir)
|
||||||
|
p = filepath.ToSlash(p[1:])
|
||||||
histories = append(histories, &sql.History{
|
histories = append(histories, &sql.History{
|
||||||
Type: 1,
|
Type: 1,
|
||||||
Op: op,
|
Op: op,
|
||||||
Title: filepath.Base(asset),
|
Title: filepath.Base(asset),
|
||||||
Path: asset,
|
Path: p,
|
||||||
Created: created,
|
Created: created,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -660,5 +686,9 @@ func indexHistory() {
|
|||||||
sql.RollbackTx(tx)
|
sql.RollbackTx(tx)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if err = sql.CommitTx(tx); nil != err {
|
||||||
|
logging.LogErrorf("commit transaction failed: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -147,13 +147,27 @@ func initDBTables() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func InitHistoryDatabase(forceRebuild bool) {
|
func InitHistoryDatabase(forceRebuild bool) {
|
||||||
|
initHistoryDBConnection()
|
||||||
|
|
||||||
if !forceRebuild && gulu.File.IsExist(util.HistoryDBPath) {
|
if !forceRebuild && gulu.File.IsExist(util.HistoryDBPath) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
historyDB.Close()
|
||||||
|
if err := os.RemoveAll(util.HistoryDBPath); nil != err {
|
||||||
|
logging.LogErrorf("remove history database file [%s] failed: %s", util.HistoryDBPath, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
initHistoryDBConnection()
|
||||||
|
initHistoryDBTables()
|
||||||
|
}
|
||||||
|
|
||||||
|
func initHistoryDBConnection() {
|
||||||
if nil != historyDB {
|
if nil != historyDB {
|
||||||
historyDB.Close()
|
historyDB.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
dsn := util.HistoryDBPath + "?_journal_mode=OFF" +
|
dsn := util.HistoryDBPath + "?_journal_mode=OFF" +
|
||||||
"&_synchronous=OFF" +
|
"&_synchronous=OFF" +
|
||||||
"&_secure_delete=OFF" +
|
"&_secure_delete=OFF" +
|
||||||
@ -172,9 +186,11 @@ func InitHistoryDatabase(forceRebuild bool) {
|
|||||||
historyDB.SetMaxIdleConns(1)
|
historyDB.SetMaxIdleConns(1)
|
||||||
historyDB.SetMaxOpenConns(1)
|
historyDB.SetMaxOpenConns(1)
|
||||||
historyDB.SetConnMaxLifetime(365 * 24 * time.Hour)
|
historyDB.SetConnMaxLifetime(365 * 24 * time.Hour)
|
||||||
|
}
|
||||||
|
|
||||||
|
func initHistoryDBTables() {
|
||||||
historyDB.Exec("DROP TABLE histories_fts_case_insensitive")
|
historyDB.Exec("DROP TABLE histories_fts_case_insensitive")
|
||||||
_, err = historyDB.Exec("CREATE VIRTUAL TABLE histories_fts_case_insensitive USING fts5(type UNINDEXED, op UNINDEXED, title, content, path UNINDEXED, created UNINDEXED, tokenize=\"siyuan case_insensitive\")")
|
_, err := historyDB.Exec("CREATE VIRTUAL TABLE histories_fts_case_insensitive USING fts5(type UNINDEXED, op UNINDEXED, title, content, path UNINDEXED, created UNINDEXED, tokenize=\"siyuan case_insensitive\")")
|
||||||
if nil != err {
|
if nil != err {
|
||||||
logging.LogFatalf("create table [histories_fts_case_insensitive] failed: %s", err)
|
logging.LogFatalf("create table [histories_fts_case_insensitive] failed: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,14 @@ type History struct {
|
|||||||
Path string
|
Path string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
HistoriesFTSCaseInsensitiveInsert = "INSERT INTO histories_fts_case_insensitive (type, op, title, content, path, created) VALUES %s"
|
HistoriesFTSCaseInsensitiveInsert = "INSERT INTO histories_fts_case_insensitive (type, op, title, content, path, created) VALUES %s"
|
||||||
HistoriesPlaceholder = "(?, ?, ?, ?, ?, ?)"
|
HistoriesPlaceholder = "(?, ?, ?, ?, ?, ?)"
|
||||||
|
Loading…
Reference in New Issue
Block a user