Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Vanessa 2024-06-22 16:54:22 +08:00
commit 6533b4f146
9 changed files with 47 additions and 57 deletions

File diff suppressed because one or more lines are too long

View file

@ -36,6 +36,25 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
func LoadTrees(ids []string) (ret map[string]*parse.Tree) {
ret, tmpCache := map[string]*parse.Tree{}, map[string]*parse.Tree{}
bts := treenode.GetBlockTrees(ids)
luteEngine := util.NewLute()
for id, bt := range bts {
tree := tmpCache[bt.RootID]
if nil == tree {
tree, _ = LoadTree(bt.BoxID, bt.Path, luteEngine)
if nil == tree {
logging.LogWarnf("load tree [%s] failed: %s", id, bt.Path)
continue
}
tmpCache[bt.RootID] = tree
}
ret[id] = tree
}
return
}
func LoadTree(boxID, p string, luteEngine *lute.Lute) (ret *parse.Tree, err error) {
filePath := filepath.Join(util.DataDir, boxID, p)
data, err := filelock.ReadFile(filePath)

View file

@ -8,7 +8,7 @@ require (
github.com/88250/epub v0.0.0-20230830085737-c19055cd1f48
github.com/88250/go-humanize v0.0.0-20240424102817-4f78fac47ea7
github.com/88250/gulu v1.2.3-0.20240612035750-c9cf5f7a4d02
github.com/88250/lute v1.7.7-0.20240620040258-d71a0f3c0c8b
github.com/88250/lute v1.7.7-0.20240622011733-13f09e554802
github.com/88250/pdfcpu v0.3.14-0.20230401044135-c7369a99720c
github.com/88250/vitess-sqlparser v0.0.0-20210205111146-56a2ded2aba1
github.com/ClarkThan/ahocorasick v0.0.0-20231011042242-30d1ef1347f4

View file

@ -12,8 +12,8 @@ github.com/88250/go-sqlite3 v1.14.13-0.20231214121541-e7f54c482950 h1:Pa5hMiBceT
github.com/88250/go-sqlite3 v1.14.13-0.20231214121541-e7f54c482950/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/88250/gulu v1.2.3-0.20240612035750-c9cf5f7a4d02 h1:3e5+yobj655pTeKOYMbJrnc1mE51ZkbXIxquTYZuYCY=
github.com/88250/gulu v1.2.3-0.20240612035750-c9cf5f7a4d02/go.mod h1:MUfzyfmbPrRDZLqxc7aPrVYveatTHRfoUa5TynPS0i8=
github.com/88250/lute v1.7.7-0.20240620040258-d71a0f3c0c8b h1:xNFiG33DopwSzL9WYoCvM6ZA5qkfSHpIRhneDkhGJio=
github.com/88250/lute v1.7.7-0.20240620040258-d71a0f3c0c8b/go.mod h1:VDAzL8b+oCh+e3NAlmwwLzC53ten0rZlS8NboB7ljtk=
github.com/88250/lute v1.7.7-0.20240622011733-13f09e554802 h1:rNntwekmFfGagGzHpGiLWkB0ebXGbUYXC1ulAXSoTA4=
github.com/88250/lute v1.7.7-0.20240622011733-13f09e554802/go.mod h1:VDAzL8b+oCh+e3NAlmwwLzC53ten0rZlS8NboB7ljtk=
github.com/88250/pdfcpu v0.3.14-0.20230401044135-c7369a99720c h1:Dl/8S9iLyPMTElnWIBxmjaLiWrkI5P4a21ivwAn5pU0=
github.com/88250/pdfcpu v0.3.14-0.20230401044135-c7369a99720c/go.mod h1:S5YT38L/GCjVjmB4PB84PymA1qfopjEhfhTNQilLpv4=
github.com/88250/vitess-sqlparser v0.0.0-20210205111146-56a2ded2aba1 h1:48T899JQDwyyRu9yXHePYlPdHtpJfrJEUGBMH3SMBWY=

View file

@ -404,20 +404,9 @@ func SearchAttributeView(keyword string, excludeAvIDs []string) (ret []*SearchAt
}
blockIDs = gulu.Str.RemoveDuplicatedElem(blockIDs)
trees := map[string]*parse.Tree{}
trees := filesys.LoadTrees(blockIDs)
for _, blockID := range blockIDs {
bt := treenode.GetBlockTree(blockID)
if nil == bt {
continue
}
tree := trees[bt.RootID]
if nil == tree {
tree, _ = LoadTreeByBlockID(blockID)
if nil != tree {
trees[bt.RootID] = tree
}
}
tree := trees[blockID]
if nil == tree {
continue
}
@ -1428,10 +1417,10 @@ func (tx *Transaction) doRemoveAttrViewView(operation *Operation) (ret *TxErr) {
}
func getMirrorBlocksNodes(avID string) (trees []*parse.Tree, nodes []*ast.Node) {
mirrorBlocks := treenode.GetMirrorAttrViewBlockIDs(avID)
mirrorBlockIDs := treenode.GetMirrorAttrViewBlockIDs(avID)
mirrorBlockTree := map[string]*parse.Tree{}
treeCache := map[string]*parse.Tree{}
for _, mirrorBlock := range mirrorBlocks {
for _, mirrorBlock := range mirrorBlockIDs {
bt := treenode.GetBlockTree(mirrorBlock)
if nil == bt {
logging.LogErrorf("get block tree by block ID [%s] failed", mirrorBlock)
@ -1450,11 +1439,11 @@ func getMirrorBlocksNodes(avID string) (trees []*parse.Tree, nodes []*ast.Node)
}
}
for _, mirrorBlock := range mirrorBlocks {
tree := mirrorBlockTree[mirrorBlock]
node := treenode.GetNodeInTree(tree, mirrorBlock)
for _, mirrorBlockID := range mirrorBlockIDs {
tree := mirrorBlockTree[mirrorBlockID]
node := treenode.GetNodeInTree(tree, mirrorBlockID)
if nil == node {
logging.LogErrorf("get node in tree by block ID [%s] failed", mirrorBlock)
logging.LogErrorf("get node in tree by block ID [%s] failed", mirrorBlockID)
continue
}
nodes = append(nodes, node)

View file

@ -292,20 +292,7 @@ func BatchGetBlockAttrs(ids []string) (ret map[string]map[string]string) {
WaitForWritingFiles()
ret = map[string]map[string]string{}
trees := map[string]*parse.Tree{}
bts := treenode.GetBlockTrees(ids)
luteEngine := util.NewLute()
for id, bt := range bts {
if nil == trees[id] {
tree, err := filesys.LoadTree(bt.BoxID, bt.Path, luteEngine)
if nil != err {
logging.LogErrorf("load tree [%s] failed: %s", bt.Path, err)
continue
}
trees[id] = tree
}
}
trees := filesys.LoadTrees(ids)
for _, id := range ids {
tree := trees[id]
if nil == tree {

View file

@ -557,11 +557,16 @@ func (box *Box) UpdateHistoryGenerated() {
func getBoxesByPaths(paths []string) (ret map[string]*Box) {
ret = map[string]*Box{}
var ids []string
for _, p := range paths {
id := strings.TrimSuffix(path.Base(p), ".sy")
bt := treenode.GetBlockTree(id)
ids = append(ids, strings.TrimSuffix(path.Base(p), ".sy"))
}
bts := treenode.GetBlockTrees(ids)
for _, id := range ids {
bt := bts[id]
if nil != bt {
ret[p] = Conf.Box(bt.BoxID)
ret[bt.Path] = Conf.Box(bt.BoxID)
}
}
return

View file

@ -465,21 +465,11 @@ func contentStat(content string, luteEngine *lute.Lute) (ret *util.BlockStatResu
func BlocksWordCount(ids []string) (ret *util.BlockStatResult) {
ret = &util.BlockStatResult{}
trees := map[string]*parse.Tree{} // 缓存
luteEngine := util.NewLute()
trees := filesys.LoadTrees(ids)
for _, id := range ids {
bt := treenode.GetBlockTree(id)
if nil == bt {
continue
}
tree := trees[bt.RootID]
tree := trees[id]
if nil == tree {
tree, _ = filesys.LoadTree(bt.BoxID, bt.Path, luteEngine)
if nil == tree {
continue
}
trees[bt.RootID] = tree
continue
}
node := treenode.GetNodeInTree(tree, id)

View file

@ -204,7 +204,7 @@ func CountBlocks() (ret int) {
func GetBlockTreeRootByPath(boxID, path string) (ret *BlockTree) {
ret = &BlockTree{}
sqlStmt := "SELECT * FROM blocktrees WHERE box_id = ? AND path = ?"
sqlStmt := "SELECT * FROM blocktrees WHERE box_id = ? AND path = ? AND type = 'd'"
err := db.QueryRow(sqlStmt, boxID, path).Scan(&ret.ID, &ret.RootID, &ret.ParentID, &ret.BoxID, &ret.Path, &ret.HPath, &ret.Updated, &ret.Type)
if nil != err {
ret = nil
@ -220,7 +220,7 @@ func GetBlockTreeRootByPath(boxID, path string) (ret *BlockTree) {
func GetBlockTreeRootByHPath(boxID, hPath string) (ret *BlockTree) {
ret = &BlockTree{}
hPath = gulu.Str.RemoveInvisible(hPath)
sqlStmt := "SELECT * FROM blocktrees WHERE box_id = ? AND hpath = ?"
sqlStmt := "SELECT * FROM blocktrees WHERE box_id = ? AND hpath = ? AND type = 'd'"
err := db.QueryRow(sqlStmt, boxID, hPath).Scan(&ret.ID, &ret.RootID, &ret.ParentID, &ret.BoxID, &ret.Path, &ret.HPath, &ret.Updated, &ret.Type)
if nil != err {
ret = nil
@ -235,7 +235,7 @@ func GetBlockTreeRootByHPath(boxID, hPath string) (ret *BlockTree) {
func GetBlockTreeRootsByHPath(boxID, hPath string) (ret []*BlockTree) {
hPath = gulu.Str.RemoveInvisible(hPath)
sqlStmt := "SELECT * FROM blocktrees WHERE box_id = ? AND hpath = ?"
sqlStmt := "SELECT * FROM blocktrees WHERE box_id = ? AND hpath = ? AND type = 'd'"
rows, err := db.Query(sqlStmt, boxID, hPath)
if nil != err {
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
@ -256,7 +256,7 @@ func GetBlockTreeRootsByHPath(boxID, hPath string) (ret []*BlockTree) {
func GetBlockTreeRootByHPathPreferredParentID(boxID, hPath, preferredParentID string) (ret *BlockTree) {
hPath = gulu.Str.RemoveInvisible(hPath)
var roots []*BlockTree
sqlStmt := "SELECT * FROM blocktrees WHERE box_id = ? AND hpath = ? AND parent_id = ?"
sqlStmt := "SELECT * FROM blocktrees WHERE box_id = ? AND hpath = ? AND parent_id = ? AND type = 'd'"
rows, err := db.Query(sqlStmt, boxID, hPath, preferredParentID)
if nil != err {
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)