Skip to content

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

:zap: Improve database table paste performance

Daniel544555b5f9c7
diff --git a/app/src/protyle/index.ts b/app/src/protyle/index.ts
index 0ca645c435..cf420a01b0 100644
--- a/app/src/protyle/index.ts
+++ b/app/src/protyle/index.ts
@@ -361,6 +361,7 @@ export class Protyle {
const hadContent = this.protyle.wysiwyg.element.childElementCount > 0;
let needCreateAction = "";
let hasDeleteOp = false;
+ const operations: IOperation[] = [];
data.data[0].doOperations.find((item: IOperation) => {
if (this.protyle.options.backlinkData && ["delete", "move"].includes(item.action)) {
// 反链上下文只展示源文档的一部分,结构操作等待索引提交后按内容版本增量同步。
@@ -369,13 +370,16 @@ export class Protyle {
if (item.action === "delete") {
hasDeleteOp = true;
}
- onTransaction(this.protyle, [item], false);
+ operations.push(item);
// 反链面板移除元素后,文档为空
if (!(item.action === "delete" && typeof item.data?.createEmptyParagraph === "boolean" && !item.data.createEmptyParagraph)) {
needCreateAction = item.action;
}
}
});
+ if (operations.length > 0) {
+ onTransaction(this.protyle, operations, false);
+ }
// 聚焦块被分屏另一侧的删除操作连带删除时(容器块删除会级联删除其所有子孙块,如列表/超级块/引述等),当前页签的聚焦块已成为孤儿但仍显示,需退出聚焦
// Improve editor state synchronization when deleting blocks https://github.com/siyuan-note/siyuan/issues/17742
if (this.protyle.block.showAll && hasDeleteOp) {
diff --git a/app/src/protyle/render/av/attributePanel.ts b/app/src/protyle/render/av/attributePanel.ts
index 28ee65b1cb..f0687d8c27 100644
--- a/app/src/protyle/render/av/attributePanel.ts
+++ b/app/src/protyle/render/av/attributePanel.ts
@@ -176,6 +176,11 @@ export class AVAttributePanel {
this.refresh();
return;
}
+ if (operation.action === "updateAttrViewCells" && operation.cellUpdates?.some(cell =>
+ ["block", "number"].includes(cell.data?.type) && this.hasItem(cell.rowID))) {
+ this.refresh();
+ return;
+ }
if (operation.action === "removeAttrViewBlock" &&
(this.hasDatabase(operation.avID) || operation.srcIDs?.some(item => this.hasItem(item)))) {
this.refresh();
diff --git a/app/src/protyle/render/av/paste.test.ts b/app/src/protyle/render/av/paste.test.ts
index 549004dd34..ae43eb92c4 100644
--- a/app/src/protyle/render/av/paste.test.ts
+++ b/app/src/protyle/render/av/paste.test.ts
@@ -1,6 +1,7 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {
+ compactAVCellOperations,
getAVPasteCellValue,
getAVPasteValueForType,
getAVPasteMatrixWidth,
@@ -146,4 +147,41 @@ describe("AV paste matrix helpers", () => {
assert.equal(shouldShowAVPasteSkeleton([Array(100)]), true);
});
+ it("compacts cell updates by database after schema operations", () => {
+ const operations = compactAVCellOperations([{
+ action: "updateAttrViewColOptions",
+ avID: "av1",
+ }, {
+ action: "updateAttrViewCell",
+ id: "cell1",
+ avID: "av1",
+ keyID: "key1",
+ rowID: "row1",
+ data: {type: "text", text: {content: "a"}},
+ }, {
+ action: "updateAttrViewCell",
+ id: "cell2",
+ avID: "av1",
+ keyID: "key2",
+ rowID: "row2",
+ data: {type: "number", number: {content: 2}},
+ }]);
+
+ assert.equal(operations.length, 2);
+ assert.equal(operations[0].action, "updateAttrViewColOptions");
+ assert.deepEqual(operations[1], {
+ action: "updateAttrViewCells",
+ avID: "av1",
+ cellUpdates: [{
+ keyID: "key1",
+ rowID: "row1",
+ data: {type: "text", text: {content: "a"}},
+ }, {
+ keyID: "key2",
+ rowID: "row2",
+ data: {type: "number", number: {content: 2}},
+ }],
+ });
+ });
+
});
diff --git a/app/src/protyle/render/av/paste.ts b/app/src/protyle/render/av/paste.ts
index a304cce8b9..8cf8da77cd 100644
--- a/app/src/protyle/render/av/paste.ts
+++ b/app/src/protyle/render/av/paste.ts
@@ -128,6 +128,37 @@ export const shouldShowAVPasteSkeleton = (rows: unknown[][]) => {
return rows.reduce((count, row) => count + row.length, 0) >= 100;
};
+export const compactAVCellOperations = (operations: IOperation[]) => {
+ const cellOperations = operations.filter(operation => operation.action === "updateAttrViewCell");
+ if (cellOperations.length < 2) {
+ return operations;
+ }
+ const otherOperations = operations.filter(operation => operation.action !== "updateAttrViewCell");
+ const operationsByAV = new Map<string, IOperation[]>();
+ cellOperations.forEach(operation => {
+ const avID = operation.avID || "";
+ const groupedOperations = operationsByAV.get(avID) || [];
+ groupedOperations.push(operation);
+ operationsByAV.set(avID, groupedOperations);
+ });
+ operationsByAV.forEach((groupedOperations, avID) => {
+ if (!avID || groupedOperations.length === 1) {
+ otherOperations.push(...groupedOperations);
+ return;
+ }
+ otherOperations.push({
+ action: "updateAttrViewCells",
+ avID,
+ cellUpdates: groupedOperations.map(operation => ({
+ keyID: operation.keyID,
+ rowID: operation.rowID,
+ data: operation.data,
+ })),
+ });
+ });
+ return otherOperations;
+};
+
export const showAVPasteSkeleton = (blockElement: HTMLElement, columnCount: number) => {
if (blockElement.querySelector(".av__paste-skeleton")) {
return false;
diff --git a/app/src/protyle/util/insertHTML.ts b/app/src/protyle/util/insertHTML.ts
index 857b6a2026..b299ea7fa9 100644
--- a/app/src/protyle/util/insertHTML.ts
+++ b/app/src/protyle/util/insertHTML.ts
@@ -39,6 +39,7 @@ import {setFold} from "./blockFold";
import {removeFoldHeading} from "./heading";
import {
AV_PASTE_READONLY_TYPES,
+ compactAVCellOperations,
getAVPasteCellValue,
getAVPasteValueForType,
getAVPasteMatrixWidth,
@@ -591,12 +592,17 @@ const pasteAVMatrix = async (options: {
removePlaceholderRows(options.blockElement);
}
- const doOperations = [...schemaDoOperations, ...widthDoOperations, ...rowDoOperations, ...cellDoOperations];
+ const doOperations = [
+ ...schemaDoOperations,
+ ...widthDoOperations,
+ ...rowDoOperations,
+ ...compactAVCellOperations(cellDoOperations),
+ ];
if (doOperations.length === 0) {
return;
}
const undoOperations = [
- ...cellUndoOperations,
+ ...compactAVCellOperations(cellUndoOperations),
...rowUndoOperations,
...widthUndoOperations.reverse(),
...schemaUndoOperations.reverse(),
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts
index 19e60fd225..7f60546ac8 100644
--- a/app/src/protyle/wysiwyg/transaction.ts
+++ b/app/src/protyle/wysiwyg/transaction.ts
@@ -1224,7 +1224,7 @@ export const onTransaction = (protyle: IProtyle, operations: IOperation[], isUnd
return;
}
if (["addAttrViewCol", "updateAttrViewCol", "updateAttrViewColOptions",
- "updateAttrViewColOption", "updateAttrViewCell", "sortAttrViewRow", "sortAttrViewCol", "setAttrViewColHidden",
+ "updateAttrViewColOption", "updateAttrViewCell", "updateAttrViewCells", "sortAttrViewRow", "sortAttrViewCol", "setAttrViewColHidden",
"setAttrViewColWrap", "setAttrViewColWidth", "setAttrViewColsWidth", "setAttrViewColAlign", "removeAttrViewColOption", "setAttrViewName", "setAttrViewFilters",
"setAttrViewSorts", "setAttrViewNewItemTemplates", "setAttrViewColCalc", "removeAttrViewCol", "updateAttrViewColNumberFormat", "setAttrViewColDateFormat", "removeAttrViewBlock",
"replaceAttrViewBlock", "updateAttrViewColTemplate", "setAttrViewColPin", "addAttrViewView", "setAttrViewColIcon",
diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts
index e4a5f48c1c..088b7567a0 100644
--- a/app/src/types/index.d.ts
+++ b/app/src/types/index.d.ts
@@ -22,6 +22,7 @@ type TOperation =
| "addFlashcards"
| "removeFlashcards"
| "updateAttrViewCell"
+ | "updateAttrViewCells"
| "updateAttrViewCol"
| "updateAttrViewColTemplate"
| "sortAttrViewRow"
@@ -698,6 +699,11 @@ interface IOperation {
format?: string // 属性视图字段格式化
keyID?: string // updateAttrViewCell 专享
rowID?: string // updateAttrViewCell 专享
+ cellUpdates?: Array<{
+ keyID: string,
+ rowID: string,
+ data: IAVCellValue,
+ }> // updateAttrViewCells 专享
data?: any, // updateAttr 时为 { old: IObject, new: IObject }, updateAttrViewCell 时为 {TAVCol: {content: string}}
parentID?: string
previousID?: string
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index 6e885adade..908c54325f 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -7540,6 +7540,30 @@ func (tx *Transaction) doBatchUpdateAttrViewCells(operations []*Operation) (ret
return
}
+func (tx *Transaction) doUpdateAttrViewCells(operation *Operation) (ret *TxErr) {
+ if "" == operation.AvID || 0 == len(operation.CellUpdates) {
+ return &TxErr{code: TxErrHandleAttributeView, id: operation.AvID, msg: "invalid attribute view cell updates"}
+ }
+ attrView, err := av.ParseAttributeView(operation.AvID)
+ if err != nil {
+ return &TxErr{code: TxErrHandleAttributeView, id: operation.AvID, msg: err.Error()}
+ }
+ for _, cell := range operation.CellUpdates {
+ if nil == cell || "" == cell.KeyID || "" == cell.RowID {
+ return &TxErr{code: TxErrHandleAttributeView, id: operation.AvID, msg: "invalid attribute view cell update"}
+ }
+ if _, err = updateAttributeViewValue(tx, attrView, cell.KeyID, cell.RowID, cell.Data, false); err != nil {
+ return &TxErr{code: TxErrHandleAttributeView, id: operation.AvID, msg: err.Error()}
+ }
+ }
+ regenAttrViewGroups(attrView)
+ if err = av.SaveAttributeView(attrView); err != nil {
+ return &TxErr{code: TxErrHandleAttributeView, id: attrView.ID, msg: err.Error()}
+ }
+ refreshRelatedSrcAvs(attrView.ID, tx)
+ return
+}
+
func BatchUpdateAttributeViewCells(tx *Transaction, avID string, values []any) (err error) {
attrView, err := av.ParseAttributeView(avID)
if err != nil {
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 2c1f5aa3a3..202c882193 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -351,6 +351,8 @@ func performTx(tx *Transaction) (ret *TxErr) {
ret = tx.doBatchUpdateAttrViewCells(operations)
operationIndex += len(operations) - 1
}
+ case "updateAttrViewCells":
+ ret = tx.doUpdateAttrViewCells(op)
case "updateAttrViewColOptions":
ret = tx.doUpdateAttrViewColOptions(op)
case "removeAttrViewColOption":
@@ -2209,28 +2211,35 @@ type Operation struct {
LockType bool `json:"-"` // 外部块更新是否禁止改变主类型
- AvID string `json:"avID"` // 属性视图 ID
- SrcIDs []string `json:"srcIDs"` // 用于从属性视图中删除行
- Srcs []map[string]any `json:"srcs"` // 用于添加属性视图行(包括绑定块){id, content, isDetached}
- IsDetached bool `json:"isDetached"` // 用于标识是否未绑定块,仅存在于属性视图中
- Name string `json:"name"` // 属性视图列名
- Typ string `json:"type"` // 属性视图列类型
- Format string `json:"format"` // 属性视图列格式化
- KeyID string `json:"keyID"` // 属性视图字段 ID
- RowID string `json:"rowID"` // 属性视图行 ID
- IsTwoWay bool `json:"isTwoWay"` // 属性视图关联列是否是双向关系
- BackRelationKeyID string `json:"backRelationKeyID"` // 属性视图关联列回链关联列的 ID
- RemoveDest bool `json:"removeDest"` // 属性视图删除关联目标
- Layout av.LayoutType `json:"layout"` // 属性视图布局类型
- GroupID string `json:"groupID"` // 属性视图分组视图 ID
- TargetGroupID string `json:"targetGroupID"` // 属性视图目标分组视图 ID
- ViewID string `json:"viewID"` // 属性视图视图 ID
- ViewIDs []string `json:"viewIDs,omitempty"` // 数据库视图 ID 列表
- IgnoreDefaultFill bool `json:"ignoreDefaultFill"` // 是否忽略默认填充
+ AvID string `json:"avID"` // 属性视图 ID
+ SrcIDs []string `json:"srcIDs"` // 用于从属性视图中删除行
+ Srcs []map[string]any `json:"srcs"` // 用于添加属性视图行(包括绑定块){id, content, isDetached}
+ IsDetached bool `json:"isDetached"` // 用于标识是否未绑定块,仅存在于属性视图中
+ Name string `json:"name"` // 属性视图列名
+ Typ string `json:"type"` // 属性视图列类型
+ Format string `json:"format"` // 属性视图列格式化
+ KeyID string `json:"keyID"` // 属性视图字段 ID
+ RowID string `json:"rowID"` // 属性视图行 ID
+ CellUpdates []*AttrViewCellUpdate `json:"cellUpdates,omitempty"` // 属性视图单元格批量更新
+ IsTwoWay bool `json:"isTwoWay"` // 属性视图关联列是否是双向关系
+ BackRelationKeyID string `json:"backRelationKeyID"` // 属性视图关联列回链关联列的 ID
+ RemoveDest bool `json:"removeDest"` // 属性视图删除关联目标
+ Layout av.LayoutType `json:"layout"` // 属性视图布局类型
+ GroupID string `json:"groupID"` // 属性视图分组视图 ID
+ TargetGroupID string `json:"targetGroupID"` // 属性视图目标分组视图 ID
+ ViewID string `json:"viewID"` // 属性视图视图 ID
+ ViewIDs []string `json:"viewIDs,omitempty"` // 数据库视图 ID 列表
+ IgnoreDefaultFill bool `json:"ignoreDefaultFill"` // 是否忽略默认填充
Context map[string]any `json:"context"` // 上下文信息
}
+type AttrViewCellUpdate struct {
+ KeyID string `json:"keyID"`
+ RowID string `json:"rowID"`
+ Data any `json:"data"`
+}
+
type listItemFoldCandidate struct {
id string
tree *parse.Tree