Skip to content

Commit

Permalink
UBER-1128: Fix to many requests from query (#3888)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
  • Loading branch information
haiodo authored and BykhovDenis committed Oct 26, 2023
1 parent b608471 commit 14d03a0
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions packages/query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ export class LiveQuery extends TxProcessor implements Client {
}

protected async txCollectionCUD (tx: TxCollectionCUD<Doc, AttachedDoc>): Promise<TxResult> {
const docCache = new Map<Ref<Doc>, Doc>()
for (const queries of this.queries) {
const isTx = this.client.getHierarchy().isDerived(queries[0], core.class.Tx)
for (const q of queries[1]) {
Expand All @@ -491,7 +492,7 @@ export class LiveQuery extends TxProcessor implements Client {
}
await this.handleDocAdd(q, TxProcessor.createDoc2Doc(d))
} else if (tx.tx._class === core.class.TxUpdateDoc) {
await this.handleDocUpdate(q, tx.tx as unknown as TxUpdateDoc<Doc>)
await this.handleDocUpdate(q, tx.tx as unknown as TxUpdateDoc<Doc>, docCache)
} else if (tx.tx._class === core.class.TxRemoveDoc) {
await this.handleDocRemove(q, tx.tx as unknown as TxRemoveDoc<Doc>)
}
Expand All @@ -501,6 +502,7 @@ export class LiveQuery extends TxProcessor implements Client {
}

protected async txUpdateDoc (tx: TxUpdateDoc<Doc>): Promise<TxResult> {
const docCache = new Map<Ref<Doc>, Doc>()
for (const queries of this.queries) {
const isTx = this.client.getHierarchy().isDerived(queries[0], core.class.Tx)
for (const q of queries[1]) {
Expand All @@ -509,13 +511,13 @@ export class LiveQuery extends TxProcessor implements Client {
await this.handleDocAdd(q, tx)
continue
}
await this.handleDocUpdate(q, tx)
await this.handleDocUpdate(q, tx, docCache)
}
}
return {}
}

private async handleDocUpdate (q: Query, tx: TxUpdateDoc<Doc>): Promise<void> {
private async handleDocUpdate (q: Query, tx: TxUpdateDoc<Doc>, docCache?: Map<Ref<Doc>, Doc>): Promise<void> {
if (q.result instanceof Promise) {
q.result = await q.result
}
Expand All @@ -539,7 +541,7 @@ export class LiveQuery extends TxProcessor implements Client {
await this.sort(q, tx)
const udoc = q.result.find((p) => p._id === tx.objectId)
await this.updatedDocCallback(udoc, q)
} else if (await this.matchQuery(q, tx)) {
} else if (await this.matchQuery(q, tx, docCache)) {
await this.sort(q, tx)
const udoc = q.result.find((p) => p._id === tx.objectId)
await this.updatedDocCallback(udoc, q)
Expand Down Expand Up @@ -648,7 +650,7 @@ export class LiveQuery extends TxProcessor implements Client {
}

// Check if query is partially matched.
private async matchQuery (q: Query, tx: TxUpdateDoc<Doc>): Promise<boolean> {
private async matchQuery (q: Query, tx: TxUpdateDoc<Doc>, docCache?: Map<Ref<Doc>, Doc>): Promise<boolean> {
if (!this.client.getHierarchy().isDerived(tx.objectClass, q._class)) {
return false
}
Expand Down Expand Up @@ -677,7 +679,11 @@ export class LiveQuery extends TxProcessor implements Client {
}

if (matched) {
const realDoc = await this.client.findOne(q._class, { _id: doc._id }, q.options)
const realDoc = docCache?.get(doc._id) ?? (await this.client.findOne(q._class, { _id: doc._id }, q.options))

if (realDoc != null && docCache != null) {
docCache?.set(realDoc._id, realDoc)
}
if (realDoc === undefined) return false
const res = matchQuery([realDoc], q.query, q._class, this.client.getHierarchy())
if (res.length === 1) {
Expand Down

0 comments on commit 14d03a0

Please sign in to comment.