@open-mercato/core 0.6.8-develop.7012.1.1dbd6f5fbd → 0.6.8-develop.7013.1.48f7adca80
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -157,6 +157,34 @@ async function deleteSearchTokensForRecord(db, params, options) {
|
|
|
157
157
|
const executor = options?.trx ?? db;
|
|
158
158
|
await executor.deleteFrom("search_tokens").where("entity_type", "=", params.entityType).where("entity_id", "=", String(params.recordId)).where(sql`organization_id is not distinct from ${organizationId}`).where(sql`tenant_id is not distinct from ${tenantId}`).execute();
|
|
159
159
|
}
|
|
160
|
+
const SIGNATURE_SEPARATOR = String.fromCharCode(0);
|
|
161
|
+
function tokenSignature(row) {
|
|
162
|
+
return [
|
|
163
|
+
String(row.field ?? ""),
|
|
164
|
+
String(row.token_hash ?? ""),
|
|
165
|
+
row.token == null ? "" : String(row.token)
|
|
166
|
+
].join(SIGNATURE_SEPARATOR);
|
|
167
|
+
}
|
|
168
|
+
function tallyEquals(a, b) {
|
|
169
|
+
const left = a ?? /* @__PURE__ */ new Map();
|
|
170
|
+
const right = b ?? /* @__PURE__ */ new Map();
|
|
171
|
+
if (left.size !== right.size) return false;
|
|
172
|
+
for (const [key, count] of left.entries()) {
|
|
173
|
+
if (right.get(key) !== count) return false;
|
|
174
|
+
}
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
function tallyTokenRows(rows, keyOf) {
|
|
178
|
+
const tallies = /* @__PURE__ */ new Map();
|
|
179
|
+
for (const row of rows) {
|
|
180
|
+
const key = keyOf(row);
|
|
181
|
+
const tally = tallies.get(key) ?? /* @__PURE__ */ new Map();
|
|
182
|
+
const signature = tokenSignature(row);
|
|
183
|
+
tally.set(signature, (tally.get(signature) ?? 0) + 1);
|
|
184
|
+
tallies.set(key, tally);
|
|
185
|
+
}
|
|
186
|
+
return tallies;
|
|
187
|
+
}
|
|
160
188
|
async function replaceSearchTokensForBatch(db, payloads) {
|
|
161
189
|
if (!payloads.length) return;
|
|
162
190
|
const config = resolveSearchConfig();
|
|
@@ -179,12 +207,60 @@ async function replaceSearchTokensForBatch(db, payloads) {
|
|
|
179
207
|
bucket.ids.add(String(payload.recordId));
|
|
180
208
|
scopeBuckets.set(key, bucket);
|
|
181
209
|
}
|
|
210
|
+
const recordKeyOf = (row) => `${scopeKey(row.organization_id ?? null, row.tenant_id ?? null)}|${String(row.entity_id)}`;
|
|
211
|
+
const builtTally = tallyTokenRows(rows, recordKeyOf);
|
|
212
|
+
const changedIdsByBucket = /* @__PURE__ */ new Map();
|
|
213
|
+
for (const [key, bucket] of scopeBuckets.entries()) {
|
|
214
|
+
const ids = Array.from(bucket.ids);
|
|
215
|
+
const builtCountById = /* @__PURE__ */ new Map();
|
|
216
|
+
for (const id of ids) {
|
|
217
|
+
let total = 0;
|
|
218
|
+
const tally = builtTally.get(`${key}|${id}`);
|
|
219
|
+
if (tally) for (const count of tally.values()) total += count;
|
|
220
|
+
builtCountById.set(id, total);
|
|
221
|
+
}
|
|
222
|
+
const storedCounts = await db.selectFrom("search_tokens").select(["entity_id", sql`count(*)`.as("token_count")]).where("entity_type", "=", payloads[0].entityType).where(sql`organization_id is not distinct from ${bucket.organizationId}`).where(sql`tenant_id is not distinct from ${bucket.tenantId}`).where("entity_id", "in", ids).groupBy("entity_id").execute();
|
|
223
|
+
const storedCountById = /* @__PURE__ */ new Map();
|
|
224
|
+
for (const row of storedCounts) {
|
|
225
|
+
storedCountById.set(String(row.entity_id), Number(row.token_count));
|
|
226
|
+
}
|
|
227
|
+
const changed = /* @__PURE__ */ new Set();
|
|
228
|
+
const contentCandidates = ids.filter((id) => {
|
|
229
|
+
const builtCount = builtCountById.get(id) ?? 0;
|
|
230
|
+
if ((storedCountById.get(id) ?? 0) !== builtCount) {
|
|
231
|
+
changed.add(id);
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
return builtCount > 0;
|
|
235
|
+
});
|
|
236
|
+
if (contentCandidates.length) {
|
|
237
|
+
const rowBudget = contentCandidates.reduce((sum, id) => sum + (builtCountById.get(id) ?? 0), 0);
|
|
238
|
+
const stored = await db.selectFrom("search_tokens").select(["entity_id", "field", "token_hash", "token"]).where("entity_type", "=", payloads[0].entityType).where(sql`organization_id is not distinct from ${bucket.organizationId}`).where(sql`tenant_id is not distinct from ${bucket.tenantId}`).where("entity_id", "in", contentCandidates).limit(rowBudget).execute();
|
|
239
|
+
const storedTally = tallyTokenRows(stored, (row) => String(row.entity_id));
|
|
240
|
+
for (const id of contentCandidates) {
|
|
241
|
+
if (!tallyEquals(builtTally.get(`${key}|${id}`), storedTally.get(id))) changed.add(id);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
changedIdsByBucket.set(key, changed);
|
|
245
|
+
}
|
|
246
|
+
const changedRecordKeys = /* @__PURE__ */ new Set();
|
|
247
|
+
for (const [key, changed] of changedIdsByBucket.entries()) {
|
|
248
|
+
for (const id of changed) changedRecordKeys.add(`${key}|${id}`);
|
|
249
|
+
}
|
|
250
|
+
debug("batch.skip", {
|
|
251
|
+
entityType: payloads[0].entityType,
|
|
252
|
+
recordCount: payloads.length,
|
|
253
|
+
changedCount: changedRecordKeys.size
|
|
254
|
+
});
|
|
255
|
+
if (!changedRecordKeys.size) return;
|
|
182
256
|
await db.transaction().execute(async (trx) => {
|
|
183
|
-
for (const [, bucket] of scopeBuckets.entries()) {
|
|
184
|
-
const
|
|
257
|
+
for (const [key, bucket] of scopeBuckets.entries()) {
|
|
258
|
+
const changed = changedIdsByBucket.get(key);
|
|
259
|
+
if (!changed?.size) continue;
|
|
260
|
+
const deleteQuery = trx.deleteFrom("search_tokens").where("entity_type", "=", payloads[0].entityType).where(sql`organization_id is not distinct from ${bucket.organizationId}`).where(sql`tenant_id is not distinct from ${bucket.tenantId}`).where("entity_id", "in", Array.from(changed));
|
|
185
261
|
await deleteQuery.execute();
|
|
186
262
|
}
|
|
187
|
-
const payloadWithTimestamps = rows.map((row) => ({ ...row, created_at: sql`now()` }));
|
|
263
|
+
const payloadWithTimestamps = rows.filter((row) => changedRecordKeys.has(recordKeyOf(row))).map((row) => ({ ...row, created_at: sql`now()` }));
|
|
188
264
|
for (const batch of chunk(payloadWithTimestamps, INSERT_BATCH_SIZE)) {
|
|
189
265
|
await trx.insertInto("search_tokens").values(batch).execute();
|
|
190
266
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/query_index/lib/search-tokens.ts"],
|
|
4
|
-
"sourcesContent": ["import { type Kysely, type Transaction, sql } from 'kysely'\nimport {\n isSearchFieldBlocklisted,\n resolveSearchConfig,\n resolveSearchTokenLimits,\n type SearchConfig,\n} from '@open-mercato/shared/lib/search/config'\nimport { tokenizeText } from '@open-mercato/shared/lib/search/tokenize'\nimport { parseBooleanToken } from '@open-mercato/shared/lib/boolean'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\n\nconst logger = createLogger('query_index').child({ component: 'search-tokens' })\n\nconst INSERT_BATCH_SIZE = 500\n\nfunction chunk<T>(items: T[], size: number): T[][] {\n if (size <= 0) return [items]\n const out: T[][] = []\n for (let i = 0; i < items.length; i += size) out.push(items.slice(i, i + size))\n return out\n}\n\nexport type SearchTokenRow = {\n entity_type: string\n entity_id: string\n organization_id: string | null\n tenant_id: string | null\n field: string\n token_hash: string\n token?: string | null\n}\n\ntype BuildTokenOptions = {\n entityType: string\n recordId: string\n organizationId?: string | null\n tenantId?: string | null\n doc?: Record<string, unknown> | null\n config?: SearchConfig\n}\n\nconst DEFAULT_SCOPE = { organizationId: null, tenantId: null }\ntype EntityFieldPair = [string, string]\ntype SearchTokenExecutor = Kysely<any> | Transaction<any>\n\nexport const isSearchDebugEnabled = (): boolean => {\n return parseBooleanToken(process.env.OM_SEARCH_DEBUG ?? '') === true\n}\n\nconst debug = (event: string, payload: Record<string, unknown>) => {\n if (!isSearchDebugEnabled()) return\n try {\n logger.debug('Search token event', { event, payload })\n } catch {\n // ignore\n }\n}\n\nfunction collectTextValues(value: unknown): string[] {\n if (typeof value === 'string') return [value]\n if (Array.isArray(value)) {\n const out: string[] = []\n for (const entry of value) {\n if (typeof entry === 'string') out.push(entry)\n }\n return out\n }\n return []\n}\n\nfunction shouldIndexField(\n field: string,\n value: unknown,\n config: SearchConfig,\n entityType: string | null,\n): boolean {\n if (typeof value !== 'string' && !Array.isArray(value)) return false\n const lower = field.toLowerCase()\n if (lower === 'id' || lower.endsWith('_id') || lower.endsWith('.id')) return false\n if (lower.endsWith('_at')) return false\n if (['created_at', 'updated_at', 'deleted_at', 'tenant_id', 'organization_id'].includes(lower)) return false\n if (isSearchFieldBlocklisted(field, entityType, config)) return false\n return collectTextValues(value).some((text) => text.length > 0)\n}\n\nexport function buildSearchTokenRows(params: BuildTokenOptions): SearchTokenRow[] {\n const config = params.config ?? resolveSearchConfig()\n if (!config.enabled) return []\n if (!params.doc) return []\n const tokens: SearchTokenRow[] = []\n const capturePairs = isSearchDebugEnabled() && params.entityType === 'customers:customer_deal'\n const debugPairs: Array<{ field: string; hash: string }> = []\n const scope = {\n organizationId: params.organizationId ?? DEFAULT_SCOPE.organizationId,\n tenantId: params.tenantId ?? DEFAULT_SCOPE.tenantId,\n }\n const limits = resolveSearchTokenLimits(config)\n const recordLimit = limits.maxTokensPerRecord > 0 ? limits.maxTokensPerRecord : Number.POSITIVE_INFINITY\n const fieldLimit = limits.maxTokensPerField > 0 ? limits.maxTokensPerField : Number.POSITIVE_INFINITY\n\n for (const [field, rawValue] of Object.entries(params.doc)) {\n if (tokens.length >= recordLimit) break\n if (!shouldIndexField(field, rawValue, config, params.entityType)) continue\n const values = collectTextValues(rawValue)\n const seen = new Set<string>()\n let fieldTokenCount = 0\n for (const text of values) {\n if (tokens.length >= recordLimit || fieldTokenCount >= fieldLimit) break\n const remainingLimit = Math.min(recordLimit - tokens.length, fieldLimit - fieldTokenCount)\n const candidateLimit = fieldTokenCount + remainingLimit\n const tokenConfig = Number.isFinite(candidateLimit)\n ? { ...config, maxTokensPerField: candidateLimit }\n : config\n const { tokens: textTokens, hashes } = tokenizeText(text, tokenConfig)\n for (let i = 0; i < textTokens.length; i += 1) {\n if (tokens.length >= recordLimit || fieldTokenCount >= fieldLimit) break\n const token = textTokens[i]\n const hash = hashes[i]\n const dedupeKey = `${field}|${hash}`\n if (seen.has(dedupeKey)) continue\n seen.add(dedupeKey)\n fieldTokenCount += 1\n debug('token.generated', { entityType: params.entityType, recordId: params.recordId, field, hash })\n tokens.push({\n entity_type: params.entityType,\n entity_id: String(params.recordId),\n organization_id: scope.organizationId,\n tenant_id: scope.tenantId,\n field,\n token_hash: hash,\n token: config.storeRawTokens ? token : null,\n })\n if (capturePairs) {\n debugPairs.push({ field, hash })\n }\n }\n }\n }\n if (capturePairs) {\n debug('deal.tokens', {\n entityType: params.entityType,\n recordId: params.recordId,\n tokenCount: debugPairs.length,\n tokens: debugPairs,\n })\n }\n debug('doc.completed', { entityType: params.entityType, recordId: params.recordId, tokenCount: tokens.length })\n\n return tokens\n}\n\nfunction buildFieldPairs(recordId: string, doc?: Record<string, unknown> | null): EntityFieldPair[] {\n if (!doc) return []\n const pairs: EntityFieldPair[] = []\n const dedupe = new Set<string>()\n for (const field of Object.keys(doc)) {\n const key = `${recordId}|${field}`\n if (dedupe.has(key)) continue\n dedupe.add(key)\n pairs.push([recordId, field])\n }\n return pairs\n}\n\nexport async function replaceSearchTokensForRecord(\n db: Kysely<any>,\n params: BuildTokenOptions,\n options?: { trx?: SearchTokenExecutor },\n): Promise<void> {\n const rows = buildSearchTokenRows(params)\n const config = params.config ?? resolveSearchConfig()\n if (!config.enabled) return\n const organizationId = params.organizationId ?? null\n const tenantId = params.tenantId ?? null\n const fieldPairs = buildFieldPairs(String(params.recordId), params.doc)\n\n const writeTokens = async (executor: SearchTokenExecutor): Promise<void> => {\n let deleteQuery = executor\n .deleteFrom('search_tokens' as any)\n .where('entity_type' as any, '=', params.entityType)\n .where(sql<boolean>`organization_id is not distinct from ${organizationId}`)\n .where(sql<boolean>`tenant_id is not distinct from ${tenantId}`)\n if (fieldPairs.length) {\n deleteQuery = deleteQuery.where((eb: any) => eb.or(\n fieldPairs.map(([rid, field]) => eb.and([\n eb('entity_id' as any, '=', rid),\n eb('field' as any, '=', field),\n ])),\n ))\n } else {\n deleteQuery = deleteQuery.where('entity_id' as any, '=', String(params.recordId))\n }\n await deleteQuery.execute()\n if (!rows.length) return\n const payloads = rows.map((row) => ({ ...row, created_at: sql`now()` }))\n for (const batch of chunk(payloads, INSERT_BATCH_SIZE)) {\n await executor.insertInto('search_tokens' as any).values(batch as any).execute()\n }\n }\n\n if (options?.trx) {\n await writeTokens(options.trx)\n return\n }\n\n await db.transaction().execute(writeTokens)\n}\n\nexport async function deleteSearchTokensForRecord(\n db: Kysely<any>,\n params: { entityType: string; recordId: string; organizationId?: string | null; tenantId?: string | null },\n options?: { trx?: SearchTokenExecutor },\n): Promise<void> {\n const organizationId = params.organizationId ?? null\n const tenantId = params.tenantId ?? null\n const executor = options?.trx ?? db\n await executor\n .deleteFrom('search_tokens' as any)\n .where('entity_type' as any, '=', params.entityType)\n .where('entity_id' as any, '=', String(params.recordId))\n .where(sql<boolean>`organization_id is not distinct from ${organizationId}`)\n .where(sql<boolean>`tenant_id is not distinct from ${tenantId}`)\n .execute()\n}\n\nexport async function replaceSearchTokensForBatch(\n db: Kysely<any>,\n payloads: Array<BuildTokenOptions & { doc: Record<string, unknown> }>\n): Promise<void> {\n if (!payloads.length) return\n const config = resolveSearchConfig()\n if (!config.enabled) return\n\n const rows = payloads.flatMap((payload) => buildSearchTokenRows({ ...payload, config }))\n if (!rows.length) {\n const entityType = payloads[0]?.entityType\n if (!entityType) return\n const ids = payloads.map((p) => String(p.recordId))\n await db\n .deleteFrom('search_tokens' as any)\n .where('entity_type' as any, '=', entityType)\n .where('entity_id' as any, 'in', ids)\n .execute()\n return\n }\n\n const scopeKey = (org: string | null, tenant: string | null) => `${org ?? '__null__'}|${tenant ?? '__null__'}`\n const scopeBuckets = new Map<string, { organizationId: string | null; tenantId: string | null; ids: Set<string> }>()\n\n for (const payload of payloads) {\n const org = payload.organizationId ?? null\n const tenant = payload.tenantId ?? null\n const key = scopeKey(org, tenant)\n const bucket = scopeBuckets.get(key) ?? { organizationId: org, tenantId: tenant, ids: new Set<string>() }\n bucket.ids.add(String(payload.recordId))\n scopeBuckets.set(key, bucket)\n }\n\n await db.transaction().execute(async (trx) => {\n for (const [, bucket] of scopeBuckets.entries()) {\n // Delete by entity_id: a batch replaces all of a record's tokens, and a per-field OR over the\n // whole batch overflows the query compiler's call stack on large batches.\n const deleteQuery = trx\n .deleteFrom('search_tokens' as any)\n .where('entity_type' as any, '=', payloads[0].entityType)\n .where(sql<boolean>`organization_id is not distinct from ${bucket.organizationId}`)\n .where(sql<boolean>`tenant_id is not distinct from ${bucket.tenantId}`)\n .where('entity_id' as any, 'in', Array.from(bucket.ids))\n await deleteQuery.execute()\n }\n const payloadWithTimestamps = rows.map((row) => ({ ...row, created_at: sql`now()` }))\n for (const batch of chunk(payloadWithTimestamps, INSERT_BATCH_SIZE)) {\n await trx.insertInto('search_tokens' as any).values(batch as any).execute()\n }\n })\n}\n"],
|
|
5
|
-
"mappings": "AAAA,SAAwC,WAAW;AACnD;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP,SAAS,oBAAoB;AAC7B,SAAS,yBAAyB;AAClC,SAAS,oBAAoB;AAE7B,MAAM,SAAS,aAAa,aAAa,EAAE,MAAM,EAAE,WAAW,gBAAgB,CAAC;AAE/E,MAAM,oBAAoB;AAE1B,SAAS,MAAS,OAAY,MAAqB;AACjD,MAAI,QAAQ,EAAG,QAAO,CAAC,KAAK;AAC5B,QAAM,MAAa,CAAC;AACpB,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK,KAAM,KAAI,KAAK,MAAM,MAAM,GAAG,IAAI,IAAI,CAAC;AAC9E,SAAO;AACT;AAqBA,MAAM,gBAAgB,EA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,OAAQ;AACtB,QAAM,SAAS,oBAAoB;AACnC,MAAI,CAAC,OAAO,QAAS;AAErB,QAAM,OAAO,SAAS,QAAQ,CAAC,YAAY,qBAAqB,EAAE,GAAG,SAAS,OAAO,CAAC,CAAC;AACvF,MAAI,CAAC,KAAK,QAAQ;AAChB,UAAM,aAAa,SAAS,CAAC,GAAG;AAChC,QAAI,CAAC,WAAY;AACjB,UAAM,MAAM,SAAS,IAAI,CAAC,MAAM,OAAO,EAAE,QAAQ,CAAC;AAClD,UAAM,GACH,WAAW,eAAsB,EACjC,MAAM,eAAsB,KAAK,UAAU,EAC3C,MAAM,aAAoB,MAAM,GAAG,EACnC,QAAQ;AACX;AAAA,EACF;AAEA,QAAM,WAAW,CAAC,KAAoB,WAA0B,GAAG,OAAO,UAAU,IAAI,UAAU,UAAU;AAC5G,QAAM,eAAe,oBAAI,IAA0F;AAEnH,aAAW,WAAW,UAAU;AAC9B,UAAM,MAAM,QAAQ,kBAAkB;AACtC,UAAM,SAAS,QAAQ,YAAY;AACnC,UAAM,MAAM,SAAS,KAAK,MAAM;AAChC,UAAM,SAAS,aAAa,IAAI,GAAG,KAAK,EAAE,gBAAgB,KAAK,UAAU,QAAQ,KAAK,oBAAI,IAAY,EAAE;AACxG,WAAO,IAAI,IAAI,OAAO,QAAQ,QAAQ,CAAC;AACvC,iBAAa,IAAI,KAAK,MAAM;AAAA,EAC9B;AAEA,QAAM,GAAG,YAAY,EAAE,QAAQ,OAAO,QAAQ;
|
|
4
|
+
"sourcesContent": ["import { type Kysely, type Transaction, sql } from 'kysely'\nimport {\n isSearchFieldBlocklisted,\n resolveSearchConfig,\n resolveSearchTokenLimits,\n type SearchConfig,\n} from '@open-mercato/shared/lib/search/config'\nimport { tokenizeText } from '@open-mercato/shared/lib/search/tokenize'\nimport { parseBooleanToken } from '@open-mercato/shared/lib/boolean'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\n\nconst logger = createLogger('query_index').child({ component: 'search-tokens' })\n\nconst INSERT_BATCH_SIZE = 500\n\nfunction chunk<T>(items: T[], size: number): T[][] {\n if (size <= 0) return [items]\n const out: T[][] = []\n for (let i = 0; i < items.length; i += size) out.push(items.slice(i, i + size))\n return out\n}\n\nexport type SearchTokenRow = {\n entity_type: string\n entity_id: string\n organization_id: string | null\n tenant_id: string | null\n field: string\n token_hash: string\n token?: string | null\n}\n\ntype BuildTokenOptions = {\n entityType: string\n recordId: string\n organizationId?: string | null\n tenantId?: string | null\n doc?: Record<string, unknown> | null\n config?: SearchConfig\n}\n\nconst DEFAULT_SCOPE = { organizationId: null, tenantId: null }\ntype EntityFieldPair = [string, string]\ntype SearchTokenExecutor = Kysely<any> | Transaction<any>\n\nexport const isSearchDebugEnabled = (): boolean => {\n return parseBooleanToken(process.env.OM_SEARCH_DEBUG ?? '') === true\n}\n\nconst debug = (event: string, payload: Record<string, unknown>) => {\n if (!isSearchDebugEnabled()) return\n try {\n logger.debug('Search token event', { event, payload })\n } catch {\n // ignore\n }\n}\n\nfunction collectTextValues(value: unknown): string[] {\n if (typeof value === 'string') return [value]\n if (Array.isArray(value)) {\n const out: string[] = []\n for (const entry of value) {\n if (typeof entry === 'string') out.push(entry)\n }\n return out\n }\n return []\n}\n\nfunction shouldIndexField(\n field: string,\n value: unknown,\n config: SearchConfig,\n entityType: string | null,\n): boolean {\n if (typeof value !== 'string' && !Array.isArray(value)) return false\n const lower = field.toLowerCase()\n if (lower === 'id' || lower.endsWith('_id') || lower.endsWith('.id')) return false\n if (lower.endsWith('_at')) return false\n if (['created_at', 'updated_at', 'deleted_at', 'tenant_id', 'organization_id'].includes(lower)) return false\n if (isSearchFieldBlocklisted(field, entityType, config)) return false\n return collectTextValues(value).some((text) => text.length > 0)\n}\n\nexport function buildSearchTokenRows(params: BuildTokenOptions): SearchTokenRow[] {\n const config = params.config ?? resolveSearchConfig()\n if (!config.enabled) return []\n if (!params.doc) return []\n const tokens: SearchTokenRow[] = []\n const capturePairs = isSearchDebugEnabled() && params.entityType === 'customers:customer_deal'\n const debugPairs: Array<{ field: string; hash: string }> = []\n const scope = {\n organizationId: params.organizationId ?? DEFAULT_SCOPE.organizationId,\n tenantId: params.tenantId ?? DEFAULT_SCOPE.tenantId,\n }\n const limits = resolveSearchTokenLimits(config)\n const recordLimit = limits.maxTokensPerRecord > 0 ? limits.maxTokensPerRecord : Number.POSITIVE_INFINITY\n const fieldLimit = limits.maxTokensPerField > 0 ? limits.maxTokensPerField : Number.POSITIVE_INFINITY\n\n for (const [field, rawValue] of Object.entries(params.doc)) {\n if (tokens.length >= recordLimit) break\n if (!shouldIndexField(field, rawValue, config, params.entityType)) continue\n const values = collectTextValues(rawValue)\n const seen = new Set<string>()\n let fieldTokenCount = 0\n for (const text of values) {\n if (tokens.length >= recordLimit || fieldTokenCount >= fieldLimit) break\n const remainingLimit = Math.min(recordLimit - tokens.length, fieldLimit - fieldTokenCount)\n const candidateLimit = fieldTokenCount + remainingLimit\n const tokenConfig = Number.isFinite(candidateLimit)\n ? { ...config, maxTokensPerField: candidateLimit }\n : config\n const { tokens: textTokens, hashes } = tokenizeText(text, tokenConfig)\n for (let i = 0; i < textTokens.length; i += 1) {\n if (tokens.length >= recordLimit || fieldTokenCount >= fieldLimit) break\n const token = textTokens[i]\n const hash = hashes[i]\n const dedupeKey = `${field}|${hash}`\n if (seen.has(dedupeKey)) continue\n seen.add(dedupeKey)\n fieldTokenCount += 1\n debug('token.generated', { entityType: params.entityType, recordId: params.recordId, field, hash })\n tokens.push({\n entity_type: params.entityType,\n entity_id: String(params.recordId),\n organization_id: scope.organizationId,\n tenant_id: scope.tenantId,\n field,\n token_hash: hash,\n token: config.storeRawTokens ? token : null,\n })\n if (capturePairs) {\n debugPairs.push({ field, hash })\n }\n }\n }\n }\n if (capturePairs) {\n debug('deal.tokens', {\n entityType: params.entityType,\n recordId: params.recordId,\n tokenCount: debugPairs.length,\n tokens: debugPairs,\n })\n }\n debug('doc.completed', { entityType: params.entityType, recordId: params.recordId, tokenCount: tokens.length })\n\n return tokens\n}\n\nfunction buildFieldPairs(recordId: string, doc?: Record<string, unknown> | null): EntityFieldPair[] {\n if (!doc) return []\n const pairs: EntityFieldPair[] = []\n const dedupe = new Set<string>()\n for (const field of Object.keys(doc)) {\n const key = `${recordId}|${field}`\n if (dedupe.has(key)) continue\n dedupe.add(key)\n pairs.push([recordId, field])\n }\n return pairs\n}\n\nexport async function replaceSearchTokensForRecord(\n db: Kysely<any>,\n params: BuildTokenOptions,\n options?: { trx?: SearchTokenExecutor },\n): Promise<void> {\n const rows = buildSearchTokenRows(params)\n const config = params.config ?? resolveSearchConfig()\n if (!config.enabled) return\n const organizationId = params.organizationId ?? null\n const tenantId = params.tenantId ?? null\n const fieldPairs = buildFieldPairs(String(params.recordId), params.doc)\n\n const writeTokens = async (executor: SearchTokenExecutor): Promise<void> => {\n let deleteQuery = executor\n .deleteFrom('search_tokens' as any)\n .where('entity_type' as any, '=', params.entityType)\n .where(sql<boolean>`organization_id is not distinct from ${organizationId}`)\n .where(sql<boolean>`tenant_id is not distinct from ${tenantId}`)\n if (fieldPairs.length) {\n deleteQuery = deleteQuery.where((eb: any) => eb.or(\n fieldPairs.map(([rid, field]) => eb.and([\n eb('entity_id' as any, '=', rid),\n eb('field' as any, '=', field),\n ])),\n ))\n } else {\n deleteQuery = deleteQuery.where('entity_id' as any, '=', String(params.recordId))\n }\n await deleteQuery.execute()\n if (!rows.length) return\n const payloads = rows.map((row) => ({ ...row, created_at: sql`now()` }))\n for (const batch of chunk(payloads, INSERT_BATCH_SIZE)) {\n await executor.insertInto('search_tokens' as any).values(batch as any).execute()\n }\n }\n\n if (options?.trx) {\n await writeTokens(options.trx)\n return\n }\n\n await db.transaction().execute(writeTokens)\n}\n\nexport async function deleteSearchTokensForRecord(\n db: Kysely<any>,\n params: { entityType: string; recordId: string; organizationId?: string | null; tenantId?: string | null },\n options?: { trx?: SearchTokenExecutor },\n): Promise<void> {\n const organizationId = params.organizationId ?? null\n const tenantId = params.tenantId ?? null\n const executor = options?.trx ?? db\n await executor\n .deleteFrom('search_tokens' as any)\n .where('entity_type' as any, '=', params.entityType)\n .where('entity_id' as any, '=', String(params.recordId))\n .where(sql<boolean>`organization_id is not distinct from ${organizationId}`)\n .where(sql<boolean>`tenant_id is not distinct from ${tenantId}`)\n .execute()\n}\n\n// NUL, not a printable separator: a field name may itself contain a space, so `a b` + hash `c`\n// would otherwise sign identically to field `a` + hash `b c`.\nconst SIGNATURE_SEPARATOR = String.fromCharCode(0)\n\n// Identifies one token row for comparison. `token` is NULL unless `storeRawTokens` is on, and a\n// stored NULL has to sign the same as the `null` a freshly built row carries \u2014 otherwise every\n// record compares as changed and the skip never fires.\nfunction tokenSignature(row: { field?: unknown; token_hash?: unknown; token?: unknown }): string {\n return [\n String(row.field ?? ''),\n String(row.token_hash ?? ''),\n row.token == null ? '' : String(row.token),\n ].join(SIGNATURE_SEPARATOR)\n}\n\n// Multiplicities, not sets: #4681 reports token rows duplicated by the concurrent-replacement\n// defect, and a set comparison reads such a record as already correct and preserves the duplicates\n// forever. Counting sends it through a full rewrite, which collapses them.\nfunction tallyEquals(a: Map<string, number> | undefined, b: Map<string, number> | undefined): boolean {\n const left = a ?? new Map<string, number>()\n const right = b ?? new Map<string, number>()\n if (left.size !== right.size) return false\n for (const [key, count] of left.entries()) {\n if (right.get(key) !== count) return false\n }\n return true\n}\n\nfunction tallyTokenRows<TRow extends { field?: unknown; token_hash?: unknown; token?: unknown }>(\n rows: Iterable<TRow>,\n keyOf: (row: TRow) => string\n): Map<string, Map<string, number>> {\n const tallies = new Map<string, Map<string, number>>()\n for (const row of rows) {\n const key = keyOf(row)\n const tally = tallies.get(key) ?? new Map<string, number>()\n const signature = tokenSignature(row)\n tally.set(signature, (tally.get(signature) ?? 0) + 1)\n tallies.set(key, tally)\n }\n return tallies\n}\n\nexport async function replaceSearchTokensForBatch(\n db: Kysely<any>,\n payloads: Array<BuildTokenOptions & { doc: Record<string, unknown> }>\n): Promise<void> {\n if (!payloads.length) return\n const config = resolveSearchConfig()\n if (!config.enabled) return\n\n const rows = payloads.flatMap((payload) => buildSearchTokenRows({ ...payload, config }))\n if (!rows.length) {\n const entityType = payloads[0]?.entityType\n if (!entityType) return\n const ids = payloads.map((p) => String(p.recordId))\n await db\n .deleteFrom('search_tokens' as any)\n .where('entity_type' as any, '=', entityType)\n .where('entity_id' as any, 'in', ids)\n .execute()\n return\n }\n\n const scopeKey = (org: string | null, tenant: string | null) => `${org ?? '__null__'}|${tenant ?? '__null__'}`\n const scopeBuckets = new Map<string, { organizationId: string | null; tenantId: string | null; ids: Set<string> }>()\n\n for (const payload of payloads) {\n const org = payload.organizationId ?? null\n const tenant = payload.tenantId ?? null\n const key = scopeKey(org, tenant)\n const bucket = scopeBuckets.get(key) ?? { organizationId: org, tenantId: tenant, ids: new Set<string>() }\n bucket.ids.add(String(payload.recordId))\n scopeBuckets.set(key, bucket)\n }\n\n const recordKeyOf = (row: SearchTokenRow) =>\n `${scopeKey(row.organization_id ?? null, row.tenant_id ?? null)}|${String(row.entity_id)}`\n const builtTally = tallyTokenRows(rows, recordKeyOf)\n\n // Read outside the transaction, deliberately. The comparison decides only whether to skip a\n // rewrite, so a concurrent writer costs us at most a rewrite we declined \u2014 declined because the\n // table already held exactly the rows this call wanted to write. One ordering is worth naming\n // though: if the read matches and a concurrent writer then commits tokens built from a *staler*\n // doc, the unconditional rewrite this call used to perform would have overwritten them by\n // accident. It no longer does, so those stale rows survive until the record's next write. That\n // is a repair we lose, not a guarantee we break.\n const changedIdsByBucket = new Map<string, Set<string>>()\n for (const [key, bucket] of scopeBuckets.entries()) {\n const ids = Array.from(bucket.ids)\n const builtCountById = new Map<string, number>()\n for (const id of ids) {\n let total = 0\n const tally = builtTally.get(`${key}|${id}`)\n if (tally) for (const count of tally.values()) total += count\n builtCountById.set(id, total)\n }\n\n // Count probe first. Its result is one row per record in the batch, so it is bounded by the\n // batch size \u2014 unlike a bare row read, which would be bounded only by how many token rows the\n // table already holds for these ids, a quantity this function does not control and (per #4681)\n // has no reason to trust.\n const storedCounts = await db\n .selectFrom('search_tokens' as any)\n .select(['entity_id' as any, sql<number>`count(*)`.as('token_count') as any])\n .where('entity_type' as any, '=', payloads[0].entityType)\n .where(sql<boolean>`organization_id is not distinct from ${bucket.organizationId}`)\n .where(sql<boolean>`tenant_id is not distinct from ${bucket.tenantId}`)\n .where('entity_id' as any, 'in', ids)\n .groupBy('entity_id' as any)\n .execute()\n const storedCountById = new Map<string, number>()\n for (const row of storedCounts as any[]) {\n storedCountById.set(String(row.entity_id), Number(row.token_count))\n }\n\n const changed = new Set<string>()\n // A record whose stored row count already differs is changed, whatever the rows say \u2014 the\n // duplicate case from #4681 resolves here without ever materializing the duplicated rows.\n const contentCandidates = ids.filter((id) => {\n const builtCount = builtCountById.get(id) ?? 0\n if ((storedCountById.get(id) ?? 0) !== builtCount) {\n changed.add(id)\n return false\n }\n return builtCount > 0\n })\n\n if (contentCandidates.length) {\n const rowBudget = contentCandidates.reduce((sum, id) => sum + (builtCountById.get(id) ?? 0), 0)\n const stored = await db\n .selectFrom('search_tokens' as any)\n .select(['entity_id' as any, 'field' as any, 'token_hash' as any, 'token' as any])\n .where('entity_type' as any, '=', payloads[0].entityType)\n .where(sql<boolean>`organization_id is not distinct from ${bucket.organizationId}`)\n .where(sql<boolean>`tenant_id is not distinct from ${bucket.tenantId}`)\n .where('entity_id' as any, 'in', contentCandidates)\n // Counts already match, so this cannot truncate \u2014 it bounds the damage if a concurrent\n // writer inserts between the probe and this read. A truncated read compares as changed,\n // which costs a rewrite rather than a wrong skip.\n .limit(rowBudget)\n .execute()\n const storedTally = tallyTokenRows(stored as any[], (row) => String(row.entity_id))\n for (const id of contentCandidates) {\n if (!tallyEquals(builtTally.get(`${key}|${id}`), storedTally.get(id))) changed.add(id)\n }\n }\n changedIdsByBucket.set(key, changed)\n }\n\n const changedRecordKeys = new Set<string>()\n for (const [key, changed] of changedIdsByBucket.entries()) {\n for (const id of changed) changedRecordKeys.add(`${key}|${id}`)\n }\n debug('batch.skip', {\n entityType: payloads[0].entityType,\n recordCount: payloads.length,\n changedCount: changedRecordKeys.size,\n })\n if (!changedRecordKeys.size) return\n\n await db.transaction().execute(async (trx) => {\n for (const [key, bucket] of scopeBuckets.entries()) {\n const changed = changedIdsByBucket.get(key)\n if (!changed?.size) continue\n // Delete by entity_id: a batch replaces all of a record's tokens, and a per-field OR over the\n // whole batch overflows the query compiler's call stack on large batches.\n const deleteQuery = trx\n .deleteFrom('search_tokens' as any)\n .where('entity_type' as any, '=', payloads[0].entityType)\n .where(sql<boolean>`organization_id is not distinct from ${bucket.organizationId}`)\n .where(sql<boolean>`tenant_id is not distinct from ${bucket.tenantId}`)\n .where('entity_id' as any, 'in', Array.from(changed))\n await deleteQuery.execute()\n }\n const payloadWithTimestamps = rows\n .filter((row) => changedRecordKeys.has(recordKeyOf(row)))\n .map((row) => ({ ...row, created_at: sql`now()` }))\n for (const batch of chunk(payloadWithTimestamps, INSERT_BATCH_SIZE)) {\n await trx.insertInto('search_tokens' as any).values(batch as any).execute()\n }\n })\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAwC,WAAW;AACnD;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP,SAAS,oBAAoB;AAC7B,SAAS,yBAAyB;AAClC,SAAS,oBAAoB;AAE7B,MAAM,SAAS,aAAa,aAAa,EAAE,MAAM,EAAE,WAAW,gBAAgB,CAAC;AAE/E,MAAM,oBAAoB;AAE1B,SAAS,MAAS,OAAY,MAAqB;AACjD,MAAI,QAAQ,EAAG,QAAO,CAAC,KAAK;AAC5B,QAAM,MAAa,CAAC;AACpB,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK,KAAM,KAAI,KAAK,MAAM,MAAM,GAAG,IAAI,IAAI,CAAC;AAC9E,SAAO;AACT;AAqBA,MAAM,gBAAgB,EAAE,gBAAgB,MAAM,UAAU,KAAK;AAItD,MAAM,uBAAuB,MAAe;AACjD,SAAO,kBAAkB,QAAQ,IAAI,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EAAE;AAAA,EAChE;AACA,QAAM,cAAc;AAAA,IAClB,YAAY,SAAS,CAAC,EAAE;AAAA,IACxB,aAAa,SAAS;AAAA,IACtB,cAAc,kBAAkB;AAAA,EAClC,CAAC;AACD,MAAI,CAAC,kBAAkB,KAAM;AAE7B,QAAM,GAAG,YAAY,EAAE,QAAQ,OAAO,QAAQ;AAC5C,eAAW,CAAC,KAAK,MAAM,KAAK,aAAa,QAAQ,GAAG;AAClD,YAAM,UAAU,mBAAmB,IAAI,GAAG;AAC1C,UAAI,CAAC,SAAS,KAAM;AAGpB,YAAM,cAAc,IACjB,WAAW,eAAsB,EACjC,MAAM,eAAsB,KAAK,SAAS,CAAC,EAAE,UAAU,EACvD,MAAM,2CAAoD,OAAO,cAAc,EAAE,EACjF,MAAM,qCAA8C,OAAO,QAAQ,EAAE,EACrE,MAAM,aAAoB,MAAM,MAAM,KAAK,OAAO,CAAC;AACtD,YAAM,YAAY,QAAQ;AAAA,IAC5B;AACA,UAAM,wBAAwB,KAC3B,OAAO,CAAC,QAAQ,kBAAkB,IAAI,YAAY,GAAG,CAAC,CAAC,EACvD,IAAI,CAAC,SAAS,EAAE,GAAG,KAAK,YAAY,WAAW,EAAE;AACpD,eAAW,SAAS,MAAM,uBAAuB,iBAAiB,GAAG;AACnE,YAAM,IAAI,WAAW,eAAsB,EAAE,OAAO,KAAY,EAAE,QAAQ;AAAA,IAC5E;AAAA,EACF,CAAC;AACH;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mercato/core",
|
|
3
|
-
"version": "0.6.8-develop.
|
|
3
|
+
"version": "0.6.8-develop.7013.1.48f7adca80",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -254,16 +254,16 @@
|
|
|
254
254
|
"zod": "^4.4.3"
|
|
255
255
|
},
|
|
256
256
|
"peerDependencies": {
|
|
257
|
-
"@open-mercato/ai-assistant": "0.6.8-develop.
|
|
258
|
-
"@open-mercato/shared": "0.6.8-develop.
|
|
259
|
-
"@open-mercato/ui": "0.6.8-develop.
|
|
257
|
+
"@open-mercato/ai-assistant": "0.6.8-develop.7013.1.48f7adca80",
|
|
258
|
+
"@open-mercato/shared": "0.6.8-develop.7013.1.48f7adca80",
|
|
259
|
+
"@open-mercato/ui": "0.6.8-develop.7013.1.48f7adca80",
|
|
260
260
|
"react": "^19.0.0",
|
|
261
261
|
"react-dom": "^19.0.0"
|
|
262
262
|
},
|
|
263
263
|
"devDependencies": {
|
|
264
|
-
"@open-mercato/ai-assistant": "0.6.8-develop.
|
|
265
|
-
"@open-mercato/shared": "0.6.8-develop.
|
|
266
|
-
"@open-mercato/ui": "0.6.8-develop.
|
|
264
|
+
"@open-mercato/ai-assistant": "0.6.8-develop.7013.1.48f7adca80",
|
|
265
|
+
"@open-mercato/shared": "0.6.8-develop.7013.1.48f7adca80",
|
|
266
|
+
"@open-mercato/ui": "0.6.8-develop.7013.1.48f7adca80",
|
|
267
267
|
"@testing-library/dom": "^10.4.1",
|
|
268
268
|
"@testing-library/jest-dom": "^7.0.0",
|
|
269
269
|
"@testing-library/react": "^16.3.1",
|
|
@@ -223,6 +223,49 @@ export async function deleteSearchTokensForRecord(
|
|
|
223
223
|
.execute()
|
|
224
224
|
}
|
|
225
225
|
|
|
226
|
+
// NUL, not a printable separator: a field name may itself contain a space, so `a b` + hash `c`
|
|
227
|
+
// would otherwise sign identically to field `a` + hash `b c`.
|
|
228
|
+
const SIGNATURE_SEPARATOR = String.fromCharCode(0)
|
|
229
|
+
|
|
230
|
+
// Identifies one token row for comparison. `token` is NULL unless `storeRawTokens` is on, and a
|
|
231
|
+
// stored NULL has to sign the same as the `null` a freshly built row carries — otherwise every
|
|
232
|
+
// record compares as changed and the skip never fires.
|
|
233
|
+
function tokenSignature(row: { field?: unknown; token_hash?: unknown; token?: unknown }): string {
|
|
234
|
+
return [
|
|
235
|
+
String(row.field ?? ''),
|
|
236
|
+
String(row.token_hash ?? ''),
|
|
237
|
+
row.token == null ? '' : String(row.token),
|
|
238
|
+
].join(SIGNATURE_SEPARATOR)
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Multiplicities, not sets: #4681 reports token rows duplicated by the concurrent-replacement
|
|
242
|
+
// defect, and a set comparison reads such a record as already correct and preserves the duplicates
|
|
243
|
+
// forever. Counting sends it through a full rewrite, which collapses them.
|
|
244
|
+
function tallyEquals(a: Map<string, number> | undefined, b: Map<string, number> | undefined): boolean {
|
|
245
|
+
const left = a ?? new Map<string, number>()
|
|
246
|
+
const right = b ?? new Map<string, number>()
|
|
247
|
+
if (left.size !== right.size) return false
|
|
248
|
+
for (const [key, count] of left.entries()) {
|
|
249
|
+
if (right.get(key) !== count) return false
|
|
250
|
+
}
|
|
251
|
+
return true
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function tallyTokenRows<TRow extends { field?: unknown; token_hash?: unknown; token?: unknown }>(
|
|
255
|
+
rows: Iterable<TRow>,
|
|
256
|
+
keyOf: (row: TRow) => string
|
|
257
|
+
): Map<string, Map<string, number>> {
|
|
258
|
+
const tallies = new Map<string, Map<string, number>>()
|
|
259
|
+
for (const row of rows) {
|
|
260
|
+
const key = keyOf(row)
|
|
261
|
+
const tally = tallies.get(key) ?? new Map<string, number>()
|
|
262
|
+
const signature = tokenSignature(row)
|
|
263
|
+
tally.set(signature, (tally.get(signature) ?? 0) + 1)
|
|
264
|
+
tallies.set(key, tally)
|
|
265
|
+
}
|
|
266
|
+
return tallies
|
|
267
|
+
}
|
|
268
|
+
|
|
226
269
|
export async function replaceSearchTokensForBatch(
|
|
227
270
|
db: Kysely<any>,
|
|
228
271
|
payloads: Array<BuildTokenOptions & { doc: Record<string, unknown> }>
|
|
@@ -256,8 +299,95 @@ export async function replaceSearchTokensForBatch(
|
|
|
256
299
|
scopeBuckets.set(key, bucket)
|
|
257
300
|
}
|
|
258
301
|
|
|
302
|
+
const recordKeyOf = (row: SearchTokenRow) =>
|
|
303
|
+
`${scopeKey(row.organization_id ?? null, row.tenant_id ?? null)}|${String(row.entity_id)}`
|
|
304
|
+
const builtTally = tallyTokenRows(rows, recordKeyOf)
|
|
305
|
+
|
|
306
|
+
// Read outside the transaction, deliberately. The comparison decides only whether to skip a
|
|
307
|
+
// rewrite, so a concurrent writer costs us at most a rewrite we declined — declined because the
|
|
308
|
+
// table already held exactly the rows this call wanted to write. One ordering is worth naming
|
|
309
|
+
// though: if the read matches and a concurrent writer then commits tokens built from a *staler*
|
|
310
|
+
// doc, the unconditional rewrite this call used to perform would have overwritten them by
|
|
311
|
+
// accident. It no longer does, so those stale rows survive until the record's next write. That
|
|
312
|
+
// is a repair we lose, not a guarantee we break.
|
|
313
|
+
const changedIdsByBucket = new Map<string, Set<string>>()
|
|
314
|
+
for (const [key, bucket] of scopeBuckets.entries()) {
|
|
315
|
+
const ids = Array.from(bucket.ids)
|
|
316
|
+
const builtCountById = new Map<string, number>()
|
|
317
|
+
for (const id of ids) {
|
|
318
|
+
let total = 0
|
|
319
|
+
const tally = builtTally.get(`${key}|${id}`)
|
|
320
|
+
if (tally) for (const count of tally.values()) total += count
|
|
321
|
+
builtCountById.set(id, total)
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// Count probe first. Its result is one row per record in the batch, so it is bounded by the
|
|
325
|
+
// batch size — unlike a bare row read, which would be bounded only by how many token rows the
|
|
326
|
+
// table already holds for these ids, a quantity this function does not control and (per #4681)
|
|
327
|
+
// has no reason to trust.
|
|
328
|
+
const storedCounts = await db
|
|
329
|
+
.selectFrom('search_tokens' as any)
|
|
330
|
+
.select(['entity_id' as any, sql<number>`count(*)`.as('token_count') as any])
|
|
331
|
+
.where('entity_type' as any, '=', payloads[0].entityType)
|
|
332
|
+
.where(sql<boolean>`organization_id is not distinct from ${bucket.organizationId}`)
|
|
333
|
+
.where(sql<boolean>`tenant_id is not distinct from ${bucket.tenantId}`)
|
|
334
|
+
.where('entity_id' as any, 'in', ids)
|
|
335
|
+
.groupBy('entity_id' as any)
|
|
336
|
+
.execute()
|
|
337
|
+
const storedCountById = new Map<string, number>()
|
|
338
|
+
for (const row of storedCounts as any[]) {
|
|
339
|
+
storedCountById.set(String(row.entity_id), Number(row.token_count))
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const changed = new Set<string>()
|
|
343
|
+
// A record whose stored row count already differs is changed, whatever the rows say — the
|
|
344
|
+
// duplicate case from #4681 resolves here without ever materializing the duplicated rows.
|
|
345
|
+
const contentCandidates = ids.filter((id) => {
|
|
346
|
+
const builtCount = builtCountById.get(id) ?? 0
|
|
347
|
+
if ((storedCountById.get(id) ?? 0) !== builtCount) {
|
|
348
|
+
changed.add(id)
|
|
349
|
+
return false
|
|
350
|
+
}
|
|
351
|
+
return builtCount > 0
|
|
352
|
+
})
|
|
353
|
+
|
|
354
|
+
if (contentCandidates.length) {
|
|
355
|
+
const rowBudget = contentCandidates.reduce((sum, id) => sum + (builtCountById.get(id) ?? 0), 0)
|
|
356
|
+
const stored = await db
|
|
357
|
+
.selectFrom('search_tokens' as any)
|
|
358
|
+
.select(['entity_id' as any, 'field' as any, 'token_hash' as any, 'token' as any])
|
|
359
|
+
.where('entity_type' as any, '=', payloads[0].entityType)
|
|
360
|
+
.where(sql<boolean>`organization_id is not distinct from ${bucket.organizationId}`)
|
|
361
|
+
.where(sql<boolean>`tenant_id is not distinct from ${bucket.tenantId}`)
|
|
362
|
+
.where('entity_id' as any, 'in', contentCandidates)
|
|
363
|
+
// Counts already match, so this cannot truncate — it bounds the damage if a concurrent
|
|
364
|
+
// writer inserts between the probe and this read. A truncated read compares as changed,
|
|
365
|
+
// which costs a rewrite rather than a wrong skip.
|
|
366
|
+
.limit(rowBudget)
|
|
367
|
+
.execute()
|
|
368
|
+
const storedTally = tallyTokenRows(stored as any[], (row) => String(row.entity_id))
|
|
369
|
+
for (const id of contentCandidates) {
|
|
370
|
+
if (!tallyEquals(builtTally.get(`${key}|${id}`), storedTally.get(id))) changed.add(id)
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
changedIdsByBucket.set(key, changed)
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const changedRecordKeys = new Set<string>()
|
|
377
|
+
for (const [key, changed] of changedIdsByBucket.entries()) {
|
|
378
|
+
for (const id of changed) changedRecordKeys.add(`${key}|${id}`)
|
|
379
|
+
}
|
|
380
|
+
debug('batch.skip', {
|
|
381
|
+
entityType: payloads[0].entityType,
|
|
382
|
+
recordCount: payloads.length,
|
|
383
|
+
changedCount: changedRecordKeys.size,
|
|
384
|
+
})
|
|
385
|
+
if (!changedRecordKeys.size) return
|
|
386
|
+
|
|
259
387
|
await db.transaction().execute(async (trx) => {
|
|
260
|
-
for (const [, bucket] of scopeBuckets.entries()) {
|
|
388
|
+
for (const [key, bucket] of scopeBuckets.entries()) {
|
|
389
|
+
const changed = changedIdsByBucket.get(key)
|
|
390
|
+
if (!changed?.size) continue
|
|
261
391
|
// Delete by entity_id: a batch replaces all of a record's tokens, and a per-field OR over the
|
|
262
392
|
// whole batch overflows the query compiler's call stack on large batches.
|
|
263
393
|
const deleteQuery = trx
|
|
@@ -265,10 +395,12 @@ export async function replaceSearchTokensForBatch(
|
|
|
265
395
|
.where('entity_type' as any, '=', payloads[0].entityType)
|
|
266
396
|
.where(sql<boolean>`organization_id is not distinct from ${bucket.organizationId}`)
|
|
267
397
|
.where(sql<boolean>`tenant_id is not distinct from ${bucket.tenantId}`)
|
|
268
|
-
.where('entity_id' as any, 'in', Array.from(
|
|
398
|
+
.where('entity_id' as any, 'in', Array.from(changed))
|
|
269
399
|
await deleteQuery.execute()
|
|
270
400
|
}
|
|
271
|
-
const payloadWithTimestamps = rows
|
|
401
|
+
const payloadWithTimestamps = rows
|
|
402
|
+
.filter((row) => changedRecordKeys.has(recordKeyOf(row)))
|
|
403
|
+
.map((row) => ({ ...row, created_at: sql`now()` }))
|
|
272
404
|
for (const batch of chunk(payloadWithTimestamps, INSERT_BATCH_SIZE)) {
|
|
273
405
|
await trx.insertInto('search_tokens' as any).values(batch as any).execute()
|
|
274
406
|
}
|