Example #1
0
        protected TSnapshot ProcessEvents <TSnapshot>(
            FileSystemConnectionContext connectionContext,
            ISnapshotProcessor <TSnapshot> snapshotProcessor,
            Action <FileSystemConnectionContext> dataInitializer)
            where TSnapshot : Snapshot
        {
            _fileSystem.CreateDirectoryIfItDoesntExist(connectionContext.DataRoot);

            var loadInfos = new Func <List <IFileSystemInfoWrapper> >(() =>
                                                                      _fileSystem.GetFileInfos(connectionContext.DataRoot, "event-*.json")
                                                                      .OrderBy(item => item.CreationTimeUtc)
                                                                      .ToList());

            var infos = loadInfos();

            if (!infos.Any())
            {
                dataInitializer?.Invoke(connectionContext);
                infos = loadInfos();
            }

            var shouldSaveSnapshot = false;
            var snapshot           = LoadSnapshot <TSnapshot>(connectionContext);

            if (snapshot == null)
            {
                // dataInitializer?.Invoke(connectionContext);
                snapshot           = Activator.CreateInstance <TSnapshot>();
                shouldSaveSnapshot = true;
            }

            foreach (var info in infos)
            {
                if (snapshot.LastFileCreationTimeUtc.HasValue && snapshot.LastFileCreationTimeUtc >= info.CreationTimeUtc)
                {
                    continue;
                }

                shouldSaveSnapshot = true;

                var contents = _fileSystem.ReadAllText(info.FullName);
                snapshotProcessor.ProcessEvent(snapshot, contents, info.CreationTimeUtc);
            }

            if (shouldSaveSnapshot)
            {
                Insert(connectionContext, snapshot);
            }

            return(snapshot);
        }
Example #2
0
 /// <summary>
 /// Initializes this object
 /// </summary>
 /// <param name="snapshotProcessor">The snapshot processor</param>
 public AggregateSavedEventHandler(ISnapshotProcessor snapshotProcessor)
 {
     _snapshotProcessor = snapshotProcessor;
 }