From 657d3435afd2b28662dfd577012030408dba7584 Mon Sep 17 00:00:00 2001 From: Sebastian Stehle Date: Thu, 19 Sep 2019 09:51:32 +0200 Subject: [PATCH] Indexer fixed. --- .../Apps/BackupApps.cs | 1 - .../Api/Controllers/Apps/AppsController.cs | 1 - .../Migrations/PopulateGrainIndexes.cs | 92 ++++++++++++------- 3 files changed, 59 insertions(+), 35 deletions(-) diff --git a/src/Squidex.Domain.Apps.Entities/Apps/BackupApps.cs b/src/Squidex.Domain.Apps.Entities/Apps/BackupApps.cs index 2c7488a7e..a137a0c72 100644 --- a/src/Squidex.Domain.Apps.Entities/Apps/BackupApps.cs +++ b/src/Squidex.Domain.Apps.Entities/Apps/BackupApps.cs @@ -8,7 +8,6 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; -using Orleans; using Squidex.Domain.Apps.Entities.Apps.Indexes; using Squidex.Domain.Apps.Entities.Backup; using Squidex.Domain.Apps.Events; diff --git a/src/Squidex/Areas/Api/Controllers/Apps/AppsController.cs b/src/Squidex/Areas/Api/Controllers/Apps/AppsController.cs index 8a989555e..3a5a92dbf 100644 --- a/src/Squidex/Areas/Api/Controllers/Apps/AppsController.cs +++ b/src/Squidex/Areas/Api/Controllers/Apps/AppsController.cs @@ -16,7 +16,6 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Net.Http.Headers; using NSwag.Annotations; using Squidex.Areas.Api.Controllers.Apps.Models; -using Squidex.Domain.Apps.Core.Apps; using Squidex.Domain.Apps.Entities; using Squidex.Domain.Apps.Entities.Apps; using Squidex.Domain.Apps.Entities.Apps.Commands; diff --git a/tools/Migrate_01/Migrations/PopulateGrainIndexes.cs b/tools/Migrate_01/Migrations/PopulateGrainIndexes.cs index 37aa3cbbc..12317a587 100644 --- a/tools/Migrate_01/Migrations/PopulateGrainIndexes.cs +++ b/tools/Migrate_01/Migrations/PopulateGrainIndexes.cs @@ -9,14 +9,14 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; using Squidex.Domain.Apps.Entities.Apps.Indexes; -using Squidex.Domain.Apps.Entities.Apps.State; using Squidex.Domain.Apps.Entities.Rules.Indexes; -using Squidex.Domain.Apps.Entities.Rules.State; using Squidex.Domain.Apps.Entities.Schemas.Indexes; -using Squidex.Domain.Apps.Entities.Schemas.State; +using Squidex.Domain.Apps.Events.Apps; +using Squidex.Domain.Apps.Events.Rules; +using Squidex.Domain.Apps.Events.Schemas; using Squidex.Infrastructure; +using Squidex.Infrastructure.EventSourcing; using Squidex.Infrastructure.Migrations; -using Squidex.Infrastructure.States; using Squidex.Infrastructure.Tasks; namespace Migrate_01.Migrations @@ -26,29 +26,26 @@ namespace Migrate_01.Migrations private readonly IAppsIndex indexApps; private readonly IRulesIndex indexRules; private readonly ISchemasIndex indexSchemas; - private readonly ISnapshotStore statesForApps; - private readonly ISnapshotStore statesForRules; - private readonly ISnapshotStore statesForSchemas; + private readonly IEventDataFormatter eventDataFormatter; + private readonly IEventStore eventStore; public PopulateGrainIndexes( IAppsIndex indexApps, IRulesIndex indexRules, ISchemasIndex indexSchemas, - ISnapshotStore statesForApps, - ISnapshotStore statesForRules, - ISnapshotStore statesForSchemas) + IEventDataFormatter eventDataFormatter, + IEventStore eventStore) { this.indexApps = indexApps; this.indexRules = indexRules; this.indexSchemas = indexSchemas; - this.statesForApps = statesForApps; - this.statesForRules = statesForRules; - this.statesForSchemas = statesForSchemas; + this.eventDataFormatter = eventDataFormatter; + this.eventStore = eventStore; } - public Task UpdateAsync() + public async Task UpdateAsync() { - return Task.WhenAll( + await Task.WhenAll( RebuildAppIndexes(), RebuildRuleIndexes(), RebuildSchemaIndexes()); @@ -59,20 +56,35 @@ namespace Migrate_01.Migrations var appsByName = new Dictionary(); var appsByUser = new Dictionary>(); - await statesForApps.ReadAllAsync((app, version) => + await eventStore.QueryAsync(storedEvent => { - if (!app.IsArchived) - { - appsByName[app.Name] = app.Id; + var @event = eventDataFormatter.Parse(storedEvent.Data); - foreach (var contributor in app.Contributors.Keys) - { - appsByUser.GetOrAddNew(contributor).Add(app.Id); - } + switch (@event.Payload) + { + case AppCreated appCreated: + appsByName[appCreated.Name] = appCreated.AppId.Id; + break; + case AppContributorAssigned appContributorAssigned: + appsByUser.GetOrAddNew(appContributorAssigned.ContributorId).Add(appContributorAssigned.AppId.Id); + break; + case AppContributorRemoved contributorRemoved: + appsByUser.GetOrAddNew(contributorRemoved.ContributorId).Remove(contributorRemoved.AppId.Id); + break; + case AppArchived appArchived: + { + foreach (var apps in appsByUser.Values) + { + apps.Remove(appArchived.AppId.Id); + } + + appsByName.Remove(appArchived.AppId.Name); + break; + } } return TaskHelper.Done; - }); + }, "^app\\-"); await indexApps.RebuildAsync(appsByName); @@ -86,15 +98,22 @@ namespace Migrate_01.Migrations { var rulesByApp = new Dictionary>(); - await statesForRules.ReadAllAsync((rule, version) => + await eventStore.QueryAsync(storedEvent => { - if (!rule.IsDeleted) + var @event = eventDataFormatter.Parse(storedEvent.Data); + + switch (@event.Payload) { - rulesByApp.GetOrAddNew(rule.AppId.Id).Add(rule.Id); + case RuleCreated ruleCreated: + rulesByApp.GetOrAddNew(ruleCreated.AppId.Id).Add(ruleCreated.RuleId); + break; + case RuleDeleted ruleDeleted: + rulesByApp.GetOrAddNew(ruleDeleted.AppId.Id).Remove(ruleDeleted.RuleId); + break; } return TaskHelper.Done; - }); + }, "^rule\\-"); foreach (var kvp in rulesByApp) { @@ -106,15 +125,22 @@ namespace Migrate_01.Migrations { var schemasByApp = new Dictionary>(); - await statesForSchemas.ReadAllAsync((schema, version) => + await eventStore.QueryAsync(storedEvent => { - if (!schema.IsDeleted) + var @event = eventDataFormatter.Parse(storedEvent.Data); + + switch (@event.Payload) { - schemasByApp.GetOrAddNew(schema.AppId.Id)[schema.SchemaDef.Name] = schema.Id; + case SchemaCreated schemaCreated: + schemasByApp.GetOrAddNew(schemaCreated.AppId.Id)[schemaCreated.SchemaId.Name] = schemaCreated.SchemaId.Id; + break; + case SchemaDeleted schemaDeleted: + schemasByApp.GetOrAddNew(schemaDeleted.AppId.Id).Remove(schemaDeleted.SchemaId.Name); + break; } return TaskHelper.Done; - }); + }, "^schema\\-"); foreach (var kvp in schemasByApp) { @@ -122,4 +148,4 @@ namespace Migrate_01.Migrations } } } -} +} \ No newline at end of file