Exemplo n.º 1
0
        public async Task Should_restore_indices_for_all_non_deleted_schemas()
        {
            var userId1 = "found1";
            var userId2 = "found2";
            var userId3 = "found3";
            var context = CreateRestoreContext();

            await sut.RestoreEventAsync(Envelope.Create(new AppContributorAssigned
            {
                ContributorId = userId1
            }), context);

            await sut.RestoreEventAsync(Envelope.Create(new AppContributorAssigned
            {
                ContributorId = userId2
            }), context);

            await sut.RestoreEventAsync(Envelope.Create(new AppContributorAssigned
            {
                ContributorId = userId3
            }), context);

            await sut.RestoreEventAsync(Envelope.Create(new AppContributorRemoved
            {
                ContributorId = userId3
            }), context);

            HashSet <string>?newIndex = null;

            A.CallTo(() => index.RebuildByContributorsAsync(appId, A <HashSet <string> > .Ignored))
            .Invokes(new Action <Guid, HashSet <string> >((_, i) => newIndex = i));

            await sut.CompleteRestoreAsync(context);

            Assert.Equal(new HashSet <string>
            {
                "found1_mapped",
                "found2_mapped"
            }, newIndex);
        }
Exemplo n.º 2
0
        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);
            }
        }
Exemplo n.º 3
0
        public async Task CompleteRestoreAsync(RestoreContext context)
        {
            await appsIndex.AddAsync(appReservation);

            await appsIndex.RebuildByContributorsAsync(context.AppId, contributors);
        }
Exemplo n.º 4
0
        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);
            }
        }
Exemplo n.º 5
0
        public override async Task CompleteRestoreAsync(Guid appId, BackupReader reader)
        {
            await appsIndex.AddAsync(appReservation);

            await appsIndex.RebuildByContributorsAsync(appId, contributors);
        }