From 983f1a8b5a3e6b68626fe1a17488323fc3b8ce87 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 2 May 2026 12:06:08 +0000 Subject: [PATCH 1/2] perf(search): optimize entity upsert by using direct ID lookup Replaced inefficient in-memory filtering of all entities with a targeted database query in `upsertToSearchIndex`. This improves performance from O(N) to O(1) for incremental search index updates. Co-authored-by: d-oit <6849456+d-oit@users.noreply.github.com> --- src/db/repository.ts | 16 ++++++++++++++++ src/lib/search.ts | 3 +-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/db/repository.ts b/src/db/repository.ts index 41eb348..850e135 100644 --- a/src/db/repository.ts +++ b/src/db/repository.ts @@ -47,6 +47,22 @@ export class Repository { } } + async getEntityById(id: string): Promise { + try { + const results = await this.db.exec({ + sql: `SELECT * FROM entities WHERE id = ?`, + bind: [id], + returnValue: 'resultRows', + rowMode: 'object', + }) as Record[]; + if (results.length === 0) return null; + return this.parseMetadata(results[0]); + } catch (err) { + logger.error('Failed to fetch entity by id', err); + throw new AppError('Failed to fetch entity by id', 'DB_ERROR', err); + } + } + async getEntityByName(name: string): Promise { try { const results = await this.db.exec({ diff --git a/src/lib/search.ts b/src/lib/search.ts index a67469f..c82ad4a 100644 --- a/src/lib/search.ts +++ b/src/lib/search.ts @@ -131,8 +131,7 @@ export const upsertToSearchIndex = async (entityId: string) => { try { await removeFromSearchIndex(entityId); - const entities = await repository.getAllEntities(); - const entity = entities.find(e => e.id === entityId); + const entity = await repository.getEntityById(entityId); if (entity) { const claims = await repository.getClaimsByEntityId(entity.id!); From 74dfa6e517b15e2b830185087e9e32f91ca192f5 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 2 May 2026 16:24:18 +0000 Subject: [PATCH 2/2] perf(search): optimize entity upsert by using direct ID lookup - Implemented getEntityById in Repository for O(1) database retrieval - Replaced O(N) in-memory filtering with targeted DB query in upsertToSearchIndex - Reduces I/O and memory overhead for incremental search index updates Repair Agent: Verified all local quality gates (lint, typecheck, test). Co-authored-by: d-oit <6849456+d-oit@users.noreply.github.com>