public void Save(Checkpoint checkpoint)
 {
     collection.Update(
         Query<ProjectionCheckpointDto>
             .EQ(c => c.Id, checkpoint.Mode),
         Update<ProjectionCheckpointDto>
             .Set(c => c.CommitIdProcessed, checkpoint.CommitIdProcessed)
             .Set(c => c.CommitStampProcessed, checkpoint.CommitStampProcessed));
 }
 public void Save(Checkpoint checkpoint)
 {
     using (var con = db.OpenDbConnection())
     {
         con.Update(new ProjectionCheckpointDto
             {
                 Mode = checkpoint.Mode,
                 CommitIdProcessed = checkpoint.CommitIdProcessed,
                 CommitStampProcessed = checkpoint.CommitStampProcessed
             });
     }
 }
 public void Save(Checkpoint checkpoint)
 {
     using (var con = db.OpenDbConnection())
     {
         using (IDbCommand cmd = con.CreateCommand())
         {
             cmd.CommandText = "UPDATE ProjectionCheckpointDto SET CommitIdProcessed = @CommitId, CommitStampProcessed = @CommitStamp WHERE Mode = @Mode";
             AddParam(cmd, "@CommitId", DbType.Guid, checkpoint.CommitIdProcessed);
             AddParam(cmd, "@CommitStamp", DbType.DateTime, checkpoint.CommitStampProcessed);
             AddParam(cmd, "@Mode", DbType.String, checkpoint.Mode);
             cmd.ExecuteNonQuery();
         }
     }
 }
 public CheckpointStoreMock MockLoad(string mode, Checkpoint result)
 {
     checkpoints[mode] = result;
     return this;
 }
 public void Save(Checkpoint checkpoint)
 {
     checkpoints[checkpoint.Mode] = checkpoint;
 }
        private void Rebuild(Checkpoint from, Checkpoint to, IEnumerable<IProjection> rawProjections)
        {
            var projections = rawProjections.Select(e => new ProjectionWrapper(e, log)).ToArray();
            if (!projections.Any())
            {
                return;
            }

            if (from.IsUndefined)
            {
                foreach (var projection in projections)
                    projection.Clear();
            }

            DateTime start;
            if (from.IsUndefined)
            {
                start = new DateTime(2013, 1, 1);
            }
            else
            {
                start = from.CommitStampProcessed.Value.AddSeconds(-2);
            }

            foreach (var projection in projections)
                projection.Begin();

            var commits = historyReader.Read(start, DateTime.UtcNow);
            commits = FilterByCheckpoints(commits, from, to);
            commits = PauseAware(commits, from);
            commits = ShowLogs(commits, from, to, projections);

            foreach (var commit in commits)
                RebuildCommit(commit, projections);

            foreach (var projection in projections)
                projection.Flush();
        }
        private IEnumerable<Commit> ShowLogs(IEnumerable<Commit> commits, Checkpoint from, Checkpoint to, ProjectionWrapper[] projections)
        {
            bool first = true;
            ICommit commitProcessed = null;
            long noOfCommits = 0;
            Stopwatch timer = new Stopwatch();
            timer.Start();
            foreach (var commit in commits)
            {
                noOfCommits++;
                if (first)
                {
                    if (from.IsUndefined)
                        log.Info("Rebuild started from the beginning");
                    else
                        log.Info(string.Format("Rebuild started from {0} due to checkpoint {1}", commit.CommitId, from));
                    first = false;
                }

                yield return commit;

                commitProcessed = commit;
                if (noOfCommits % 1000 == 0)
                    log.Info(string.Format("{0} commits were processed {1}", noOfCommits, timer.Elapsed));
                if (noOfCommits % 10000 == 0)
                    log.Debug("Projection statistics" + Environment.NewLine + ProjectionWrapper.GetStatistic(projections, take: 3));
            }
            if (commitProcessed != null)
            {
                log.Info(string.Format("{0} commits were processed {1:mm\\:ss\\.ff}", noOfCommits, timer.Elapsed));
                if (to.IsUndefined)
                    log.Info(string.Format("Rebuild finished on the end {0}", commitProcessed.CommitId));
                else
                    log.Info(string.Format("Rebuild finished on checkpoint {0}", to));
                log.Info("Projection statistics" + Environment.NewLine + ProjectionWrapper.GetStatistic(projections, take: 20, takeEvents: 5));
            }
        }
        private IEnumerable<Commit> PauseAware(IEnumerable<Commit> commits, Checkpoint from)
        {
            ICommit commitProcessed = null;
            foreach (var commit in commits)
            {
                if (commit.Headers.ContainsKey("SagaType"))
                    continue;

                yield return commit;

                commitProcessed = commit;
                if (running.IsCancellationRequested)
                    break;
            }

            if (commitProcessed != null)
            {
                from.Set(commitProcessed);
                if (running.IsCancellationRequested == false && from.Mode == Checkpoint.PROJECTION_CHANGE)
                {
                    from.Set(null);
                }
                checkpoints.Save(from);
            }
        }
 private IEnumerable<Commit> FilterByCheckpoints(IEnumerable<Commit> commits, Checkpoint from, Checkpoint to)
 {
     bool startFound = from.IsUndefined;
     foreach (var commit in commits)
     {
         if (!startFound)
         {
             if (from.IsProcessed(commit))
             {
                 startFound = true;
             }
             continue;
         }
         yield return commit;
         if (!to.IsUndefined && to.IsProcessed(commit))
             yield break;
     }
     if (!startFound)
         throw new Exception(string.Format("Checkpoint {0} {1} can not be found. At the end", from.CommitIdProcessed, from.CommitStampProcessed));
 }