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

This commit is contained in:
Daniel 2024-01-05 20:54:46 +08:00
parent 318e570343
commit 286654cb00
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
5 changed files with 112 additions and 13 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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)

View file

@ -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, ","))