Skip to content

Commit

Permalink
♻️ Improve database loading performance #12818
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Oct 17, 2024
1 parent c42064e commit 95c8218
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 53 deletions.
5 changes: 3 additions & 2 deletions kernel/api/attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/88250/gulu"
"github.com/gin-gonic/gin"
"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/util"
)
Expand All @@ -48,7 +49,7 @@ func batchGetBlockAttrs(c *gin.Context) {
idList = append(idList, id.(string))
}

ret.Data = model.BatchGetBlockAttrs(idList)
ret.Data = sql.BatchGetBlockAttrs(idList)
}

func getBlockAttrs(c *gin.Context) {
Expand All @@ -65,7 +66,7 @@ func getBlockAttrs(c *gin.Context) {
return
}

ret.Data = model.GetBlockAttrs(id)
ret.Data = sql.GetBlockAttrs(id)
}

func setBlockAttrs(c *gin.Context) {
Expand Down
6 changes: 3 additions & 3 deletions kernel/model/attribute_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) {
waitForSyncingStorages()

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

if nil == kv.Values[0].Template {
Expand Down
36 changes: 3 additions & 33 deletions kernel/model/blockial.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/88250/lute/parse"
"github.com/araddon/dateparse"
"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/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
Expand All @@ -51,7 +50,9 @@ func SetBlockReminder(id string, timed string) (err error) {
timedMills = t.UnixMilli()
}

attrs := GetBlockAttrs(id) // 获取属性是会等待树写入
WaitForWritingFiles()

attrs := sql.GetBlockAttrs(id)
tree, err := LoadTreeByBlockID(id)
if err != nil {
return
Expand Down Expand Up @@ -283,34 +284,3 @@ func ResetBlockAttrs(id string, nameValues map[string]string) (err error) {
cache.RemoveBlockIAL(id)
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
}
2 changes: 1 addition & 1 deletion kernel/model/flashcard.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ func GetTreeDueFlashcards(rootID string, reviewedCardIDs []string) (ret []*Flash
newCardLimit := Conf.Flashcard.NewCardLimit
reviewCardLimit := Conf.Flashcard.ReviewCardLimit
// 文档级新卡/复习卡上限控制 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 {
var convertErr error
newCardLimit, convertErr = strconv.Atoi(newCardLimitStr)
Expand Down
4 changes: 2 additions & 2 deletions kernel/sql/av.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func RenderAttributeViewTable(attrView *av.AttributeView, view *av.View, query s
ial := map[string]string{}
block := row.GetBlockValue()
if nil != block && !block.IsDetached {
ial = GetBlockAttrsWithoutWaitWriting(row.ID)
ial = GetBlockAttrs(row.ID)
}
updatedStr := ial["updated"]
if "" == updatedStr && nil != block {
Expand Down Expand Up @@ -305,7 +305,7 @@ func RenderAttributeViewTable(attrView *av.AttributeView, view *av.View, query s
ial := map[string]string{}
block := row.GetBlockValue()
if nil != block && !block.IsDetached {
ial = GetBlockAttrsWithoutWaitWriting(row.ID)
ial = GetBlockAttrs(row.ID)
}
content, renderErr := RenderTemplateCol(ial, keyValues, cell.Value.Template.Content)
cell.Value.Template.Content = content
Expand Down
33 changes: 21 additions & 12 deletions kernel/sql/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ package sql
import (
"bytes"
"database/sql"
"github.com/88250/lute/parse"
"github.com/siyuan-note/logging"
"strings"

"github.com/88250/gulu"
"github.com/88250/lute/ast"
"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/cache"
"github.com/siyuan-note/siyuan/kernel/filesys"
Expand Down Expand Up @@ -275,31 +275,39 @@ func nodeStaticContent(node *ast.Node, excludeTypes []string, includeTextMarkATi
return buf.String()
}

func GetBlockAttrsWithoutWaitWriting(id string) (ret map[string]string) {
ret = map[string]string{}
if cached := cache.GetBlockIAL(id); nil != cached {
ret = cached
return
}
func BatchGetBlockAttrs(ids []string) (ret map[string]map[string]string) {
ret = map[string]map[string]string{}
trees := filesys.LoadTrees(ids)
for _, id := range ids {
tree := trees[id]
if nil == tree {
continue
}

ret = GetBlockAttrs(id)
cache.PutBlockIAL(id, ret)
ret[id] = getBlockAttrsFromTree(id, tree)
}
return
}

func GetBlockAttrs(id 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)
if nil == tree {
return
}

ret = GetBlockAttrs0(id, tree)
ret = getBlockAttrsFromTree(id, tree)
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{}
node := treenode.GetNodeInTree(tree, id)
if nil == node {
Expand All @@ -310,6 +318,7 @@ func GetBlockAttrs0(id string, tree *parse.Tree) (ret map[string]string) {
for _, kv := range node.KramdownIAL {
ret[kv[0]] = html.UnescapeAttrVal(kv[1])
}
cache.PutBlockIAL(id, ret)
return
}

Expand Down

0 comments on commit 95c8218

Please sign in to comment.