♻️ Improve database loading performance https://github.com/siyuan-note/siyuan/issues/12818

This commit is contained in:
Daniel 2024-10-17 23:44:55 +08:00
parent c42064ec0b
commit 95c82187af
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
6 changed files with 33 additions and 53 deletions

View file

@ -22,6 +22,7 @@ import (
"github.com/88250/gulu" "github.com/88250/gulu"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/siyuan-note/siyuan/kernel/model" "github.com/siyuan-note/siyuan/kernel/model"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode" "github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util" "github.com/siyuan-note/siyuan/kernel/util"
) )
@ -48,7 +49,7 @@ func batchGetBlockAttrs(c *gin.Context) {
idList = append(idList, id.(string)) idList = append(idList, id.(string))
} }
ret.Data = model.BatchGetBlockAttrs(idList) ret.Data = sql.BatchGetBlockAttrs(idList)
} }
func getBlockAttrs(c *gin.Context) { func getBlockAttrs(c *gin.Context) {
@ -65,7 +66,7 @@ func getBlockAttrs(c *gin.Context) {
return return
} }
ret.Data = model.GetBlockAttrs(id) ret.Data = sql.GetBlockAttrs(id)
} }
func setBlockAttrs(c *gin.Context) { func setBlockAttrs(c *gin.Context) {

View file

@ -493,7 +493,7 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) {
waitForSyncingStorages() waitForSyncingStorages()
ret = []*BlockAttributeViewKeys{} ret = []*BlockAttributeViewKeys{}
attrs := sql.GetBlockAttrsWithoutWaitWriting(blockID) attrs := sql.GetBlockAttrs(blockID)
avs := attrs[av.NodeAttrNameAvs] avs := attrs[av.NodeAttrNameAvs]
if "" == avs { if "" == avs {
return return
@ -631,7 +631,7 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) {
kv.Values[0].Created = av.NewFormattedValueCreated(time.Now().UnixMilli(), 0, av.CreatedFormatNone) kv.Values[0].Created = av.NewFormattedValueCreated(time.Now().UnixMilli(), 0, av.CreatedFormatNone)
} }
case av.KeyTypeUpdated: case av.KeyTypeUpdated:
ial := sql.GetBlockAttrsWithoutWaitWriting(blockID) ial := sql.GetBlockAttrs(blockID)
updatedStr := ial["updated"] updatedStr := ial["updated"]
updated, parseErr := time.ParseInLocation("20060102150405", updatedStr, time.Local) updated, parseErr := time.ParseInLocation("20060102150405", updatedStr, time.Local)
if nil == parseErr { if nil == parseErr {
@ -655,7 +655,7 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) {
ial := map[string]string{} ial := map[string]string{}
block := av.GetKeyBlockValue(keyValues) block := av.GetKeyBlockValue(keyValues)
if nil != block && !block.IsDetached { if nil != block && !block.IsDetached {
ial = sql.GetBlockAttrsWithoutWaitWriting(block.BlockID) ial = sql.GetBlockAttrs(block.BlockID)
} }
if nil == kv.Values[0].Template { if nil == kv.Values[0].Template {

View file

@ -28,7 +28,6 @@ import (
"github.com/88250/lute/parse" "github.com/88250/lute/parse"
"github.com/araddon/dateparse" "github.com/araddon/dateparse"
"github.com/siyuan-note/siyuan/kernel/cache" "github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/sql" "github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode" "github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util" "github.com/siyuan-note/siyuan/kernel/util"
@ -51,7 +50,9 @@ func SetBlockReminder(id string, timed string) (err error) {
timedMills = t.UnixMilli() timedMills = t.UnixMilli()
} }
attrs := GetBlockAttrs(id) // 获取属性是会等待树写入 WaitForWritingFiles()
attrs := sql.GetBlockAttrs(id)
tree, err := LoadTreeByBlockID(id) tree, err := LoadTreeByBlockID(id)
if err != nil { if err != nil {
return return
@ -283,34 +284,3 @@ func ResetBlockAttrs(id string, nameValues map[string]string) (err error) {
cache.RemoveBlockIAL(id) cache.RemoveBlockIAL(id)
return return
} }
func BatchGetBlockAttrs(ids []string) (ret map[string]map[string]string) {
WaitForWritingFiles()
ret = map[string]map[string]string{}
trees := filesys.LoadTrees(ids)
for _, id := range ids {
tree := trees[id]
if nil == tree {
continue
}
ret[id] = sql.GetBlockAttrs0(id, tree)
cache.PutBlockIAL(id, ret[id])
}
return
}
func GetBlockAttrs(id string) (ret map[string]string) {
ret = map[string]string{}
if cached := cache.GetBlockIAL(id); nil != cached {
ret = cached
return
}
WaitForWritingFiles()
ret = sql.GetBlockAttrs(id)
cache.PutBlockIAL(id, ret)
return
}

View file

@ -598,7 +598,7 @@ func GetTreeDueFlashcards(rootID string, reviewedCardIDs []string) (ret []*Flash
newCardLimit := Conf.Flashcard.NewCardLimit newCardLimit := Conf.Flashcard.NewCardLimit
reviewCardLimit := Conf.Flashcard.ReviewCardLimit reviewCardLimit := Conf.Flashcard.ReviewCardLimit
// 文档级新卡/复习卡上限控制 Document-level new card/review card limit control https://github.com/siyuan-note/siyuan/issues/9365 // 文档级新卡/复习卡上限控制 Document-level new card/review card limit control https://github.com/siyuan-note/siyuan/issues/9365
ial := GetBlockAttrs(rootID) ial := sql.GetBlockAttrs(rootID)
if newCardLimitStr := ial["custom-riff-new-card-limit"]; "" != newCardLimitStr { if newCardLimitStr := ial["custom-riff-new-card-limit"]; "" != newCardLimitStr {
var convertErr error var convertErr error
newCardLimit, convertErr = strconv.Atoi(newCardLimitStr) newCardLimit, convertErr = strconv.Atoi(newCardLimitStr)

View file

@ -269,7 +269,7 @@ func RenderAttributeViewTable(attrView *av.AttributeView, view *av.View, query s
ial := map[string]string{} ial := map[string]string{}
block := row.GetBlockValue() block := row.GetBlockValue()
if nil != block && !block.IsDetached { if nil != block && !block.IsDetached {
ial = GetBlockAttrsWithoutWaitWriting(row.ID) ial = GetBlockAttrs(row.ID)
} }
updatedStr := ial["updated"] updatedStr := ial["updated"]
if "" == updatedStr && nil != block { if "" == updatedStr && nil != block {
@ -305,7 +305,7 @@ func RenderAttributeViewTable(attrView *av.AttributeView, view *av.View, query s
ial := map[string]string{} ial := map[string]string{}
block := row.GetBlockValue() block := row.GetBlockValue()
if nil != block && !block.IsDetached { if nil != block && !block.IsDetached {
ial = GetBlockAttrsWithoutWaitWriting(row.ID) ial = GetBlockAttrs(row.ID)
} }
content, renderErr := RenderTemplateCol(ial, keyValues, cell.Value.Template.Content) content, renderErr := RenderTemplateCol(ial, keyValues, cell.Value.Template.Content)
cell.Value.Template.Content = content cell.Value.Template.Content = content

View file

@ -19,13 +19,13 @@ package sql
import ( import (
"bytes" "bytes"
"database/sql" "database/sql"
"github.com/88250/lute/parse"
"github.com/siyuan-note/logging"
"strings" "strings"
"github.com/88250/gulu" "github.com/88250/gulu"
"github.com/88250/lute/ast" "github.com/88250/lute/ast"
"github.com/88250/lute/html" "github.com/88250/lute/html"
"github.com/88250/lute/parse"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/av" "github.com/siyuan-note/siyuan/kernel/av"
"github.com/siyuan-note/siyuan/kernel/cache" "github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/filesys" "github.com/siyuan-note/siyuan/kernel/filesys"
@ -275,31 +275,39 @@ func nodeStaticContent(node *ast.Node, excludeTypes []string, includeTextMarkATi
return buf.String() return buf.String()
} }
func GetBlockAttrsWithoutWaitWriting(id string) (ret map[string]string) { func BatchGetBlockAttrs(ids []string) (ret map[string]map[string]string) {
ret = map[string]string{} ret = map[string]map[string]string{}
if cached := cache.GetBlockIAL(id); nil != cached { trees := filesys.LoadTrees(ids)
ret = cached for _, id := range ids {
return tree := trees[id]
} if nil == tree {
continue
}
ret = GetBlockAttrs(id) ret[id] = getBlockAttrsFromTree(id, tree)
cache.PutBlockIAL(id, ret) }
return return
} }
func GetBlockAttrs(id string) (ret map[string]string) { func GetBlockAttrs(id string) (ret map[string]string) {
ret = map[string]string{} ret = map[string]string{}
ret = map[string]string{}
if cached := cache.GetBlockIAL(id); nil != cached {
ret = cached
return
}
tree := loadTreeByBlockID(id) tree := loadTreeByBlockID(id)
if nil == tree { if nil == tree {
return return
} }
ret = GetBlockAttrs0(id, tree) ret = getBlockAttrsFromTree(id, tree)
return return
} }
func GetBlockAttrs0(id string, tree *parse.Tree) (ret map[string]string) { func getBlockAttrsFromTree(id string, tree *parse.Tree) (ret map[string]string) {
ret = map[string]string{} ret = map[string]string{}
node := treenode.GetNodeInTree(tree, id) node := treenode.GetNodeInTree(tree, id)
if nil == node { if nil == node {
@ -310,6 +318,7 @@ func GetBlockAttrs0(id string, tree *parse.Tree) (ret map[string]string) {
for _, kv := range node.KramdownIAL { for _, kv := range node.KramdownIAL {
ret[kv[0]] = html.UnescapeAttrVal(kv[1]) ret[kv[0]] = html.UnescapeAttrVal(kv[1])
} }
cache.PutBlockIAL(id, ret)
return return
} }