Skip to content

Commit 4c738f5

Browse files
Copilothotlong
andcommitted
perf: optimize bulkUpdate to use pipeline for GET operations
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent f5e8b5a commit 4c738f5

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

packages/drivers/redis/src/index.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -547,17 +547,27 @@ export class RedisDriver implements Driver, DriverInterface {
547547
if (!command.updates || !Array.isArray(command.updates)) {
548548
throw new Error('BulkUpdate command requires updates array');
549549
}
550-
// Use Redis PIPELINE for batch operations
551-
const updatePipeline = this.client.multi();
552-
const updateResults: any[] = [];
553-
const updateTime = new Date().toISOString();
554550

551+
// First, batch GET all existing records using PIPELINE
552+
const getPipeline = this.client.multi();
555553
for (const update of command.updates) {
556554
const key = this.generateRedisKey(command.object, update.id);
557-
const existing = await this.client.get(key);
555+
getPipeline.get(key);
556+
}
557+
const getResults = await getPipeline.exec();
558+
559+
// Then, batch SET updated records using PIPELINE
560+
const setPipeline = this.client.multi();
561+
const updateResults: any[] = [];
562+
const updateTime = new Date().toISOString();
563+
564+
for (let i = 0; i < command.updates.length; i++) {
565+
const update = command.updates[i];
566+
const result = getResults?.[i] as any;
567+
const existingData = result?.[1];
558568

559-
if (existing) {
560-
const existingDoc = JSON.parse(existing);
569+
if (existingData && typeof existingData === 'string') {
570+
const existingDoc = JSON.parse(existingData);
561571
const doc = {
562572
...existingDoc,
563573
...update.data,
@@ -566,11 +576,12 @@ export class RedisDriver implements Driver, DriverInterface {
566576
updated_at: updateTime
567577
};
568578
updateResults.push(doc);
569-
updatePipeline.set(key, JSON.stringify(doc));
579+
const key = this.generateRedisKey(command.object, update.id);
580+
setPipeline.set(key, JSON.stringify(doc));
570581
}
571582
}
572583

573-
await updatePipeline.exec();
584+
await setPipeline.exec();
574585

575586
return {
576587
success: true,
@@ -646,6 +657,9 @@ export class RedisDriver implements Driver, DriverInterface {
646657
switch (node.type) {
647658
case 'comparison':
648659
// Convert comparison node to [field, operator, value] format
660+
if (!node.operator) {
661+
console.warn('[RedisDriver] FilterNode comparison missing operator, defaulting to "="');
662+
}
649663
const operator = node.operator || '=';
650664
return [[node.field, operator, node.value]];
651665

0 commit comments

Comments
 (0)