From 2ce8ce79bd7a30d984ae3aad81f4756e1bf08e0d Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sat, 26 Nov 2022 22:19:48 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=20=E6=94=B9=E8=BF=9B=E5=8F=8D=E9=93=BE?= =?UTF-8?q?=E9=9D=A2=E6=9D=BF=E5=8A=A0=E8=BD=BD=E6=80=A7=E8=83=BD=20Fix=20?= =?UTF-8?q?https://github.com/siyuan-note/siyuan/issues/6724?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/sql/block_query.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/kernel/sql/block_query.go b/kernel/sql/block_query.go index 7f3356e28..a1a4b1214 100644 --- a/kernel/sql/block_query.go +++ b/kernel/sql/block_query.go @@ -598,11 +598,29 @@ func GetAllRootBlocks() (ret []*Block) { } func GetBlocks(ids []string) (ret []*Block) { - length := len(ids) + var notHitIDs []string + cached := map[string]*Block{} + for _, id := range ids { + b := getBlockCache(id) + if nil != b { + cached[id] = b + } else { + notHitIDs = append(notHitIDs, id) + } + } + + if 1 > len(notHitIDs) { + for _, id := range ids { + ret = append(ret, cached[id]) + } + return + } + + length := len(notHitIDs) stmtBuilder := bytes.Buffer{} stmtBuilder.WriteString("SELECT * FROM blocks WHERE id IN (") var args []interface{} - for i, id := range ids { + for i, id := range notHitIDs { args = append(args, id) stmtBuilder.WriteByte('?') if i < length-1 { @@ -619,10 +637,13 @@ func GetBlocks(ids []string) (ret []*Block) { defer rows.Close() for rows.Next() { if block := scanBlockRows(rows); nil != block { - ret = append(ret, block) putBlockCache(block) + cached[block.ID] = block } } + for _, id := range ids { + ret = append(ret, cached[id]) + } return }