mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-18 15:40:12 +01:00
⚡ 改进创建文档性能 Fix https://github.com/siyuan-note/siyuan/issues/7175
This commit is contained in:
parent
258acc1142
commit
a3940c220f
5 changed files with 25 additions and 30 deletions
|
|
@ -377,7 +377,7 @@ func createDoc(c *gin.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err := model.CreateDocByMd(notebook, p, title, md, sorts)
|
tree, err := model.CreateDocByMd(notebook, p, title, md, sorts)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
ret.Code = -1
|
ret.Code = -1
|
||||||
ret.Msg = err.Error()
|
ret.Msg = err.Error()
|
||||||
|
|
@ -386,13 +386,6 @@ func createDoc(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
box := model.Conf.Box(notebook)
|
box := model.Conf.Box(notebook)
|
||||||
tree, err := model.LoadTree(box.ID, p)
|
|
||||||
if nil != err {
|
|
||||||
ret.Code = -1
|
|
||||||
ret.Msg = err.Error()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
pushCreate(box, p, tree.Root.ID, arg)
|
pushCreate(box, p, tree.Root.ID, arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ func StartCron() {
|
||||||
go every(2*time.Hour, model.StatJob)
|
go every(2*time.Hour, model.StatJob)
|
||||||
go every(2*time.Hour, model.RefreshCheckJob)
|
go every(2*time.Hour, model.RefreshCheckJob)
|
||||||
go every(3*time.Second, model.FlushUpdateRefTextRenameDocJob)
|
go every(3*time.Second, model.FlushUpdateRefTextRenameDocJob)
|
||||||
go every(2*time.Second, model.FlushTxJob)
|
go every(50*time.Millisecond, model.FlushTxJob)
|
||||||
go every(util.SQLFlushInterval, sql.FlushTxJob)
|
go every(util.SQLFlushInterval, sql.FlushTxJob)
|
||||||
go every(10*time.Minute, model.FixIndexJob)
|
go every(10*time.Minute, model.FixIndexJob)
|
||||||
go every(10*time.Minute, model.IndexEmbedBlockJob)
|
go every(10*time.Minute, model.IndexEmbedBlockJob)
|
||||||
|
|
|
||||||
|
|
@ -916,17 +916,16 @@ func createTreeTx(tree *parse.Tree) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateDocByMd(boxID, p, title, md string, sorts []string) (err error) {
|
func CreateDocByMd(boxID, p, title, md string, sorts []string) (tree *parse.Tree, err error) {
|
||||||
WaitForWritingFiles()
|
|
||||||
|
|
||||||
box := Conf.Box(boxID)
|
box := Conf.Box(boxID)
|
||||||
if nil == box {
|
if nil == box {
|
||||||
return errors.New(Conf.Language(0))
|
err = errors.New(Conf.Language(0))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
luteEngine := NewLute()
|
luteEngine := NewLute()
|
||||||
dom := luteEngine.Md2BlockDOM(md, false)
|
dom := luteEngine.Md2BlockDOM(md, false)
|
||||||
err = createDoc(box.ID, p, title, dom)
|
tree, err = createDoc(box.ID, p, title, dom)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -1389,26 +1388,30 @@ func CreateDailyNote(boxID string) (p string, existed bool, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func createDoc(boxID, p, title, dom string) (err error) {
|
func createDoc(boxID, p, title, dom string) (tree *parse.Tree, err error) {
|
||||||
title = gulu.Str.RemoveInvisible(title)
|
title = gulu.Str.RemoveInvisible(title)
|
||||||
if 512 < utf8.RuneCountInString(title) {
|
if 512 < utf8.RuneCountInString(title) {
|
||||||
// 限制笔记本名和文档名最大长度为 `512` https://github.com/siyuan-note/siyuan/issues/6299
|
// 限制笔记本名和文档名最大长度为 `512` https://github.com/siyuan-note/siyuan/issues/6299
|
||||||
return errors.New(Conf.Language(106))
|
err = errors.New(Conf.Language(106))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
title = strings.ReplaceAll(title, "/", "")
|
title = strings.ReplaceAll(title, "/", "")
|
||||||
|
|
||||||
baseName := strings.TrimSpace(path.Base(p))
|
baseName := strings.TrimSpace(path.Base(p))
|
||||||
if "" == strings.TrimSuffix(baseName, ".sy") {
|
if "" == strings.TrimSuffix(baseName, ".sy") {
|
||||||
return errors.New(Conf.Language(16))
|
err = errors.New(Conf.Language(16))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(baseName, ".") {
|
if strings.HasPrefix(baseName, ".") {
|
||||||
return errors.New(Conf.Language(13))
|
err = errors.New(Conf.Language(13))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
box := Conf.Box(boxID)
|
box := Conf.Box(boxID)
|
||||||
if nil == box {
|
if nil == box {
|
||||||
return errors.New(Conf.Language(0))
|
err = errors.New(Conf.Language(0))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
id := strings.TrimSuffix(path.Base(p), ".sy")
|
id := strings.TrimSuffix(path.Base(p), ".sy")
|
||||||
|
|
@ -1416,10 +1419,11 @@ func createDoc(boxID, p, title, dom string) (err error) {
|
||||||
folder := path.Dir(p)
|
folder := path.Dir(p)
|
||||||
if "/" != folder {
|
if "/" != folder {
|
||||||
parentID := path.Base(folder)
|
parentID := path.Base(folder)
|
||||||
parentTree, err := loadTreeByBlockID(parentID)
|
parentTree, loadErr := loadTreeByBlockID(parentID)
|
||||||
if nil != err {
|
if nil != loadErr {
|
||||||
logging.LogErrorf("get parent tree [id=%s] failed", parentID)
|
logging.LogErrorf("get parent tree [id=%s] failed", parentID)
|
||||||
return ErrBlockNotFound
|
err = ErrBlockNotFound
|
||||||
|
return
|
||||||
}
|
}
|
||||||
hPath = path.Join(parentTree.HPath, title)
|
hPath = path.Join(parentTree.HPath, title)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -1433,15 +1437,15 @@ func createDoc(boxID, p, title, dom string) (err error) {
|
||||||
|
|
||||||
if !box.Exist(folder) {
|
if !box.Exist(folder) {
|
||||||
if err = box.MkdirAll(folder); nil != err {
|
if err = box.MkdirAll(folder); nil != err {
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if box.Exist(p) {
|
if box.Exist(p) {
|
||||||
return errors.New(Conf.Language(1))
|
err = errors.New(Conf.Language(1))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var tree *parse.Tree
|
|
||||||
luteEngine := NewLute()
|
luteEngine := NewLute()
|
||||||
tree = luteEngine.BlockDOM2Tree(dom)
|
tree = luteEngine.BlockDOM2Tree(dom)
|
||||||
tree.Box = boxID
|
tree.Box = boxID
|
||||||
|
|
|
||||||
|
|
@ -50,11 +50,11 @@ func createDocsByHPath(boxID, hPath, content string) (id string, existed bool, e
|
||||||
pathBuilder.WriteString(id)
|
pathBuilder.WriteString(id)
|
||||||
docP := pathBuilder.String() + ".sy"
|
docP := pathBuilder.String() + ".sy"
|
||||||
if isNotLast {
|
if isNotLast {
|
||||||
if err = createDoc(boxID, docP, part, ""); nil != err {
|
if _, err = createDoc(boxID, docP, part, ""); nil != err {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err = createDoc(boxID, docP, part, content); nil != err {
|
if _, err = createDoc(boxID, docP, part, content); nil != err {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -61,8 +61,6 @@ func IsUnfoldHeading(transactions *[]*Transaction) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
const txFixDelay = 10
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
txQueue []*Transaction
|
txQueue []*Transaction
|
||||||
txQueueLock = sync.Mutex{}
|
txQueueLock = sync.Mutex{}
|
||||||
|
|
@ -87,7 +85,7 @@ func WaitForWritingFiles() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func isWritingFiles() bool {
|
func isWritingFiles() bool {
|
||||||
time.Sleep(time.Duration(txFixDelay+10) * time.Millisecond)
|
time.Sleep(time.Duration(20) * time.Millisecond)
|
||||||
if 0 < len(txQueue) || util.IsMutexLocked(&txQueueLock) {
|
if 0 < len(txQueue) || util.IsMutexLocked(&txQueueLock) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue