Example #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EsRepository{I}"/> class.
 /// </summary>
 /// <param name="eventStore">Event store</param>
 /// <param name="streams">Stream locator</param>
 /// <param name="timeline">Active timeline tracker</param>
 /// <param name="bus">Message bus</param>
 /// <param name="registry">Event sourced registry</param>
 /// <param name="log">Log service</param>
 public EsRepository(IEventStore <TEventSourced> eventStore, IStreamLocator streams, ITimeline timeline, IBus bus, IEsRegistry registry, ILog log)
 {
     _eventStore = eventStore;
     _streams    = streams;
     _timeline   = timeline;
     _bus        = bus;
     _registry   = registry;
     _log        = log;
 }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HistoricalProjection{TState}"/> class.
        /// </summary>
        /// <param name="eventStore">Aggregate event store</param>
        /// <param name="log">Application log</param>
        /// <param name="iProjection">Original projection</param>
        /// <param name="activeTimeline">Active branch</param>
        /// <param name="streamLocator">Stream locator</param>
        public HistoricalProjection(
            IEventStore <IAggregate> eventStore,
            ILog log,
            IProjection <TState> iProjection,
            ITimeline activeTimeline,
            IStreamLocator streamLocator)
            : base(eventStore, log, activeTimeline, streamLocator)
        {
            var projection = (ProjectionBase <TState>)iProjection;

            Predicate = projection.Predicate;
            foreach (var h in projection.Handlers)
            {
                Register(h.Key, (e, s) => h.Value(e, s));
            }
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ProjectionBase{TState}"/> class.
        /// </summary>
        /// <param name="eventStore">Event store service</param>
        /// <param name="log">Log service</param>
        /// <param name="activeTimeline">Timeline service</param>
        /// <param name="streamLocator">Stream locator</param>
        public ProjectionBase(IEventStore <IAggregate> eventStore, ILog log, ITimeline activeTimeline, IStreamLocator streamLocator)
        {
            EventStore         = eventStore;
            Log                = log;
            ActiveTimeline     = activeTimeline;
            _streamLocator     = streamLocator;
            CancellationSource = new RepeatableCancellationTokenSource();
            _start             = new Lazy <Task>(() => Task.Run(Start));
            var options = new DataflowOptions {
                RecommendedParallelismIfMultiThreaded = 1
            };

            Build = new BuildFlow(options, this);

            StatusSubject.Where(s => s != Sleeping)
            .Subscribe(s => Log?.Info($"[{Timeline}]{GetType().GetFriendlyName()}/{RuntimeHelpers.GetHashCode(this)}/ : {s.ToString()}"));
        }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Retroactive"/> class.
 /// </summary>
 /// <param name="eventStore">Event store</param>
 /// <param name="sagaStore">Saga store</param>
 /// <param name="graph">Graph</param>
 /// <param name="manager">Branch manager</param>
 /// <param name="streamLocator">Stream locator</param>
 /// <param name="repository">Aggregate repository</param>
 /// <param name="sagaRepository">Saga repository</param>
 /// <param name="log">Log service</param>
 /// <param name="commandRegistry">Command registry</param>
 public Retroactive(
     IEventStore<IAggregate> eventStore,
     IEventStore<ISaga> sagaStore,
     IGraph graph,
     IBranchManager manager,
     IStreamLocator streamLocator,
     IEsRepository<IAggregate> repository,
     IEsRepository<ISaga> sagaRepository,
     ILog log, 
     ICommandRegistry commandRegistry)
 {
     _eventStore = eventStore;
     _graph = graph;
     _manager = manager;
     _streamLocator = streamLocator;
     _sagaStore = sagaStore;
     _repository = repository;
     _sagaRepository = sagaRepository;
     _log = log;
     _commandRegistry = commandRegistry;
 }
Example #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BranchManager"/> class.
        /// </summary>
        /// <param name="log">Application logger</param>
        /// <param name="activeTimeline">Root timeline</param>
        /// <param name="messageQueue">Message queue</param>
        /// <param name="eventStore">Event store</param>
        /// <param name="sagaStore">Saga store</param>
        /// <param name="streamLocator">Stream locator</param>
        /// <param name="graph">Graph</param>
        /// <param name="commandLog">Command log</param>
        /// <param name="clock">Logical clock</param>
        public BranchManager(
            ILog log,
            ITimeline activeTimeline,
            IMessageQueue messageQueue,
            IEventStore <IAggregate> eventStore,
            IEventStore <ISaga> sagaStore,
            IStreamLocator streamLocator,
            IGraph graph,
            ICommandLog commandLog,
            IClock clock)
        {
            _log            = log;
            _activeTimeline = activeTimeline as Timeline;
            _messageQueue   = messageQueue;
            _eventStore     = eventStore;
            _streamLocator  = streamLocator;
            _graph          = graph;
            _commandLog     = commandLog;
            _clock          = clock;
            _sagaStore      = sagaStore;

            _branches.TryAdd(Master, activeTimeline.New(Master));
        }
Example #6
0
 public StreamWriterFunction(IStreamLocator locator)
 {
     this.locator = locator;
 }
Example #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultProjection{TState}"/> class.
        /// </summary>
        /// <param name="eventStore">Event store</param>
        /// <param name="log">Log service</param>
        /// <param name="activeTimeline">Branch</param>
        /// <param name="messageQueue">Message queue</param>
        /// <param name="streamLocator">Stream locator</param>
        /// <param name="handlers">Event handlers</param>
        public DefaultProjection(IEventStore <IAggregate> eventStore, ILog log, ITimeline activeTimeline, IMessageQueue messageQueue, IStreamLocator streamLocator, IEnumerable <IProjectionHandler <TState> > handlers)
            : base(eventStore, log, activeTimeline, messageQueue, streamLocator)
        {
            State = new TState();
            foreach (var h in handlers)
            {
                var tEvents =
                    h.GetType().GetInterfaces().Where(i => i.GenericTypeArguments.Length > 1)
                    .Select(i => i.GenericTypeArguments[1]).ToList();

                // register remainder using reflection
                var otherMethods = h.GetType().GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly |
                                                          BindingFlags.InvokeMethod | BindingFlags.Instance)
                                   .Where(m => m.Name.Equals("Handle", StringComparison.OrdinalIgnoreCase));

                foreach (var method in otherMethods)
                {
                    var tEvent = method.GetParameters().First().ParameterType;
                    if (tEvent != typeof(IEvent) && !tEvents.Contains(tEvent))
                    {
                        // MethodInfo.Invoke >> EfficientInvoker.Invoke >> dynamic keyword
                        tEvents.Add(tEvent);

                        // var func = method.CreateDelegate(typeof(Func<,,,>).MakeGenericType(h.GetType(), tEvent, typeof(TState), typeof(TState)));
                        // var invoker = EfficientInvoker.ForMethodInfo(h.GetType(), method);
                        // if (invoker != null)
                        //    Register(tEvent, (e, state) => (TState)invoker.Invoke(h, e, state));
                        // Register(tEvent, (e, state) => (TState)method.Invoke(h, new object[] { e, state }));
                    }
                }

                foreach (var tEvent in tEvents)
                {
                    Register(tEvent, h.Handle);
                }
            }
        }
Example #8
0
 /// <summary>
 /// Gets a binary writer for combined path
 /// </summary>
 /// <returns>StreamWriter for the file.</returns>
 /// <param name="args">Paths and filename.</param>
 public static BinaryWriter BinaryWriterCurrent(this IStreamLocator locator, string fileName)
 {
     return(locator.BinaryWriterFor(Path.Combine(locator.CurrentPath, fileName)));
 }
Example #9
0
 /// <summary>
 /// Gets a binary writer for combined path
 /// </summary>
 /// <returns>StreamWriter for the file.</returns>
 /// <param name="args">Paths and filename.</param>
 public static BinaryWriter BinaryWriterFor(this IStreamLocator locator, string[] args)
 {
     return(locator.BinaryWriterFor(Path.Combine(args)));
 }
Example #10
0
            public MergeFlow(ITimeline currentBranch, IEventStore <T> eventStore, IStreamLocator streamLocator, bool doMerge)
                : base(Configuration.DataflowOptions)
            {
                _inputBlock = new ActionBlock <IStream>(
                    async s =>
                {
                    var version      = ExpectedVersion.EmptyStream;
                    var parentStream = s.Branch(currentBranch?.Id, ExpectedVersion.EmptyStream);
                    var parent       = s.Parent;
                    while (parent != null && parent.Version > ExpectedVersion.EmptyStream)
                    {
                        parentStream = await streamLocator.Find(parent);
                        version      = parentStream.Version;

                        if (currentBranch != null &&
                            (version > parent.Version && parent.Timeline == currentBranch.Id))
                        {
                            var theseEvents = await eventStore
                                              .ReadStream <IEvent>(parentStream, parent.Version + 1, version - parent.Version)
                                              .Select(e => e.MessageId).ToList();
                            var thoseEvents = await eventStore
                                              .ReadStream <IEvent>(s, parent.Version + 1, version - parent.Version)
                                              .Select(e => e.MessageId).ToList();
                            if (theseEvents.Zip(thoseEvents, (e1, e2) => (e1, e2)).Any(x => x.Item1 != x.Item2))
                            {
                                throw new InvalidOperationException(
                                    $"{currentBranch?.Id} timeline has moved on in the meantime, aborting...( {parentStream.Key} : {version} > {parent.Version} )");
                            }

                            return;
                        }

                        if (s.Version == version)
                        {
                            return;
                        }

                        if (parentStream.Timeline == currentBranch?.Id)
                        {
                            break;
                        }

                        parent = parent.Parent;
                    }

                    if (!doMerge)
                    {
                        return;
                    }

                    Interlocked.Increment(ref _numberOfStreams);

                    var events = await eventStore.ReadStream <IEvent>(s, version + 1, s.Version - version).ToList();
                    foreach (var e in events.OfType <Event>())
                    {
                        e.Stream = parentStream.Key;
                    }

                    Interlocked.Add(ref _numberOfEvents, events.Count);

                    await eventStore.AppendToStream(parentStream, events, false);
                }, Configuration.DataflowOptions.ToDataflowBlockOptions(true));     // .ToExecutionBlockOption(true));

                RegisterChild(_inputBlock);
            }
Example #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GlobalProjection{TState}"/> class.
 /// </summary>
 /// <param name="eventStore">Event store service</param>
 /// <param name="log">Log service</param>
 /// <param name="activeTimeline">Timeline service</param>
 /// <param name="messageQueue">Message queue service</param>
 /// <param name="streamLocator">Stream locator</param>
 public GlobalProjection(IEventStore <IAggregate> eventStore, ILog log, ITimeline activeTimeline, IMessageQueue messageQueue, IStreamLocator streamLocator)
     : base(eventStore, log, activeTimeline, streamLocator)
 {
     InvalidateSubscription = new LazySubscription(() =>
                                                   messageQueue.Alerts.OfType <InvalidateProjections>()
                                                   .Throttle(Configuration.Throttle)
                                                   .Subscribe(Build.InputBlock.AsObserver()));
 }
Example #12
0
 public DomainRepository(IEventStore eventStore, IStreamLocator streams, IClock clock)
 {
     _eventStore = eventStore;
     _streams    = streams;
     _clock      = clock;
 }
Example #13
0
 public StreamWriterFunction (IStreamLocator locator)
 {
     this.locator = locator;
 }