private void OnEvent(RecordedEvent recordedEvent, Position position)
 {
     if (Event != null)
     {
         Event(this, new RecordedEventEventArgs(recordedEvent));
     }
 }
 public void Save(Position position)
 {
     var lastProcessedPosition = new LastProcessedPosition
     {
         CommitPosition = position.CommitPosition,
         PreparePosition = position.PreparePosition
     };
     collection.Save(lastProcessedPosition);
 }
Пример #3
0
 public void Start()
 {
     ViewBuilderData mainData;
     using (var session = _store.OpenSession())
     {
         mainData = session.Load<ViewBuilderData>("main");
     }
     Position pos = Position.Start;
     if (mainData != null)
     {
         pos = new Position(mainData.CommitPosition, mainData.PreparePosition);
     }
     _subscription = _eventStoreConnection.SubscribeToAllFrom(
         pos, false, HandleEvent);
 }
        internal static Position? ParsePosition(this string checkpointToken)
        {
            var position = default(Position?);

            if(checkpointToken != null)
            {
                var positions = checkpointToken.Split(new[] {'/'}, 2).Select(s => s.Trim()).ToArray();
                if(positions.Length != 2)
                {
                    throw new ArgumentException();
                }

                position = new Position(long.Parse(positions[0]), long.Parse(positions[1]));
            }
            return position;
        }
 internal AllEventsSlice(ReadDirection readDirection, Position fromPosition, Position nextPosition, ClientMessage.ResolvedEvent[] events)
 {
     ReadDirection = readDirection;
     FromPosition = fromPosition;
     NextPosition = nextPosition;
     if (events == null)
         Events = EmptyEvents;
     else
     {
         Events = new ResolvedEvent[events.Length];
         for (int i = 0; i < Events.Length; ++i)
         {
             Events[i] = new ResolvedEvent(events[i]);
         }
     }
 }
        public void Start()
        {
            Init();

            Position current;
            _accessor.Read(0, out current);
            var mre = new ManualResetEventSlim(false);
            var _lastRead = Enumerable.Empty<RecordedEvent>();

            _eventStoreConnection.SubscribeToAllStreamsAsync((ev, pos) =>
            {
                if (!mre.IsSet)
                    mre.Wait();

                _accessor.Read(0, out current);

                if (current.Commit > pos.CommitPosition)
                    return;

                Publish(new[] { ev });
            }, () =>
            {
                // dropped, refresh
            });

            
            _eventStoreConnection.ReadAllEventsForwardAsync(new ClientAPI.Position(current.Commit, current.Prepare), _batch)
              .ContinueWith(s =>
              {
                  Publish(s.Result.Events);

                  _lastRead = s.Result.Events;

                  int lastCount = current.Count;
                  current = new Position
                  {
                      Prepare = s.Result.Position.PreparePosition,
                      Commit = s.Result.Position.CommitPosition,
                      Count = lastCount + s.Result.Events.Count()
                  };
                  _accessor.Write(0, ref current);

                  mre.Set();
              }).Wait();
        }
        public void Save(String endpoint, EventStore.ClientAPI.Position position)
        {
            using (var session = _store.OpenSession())
            {
                var db = session.Load <Position>(endpoint);

                if (db == null)
                {
                    db = new Position {
                        Id = endpoint
                    };
                    session.Store(db);
                }

                db.CommitPosition  = position.CommitPosition;
                db.PreparePosition = position.PreparePosition;

                session.SaveChanges();
            }
        }
 public EventStoreAllCatchUpSubscription SubscribeToAllFrom(
         Position? fromPositionExclusive,
         bool resolveLinkTos,
         Action<EventStoreCatchUpSubscription, ResolvedEvent> eventAppeared,
         Action<EventStoreCatchUpSubscription> liveProcessingStarted = null,
         Action<EventStoreCatchUpSubscription, SubscriptionDropReason, Exception> subscriptionDropped = null,
         UserCredentials userCredentials = null,
         int readBatchSize = 500)
 {
     Ensure.NotNull(eventAppeared, "eventAppeared");
     var catchUpSubscription = 
             new EventStoreAllCatchUpSubscription(this, _settings.Log, fromPositionExclusive, resolveLinkTos,
                                                  userCredentials, eventAppeared, liveProcessingStarted, 
                                                  subscriptionDropped, _settings.VerboseLogging, readBatchSize);
     catchUpSubscription.Start();
     return catchUpSubscription;
 }
        public Task<AllEventsSlice> ReadAllEventsBackwardAsync(Position position, int maxCount, bool resolveLinkTos, UserCredentials userCredentials = null)
        {
            Ensure.Positive(maxCount, "maxCount");

            var source = new TaskCompletionSource<AllEventsSlice>();
            var operation = new ReadAllEventsBackwardOperation(_settings.Log, source, position, maxCount,
                                                               resolveLinkTos, _settings.RequireMaster, userCredentials);
            EnqueueOperation(operation);
            return source.Task;
        }
 public AllEventsSlice ReadAllEventsBackward(Position position, int maxCount, bool resolveLinkTos, UserCredentials userCredentials = null)
 {
     return ReadAllEventsBackwardAsync(position, maxCount, resolveLinkTos, userCredentials).Result;
 }
 private EventStoreCatchUpSubscription SubscribeToAllFrom(Position? lastCheckpoint)
 {
     return _eventStore.SubscribeToAllFrom(lastCheckpoint,
         false,
         EventAppeared,
         _ => _onCaughtUp(),
         SubscriptionDropped,
         _userCredentials);
 }
 public EventStoreAllCatchUpSubscription SubscribeToAllFrom(
     Position? lastCheckpoint,
     CatchUpSubscriptionSettings settings,
     Action<EventStoreCatchUpSubscription, ResolvedEvent> eventAppeared,
     Action<EventStoreCatchUpSubscription> liveProcessingStarted = null,
     Action<EventStoreCatchUpSubscription, SubscriptionDropReason, Exception> subscriptionDropped = null,
     UserCredentials userCredentials = null)
 {
     throw new NotImplementedException();
 }
 internal EventStoreAllCatchUpSubscription(IEventStoreConnection connection,
                                           ILogger log,
                                           Position? fromPositionExclusive, /* if null -- from the very beginning */
                                           UserCredentials userCredentials,
                                           Action<EventStoreCatchUpSubscription, ResolvedEvent> eventAppeared,
                                           Action<EventStoreCatchUpSubscription> liveProcessingStarted,
                                           Action<EventStoreCatchUpSubscription, SubscriptionDropReason, Exception> subscriptionDropped,
                                          CatchUpSubscriptionSettings settings)
         : base(connection, log, string.Empty, userCredentials,
                eventAppeared, liveProcessingStarted, subscriptionDropped, settings)
 {
     _lastProcessedPosition = fromPositionExclusive ?? new Position(-1, -1);
     _nextReadPosition = fromPositionExclusive ?? Position.Start;
 }
Пример #14
0
 public RavenOperation(Action<IDocumentSession> execute, Position position)
 {
     Execute = execute;
     Position = position;
 }
 public Task<AllEventsSlice> ReadAllEventsBackwardAsync(Position position, int maxCount, bool resolveLinkTos, UserCredentials userCredentials = null)
 {
     return _conn.ReadAllEventsBackwardAsync(position, maxCount, resolveLinkTos, userCredentials);
 }
 public AllEventsSlice ReadAllEventsForward(Position position, int maxCount, bool resolveLinkTos, UserCredentials userCredentials = null)
 {
     return _conn.ReadAllEventsForward(position, maxCount, resolveLinkTos, userCredentials);
 }
 internal EventStoreAllCatchUpSubscription(IEventStoreConnection connection,
                                           ILogger log,
                                           Position? fromPositionExclusive, /* if null -- from the very beginning */
                                           bool resolveLinkTos,
                                           UserCredentials userCredentials,
                                           Action<EventStoreCatchUpSubscription, ResolvedEvent> eventAppeared,
                                           Action<EventStoreCatchUpSubscription> liveProcessingStarted,
                                           Action<EventStoreCatchUpSubscription, SubscriptionDropReason, Exception> subscriptionDropped,
                                           bool verboseLogging,
                                           int readBatchSize = 500)
         : base(connection, log, string.Empty, resolveLinkTos, userCredentials, 
                eventAppeared, liveProcessingStarted, subscriptionDropped, verboseLogging, readBatchSize)
 {
     _lastProcessedPosition = fromPositionExclusive ?? new Position(-1, -1);
     _nextReadPosition = fromPositionExclusive ?? Position.Start;
 }
Пример #18
0
 /// <summary>
 /// Compares this instance of <see cref="Position" /> for equality
 /// with another instance.
 /// </summary>
 /// <param name="other">A <see cref="Position" /></param>
 /// <returns>True if this instance is equal to the other instance.</returns>
 public bool Equals(Position other)
 {
     return this == other;
 }
 public IEnumerable<object> ReadSlice()
 {
     var slice = connection.ReadAllEventsForward(currentPosition, SliceSize);
     currentPosition = slice.Position;
     return slice.Events;
 }
 public AllEventStreamsPullSource(EventStoreConnection connection, Position startFrom)
 {
     this.connection = connection;
     currentPosition = startFrom;
 }
 /// <summary>
 /// Reads All Events in the node backwards (e.g. end to beginning)
 /// </summary>
 /// <param name="position">The position to start reading from</param>
 /// <param name="maxCount">The maximum count to read</param>
 /// <param name="resolveLinkTos">Whether to resolve LinkTo events automatically</param>
 /// <returns>A <see cref="AllEventsSlice"/> containing the records read</returns>
 public AllEventsSlice ReadAllEventsBackward(Position position, int maxCount, bool resolveLinkTos)
 {
     return ReadAllEventsBackwardAsync(position, maxCount, resolveLinkTos).Result;
 }
        /// <summary>
        /// Reads All Events in the node backwards (e.g. end to beginning)
        /// </summary>
        /// <param name="position">The position to start reading from</param>
        /// <param name="maxCount">The maximum count to read</param>
        /// <param name="resolveLinkTos">Whether to resolve LinkTo events automatically</param>
        /// <returns>A <see cref="AllEventsSlice"/> containing the records read</returns>
        public Task<AllEventsSlice> ReadAllEventsBackwardAsync(Position position, int maxCount, bool resolveLinkTos)
        {
            Ensure.Positive(maxCount, "maxCount");
            EnsureActive();

            var source = new TaskCompletionSource<AllEventsSlice>();
            var operation = new ReadAllEventsBackwardOperation(source, Guid.NewGuid(), position, maxCount, resolveLinkTos);

            EnqueueOperation(operation);
            return source.Task;
        }
 public EventStoreAllCatchUpSubscription SubscribeToAllFrom(
         Position? fromPositionExclusive,
         bool resolveLinkTos,
         Action<EventStoreCatchUpSubscription, ResolvedEvent> eventAppeared,
         Action<EventStoreCatchUpSubscription> liveProcessingStarted = null,
         Action<EventStoreCatchUpSubscription, SubscriptionDropReason, Exception> subscriptionDropped = null,
         UserCredentials userCredentials = null)
 {
     return _conn.SubscribeToAllFrom(fromPositionExclusive, resolveLinkTos,
                                     eventAppeared, liveProcessingStarted, subscriptionDropped, userCredentials);
 }
        protected override void ReadEventsTill(IEventStoreConnection connection, bool resolveLinkTos, 
                                               UserCredentials userCredentials, long? lastCommitPosition, int? lastEventNumber)
        {
            bool done;
            do
            {
                AllEventsSlice slice = connection.ReadAllEventsForward(_nextReadPosition, ReadBatchSize, resolveLinkTos, userCredentials);
                foreach (var e in slice.Events)
                {
                    if (e.OriginalPosition == null) throw new Exception("Subscription event came up with no OriginalPosition.");
                    TryProcess(e);
                }
                _nextReadPosition = slice.NextPosition;

                done = lastCommitPosition == null
                               ? slice.IsEndOfStream
                               : slice.NextPosition >= new Position(lastCommitPosition.Value, lastCommitPosition.Value);

                if (!done && slice.IsEndOfStream)
                    Thread.Sleep(1); // we are waiting for server to flush its data
            } while (!done);

            if (Verbose) 
                Log.Debug("Catch-up Subscription to {0}: finished reading events, nextReadPosition = {1}.", 
                          IsSubscribedToAll ? "<all>" : StreamId, _nextReadPosition);
        }
 /// <summary>
 /// Try to process a single <see cref="ResolvedEvent"/>.
 /// </summary>
 /// <param name="e">The <see cref="ResolvedEvent"/> to process.</param>
 protected override void TryProcess(ResolvedEvent e)
 {
     bool processed = false;
     if (e.OriginalPosition > _lastProcessedPosition)
     {
         EventAppeared(this, e);
         _lastProcessedPosition = e.OriginalPosition.Value;
         processed = true;
     }
     if (Verbose)
         Log.Debug("Catch-up Subscription to {0}: {1} event ({2}, {3}, {4} @ {5}).",
                   IsSubscribedToAll ? "<all>" : StreamId, processed ? "processed" : "skipping",
                   e.OriginalEvent.EventStreamId, e.OriginalEvent.EventNumber, e.OriginalEvent.EventType, e.OriginalPosition);
 }
        private bool ProcessEvents(long? lastCommitPosition, AllEventsSlice slice)
        {
            foreach (var e in slice.Events)
            {
                if (e.OriginalPosition == null) throw new Exception("Subscription event came up with no OriginalPosition.");
                TryProcess(e);
            }
            _nextReadPosition = slice.NextPosition;

            var done = lastCommitPosition == null
                ? slice.IsEndOfStream
                : slice.NextPosition >= new Position(lastCommitPosition.Value, lastCommitPosition.Value);

            if (!done && slice.IsEndOfStream)
                Thread.Sleep(1); // we are waiting for server to flush its data
            return done;
        }
 public Task<AllEventsSlice> ReadAllEventsBackwardAsync(Position position, int maxCount, bool resolveLinkTos,
     UserCredentials userCredentials = null)
 {
     throw new NotImplementedException();
 }
 public Subject<ResolvedEvent> SubscribeToAllFrom(Position? position)
 {
     this.connection.SubscribeToAllFrom(position, false, (s, e) => events.OnNext(e), null, HandleSubscriptionDropped, null);
     return events;
 }
 private async Task WriteCheckpoint(Position? checkpoint)
 {
     await AppendToStream("checkpoints",
         ExpectedVersion.Any,
         new CheckpointReached
         {
             CheckpointToken = checkpoint.ToCheckpointToken(),
             CorrelationId = _correlationId,
             ProcessId =
                 ProcessHandler<OrderFulfillment, CompareablePosition>.DefaultBuildProcessManagerId(
                     _correlationId)
         });
 }
        private void Init()
        {
            if (!File.Exists(_fileName))
            {
                _file = MemoryMappedFile.CreateNew(_fileName, 4096);
                _accessor = _file.CreateViewAccessor();
                var begin = new Position();
                _accessor.Write(0, ref begin);
            }
            else
            {
                _file = MemoryMappedFile.CreateOrOpen(_fileName, 4096);
                _accessor = _file.CreateViewAccessor();
            }

            while (true)
            { 
                Position current;
                _accessor.Read(0, out current);

                var _lastRead = Enumerable.Empty<RecordedEvent>();

                _eventStoreConnection.ReadAllEventsForwardAsync(new ClientAPI.Position(current.Commit, current.Prepare), _batch)
                    .ContinueWith(s =>
                    {
                        Publish(s.Result.Events);

                        _lastRead = s.Result.Events;

                        int lastCount = current.Count;
                        current = new Position
                        {
                            Prepare = s.Result.Position.PreparePosition,
                            Commit = s.Result.Position.CommitPosition,
                            Count = lastCount + s.Result.Events.Count()
                        };
                        _accessor.Write(0, ref current);
                    }).Wait();

                if (_lastRead.Count() < _batch)
                    break;
            }

        }
Пример #31
0
 public static AllEventsSlice ReadAllEventsForward(this IEventStoreConnection con, Position position, int maxCount, bool resolveLinkTos, UserCredentials userCredentials = null)
 {
     var task = con.ReadAllEventsForwardAsync(position, maxCount, resolveLinkTos, userCredentials);
     task.Wait();
     return task.Result;
 }