private void CompleteScavengeWithStats(IncompleteScavengeStats incompleteScavengeStats)
        {
            var log = CreateLogInternal(incompleteScavengeStats.ScavengeId);

            log.ScavengeCompleted(ScavengeResult.Failed,
                                  "The node was restarted.",
                                  incompleteScavengeStats.TimeTaken,
                                  incompleteScavengeStats.SpaceSaved,
                                  incompleteScavengeStats.MaxChunkScavenged);
        }
        private void GatherIncompleteScavengeStats(long from, IncompleteScavengeStats incompleteScavengeStats)
        {
            _ioDispatcher.ReadBackward(incompleteScavengeStats.ScavengeStream, from, 50, true, SystemAccounts.System,
                                       readResult => {
                if (readResult.Result != ReadStreamResult.Success &&
                    readResult.Result != ReadStreamResult.NoStream)
                {
                    Log.Debug("Unable to read {stream} for scavenge log clean up. Result: {result}",
                              incompleteScavengeStats.ScavengeStream, readResult.Result);
                    return;
                }

                foreach (var ev in readResult.Events)
                {
                    if (ev.ResolveResult == ReadEventResult.Success)
                    {
                        var dictionary = ev.Event.Data.ParseJson <Dictionary <string, object> >();

                        if (ev.Event.EventType == SystemEventTypes.ScavengeChunksCompleted ||
                            ev.Event.EventType == SystemEventTypes.ScavengeMergeCompleted ||
                            ev.Event.EventType == SystemEventTypes.ScavengeIndexCompleted)
                        {
                            if (dictionary.TryGetValue("spaceSaved", out var spaceSavedEntry))
                            {
                                incompleteScavengeStats.SpaceSaved += (int)spaceSavedEntry;
                            }
                            if (dictionary.TryGetValue("timeTaken", out var timeTakenEntry))
                            {
                                incompleteScavengeStats.TimeTaken += TimeSpan.Parse(timeTakenEntry.ToString());
                            }

                            if (ev.Event.EventType == SystemEventTypes.ScavengeChunksCompleted)
                            {
                                if (dictionary.TryGetValue("chunkEndNumber", out var chunkEndNumberEntry))
                                {
                                    incompleteScavengeStats.MaxChunkScavenged = Math.Max(incompleteScavengeStats.MaxChunkScavenged, (int)chunkEndNumberEntry);
                                }
                            }
                        }
                    }
                }

                if (readResult.IsEndOfStream || readResult.Events.Length == 0)
                {
                    CompleteScavengeWithStats(incompleteScavengeStats);
                }
                else
                {
                    GatherIncompleteScavengeStats(readResult.NextEventNumber, incompleteScavengeStats);
                }
            });
        }