Skip to content

Commit d689cff

Browse files
authored
Fix sql batch update staging (#8317)
Signed-off-by: Denis Bykhov <[email protected]>
1 parent dabf2d4 commit d689cff

File tree

1 file changed

+44
-28
lines changed

1 file changed

+44
-28
lines changed

Diff for: server/postgres/src/storage.ts

+44-28
Original file line numberDiff line numberDiff line change
@@ -1767,26 +1767,49 @@ class PostgresAdapter extends PostgresAdapterBase {
17671767
this._helper.domains = new Set(resultDomains as Domain[])
17681768
}
17691769

1770-
private process (ops: OperationBulk, tx: Tx): void {
1771-
switch (tx._class) {
1772-
case core.class.TxCreateDoc:
1773-
ops.add.push(TxProcessor.createDoc2Doc(tx as TxCreateDoc<Doc>))
1774-
break
1775-
case core.class.TxUpdateDoc:
1776-
ops.updates.push(tx as TxUpdateDoc<Doc>)
1777-
break
1778-
case core.class.TxRemoveDoc:
1779-
ops.removes.push(tx as TxRemoveDoc<Doc>)
1780-
break
1781-
case core.class.TxMixin:
1782-
ops.mixins.push(tx as TxMixin<Doc, Doc>)
1783-
break
1784-
case core.class.TxApplyIf:
1785-
return undefined
1786-
default:
1787-
console.error('Unknown/Unsupported operation:', tx._class, tx)
1788-
break
1770+
private process (txes: Tx[]): OperationBulk {
1771+
const ops: OperationBulk = {
1772+
add: [],
1773+
mixins: [],
1774+
removes: [],
1775+
updates: []
1776+
}
1777+
const updateGroup = new Map<Ref<Doc>, TxUpdateDoc<Doc>>()
1778+
for (const tx of txes) {
1779+
switch (tx._class) {
1780+
case core.class.TxCreateDoc:
1781+
ops.add.push(TxProcessor.createDoc2Doc(tx as TxCreateDoc<Doc>))
1782+
break
1783+
case core.class.TxUpdateDoc: {
1784+
const updateTx = tx as TxUpdateDoc<Doc>
1785+
if (isOperator(updateTx.operations)) {
1786+
ops.updates.push(updateTx)
1787+
} else {
1788+
const current = updateGroup.get(updateTx.objectId)
1789+
if (current !== undefined) {
1790+
current.operations = { ...current.operations, ...updateTx.operations }
1791+
updateGroup.set(updateTx.objectId, current)
1792+
} else {
1793+
updateGroup.set(updateTx.objectId, updateTx)
1794+
}
1795+
}
1796+
break
1797+
}
1798+
case core.class.TxRemoveDoc:
1799+
ops.removes.push(tx as TxRemoveDoc<Doc>)
1800+
break
1801+
case core.class.TxMixin:
1802+
ops.mixins.push(tx as TxMixin<Doc, Doc>)
1803+
break
1804+
case core.class.TxApplyIf:
1805+
break
1806+
default:
1807+
console.error('Unknown/Unsupported operation:', tx._class, tx)
1808+
break
1809+
}
17891810
}
1811+
ops.updates.push(...updateGroup.values())
1812+
return ops
17901813
}
17911814

17921815
private async txMixin (ctx: MeasureContext, tx: TxMixin<Doc, Doc>, schemaFields: SchemaAndFields): Promise<TxResult> {
@@ -1835,15 +1858,8 @@ class PostgresAdapter extends PostgresAdapterBase {
18351858
if (domain === undefined) {
18361859
continue
18371860
}
1838-
const ops: OperationBulk = {
1839-
add: [],
1840-
mixins: [],
1841-
removes: [],
1842-
updates: []
1843-
}
1844-
for (const tx of txs) {
1845-
this.process(ops, tx)
1846-
}
1861+
1862+
const ops = this.process(txs)
18471863

18481864
const domainFields = getSchemaAndFields(domain)
18491865
if (ops.add.length > 0) {

0 commit comments

Comments
 (0)