Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Improve graph https://github.com/siyuan-note/siyuan/issues/13040

Daniela48609755e1d
diff --git a/kernel/model/backlink.go b/kernel/model/backlink.go
index 1d0736a68f..97941b374c 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 fc13b08167..cce24cde02 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 796235eeb0..7ba9287c65 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 a5272f5c0b..ebc28e1175 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