优化清理未引用资源内存占用 https://github.com/siyuan-note/siyuan/issues/5200

This commit is contained in:
Liang Ding 2022-06-16 15:08:11 +08:00
parent f8b1bedfe5
commit 8589a53ee4
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
2 changed files with 43 additions and 23 deletions

View file

@ -22,6 +22,7 @@ import (
"path/filepath"
"strings"
"github.com/88250/lute"
"github.com/88250/lute/parse"
"github.com/88250/protyle"
"github.com/siyuan-note/filelock"
@ -30,8 +31,9 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
func loadTrees(localPath string) (ret []*parse.Tree) {
luteEngine := NewLute()
func pagedPaths(localPath string, pageSize int) (ret map[int][]string) {
ret = map[int][]string{}
page := 1
filepath.Walk(localPath, func(path string, info fs.FileInfo, err error) error {
if info.IsDir() && strings.HasPrefix(info.Name(), ".") {
return filepath.SkipDir
@ -41,23 +43,30 @@ func loadTrees(localPath string) (ret []*parse.Tree) {
return nil
}
data, err := filelock.NoLockFileRead(path)
if nil != err {
util.LogErrorf("get data [path=%s] failed: %s", path, err)
return nil
ret[page] = append(ret[page], path)
if pageSize <= len(ret[page]) {
page++
}
tree, err := protyle.ParseJSONWithoutFix(luteEngine, data)
if nil != err {
util.LogErrorf("parse json to tree [%s] failed: %s", path, err)
return nil
}
ret = append(ret, tree)
return nil
})
return
}
func loadTree(localPath string, luteEngine *lute.Lute) (ret *parse.Tree, err error) {
data, err := filelock.NoLockFileRead(localPath)
if nil != err {
util.LogErrorf("get data [path=%s] failed: %s", localPath, err)
return
}
ret, err = protyle.ParseJSONWithoutFix(luteEngine, data)
if nil != err {
util.LogErrorf("parse json to tree [%s] failed: %s", localPath, err)
return
}
return
}
var ErrBoxNotFound = errors.New("notebook not found")
var ErrBlockNotFound = errors.New("block not found")
var ErrTreeNotFound = errors.New("tree not found")