private async Task RebuildAppIndexes() { var appsByName = new Dictionary <string, Guid>(); var appsByUser = new Dictionary <string, HashSet <Guid> >(); bool HasApp(NamedId <Guid> appId, bool consistent, out Guid id) { return(appsByName.TryGetValue(appId.Name, out id) && (!consistent || id == appId.Id)); } HashSet <Guid> Index(string contributorId) { return(appsByUser.GetOrAddNew(contributorId)); } void RemoveApp(NamedId <Guid> appId, bool consistent) { if (HasApp(appId, consistent, out var id)) { foreach (var apps in appsByUser.Values) { apps.Remove(id); } appsByName.Remove(appId.Name); } } await eventStore.QueryAsync(storedEvent => { var @event = eventDataFormatter.Parse(storedEvent.Data); switch (@event.Payload) { case AppCreated appCreated: { RemoveApp(appCreated.AppId, false); appsByName[appCreated.Name] = appCreated.AppId.Id; break; } case AppContributorAssigned appContributorAssigned: { if (HasApp(appContributorAssigned.AppId, true, out _)) { Index(appContributorAssigned.ContributorId).Add(appContributorAssigned.AppId.Id); } break; } case AppContributorRemoved contributorRemoved: Index(contributorRemoved.ContributorId).Remove(contributorRemoved.AppId.Id); break; case AppArchived appArchived: RemoveApp(appArchived.AppId, true); break; } return(TaskHelper.Done); }, "^app\\-"); await indexApps.RebuildAsync(appsByName); foreach (var kvp in appsByUser) { await indexApps.RebuildByContributorsAsync(kvp.Key, kvp.Value); } }
private async Task RebuildAppIndexes() { var appsByName = new Dictionary <string, DomainId>(); var appsByUser = new Dictionary <string, HashSet <DomainId> >(); bool HasApp(NamedId <DomainId> appId, bool consistent, out DomainId id) { return(appsByName !.TryGetValue(appId.Name, out id) && (!consistent || id == appId.Id)); } HashSet <DomainId> Index(string contributorId) { return(appsByUser !.GetOrAddNew(contributorId)); } void RemoveApp(NamedId <DomainId> appId, bool consistent) { if (HasApp(appId, consistent, out var id)) { foreach (var apps in appsByUser !.Values) { apps.Remove(id); } appsByName !.Remove(appId.Name); } } await foreach (var storedEvent in eventStore.QueryAllAsync("^app\\-")) { var @event = eventDataFormatter.ParseIfKnown(storedEvent); if (@event != null) { switch (@event.Payload) { case AppCreated created: { RemoveApp(created.AppId, false); appsByName[created.Name] = created.AppId.Id; break; } case AppContributorAssigned contributorAssigned: { if (HasApp(contributorAssigned.AppId, true, out _)) { Index(contributorAssigned.ContributorId).Add(contributorAssigned.AppId.Id); } break; } case AppContributorRemoved contributorRemoved: Index(contributorRemoved.ContributorId).Remove(contributorRemoved.AppId.Id); break; case AppArchived archived: RemoveApp(archived.AppId, true); break; } } } await indexApps.RebuildAsync(appsByName); foreach (var(contributorId, apps) in appsByUser) { await indexApps.RebuildByContributorsAsync(contributorId, apps); } }