Pārlūkot izejas kodu

:zap: 改进创建文档性能 Fix https://github.com/siyuan-note/siyuan/issues/7175

Liang Ding 2 gadi atpakaļ
vecāks
revīzija
a3940c220f

+ 1 - 8
kernel/api/filetree.go

@@ -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 {
 		ret.Code = -1
 		ret.Msg = err.Error()
@@ -386,13 +386,6 @@ func createDoc(c *gin.Context) {
 	}
 
 	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)
 }
 

+ 1 - 1
kernel/job/cron.go

@@ -35,7 +35,7 @@ func StartCron() {
 	go every(2*time.Hour, model.StatJob)
 	go every(2*time.Hour, model.RefreshCheckJob)
 	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(10*time.Minute, model.FixIndexJob)
 	go every(10*time.Minute, model.IndexEmbedBlockJob)

+ 20 - 16
kernel/model/file.go

@@ -916,17 +916,16 @@ func createTreeTx(tree *parse.Tree) {
 	}
 }
 
-func CreateDocByMd(boxID, p, title, md string, sorts []string) (err error) {
-	WaitForWritingFiles()
-
+func CreateDocByMd(boxID, p, title, md string, sorts []string) (tree *parse.Tree, err error) {
 	box := Conf.Box(boxID)
 	if nil == box {
-		return errors.New(Conf.Language(0))
+		err = errors.New(Conf.Language(0))
+		return
 	}
 
 	luteEngine := NewLute()
 	dom := luteEngine.Md2BlockDOM(md, false)
-	err = createDoc(box.ID, p, title, dom)
+	tree, err = createDoc(box.ID, p, title, dom)
 	if nil != err {
 		return
 	}
@@ -1389,26 +1388,30 @@ func CreateDailyNote(boxID string) (p string, existed bool, err error) {
 	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)
 	if 512 < utf8.RuneCountInString(title) {
 		// 限制笔记本名和文档名最大长度为 `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, "/", "")
 
 	baseName := strings.TrimSpace(path.Base(p))
 	if "" == strings.TrimSuffix(baseName, ".sy") {
-		return errors.New(Conf.Language(16))
+		err = errors.New(Conf.Language(16))
+		return
 	}
 
 	if strings.HasPrefix(baseName, ".") {
-		return errors.New(Conf.Language(13))
+		err = errors.New(Conf.Language(13))
+		return
 	}
 
 	box := Conf.Box(boxID)
 	if nil == box {
-		return errors.New(Conf.Language(0))
+		err = errors.New(Conf.Language(0))
+		return
 	}
 
 	id := strings.TrimSuffix(path.Base(p), ".sy")
@@ -1416,10 +1419,11 @@ func createDoc(boxID, p, title, dom string) (err error) {
 	folder := path.Dir(p)
 	if "/" != folder {
 		parentID := path.Base(folder)
-		parentTree, err := loadTreeByBlockID(parentID)
-		if nil != err {
+		parentTree, loadErr := loadTreeByBlockID(parentID)
+		if nil != loadErr {
 			logging.LogErrorf("get parent tree [id=%s] failed", parentID)
-			return ErrBlockNotFound
+			err = ErrBlockNotFound
+			return
 		}
 		hPath = path.Join(parentTree.HPath, title)
 	} else {
@@ -1433,15 +1437,15 @@ func createDoc(boxID, p, title, dom string) (err error) {
 
 	if !box.Exist(folder) {
 		if err = box.MkdirAll(folder); nil != err {
-			return err
+			return
 		}
 	}
 
 	if box.Exist(p) {
-		return errors.New(Conf.Language(1))
+		err = errors.New(Conf.Language(1))
+		return
 	}
 
-	var tree *parse.Tree
 	luteEngine := NewLute()
 	tree = luteEngine.BlockDOM2Tree(dom)
 	tree.Box = boxID

+ 2 - 2
kernel/model/path.go

@@ -50,11 +50,11 @@ func createDocsByHPath(boxID, hPath, content string) (id string, existed bool, e
 			pathBuilder.WriteString(id)
 			docP := pathBuilder.String() + ".sy"
 			if isNotLast {
-				if err = createDoc(boxID, docP, part, ""); nil != err {
+				if _, err = createDoc(boxID, docP, part, ""); nil != err {
 					return
 				}
 			} else {
-				if err = createDoc(boxID, docP, part, content); nil != err {
+				if _, err = createDoc(boxID, docP, part, content); nil != err {
 					return
 				}
 			}

+ 1 - 3
kernel/model/transaction.go

@@ -61,8 +61,6 @@ func IsUnfoldHeading(transactions *[]*Transaction) bool {
 	return false
 }
 
-const txFixDelay = 10
-
 var (
 	txQueue     []*Transaction
 	txQueueLock = sync.Mutex{}
@@ -87,7 +85,7 @@ func WaitForWritingFiles() {
 }
 
 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) {
 		return true
 	}