Skip to content

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

:zap: Optimize deletion checks for database-bound blocks https://github.com/siyuan-note/siyuan/issues/13396

Danielba639fce5669
diff --git a/app/src/protyle/wysiwyg/removeRange.test.ts b/app/src/protyle/wysiwyg/removeRange.test.ts
index dab2b14e22..486363041b 100644
--- a/app/src/protyle/wysiwyg/removeRange.test.ts
+++ b/app/src/protyle/wysiwyg/removeRange.test.ts
@@ -12,6 +12,9 @@ class TestElement {
parentElement: TestElement | null = null;
children: TestElement[] = [];
private attributes = new Map<string, string>();
+ classList = {
+ contains: (name: string) => this.attributes.get("class")?.split(/\s+/).includes(name) || false,
+ };
constructor(public name: string, type?: string) {
if (type) {
@@ -28,6 +31,11 @@ class TestElement {
return this;
}
+ addClass(name: string) {
+ this.attributes.set("class", name);
+ return this;
+ }
+
get nextElementSibling() {
if (!this.parentElement) {
return null;
@@ -126,6 +134,17 @@ describe("getDeletedBlockElements", () => {
assert.deepEqual(result.elements, [asHTMLElement(root), asHTMLElement(deletedChild)]);
assert.deepEqual(Array.from(result.expansionStopIDs), ["root"]);
});
+
+ it("排除查询嵌入块的渲染结果", () => {
+ const renderedBlock = block("renderedBlock", "NodeParagraph");
+ const renderedResult = new TestElement("renderedResult").addClass("protyle-wysiwyg__embed")
+ .append(renderedBlock);
+ const embed = block("embed", "NodeBlockQueryEmbed", renderedResult);
+
+ const result = getDeletedBlockElements([asHTMLElement(embed)], []);
+
+ assert.deepEqual(result.elements, [asHTMLElement(embed)]);
+ });
});
describe("getCrossBlockMergeRemoveElement", () => {
diff --git a/app/src/protyle/wysiwyg/removeRange.ts b/app/src/protyle/wysiwyg/removeRange.ts
index 2ff9ac7758..0324c24c42 100644
--- a/app/src/protyle/wysiwyg/removeRange.ts
+++ b/app/src/protyle/wysiwyg/removeRange.ts
@@ -274,6 +274,13 @@ export const getDeletedBlockElements = (removedElements: HTMLElement[], retained
const expansionStopIDs = new Set<string>();
removedElements.forEach(item => {
[item, ...Array.from(item.querySelectorAll<HTMLElement>("[data-node-id]"))].forEach(element => {
+ let currentElement: HTMLElement | null = element;
+ while (currentElement && !currentElement.classList.contains("protyle-wysiwyg__embed")) {
+ currentElement = currentElement.parentElement;
+ }
+ if (currentElement) {
+ return;
+ }
if (retainedElements.some(retainedElement =>
retainedElement === element || retainedElement.contains(element))) {
return;
diff --git a/kernel/model/block.go b/kernel/model/block.go
index 47e9a1e0ec..199c293845 100644
--- a/kernel/model/block.go
+++ b/kernel/model/block.go
@@ -18,7 +18,6 @@ package model
import (
"bytes"
- "errors"
"fmt"
"html"
"path"
@@ -307,55 +306,22 @@ func existBoundBlockGroup(group *blockRefCheckGroup) (ret bool, err error) {
return false, err
}
- validBoundAVIDs := map[string][]string{}
- for blockID, avIDs := range boundAVIDs {
- for _, avID := range avIDs {
- if !ast.IsNodeIDPattern(avID) {
- continue
- }
- attrView, parseErr := av.ParseAttributeView(avID)
- if errors.Is(parseErr, av.ErrViewNotFound) {
- continue
- }
- if nil != parseErr {
- return false, parseErr
- }
- if nil == attrView {
- return false, fmt.Errorf("attribute view [%s] is unavailable", avID)
- }
- blockValues := attrView.GetBlockKeyValues()
- if nil == blockValues {
- continue
- }
- for _, blockValue := range blockValues.Values {
- if nil != blockValue && !blockValue.IsDetached && nil != blockValue.Block &&
- blockID == blockValue.Block.ID {
- validBoundAVIDs[blockID] = append(validBoundAVIDs[blockID], avID)
- break
- }
- }
- }
- }
- if 0 == len(validBoundAVIDs) {
- return false, nil
- }
-
- validAVIDSet := map[string]struct{}{}
- for _, avIDs := range validBoundAVIDs {
+ avIDSet := map[string]struct{}{}
+ for _, avIDs := range boundAVIDs {
for _, avID := range avIDs {
- validAVIDSet[avID] = struct{}{}
+ avIDSet[avID] = struct{}{}
}
}
- validAVIDs := make([]string, 0, len(validAVIDSet))
- for avID := range validAVIDSet {
- validAVIDs = append(validAVIDs, avID)
+ avIDs := make([]string, 0, len(avIDSet))
+ for avID := range avIDSet {
+ avIDs = append(avIDs, avID)
}
- avBlockRels, err := av.GetBlockRelsByAVIDs(validAVIDs)
+ avBlockRels, err := av.GetBlockRelsByAVIDs(avIDs)
if nil != err {
return false, err
}
avBlockIDSet := map[string]struct{}{}
- for _, avIDs := range validBoundAVIDs {
+ for _, avIDs := range boundAVIDs {
for _, avID := range avIDs {
for _, blockID := range avBlockRels[avID] {
avBlockIDSet[blockID] = struct{}{}
@@ -366,7 +332,7 @@ func existBoundBlockGroup(group *blockRefCheckGroup) (ret bool, err error) {
for id := range avBlockIDSet {
avBlockIDs = append(avBlockIDs, id)
}
- return hasSurvivingAttributeViewBlock(group, validBoundAVIDs, avBlockRels, treenode.GetBlockTrees(avBlockIDs)), nil
+ return hasSurvivingAttributeViewBlock(group, boundAVIDs, avBlockRels, treenode.GetBlockTrees(avBlockIDs)), nil
}
func hasSurvivingAttributeViewBlock(group *blockRefCheckGroup, boundAVIDs, avBlockRels map[string][]string,
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 7cead58b3a..54faa10a7d 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -454,6 +454,10 @@ func performTx(tx *Transaction) (ret *TxErr) {
tx.rollback()
return
}
+ if "delete" != op.Action || operationIndex == len(tx.DoOperations)-1 ||
+ "delete" != tx.DoOperations[operationIndex+1].Action {
+ tx.flushDeletedAttributeViewBlocks()
+ }
}
}
@@ -503,6 +507,7 @@ func (tx *Transaction) processLargeDelete() bool {
}
tx.doLargeDelete(deleteOps)
+ tx.flushDeletedAttributeViewBlocks()
return true
}
@@ -539,10 +544,12 @@ func (tx *Transaction) processLargeInsert() bool {
if nil != firstDeleteOp {
tx.doDelete(firstDeleteOp)
+ tx.flushDeletedAttributeViewBlocks()
}
tx.doLargeInsert(insertOps)
if nil != lastDeleteOp {
tx.doDelete(lastDeleteOp)
+ tx.flushDeletedAttributeViewBlocks()
}
return true
}
@@ -1214,12 +1221,8 @@ func (tx *Transaction) doDelete0(operation *Operation, tree *parse.Tree) (delete
}
func syncDelete2AvBlock(node *ast.Node, nodeTree *parse.Tree, delChildrenWhenDelParent bool, tx *Transaction) {
- changedAvIDs := syncDelete2AttributeView(node, delChildrenWhenDelParent)
- avIDs := tx.syncDelete2Block(node, nodeTree)
- changedAvIDs = append(changedAvIDs, avIDs...)
- changedAvIDs = gulu.Str.RemoveDuplicatedElem(changedAvIDs)
-
- for _, avID := range changedAvIDs {
+ tx.collectDeletedAttributeViewBlocks(node, delChildrenWhenDelParent)
+ for _, avID := range tx.syncDelete2Block(node, nodeTree) {
ReloadAttrView(avID)
}
}
@@ -1276,63 +1279,69 @@ func (tx *Transaction) syncDelete2Block(node *ast.Node, nodeTree *parse.Tree) (c
return
}
-func syncDelete2AttributeView(node *ast.Node, delChildrenWhenDelParent bool) (changedAvIDs []string) {
+func (tx *Transaction) collectDeletedAttributeViewBlocks(node *ast.Node, delChildrenWhenDelParent bool) {
+ collect := func(n *ast.Node) {
+ avs := n.IALAttr(av.NodeAttrNameAvs)
+ if "" == avs {
+ return
+ }
+ for avID := range strings.SplitSeq(avs, ",") {
+ blockIDs := tx.deletedAttrViewBlockIDs[avID]
+ if nil == blockIDs {
+ blockIDs = map[string]struct{}{}
+ tx.deletedAttrViewBlockIDs[avID] = blockIDs
+ }
+ blockIDs[n.ID] = struct{}{}
+ }
+ }
if !delChildrenWhenDelParent {
- changedAvIDs = deleteAttrView(node, changedAvIDs)
+ collect(node)
return
}
-
ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
- if !entering || !n.IsBlock() {
- return ast.WalkContinue
+ if entering && n.IsBlock() {
+ collect(n)
}
-
- changedAvIDs = append(changedAvIDs, deleteAttrView(n, changedAvIDs)...)
return ast.WalkContinue
})
-
- changedAvIDs = gulu.Str.RemoveDuplicatedElem(changedAvIDs)
- return
}
-func deleteAttrView(n *ast.Node, changedAvIDs []string) []string {
- avs := n.IALAttr(av.NodeAttrNameAvs)
- if "" == avs {
- return nil
- }
-
- avIDs := strings.SplitSeq(avs, ",")
- for avID := range avIDs {
- attrView, parseErr := av.ParseAttributeView(avID)
- if nil != parseErr {
+func (tx *Transaction) flushDeletedAttributeViewBlocks() {
+ for avID, deletedBlockIDs := range tx.deletedAttrViewBlockIDs {
+ attrView, err := av.ParseAttributeView(avID)
+ if nil != err || !removeAttributeViewBoundBlocks(attrView, deletedBlockIDs) {
continue
}
+ regenAttrViewGroups(attrView)
+ av.SaveAttributeView(attrView)
+ ReloadAttrView(avID)
+ }
+ tx.deletedAttrViewBlockIDs = map[string]map[string]struct{}{}
+}
- changedAv := false
- blockValues := attrView.GetBlockKeyValues()
- if nil == blockValues {
- continue
- }
+func removeAttributeViewBoundBlocks(attrView *av.AttributeView, deletedBlockIDs map[string]struct{}) (changed bool) {
+ if nil == attrView {
+ return false
+ }
- for i, blockValue := range blockValues.Values {
- if nil == blockValue.Block {
+ blockValues := attrView.GetBlockKeyValues()
+ if nil == blockValues {
+ return false
+ }
+ values := make([]*av.Value, 0, len(blockValues.Values))
+ for _, blockValue := range blockValues.Values {
+ if nil != blockValue && nil != blockValue.Block {
+ if _, deleted := deletedBlockIDs[blockValue.Block.ID]; deleted {
+ changed = true
continue
}
-
- if blockValue.Block.ID == n.ID {
- blockValues.Values = append(blockValues.Values[:i], blockValues.Values[i+1:]...)
- changedAv = true
- break
- }
- }
-
- if changedAv {
- regenAttrViewGroups(attrView)
- av.SaveAttributeView(attrView)
- changedAvIDs = append(changedAvIDs, avID)
}
+ values = append(values, blockValue)
}
- return changedAvIDs
+ if changed {
+ blockValues.Values = values
+ }
+ return
}
func (tx *Transaction) doLargeInsert(operations []*Operation) {
@@ -2180,6 +2189,7 @@ type Transaction struct {
listItemFoldCandidates []listItemFoldCandidate
listItemFoldCandidateIDs map[string]struct{}
+ deletedAttrViewBlockIDs map[string]map[string]struct{}
luteEngine *lute.Lute
m *sync.Mutex
@@ -2236,6 +2246,7 @@ func (tx *Transaction) begin() (err error) {
tx.restoredCreatedDocs = nil
tx.listItemFoldCandidates = nil
tx.listItemFoldCandidateIDs = map[string]struct{}{}
+ tx.deletedAttrViewBlockIDs = map[string]map[string]struct{}{}
tx.luteEngine = util.NewLute()
tx.m.Lock()
tx.state.Store(1)
@@ -2310,6 +2321,7 @@ func (tx *Transaction) commit() (err error) {
func (tx *Transaction) rollback() {
tx.trees, tx.nodes, tx.boxIcons, tx.removedCreatedDocs, tx.restoredCreatedDocs = nil, nil, nil, nil, nil
tx.listItemFoldCandidates, tx.listItemFoldCandidateIDs = nil, nil
+ tx.deletedAttrViewBlockIDs = nil
tx.state.Store(3)
tx.m.Unlock()
return
diff --git a/kernel/model/transaction_attribute_view_test.go b/kernel/model/transaction_attribute_view_test.go
new file mode 100644
index 0000000000..76fb7b92ac
--- /dev/null
+++ b/kernel/model/transaction_attribute_view_test.go
@@ -0,0 +1,56 @@
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package model
+
+import (
+ "testing"
+
+ "github.com/siyuan-note/siyuan/kernel/av"
+)
+
+func TestRemoveAttributeViewBoundBlocks(t *testing.T) {
+ deletedValue1 := &av.Value{Block: &av.ValueBlock{ID: "20260805000000-deleted1"}}
+ keptValue := &av.Value{Block: &av.ValueBlock{ID: "20260805000000-kept"}}
+ deletedValue2 := &av.Value{Block: &av.ValueBlock{ID: "20260805000000-deleted2"}}
+ nilBlockValue := &av.Value{}
+ attrView := &av.AttributeView{KeyValues: []*av.KeyValues{{
+ Key: &av.Key{Type: av.KeyTypeBlock},
+ Values: []*av.Value{
+ deletedValue1,
+ keptValue,
+ deletedValue2,
+ nil,
+ nilBlockValue,
+ },
+ }}}
+
+ changed := removeAttributeViewBoundBlocks(attrView, map[string]struct{}{
+ "20260805000000-deleted1": {},
+ "20260805000000-deleted2": {},
+ })
+
+ if !changed {
+ t.Fatal("expected bound blocks to be removed")
+ }
+ values := attrView.GetBlockKeyValues().Values
+ if 3 != len(values) || keptValue != values[0] || nil != values[1] || nilBlockValue != values[2] {
+ t.Fatalf("unexpected remaining values: %#v", values)
+ }
+ if removeAttributeViewBoundBlocks(attrView, map[string]struct{}{"20260805000000-missing": {}}) {
+ t.Fatal("an unrelated block ID should not change the attribute view")
+ }
+}