From a48609755e1d3b2adb30d0e94441bafc5f664e49 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Fri, 22 Nov 2024 23:27:38 +0800 Subject: [PATCH] :art: Improve graph https://github.com/siyuan-note/siyuan/issues/13040 --- kernel/model/backlink.go | 2 +- kernel/model/graph.go | 6 +++--- kernel/sql/block_query.go | 6 ++++-- kernel/sql/block_ref_query.go | 4 ++-- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/kernel/model/backlink.go b/kernel/model/backlink.go index 1d0736a68fa..97941b374cf 100644 --- a/kernel/model/backlink.go +++ b/kernel/model/backlink.go @@ -845,7 +845,7 @@ func buildFullLinks(condition string) (forwardlinks, backlinks []*Block) { func buildDefsAndRefs(condition string) (defBlocks []*Block) { defBlockMap := map[string]*Block{} refBlockMap := map[string]*Block{} - defRefs := sql.DefRefs(condition) + defRefs := sql.DefRefs(condition, Conf.Graph.MaxBlocks) // 将 sql block 转为 block for _, row := range defRefs { diff --git a/kernel/model/graph.go b/kernel/model/graph.go index fc13b08167c..cce24cde023 100644 --- a/kernel/model/graph.go +++ b/kernel/model/graph.go @@ -84,10 +84,10 @@ func BuildTreeGraph(id, query string) (boxID string, nodes []*GraphNode, links [ var sqlBlocks []*sql.Block var rootID string if ast.NodeDocument == node.Type { - sqlBlocks = sql.GetAllChildBlocks([]string{block.ID}, stmt) + sqlBlocks = sql.GetAllChildBlocks([]string{block.ID}, stmt, Conf.Graph.MaxBlocks) rootID = block.ID } else { - sqlBlocks = sql.GetChildBlocks(block.ID, stmt) + sqlBlocks = sql.GetChildBlocks(block.ID, stmt, Conf.Graph.MaxBlocks) } blocks := fromSQLBlocks(&sqlBlocks, "", 0) if "" != rootID { @@ -185,7 +185,7 @@ func BuildGraph(query string) (boxID string, nodes []*GraphNode, links []*GraphL } rootIDs = gulu.Str.RemoveDuplicatedElem(rootIDs) - sqlBlocks := sql.GetAllChildBlocks(rootIDs, stmt) + sqlBlocks := sql.GetAllChildBlocks(rootIDs, stmt, Conf.Graph.MaxBlocks) treeBlocks := fromSQLBlocks(&sqlBlocks, "", 0) genTreeNodes(treeBlocks, &nodes, &links, false) blocks = append(blocks, treeBlocks...) diff --git a/kernel/sql/block_query.go b/kernel/sql/block_query.go index 796235eeb0c..7ba9287c65c 100644 --- a/kernel/sql/block_query.go +++ b/kernel/sql/block_query.go @@ -726,7 +726,7 @@ func scanBlockRow(row *sql.Row) (ret *Block) { return } -func GetChildBlocks(parentID, condition string) (ret []*Block) { +func GetChildBlocks(parentID, condition string, limit int) (ret []*Block) { blockIDs := queryBlockChildrenIDs(parentID) var params []string for _, id := range blockIDs { @@ -738,6 +738,7 @@ func GetChildBlocks(parentID, condition string) (ret []*Block) { if "" != condition { sqlStmt += " AND " + condition } + sqlStmt += " LIMIT " + strconv.Itoa(limit) rows, err := query(sqlStmt) if err != nil { logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err) @@ -752,12 +753,13 @@ func GetChildBlocks(parentID, condition string) (ret []*Block) { return } -func GetAllChildBlocks(rootIDs []string, condition string) (ret []*Block) { +func GetAllChildBlocks(rootIDs []string, condition string, limit int) (ret []*Block) { ret = []*Block{} sqlStmt := "SELECT * FROM blocks AS ref WHERE ref.root_id IN ('" + strings.Join(rootIDs, "','") + "')" if "" != condition { sqlStmt += " AND " + condition } + sqlStmt += " LIMIT " + strconv.Itoa(limit) rows, err := query(sqlStmt) if err != nil { logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err) diff --git a/kernel/sql/block_ref_query.go b/kernel/sql/block_ref_query.go index a5272f5c0bb..ebc28e11758 100644 --- a/kernel/sql/block_ref_query.go +++ b/kernel/sql/block_ref_query.go @@ -428,7 +428,7 @@ func QueryRefsByDefIDRefID(defBlockID, refBlockID string) (ret []*Ref) { return } -func DefRefs(condition string) (ret []map[*Block]*Block) { +func DefRefs(condition string, limit int) (ret []map[*Block]*Block) { ret = []map[*Block]*Block{} stmt := "SELECT ref.*, r.block_id || '@' || r.def_block_id AS rel FROM blocks AS ref, refs AS r WHERE ref.id = r.block_id" if "" != condition { @@ -453,7 +453,7 @@ func DefRefs(condition string) (ret []map[*Block]*Block) { refs[rel] = &ref } - rows, err = query("SELECT def.* FROM blocks AS def, refs AS r WHERE def.id = r.def_block_id") + rows, err = query("SELECT def.* FROM blocks AS def, refs AS r WHERE def.id = r.def_block_id LIMIT ?", limit) if err != nil { logging.LogErrorf("sql query failed: %s", err) return