Ejemplo n.º 1
0
        public void SaveChanges()
        {
            var aggregatesAndEvents = from entry in unitOfWork
                                      let aggregateRoot = entry.AggregateRoot
                                                          let eventStream = entry.EventStream
                                                                            from @event in aggregateRoot.GetUncommittedChanges()
                                                                            orderby @event.TimeStamp
                                                                            select
                                                                            new
            {
                EventStream = eventStream,
                Event       = @event
            };
            var currentChangeSequence = new Lazy <int>(GenerateChangeSequence);

            foreach (var aggregatesAndEvent in aggregatesAndEvents)
            {
                IDomainEvent pendingEvent = aggregatesAndEvent.Event;
                EventStream  eventStream  = aggregatesAndEvent.EventStream;
                dynamic      asDynamic    = pendingEvent.AsDynamic();
                asDynamic.SetChangeSequence(currentChangeSequence.Value);
                dispatcher.Dispatch(pendingEvent, eventStream.Id);
                eventStream.History.Add(pendingEvent);
            }

            foreach (AggregateRoot aggregateRoot in unitOfWork.Select(x => x.AggregateRoot))
            {
                aggregateRoot.ClearUncommittedChanges();
            }

            documentSession.SaveChanges();
        }
Ejemplo n.º 2
0
        private static void DoReplayEvents(IWindsorContainer container, IDocumentStore documentStore)
        {
            // wait for indexing to complete
            WaitForIndexing(documentStore);

            // delete all read models
            documentStore.DatabaseCommands.DeleteByIndex("ReadModelIndex", new IndexQuery());

            // load all event streams and dispatch events
            var dispatcher = new EventDispatcher(container);

            int current = 0;

            while (true)
            {
                IDocumentSession session = null;

                try
                {
                    session = (IDocumentSession)container.Resolve(typeof(IDocumentSession));
                    IRavenQueryable <EventsIndex.Result> eventsQuery =
                        session.Query <EventsIndex.Result, EventsIndex>()
                        .Customize(
                            x => x.WaitForNonStaleResultsAsOf(DateTime.Now.AddSeconds(15)))
                        .OrderBy(x => x.ChangeSequence);
                    var results = eventsQuery.Skip(current).Take(128).ToList();
                    if (results.Count == 0)
                    {
                        break;
                    }
                    foreach (EventsIndex.Result result in results)
                    {
                        int changeSequence           = result.ChangeSequence;
                        IEnumerable <string> ids     = result.Id.Select(x => x.Id);
                        EventStream[]        streams = session.Load <EventStream>(ids);

                        var events = from stream in streams
                                     from @event in stream.History
                                     where @event.ChangeSequence == changeSequence
                                     orderby @event.TimeStamp
                                     select new { stream.Id, Event = @event };
                        foreach (var item in events)
                        {
                            dispatcher.Dispatch(item.Event, item.Id);
                        }
                    }

                    session.SaveChanges();
                    current += results.Count;
                }
                finally
                {
                    if (session != null)
                    {
                        container.Release(session);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private static void DoReplayEvents(IServiceLocator locator, IDocumentStore documentStore)
        {
            // wait for indexing to complete
            WaitForIndexing(documentStore);

            // delete all read models
            documentStore.DatabaseCommands.DeleteByIndex("ReadModelIndex", new IndexQuery());

            // load all event streams and dispatch events
            var dispatcher = new EventDispatcher(locator);

            var current = 0;
            while (true)
            {
                IDocumentSession session = null;

                try
                {
                    session = (IDocumentSession)locator.Resolve(typeof(IDocumentSession));
                    var eventsQuery =
                        session.Query<EventsIndex.Result, EventsIndex>()
                               .Customize(
                                   x => x.WaitForNonStaleResultsAsOf(DateTime.Now.AddSeconds(15)))
                               .OrderBy(x => x.ChangeSequence);
                    var results = eventsQuery.Skip(current).Take(128).ToList();
                    if (results.Count == 0) break;
                    foreach (var result in results)
                    {
                        var changeSequence = result.ChangeSequence;
                        var ids = result.Id.Select(x => x.Id);
                        var streams = session.Load<EventStream>(ids);

                        var events = from stream in streams
                                     from @event in stream.History
                                     where @event.ChangeSequence == changeSequence
                                     orderby @event.TimeStamp
                                     select new { stream.Id, Event = @event };
                        foreach (var item in events)
                        {
                            dispatcher.Dispatch(item.Event, item.Id);
                        }
                    }

                    session.SaveChanges();
                    current += results.Count;
                }
                finally
                {
                    if (session != null)
                        locator.Release(session);
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Rebuilds all read models. This is a potentially lengthy operation!
        /// </summary>
        public void RebuildReadModels()
        {
            var documentStore = (IDocumentStore)this.container.Resolve(typeof(IDocumentStore));

            // wait for indexing to complete
            var indexingTask = Task.Factory.StartNew(
                () =>
                    {
                        while (true)
                        {
                            var s = documentStore.DatabaseCommands.GetStatistics().StaleIndexes;
                            if (!s.Contains("ReadModelIndex")) break;
                            Thread.Sleep(500);
                        }
                    });
            indexingTask.Wait(15000);

            // delete all read models
            documentStore.DatabaseCommands.DeleteByIndex("ReadModelIndex", new IndexQuery());

            // load all event streams and dispatch events
            var dispatcher = new EventDispatcher(this.container);
            var current = 0;
            while (true)
            {
                var session = (IDocumentSession)this.container.Resolve(typeof(IDocumentSession));
                try
                {
                    // allow indexing to take its time
                    var q =
                        session.Query<EventStream>()
                               .Customize(x => x.WaitForNonStaleResultsAsOf(DateTime.Now.AddSeconds(15)));

                    var eventStreams = q.Skip(current).Take(128).ToList();
                    if (eventStreams.Count == 0) break;
                    foreach (var eventStream in eventStreams)
                    {
                        foreach (var domainEvent in eventStream.History)
                        {
                            dispatcher.Dispatch(domainEvent, eventStream.Id);
                        }
                    }

                    session.SaveChanges();
                    current += eventStreams.Count;
                }
                finally
                {
                    this.container.Release(session);
                }
            }
        }