From 9e7d0dd1c473d2a9a0d9a87a3d93c44fbc3041f3 Mon Sep 17 00:00:00 2001 From: Andrey Sobolev Date: Mon, 5 Aug 2024 15:25:17 +0700 Subject: [PATCH] Fix Doc index state indexes (#6254) Signed-off-by: Andrey Sobolev --- models/core/src/index.ts | 6 +++-- server/core/src/indexer/utils.ts | 2 +- server/core/src/server/domainHelper.ts | 33 ++++++++++++++++---------- server/tool/src/index.ts | 2 +- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/models/core/src/index.ts b/models/core/src/index.ts index f4a97a5818..e8b21d7c19 100644 --- a/models/core/src/index.ts +++ b/models/core/src/index.ts @@ -283,10 +283,12 @@ export function createModel (builder: Builder): void { builder.createDoc(core.class.DomainIndexConfiguration, core.space.Model, { domain: DOMAIN_DOC_INDEX_STATE, - indexes: [{ keys: { removed: 1 }, filter: { removed: true } }], + indexes: [ + { keys: { removed: 1 }, filter: { removed: true } }, + { keys: { _class: 1 }, filter: { _class: core.class.DocIndexState } } + ], disabled: [ { attachedToClass: 1 }, - { objectClass: 1 }, { stages: 1 }, { generationId: 1 }, { space: 1 }, diff --git a/server/core/src/indexer/utils.ts b/server/core/src/indexer/utils.ts index fee342200c..4fbc8a687b 100644 --- a/server/core/src/indexer/utils.ts +++ b/server/core/src/indexer/utils.ts @@ -93,7 +93,7 @@ export async function loadIndexStageStage ( newValue: any ): Promise<[boolean | string, IndexStageState]> { if (state === undefined) { - ;[state] = await storage.findAll(ctx, core.class.IndexStageState, { stageId }) + ;[state] = await storage.findAll(ctx, core.class.IndexStageState, { stageId }, { limit: 1 }) } const attributes: Record = state?.attributes ?? {} diff --git a/server/core/src/server/domainHelper.ts b/server/core/src/server/domainHelper.ts index b0347f9319..23f6fb3fa5 100644 --- a/server/core/src/server/domainHelper.ts +++ b/server/core/src/server/domainHelper.ts @@ -93,7 +93,7 @@ export class DomainIndexHelperImpl implements DomainHelper { const allIndexes = (await operations.listIndexes(domain)).filter((it) => it.name !== '_id_') ctx.info('check indexes', { domain, has50Documents, documents }) if (has50Documents) { - for (const vv of [...(domainInfo?.values() ?? []), ...(cfg?.indexes ?? [])]) { + async function checkIndex (vv: string | FieldIndexConfig, checkDisabled: boolean): Promise { try { let name: string if (typeof vv === 'string') { @@ -111,19 +111,23 @@ export class DomainIndexHelperImpl implements DomainHelper { } // Check if index is disabled or not - const isDisabled = + let isDisabled = cfg?.disabled?.some((it) => { const _it = typeof it === 'string' ? { [it]: 1 } : it const _vv = typeof vv === 'string' ? { [vv]: 1 } : vv.keys return deepEqual(_it, _vv) }) ?? false + + if (!checkDisabled) { + isDisabled = false + } if (isDisabled) { // skip index since it is disabled - continue + return } if (added.has(name)) { // Index already added - continue + return } added.add(name) @@ -133,20 +137,25 @@ export class DomainIndexHelperImpl implements DomainHelper { } const exists = existingOne !== -1 // Check if index exists - if (!exists) { - if (!isDisabled) { - // Check if not disabled - bb.push(vv) - await operations.createIndex(domain, vv, { - name - }) - } + if (!exists && !isDisabled) { + // Check if not disabled + bb.push(vv) + await operations.createIndex(domain, vv, { + name + }) } } catch (err: any) { Analytics.handleError(err) ctx.error('error: failed to create index', { domain, vv, err }) } } + + for (const vv of [...(domainInfo?.values() ?? [])]) { + await checkIndex(vv, true) + } + for (const vv of [...(cfg?.indexes ?? [])]) { + await checkIndex(vv, false) + } } if (allIndexes.length > 0) { for (const c of allIndexes) { diff --git a/server/tool/src/index.ts b/server/tool/src/index.ts index 36d54b0d79..58c7b4f186 100644 --- a/server/tool/src/index.ts +++ b/server/tool/src/index.ts @@ -385,7 +385,7 @@ export async function upgradeModel ( await tryMigrate(migrateClient, coreId, [ { - state: 'sparse', + state: 'indexes-v2', func: upgradeIndexes } ])