mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-01-01 06:18:49 +01:00
🧑💻 siyuan-note#12718 (#12723)
This commit is contained in:
parent
f965d6b91b
commit
7afea33d36
4 changed files with 157 additions and 15 deletions
|
|
@ -21,10 +21,6 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/88250/lute"
|
||||
"github.com/88250/lute/parse"
|
||||
"github.com/88250/lute/render"
|
||||
|
|
@ -34,23 +30,39 @@ import (
|
|||
"github.com/siyuan-note/siyuan/kernel/cache"
|
||||
"github.com/siyuan-note/siyuan/kernel/treenode"
|
||||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func LoadTrees(ids []string) (ret map[string]*parse.Tree) {
|
||||
ret, tmpCache := map[string]*parse.Tree{}, map[string]*parse.Tree{}
|
||||
ret = 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
|
||||
var boxIDs []string
|
||||
var paths []string
|
||||
seen := make(map[string]bool)
|
||||
for _, bt := range bts {
|
||||
key := bt.BoxID + bt.Path
|
||||
if !seen[key] {
|
||||
seen[key] = true
|
||||
boxIDs = append(boxIDs, bt.BoxID)
|
||||
paths = append(paths, bt.Path)
|
||||
}
|
||||
ret[id] = tree
|
||||
}
|
||||
|
||||
trees, errs := BatchLoadTrees(boxIDs, paths, luteEngine)
|
||||
|
||||
for i := range trees {
|
||||
tree := trees[i]
|
||||
err := errs[i]
|
||||
if err != nil || tree == nil {
|
||||
logging.LogErrorf("load tree failed: %s", err)
|
||||
continue
|
||||
}
|
||||
|
||||
ret[tree.ID] = tree
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
@ -67,6 +79,31 @@ func LoadTree(boxID, p string, luteEngine *lute.Lute) (ret *parse.Tree, err erro
|
|||
return
|
||||
}
|
||||
|
||||
func BatchLoadTrees(boxIDs, paths []string, luteEngine *lute.Lute) ([]*parse.Tree, []error) {
|
||||
var wg sync.WaitGroup
|
||||
results := make([]*parse.Tree, len(paths))
|
||||
errors := make([]error, len(paths))
|
||||
|
||||
for i := range paths {
|
||||
wg.Add(1)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
|
||||
boxID := boxIDs[i]
|
||||
path := paths[i]
|
||||
|
||||
tree, err := LoadTree(boxID, path, luteEngine)
|
||||
|
||||
results[i] = tree
|
||||
errors[i] = err
|
||||
}(i)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
return results, errors
|
||||
}
|
||||
|
||||
func LoadTreeByData(data []byte, boxID, p string, luteEngine *lute.Lute) (ret *parse.Tree, err error) {
|
||||
ret = parseJSON2Tree(boxID, p, data, luteEngine)
|
||||
if nil == ret {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue