Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Add internal kernel API `/api/filetree/upsertIndexes` and `/api/filetree/removeIndexes` https://github.com/siyuan-note/siyuan/issues/10079

Daniel286654cb0006
diff --git a/kernel/api/filetree.go b/kernel/api/filetree.go
index ef6efc83d1..e32a0ffc4f 100644
--- a/kernel/api/filetree.go
+++ b/kernel/api/filetree.go
@@ -33,6 +33,40 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
+func upsertIndexes(c *gin.Context) {
+ ret := gulu.Ret.NewResult()
+ defer c.JSON(http.StatusOK, ret)
+
+ arg, ok := util.JsonArg(c, ret)
+ if !ok {
+ return
+ }
+
+ pathsArg := arg["paths"].([]interface{})
+ var paths []string
+ for _, p := range pathsArg {
+ paths = append(paths, p.(string))
+ }
+ model.UpsertIndexes(paths)
+}
+
+func removeIndexes(c *gin.Context) {
+ ret := gulu.Ret.NewResult()
+ defer c.JSON(http.StatusOK, ret)
+
+ arg, ok := util.JsonArg(c, ret)
+ if !ok {
+ return
+ }
+
+ pathsArg := arg["paths"].([]interface{})
+ var paths []string
+ for _, p := range pathsArg {
+ paths = append(paths, p.(string))
+ }
+ model.RemoveIndexes(paths)
+}
+
func refreshFiletree(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
diff --git a/kernel/api/router.go b/kernel/api/router.go
index e8ae70c3d5..17ae6cb4e4 100644
--- a/kernel/api/router.go
+++ b/kernel/api/router.go
@@ -113,6 +113,8 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/filetree/heading2Doc", model.CheckAuth, model.CheckReadonly, heading2Doc)
ginServer.Handle("POST", "/api/filetree/li2Doc", model.CheckAuth, model.CheckReadonly, li2Doc)
ginServer.Handle("POST", "/api/filetree/refreshFiletree", model.CheckAuth, model.CheckReadonly, refreshFiletree)
+ ginServer.Handle("POST", "/api/filetree/upsertIndexes", model.CheckAuth, model.CheckReadonly, upsertIndexes)
+ ginServer.Handle("POST", "/api/filetree/removeIndexes", model.CheckAuth, model.CheckReadonly, removeIndexes)
ginServer.Handle("POST", "/api/format/autoSpace", model.CheckAuth, model.CheckReadonly, autoSpace)
ginServer.Handle("POST", "/api/format/netImg2LocalAssets", model.CheckAuth, model.CheckReadonly, netImg2LocalAssets)
diff --git a/kernel/model/index.go b/kernel/model/index.go
index a8bde2c19f..2f5fd16dac 100644
--- a/kernel/model/index.go
+++ b/kernel/model/index.go
@@ -19,6 +19,7 @@ package model
import (
"bytes"
"fmt"
+ "os"
"path/filepath"
"runtime"
"runtime/debug"
@@ -43,6 +44,64 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
+func UpsertIndexes(paths []string) {
+ var syFiles []string
+ for _, p := range paths {
+ if strings.HasSuffix(p, "/") {
+ syFiles = append(syFiles, listSyFiles(p)...)
+ continue
+ }
+
+ if strings.HasSuffix(p, ".sy") {
+ syFiles = append(syFiles, p)
+ }
+ }
+
+ syFiles = gulu.Str.RemoveDuplicatedElem(syFiles)
+ upsertIndexes(syFiles)
+}
+
+func RemoveIndexes(paths []string) {
+ var syFiles []string
+ for _, p := range paths {
+ if strings.HasSuffix(p, "/") {
+ syFiles = append(syFiles, listSyFiles(p)...)
+ continue
+ }
+
+ if strings.HasSuffix(p, ".sy") {
+ syFiles = append(syFiles, p)
+ }
+ }
+
+ syFiles = gulu.Str.RemoveDuplicatedElem(syFiles)
+ removeIndexes(syFiles)
+}
+
+func listSyFiles(dir string) (ret []string) {
+ dirPath := filepath.Join(util.DataDir, dir)
+ err := filepath.WalkDir(dirPath, func(path string, d os.DirEntry, err error) error {
+ if nil != err {
+ logging.LogWarnf("walk dir [%s] failed: %s", dirPath, err)
+ return err
+ }
+
+ if d.IsDir() {
+ return nil
+ }
+
+ if strings.HasSuffix(path, ".sy") {
+ p := filepath.ToSlash(strings.TrimPrefix(path, util.DataDir))
+ ret = append(ret, p)
+ }
+ return nil
+ })
+ if nil != err {
+ logging.LogWarnf("walk dir [%s] failed: %s", dirPath, err)
+ }
+ return
+}
+
func (box *Box) Unindex() {
task.AppendTask(task.DatabaseIndex, unindex, box.ID)
}
@@ -267,6 +326,7 @@ func init() {
}
func subscribeSQLEvents() {
+ // 使用下面的 EvtSQLInsertBlocksFTS 就可以了
//eventbus.Subscribe(eventbus.EvtSQLInsertBlocks, func(context map[string]interface{}, current, total, blockCount int, hash string) {
// if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container {
// // Android/iOS 端不显示数据索引和搜索索引状态提示 https://github.com/siyuan-note/siyuan/issues/6392
diff --git a/kernel/model/sync.go b/kernel/model/sync.go
index b12c2f9cea..a2ec3e46b0 100644
--- a/kernel/model/sync.go
+++ b/kernel/model/sync.go
@@ -277,13 +277,14 @@ func incReindex(upserts, removes []string) (upsertRootIDs, removeRootIDs []strin
removeRootIDs = []string{}
util.IncBootProgress(3, "Sync reindexing...")
- msg := fmt.Sprintf(Conf.Language(35))
- util.PushStatusBar(msg)
+ removeRootIDs = removeIndexes(removes) // 先执行 remove,否则移动文档时 upsert 会被忽略,导致未被索引
+ upsertRootIDs = upsertIndexes(upserts)
+ return
+}
- luteEngine := util.NewLute()
- // 先执行 remove,否则移动文档时 upsert 会被忽略,导致未被索引
- bootProgressPart := int32(10 / float64(len(removes)))
- for _, removeFile := range removes {
+func removeIndexes(removeFilePaths []string) (removeRootIDs []string) {
+ bootProgressPart := int32(10 / float64(len(removeFilePaths)))
+ for _, removeFile := range removeFilePaths {
if !strings.HasSuffix(removeFile, ".sy") {
continue
}
@@ -292,7 +293,7 @@ func incReindex(upserts, removes []string) (upsertRootIDs, removeRootIDs []strin
removeRootIDs = append(removeRootIDs, id)
block := treenode.GetBlockTree(id)
if nil != block {
- msg = fmt.Sprintf(Conf.Language(39), block.RootID)
+ msg := fmt.Sprintf(Conf.Language(39), block.RootID)
util.IncBootProgress(bootProgressPart, msg)
util.PushStatusBar(msg)
@@ -300,12 +301,13 @@ func incReindex(upserts, removes []string) (upsertRootIDs, removeRootIDs []strin
sql.RemoveTreeQueue(block.BoxID, block.RootID)
}
}
+ return
+}
- msg = fmt.Sprintf(Conf.Language(35))
- util.PushStatusBar(msg)
-
- bootProgressPart = int32(10 / float64(len(upserts)))
- for _, upsertFile := range upserts {
+func upsertIndexes(upsertFilePaths []string) (upsertRootIDs []string) {
+ luteEngine := util.NewLute()
+ bootProgressPart := int32(10 / float64(len(upsertFilePaths)))
+ for _, upsertFile := range upsertFilePaths {
if !strings.HasSuffix(upsertFile, ".sy") {
continue
}
@@ -322,7 +324,7 @@ func incReindex(upserts, removes []string) (upsertRootIDs, removeRootIDs []strin
box := upsertFile[:idx]
p := strings.TrimPrefix(upsertFile, box)
- msg = fmt.Sprintf(Conf.Language(40), strings.TrimSuffix(path.Base(p), ".sy"))
+ msg := fmt.Sprintf(Conf.Language(40), strings.TrimSuffix(path.Base(p), ".sy"))
util.IncBootProgress(bootProgressPart, msg)
util.PushStatusBar(msg)
diff --git a/kernel/sql/upsert.go b/kernel/sql/upsert.go
index 4bf04782e5..52811d1074 100644
--- a/kernel/sql/upsert.go
+++ b/kernel/sql/upsert.go
@@ -114,6 +114,7 @@ func insertBlocks0(tx *sql.Tx, bulk []*Block, context map[string]interface{}) (e
}
hashBuf.WriteString("blocks")
evtHash := fmt.Sprintf("%x", sha256.Sum256(hashBuf.Bytes()))[:7]
+ // 使用下面的 EvtSQLInsertBlocksFTS 就可以了
//eventbus.Publish(eventbus.EvtSQLInsertBlocks, context, current, total, len(bulk), evtHash)
stmt = fmt.Sprintf(BlocksFTSInsert, strings.Join(valueStrings, ","))