Skip to content

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

:zap: Optimize database backlink lookup https://github.com/siyuan-note/siyuan/issues/10599

Daniel152e053ce27f
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index d71af8a095..cba153384f 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -2111,6 +2111,85 @@ type attributeViewBacklinkTarget struct {
itemID string
}
+func getAttributeViewBacklinkMatches(srcAttrView *av.AttributeView, target *attributeViewBacklinkTarget) (ret map[string][]*AttributeViewBacklinkRelation) {
+ ret = map[string][]*AttributeViewBacklinkRelation{}
+ seenRelations := map[string]bool{}
+ for _, keyValues := range srcAttrView.KeyValues {
+ key := keyValues.Key
+ if av.KeyTypeRelation != key.Type || nil == key.Relation || key.Relation.AvID != target.avID {
+ continue
+ }
+
+ for _, value := range keyValues.Values {
+ if nil == value.Relation || !slices.Contains(value.Relation.BlockIDs, target.itemID) {
+ continue
+ }
+
+ relationID := value.BlockID + "\x00" + key.ID + "\x00" + target.avID + "\x00" + target.itemID
+ if seenRelations[relationID] {
+ continue
+ }
+ seenRelations[relationID] = true
+ ret[value.BlockID] = append(ret[value.BlockID], &AttributeViewBacklinkRelation{
+ KeyID: key.ID,
+ KeyName: key.Name,
+ TargetAvID: target.avID,
+ TargetItemID: target.itemID,
+ })
+ }
+ }
+ return
+}
+
+func getAttributeViewBacklinkBlockValues(attrView *av.AttributeView) (ret map[string]*av.Value) {
+ ret = map[string]*av.Value{}
+ blockKeyValues := attrView.GetBlockKeyValues()
+ if nil == blockKeyValues {
+ return
+ }
+ for _, value := range blockKeyValues.Values {
+ ret[value.BlockID] = value
+ }
+ return
+}
+
+func resolveAttributeViewBacklinkItemID(attrView *av.AttributeView, itemID, valueID string) string {
+ if nil != attrView.GetBlockValue(itemID) {
+ return itemID
+ }
+ if "" == valueID {
+ return ""
+ }
+ for _, value := range getAttributeViewBacklinkBlockValues(attrView) {
+ if value.ID == valueID {
+ return value.BlockID
+ }
+ }
+ return ""
+}
+
+func sortAttributeViewBacklinkBlockIDs(blockIDs []string, blockTrees map[string]*treenode.BlockTree) {
+ sort.Slice(blockIDs, func(i, j int) bool {
+ left, right := blockTrees[blockIDs[i]], blockTrees[blockIDs[j]]
+ if nil == left || nil == right {
+ if nil == left && nil != right {
+ return false
+ }
+ if nil != left && nil == right {
+ return true
+ }
+ return blockIDs[i] < blockIDs[j]
+ }
+ if left.HPath != right.HPath {
+ return left.HPath < right.HPath
+ }
+ if left.BoxID != right.BoxID {
+ return left.BoxID < right.BoxID
+ }
+ return blockIDs[i] < blockIDs[j]
+ })
+}
+
func GetAttributeViewBacklinks(nodeID, avID, itemID, valueID string) (ret *AttributeViewBacklinks) {
waitForSyncingStorages()
@@ -2123,6 +2202,9 @@ func GetAttributeViewBacklinks(nodeID, avID, itemID, valueID string) (ret *Attri
backlinks := map[string]*AttributeViewBacklink{}
relationKeys := map[string]map[string]bool{}
cachedAttrViews := map[string]*av.AttributeView{}
+ cachedBlockValues := map[string]map[string]*av.Value{}
+ cachedBlockIDs := map[string][]string{}
+ cachedBlockTrees := map[string]map[string]*treenode.BlockTree{}
for _, target := range targets {
for _, srcAvID := range av.GetSrcAvIDs(target.avID) {
srcAttrView := cachedAttrViews[srcAvID]
@@ -2136,60 +2218,59 @@ func GetAttributeViewBacklinks(nodeID, avID, itemID, valueID string) (ret *Attri
cachedAttrViews[srcAvID] = srcAttrView
}
- for _, keyValues := range srcAttrView.KeyValues {
- key := keyValues.Key
- if av.KeyTypeRelation != key.Type || nil == key.Relation || key.Relation.AvID != target.avID {
+ blockValues := cachedBlockValues[srcAvID]
+ if nil == blockValues {
+ blockValues = getAttributeViewBacklinkBlockValues(srcAttrView)
+ cachedBlockValues[srcAvID] = blockValues
+ }
+ for sourceItemID, relations := range getAttributeViewBacklinkMatches(srcAttrView, target) {
+ blockValue := blockValues[sourceItemID]
+ if nil == blockValue || nil == blockValue.Block {
continue
}
- for _, value := range keyValues.Values {
- if nil == value.Relation || !slices.Contains(value.Relation.BlockIDs, target.itemID) {
- continue
+ backlinkID := srcAvID + "\x00" + sourceItemID
+ backlink := backlinks[backlinkID]
+ if nil == backlink {
+ blockIDs, cached := cachedBlockIDs[srcAvID]
+ blockTrees := cachedBlockTrees[srcAvID]
+ if !cached {
+ blockIDs = treenode.GetMirrorAttrViewBlockIDs(srcAvID)
+ blockTrees = treenode.GetBlockTrees(blockIDs)
+ sortAttributeViewBacklinkBlockIDs(blockIDs, blockTrees)
+ cachedBlockIDs[srcAvID] = blockIDs
+ cachedBlockTrees[srcAvID] = blockTrees
}
-
- blockValue := srcAttrView.GetBlockValue(value.BlockID)
- if nil == blockValue || nil == blockValue.Block {
- continue
+ backlink = &AttributeViewBacklink{
+ AvID: srcAvID,
+ AvName: getAttrViewName(srcAttrView),
+ BlockIDs: blockIDs,
+ ItemID: sourceItemID,
+ ValueID: blockValue.ID,
+ Title: blockValue.Block.Content,
+ Icon: blockValue.Block.Icon,
+ BoundBlockID: blockValue.Block.ID,
+ IsDetached: blockValue.IsDetached || "" == blockValue.Block.ID,
+ Relations: []*AttributeViewBacklinkRelation{},
}
-
- backlinkID := srcAvID + "\x00" + value.BlockID
- backlink := backlinks[backlinkID]
- if nil == backlink {
- blockIDs := treenode.GetMirrorAttrViewBlockIDs(srcAvID)
- backlink = &AttributeViewBacklink{
- AvID: srcAvID,
- AvName: getAttrViewName(srcAttrView),
- BlockIDs: blockIDs,
- ItemID: value.BlockID,
- ValueID: blockValue.ID,
- Title: blockValue.Block.Content,
- Icon: blockValue.Block.Icon,
- BoundBlockID: blockValue.Block.ID,
- IsDetached: blockValue.IsDetached || "" == blockValue.Block.ID,
- Relations: []*AttributeViewBacklinkRelation{},
- }
- if 0 < len(blockIDs) {
- backlink.DatabaseBlockID = blockIDs[0]
- if bt := treenode.GetBlockTree(blockIDs[0]); nil != bt {
- backlink.BoxID = bt.BoxID
- backlink.DatabasePath = bt.HPath
- }
+ if 0 < len(blockIDs) {
+ backlink.DatabaseBlockID = blockIDs[0]
+ if bt := blockTrees[blockIDs[0]]; nil != bt {
+ backlink.BoxID = bt.BoxID
+ backlink.DatabasePath = bt.HPath
}
- backlinks[backlinkID] = backlink
- relationKeys[backlinkID] = map[string]bool{}
}
+ backlinks[backlinkID] = backlink
+ relationKeys[backlinkID] = map[string]bool{}
+ }
- relationID := key.ID + "\x00" + target.avID + "\x00" + target.itemID
+ for _, relation := range relations {
+ relationID := relation.KeyID + "\x00" + relation.TargetAvID + "\x00" + relation.TargetItemID
if relationKeys[backlinkID][relationID] {
continue
}
relationKeys[backlinkID][relationID] = true
- backlink.Relations = append(backlink.Relations, &AttributeViewBacklinkRelation{
- KeyID: key.ID,
- KeyName: key.Name,
- TargetAvID: target.avID,
- TargetItemID: target.itemID,
- })
+ backlink.Relations = append(backlink.Relations, relation)
}
}
}
@@ -2218,18 +2299,8 @@ func getAttributeViewBacklinkTargets(nodeID, avID, itemID, valueID string) (ret
logging.LogErrorf("parse attribute view [%s] failed: %s", avID, err)
return
}
- if nil == attrView.GetBlockValue(itemID) && "" != valueID {
- blockKeyValues := attrView.GetBlockKeyValues()
- if nil != blockKeyValues {
- for _, value := range blockKeyValues.Values {
- if value.ID == valueID {
- itemID = value.BlockID
- break
- }
- }
- }
- }
- if nil != attrView.GetBlockValue(itemID) {
+ itemID = resolveAttributeViewBacklinkItemID(attrView, itemID, valueID)
+ if "" != itemID {
ret = append(ret, &attributeViewBacklinkTarget{avID: avID, itemID: itemID})
}
return
diff --git a/kernel/model/attribute_view_backlink_test.go b/kernel/model/attribute_view_backlink_test.go
new file mode 100644
index 0000000000..ea655e2895
--- /dev/null
+++ b/kernel/model/attribute_view_backlink_test.go
@@ -0,0 +1,85 @@
+package model
+
+import (
+ "slices"
+ "testing"
+
+ "github.com/siyuan-note/siyuan/kernel/av"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+)
+
+func TestGetAttributeViewBacklinkMatches(t *testing.T) {
+ srcAttrView := &av.AttributeView{
+ ID: "source-av",
+ KeyValues: []*av.KeyValues{
+ {
+ Key: &av.Key{ID: "primary", Type: av.KeyTypeBlock},
+ Values: []*av.Value{
+ {ID: "primary-value-1", BlockID: "source-item-1", Type: av.KeyTypeBlock, Block: &av.ValueBlock{Content: "Source 1"}},
+ {ID: "primary-value-2", BlockID: "source-item-2", Type: av.KeyTypeBlock, Block: &av.ValueBlock{Content: "Source 2"}},
+ },
+ },
+ {
+ Key: &av.Key{ID: "relation-1", Name: "One-way", Type: av.KeyTypeRelation, Relation: &av.Relation{AvID: "target-av"}},
+ Values: []*av.Value{
+ {BlockID: "source-item-1", Type: av.KeyTypeRelation, Relation: &av.ValueRelation{BlockIDs: []string{"target-item"}}},
+ {BlockID: "source-item-1", Type: av.KeyTypeRelation, Relation: &av.ValueRelation{BlockIDs: []string{"target-item"}}},
+ {BlockID: "source-item-2", Type: av.KeyTypeRelation, Relation: &av.ValueRelation{BlockIDs: []string{"other-item"}}},
+ },
+ },
+ {
+ Key: &av.Key{ID: "relation-2", Name: "Two-way", Type: av.KeyTypeRelation, Relation: &av.Relation{AvID: "target-av", IsTwoWay: true}},
+ Values: []*av.Value{
+ {BlockID: "source-item-1", Type: av.KeyTypeRelation, Relation: &av.ValueRelation{BlockIDs: []string{"target-item"}}},
+ },
+ },
+ {
+ Key: &av.Key{ID: "relation-3", Name: "Other database", Type: av.KeyTypeRelation, Relation: &av.Relation{AvID: "other-av"}},
+ Values: []*av.Value{
+ {BlockID: "source-item-2", Type: av.KeyTypeRelation, Relation: &av.ValueRelation{BlockIDs: []string{"target-item"}}},
+ },
+ },
+ },
+ }
+
+ matches := getAttributeViewBacklinkMatches(srcAttrView, &attributeViewBacklinkTarget{avID: "target-av", itemID: "target-item"})
+ if 1 != len(matches) {
+ t.Fatalf("expected one source item, got %d", len(matches))
+ }
+ relations := matches["source-item-1"]
+ if 2 != len(relations) {
+ t.Fatalf("expected two relation fields, got %d", len(relations))
+ }
+ if "relation-1" != relations[0].KeyID || "relation-2" != relations[1].KeyID {
+ t.Fatalf("unexpected relation fields: %+v", relations)
+ }
+
+ blockValues := getAttributeViewBacklinkBlockValues(srcAttrView)
+ if 2 != len(blockValues) || "primary-value-1" != blockValues["source-item-1"].ID {
+ t.Fatalf("unexpected primary value index: %+v", blockValues)
+ }
+ if "source-item-1" != resolveAttributeViewBacklinkItemID(srcAttrView, "source-item-1", "") {
+ t.Fatal("failed to resolve a valid item ID")
+ }
+ if "source-item-2" != resolveAttributeViewBacklinkItemID(srcAttrView, "missing-item", "primary-value-2") {
+ t.Fatal("failed to resolve an item ID from its primary value ID")
+ }
+ if "" != resolveAttributeViewBacklinkItemID(srcAttrView, "missing-item", "missing-value") {
+ t.Fatal("unexpected item ID for invalid input")
+ }
+}
+
+func TestSortAttributeViewBacklinkBlockIDs(t *testing.T) {
+ blockIDs := []string{"missing-z", "block-b", "block-a", "missing-a", "block-c"}
+ blockTrees := map[string]*treenode.BlockTree{
+ "block-a": {ID: "block-a", BoxID: "box-2", HPath: "/a"},
+ "block-b": {ID: "block-b", BoxID: "box-1", HPath: "/z"},
+ "block-c": {ID: "block-c", BoxID: "box-1", HPath: "/a"},
+ }
+
+ sortAttributeViewBacklinkBlockIDs(blockIDs, blockTrees)
+ expected := []string{"block-c", "block-a", "block-b", "missing-a", "missing-z"}
+ if !slices.Equal(expected, blockIDs) {
+ t.Fatalf("expected %v, got %v", expected, blockIDs)
+ }
+}
diff --git a/kernel/model/publish_access.go b/kernel/model/publish_access.go
index cfe21a7772..a168672a43 100644
--- a/kernel/model/publish_access.go
+++ b/kernel/model/publish_access.go
@@ -430,6 +430,7 @@ func FilterAttributeViewBacklinksByPublishAccess(c *gin.Context, publishAccess P
publishIgnore := GetDisablePublishAccess(publishAccess)
accessibleTargetAvIDs := map[string]bool{}
checkedTargetAvIDs := map[string]bool{}
+ cachedBlockTrees := map[string]map[string]*treenode.BlockTree{}
for _, backlink := range backlinks.Items {
var relations []*AttributeViewBacklinkRelation
for _, relation := range backlink.Relations {
@@ -454,7 +455,16 @@ func FilterAttributeViewBacklinksByPublishAccess(c *gin.Context, publishAccess P
backlink.Relations = relations
databaseAccessible := false
- for _, bt := range treenode.GetBlockTrees(backlink.BlockIDs) {
+ blockTrees := cachedBlockTrees[backlink.AvID]
+ if nil == blockTrees {
+ blockTrees = treenode.GetBlockTrees(backlink.BlockIDs)
+ cachedBlockTrees[backlink.AvID] = blockTrees
+ }
+ for _, blockID := range backlink.BlockIDs {
+ bt := blockTrees[blockID]
+ if nil == bt {
+ continue
+ }
passwordID, password := GetPathPasswordByPublishAccess(bt.BoxID, bt.Path, publishAccess)
if ("" == password || CheckPublishAuthCookie(c, passwordID, password)) &&
CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishIgnore) {