protected override async Task <SelectedSnapshot> LoadAsync(string persistenceId,
                                                                   SnapshotSelectionCriteria criteria)
        {
            if (criteria.Equals(SnapshotSelectionCriteria.None))
            {
                return(null);
            }

            var streamName = GetStreamName(persistenceId);


            if (!criteria.Equals(SnapshotSelectionCriteria.Latest))
            {
                var result = await FindSnapshot(streamName, criteria.MaxSequenceNr, criteria.MaxTimeStamp);

                return(result.Snapshot);
            }

            var slice = await _conn.ReadStreamEventsBackwardAsync(streamName, StreamPosition.End, 1, false);

            if (slice == null || slice.Status != SliceReadStatus.Success || !slice.Events.Any())
            {
                return(null);
            }

            return(slice.Events.Select(_snapshotAdapter.Adapt).First());
        }
Exemplo n.º 2
0
        protected void AssertStreamTail(string streamId, params string[] events)
        {
#if DEBUG
            var result = _conn.ReadStreamEventsBackwardAsync(streamId, -1, events.Length, true, _admin).Result;
            switch (result.Status)
            {
                case SliceReadStatus.StreamDeleted:
                    Assert.Fail("Stream '{0}' is deleted", streamId);
                    break;
                case SliceReadStatus.StreamNotFound:
                    Assert.Fail("Stream '{0}' does not exist", streamId);
                    break;
                case SliceReadStatus.Success:
                    var resultEventsReversed = result.Events.Reverse().ToArray();
                    if (resultEventsReversed.Length < events.Length)
                        DumpFailed("Stream does not contain enough events", streamId, events, result.Events);
                    else
                    {
                        for (var index = 0; index < events.Length; index++)
                        {
                            var parts = events[index].Split(new char[] { ':' }, 2);
                            var eventType = parts[0];
                            var eventData = parts[1];

                            if (resultEventsReversed[index].Event.EventType != eventType)
                                DumpFailed("Invalid event type", streamId, events, resultEventsReversed);
                            else
                                if (resultEventsReversed[index].Event.DebugDataView != eventData)
                                    DumpFailed("Invalid event body", streamId, events, resultEventsReversed);
                        }
                    }
                    break;
            }
#endif
        }
Exemplo n.º 3
0
        public override async Task <long> ReadHighestSequenceNrAsync(string persistenceId, long fromSequenceNr)
        {
            try
            {
                var slice = await _conn.ReadStreamEventsBackwardAsync(persistenceId, StreamPosition.End, 1, false);

                long sequence = 0;

                if (slice.Events.Any())
                {
                    var @event  = slice.Events.First();
                    var adapted = _eventAdapter.Adapt(@event);
                    sequence = adapted.SequenceNr;
                }
                else
                {
                    var metadata = await _conn.GetStreamMetadataAsync(persistenceId);

                    if (metadata.StreamMetadata.TruncateBefore != null)
                    {
                        sequence = metadata.StreamMetadata.TruncateBefore.Value;
                    }
                }

                return(sequence);
            }
            catch (Exception e)
            {
                _log.Error(e, e.Message);
                throw;
            }
        }
        public async Task read_stream_backward_respects_max_count()
        {
            var res = await _connection.ReadStreamEventsBackwardAsync(Stream, -1, 100, false);

            Assert.AreEqual(SliceReadStatus.Success, res.Status);
            Assert.AreEqual(3, res.Events.Length);
            Assert.AreEqual(_testEvents.Skip(2).Select(x => x.EventId).ToArray(),
                            res.Events.Reverse().Select(x => x.Event.EventId).ToArray());
        }
Exemplo n.º 5
0
        public async Task SaveAsync <T>(T aggregate) where T : AggregateRoot, new()
        {
            var changes = aggregate.GetChanges()
                          .Select((change) => GetEventDataByEvent(change))
                          .ToArray();

            if (!changes.Any())
            {
                return;
            }

            var streamName = GetStreamName <T>(aggregate, aggregate.Id.ToString());

            try
            {
                await _eventStoreConnection.AppendToStreamAsync(streamName, aggregate.Version, changes, null);
            }
            catch (WrongExpectedVersionException)
            {
                var page = await _eventStoreConnection.ReadStreamEventsBackwardAsync(streamName, -1, 1, false, null);

                _logger.LogError("Version failed. Stream: {Stream}. Expected Version: {Version}. Last Version: {LastEventNumber}",
                                 page.Stream, page.Status, aggregate.Version, page.LastEventNumber);

                throw new AggregateVersionFailedException(page.Stream, aggregate.Version, page.LastEventNumber);
            }
        }
Exemplo n.º 6
0
        protected IEnumerable <dynamic> GetDataByStreamName(string streamName)
        {
            StreamEventsSlice eventCollection;

            using (IEventStoreConnection connection = EventStoreConnectionHelper.GetEventStoreConnection())
            {
                eventCollection = connection.ReadStreamEventsBackwardAsync(streamName, StreamPosition.End, 1, false).Result;
            }
            var jsonSerialiserSettings = EventDeserialiser.GetSerialisationSettings();
            var encoder = new UTF8Encoding();

            return
                ((
                     (
                         (IEnumerable <dynamic>)eventCollection.Events
                         .Select(e => JsonConvert.DeserializeObject(((dynamic)encoder.GetString(e.Event.Data)), jsonSerialiserSettings))
                         .SingleOrDefault()
                     )
                     ??
                     (
                         Enumerable.Empty <dynamic>()
                     )
                     )
                 .Select(x => x.Value));
        }
        public static async Task <(object Event, long Version)> ReadLastEvent(this IEventStoreConnection connection,
                                                                              string streamName, Func <string, Type> stringToType)
        {
            try
            {
                var slice = await connection
                            .ReadStreamEventsBackwardAsync(streamName, StreamPosition.End, 1, true)
                            .ConfigureAwait(false);

                if (slice.Status == SliceReadStatus.StreamDeleted || slice.Status == SliceReadStatus.StreamNotFound)
                {
                    return(null, -1);
                }

                if (!slice.Events.Any())
                {
                    return(null, 0);
                }

                var @event = Deserialize(slice.Events.First(), stringToType);
                return(@event.@event, @event.index);
            }
            catch (Exception e)
            {
                Log.LogError(e, "Cannot read last event from stream {stream}: {message}", streamName, e.Message);
                throw;
            }
        }
 /// <summary>
 /// Get the Last published event
 /// </summary>
 /// <param name="connection">connection to ES</param>
 /// <param name="accountStream">Name of the stream</param>
 /// <returns>json string of the last event</returns>
 public static string GetLastEvent(IEventStoreConnection connection, string accountStream)
 {
     if (AccountManager.StreamExits(connection, accountStream))
     {
         var eventsStream = connection.ReadStreamEventsBackwardAsync(accountStream, 0, 1, true).Result;
         var lastEventNr  = eventsStream.LastEventNumber;
         eventsStream = connection.ReadStreamEventsBackwardAsync(accountStream, lastEventNr, 1, true).Result;
         var lastEvent = eventsStream.Events[0].Event;
         var json      = Encoding.UTF8.GetString(lastEvent.Data);
         return(json);
     }
     else
     {
         return(null);
     }
 }
Exemplo n.º 9
0
        public async Task <CompareablePosition> GetCheckpoint(string processId, CancellationToken ct, string bucketId = null)
        {
            int streamPosition = StreamPosition.End;
            var streamName     = GetStreamId(processId, bucketId);

            do
            {
                if (ct.IsCancellationRequested)
                {
                    return(new CompareablePosition());
                }

                var slice = await _connection.ReadStreamEventsBackwardAsync(streamName, streamPosition, 1, false);

                if (slice.Status != SliceReadStatus.Success || slice.Events.Length == 0)
                {
                    return(new CompareablePosition());
                }

                var resolvedEvent = slice.Events[0];

                var checkpointReached = _serializer.DeserializeEventData(resolvedEvent) as CheckpointReached;

                if (checkpointReached != null)
                {
                    return(new CompareablePosition(checkpointReached.CheckpointToken.ParsePosition()));
                }

                streamPosition = resolvedEvent.Event.EventNumber - 1;
            } while(streamPosition >= 0);

            return(new CompareablePosition());
        }
 public static string GetLastEvent(this IEventStoreConnection connection, string streamName)
 {
     if (StreamExits(connection, streamName))
     {
         var eventsStream = connection.ReadStreamEventsBackwardAsync(streamName, 0, 1, true).Result;
         var lastEventNr  = eventsStream.LastEventNumber;
         eventsStream = connection.ReadStreamEventsBackwardAsync(streamName, lastEventNr, 1, true).Result;
         var lastEvent = eventsStream.Events[0].Event;
         var json      = Encoding.UTF8.GetString(lastEvent.Data);
         return(json);
     }
     else
     {
         return(null);
     }
 }
Exemplo n.º 11
0
        public List <EventstoreEvent> ReadLastEvents(string streamName, int eventsToRead)
        {
            var result       = _connection.ReadStreamEventsBackwardAsync(streamName, StreamPosition.End, eventsToRead, false).Result;
            var mappedEvents = result.Events.Select(MapEventstoreEvent);

            return(mappedEvents.Reverse().ToList());
        }
Exemplo n.º 12
0
        public async Task TheExampleCanRun()
        {
            await _eventStoreConnection.ConnectAsync();

            await WriteBytesToStream("test-stream", "testEvent", "some data");

            var streamEvents = await
                               _eventStoreConnection.ReadStreamEventsBackwardAsync("test-stream", StreamPosition.End, 1, false);

            var returnedEvent    = streamEvents.Events[0].Event;
            var returnedData     = Encoding.UTF8.GetString(returnedEvent.Data);
            var returnedMetaData = Encoding.UTF8.GetString(returnedEvent.Metadata);

            Assert.Equal("some data", returnedData);
            Assert.Equal("some metadata", returnedMetaData);
        }
Exemplo n.º 13
0
        public async Task <IEnumerable <EventLog> > LoadLogs <T>(int aggregateId, int take) where T : Aggregate, new()
        {
            if (aggregateId < 0)
            {
                throw new ArgumentException("Value cannot be below zero", nameof(aggregateId));
            }

            var aggregate  = new T();
            var streamName = GetStreamName(aggregate, aggregateId);

            var results = new List <EventLog>();

            var page = await _eventStore.ReadStreamEventsBackwardAsync(
                streamName,
                StreamPosition.End,
                take,
                false);

            if (page.Events.Length > 0)
            {
                results.AddRange(page.Events.Select(@event =>
                {
                    return(new EventLog
                    {
                        EventParameters = JsonSerializer.Deserialize(
                            Encoding.UTF8.GetString(@event.OriginalEvent.Data),
                            Type.GetType(Encoding.UTF8.GetString(@event.OriginalEvent.Metadata))),
                        EventType = @event.Event.EventType,
                        TimeStamp = @event.Event.Created
                    });
                }));
            }

            return(results);
        }
Exemplo n.º 14
0
        public T BuscarSnapshotPor <T>(Guid entidadeId) where T : Snapshot
        {
            var stream = string.Format("{0}-ss", entidadeId);
            var ev     = conexao.ReadStreamEventsBackwardAsync(stream, StreamPosition.End, 1, false).Result;

            return(ev.Events.Any() ? (T)Deserialize(ev.Events.Single()) : null);
        }
Exemplo n.º 15
0
        private static async Task AppendToStream <T>(ConcurrentUnitOfWork uow, IEventStoreConnection connection, Now now, IStreamNameResolver getStreamName) where T : IAggregateRootEntity
        {
            foreach (Aggregate aggregate in uow.GetChanges())
            {
                EventData[] changes = aggregate.Root.GetChanges()
                                      .Select(@event => new EventData(
                                                  Guid.NewGuid(),
                                                  @event.GetType().TypeQualifiedName(),
                                                  true,
                                                  Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(@event)),
                                                  Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new EventMetadata
                {
                    TimeStamp     = now(),
                    AggregateType = typeof(T).Name,
                    AggregateAssemblyQualifiedName = typeof(T).AssemblyQualifiedName,
                    IsSnapshot = false
                }))
                                                  )).ToArray();
                try
                {
                    await connection.AppendToStreamAsync(getStreamName.Resolve(aggregate.Identifier), aggregate.ExpectedVersion, changes);
                }
                catch (WrongExpectedVersionException)
                {
                    StreamEventsSlice page = await connection.ReadStreamEventsBackwardAsync(aggregate.Identifier, -1, 1, false);

                    throw new WrongExpectedStreamVersionException(
                              $"Failed to append stream {aggregate.Identifier} with expected version {aggregate.ExpectedVersion}. " +
                              $"{(page.Status == SliceReadStatus.StreamNotFound ? "Stream not found!" : $"Current Version: {page.LastEventNumber}")}");
                }
            }
        }
Exemplo n.º 16
0
        private async IAsyncEnumerable <StoredEvent> QueryReverseAsync(string streamName, long sliceStart, long take = int.MaxValue,
                                                                       [EnumeratorCancellation] CancellationToken ct = default)
        {
            var taken = take;

            StreamEventsSlice currentSlice;

            do
            {
                currentSlice = await connection.ReadStreamEventsBackwardAsync(streamName, sliceStart, ReadPageSize, true);

                if (currentSlice.Status == SliceReadStatus.Success)
                {
                    sliceStart = currentSlice.NextEventNumber;

                    foreach (var resolved in currentSlice.Events.OrderByDescending(x => x.Event.EventNumber))
                    {
                        var storedEvent = Formatter.Read(resolved, prefix, serializer);

                        yield return(storedEvent);

                        if (taken == take)
                        {
                            break;
                        }

                        taken++;
                    }
                }
            }while (!currentSlice.IsEndOfStream && !ct.IsCancellationRequested && taken < take);
        }
        /// <summary>
        /// Check if stream exists
        /// </summary>
        /// <param name="connection">connection to ES</param>
        /// <param name="streamName">Name of the stream</param>
        /// <returns>True if stream exists</returns>
        public static bool StreamExits(IEventStoreConnection connection, string streamName)
        {
            var             lastEventsStream = connection.ReadStreamEventsBackwardAsync(streamName.ToLower(), 0, 1, false).Result;
            SliceReadStatus status           = lastEventsStream.Status;

            return(status == 0);
        }
Exemplo n.º 18
0
#pragma warning disable 1998
        protected async Task AssertStreamTail(string streamId, params string[] events)
        {
#pragma warning restore 1998
#if DEBUG
            await Task.Delay(TimeSpan.FromMilliseconds(500));

            var result = await _conn.ReadStreamEventsBackwardAsync(streamId, -1, events.Length, true, _admin);

            switch (result.Status)
            {
            case SliceReadStatus.StreamDeleted:
                Assert.Fail("Stream '{0}' is deleted", streamId);
                break;

            case SliceReadStatus.StreamNotFound:
                Assert.Fail("Stream '{0}' does not exist", streamId);
                break;

            case SliceReadStatus.Success:
                var resultEventsReversed = result.Events.Reverse().ToArray();
                if (resultEventsReversed.Length < events.Length)
                {
                    DumpFailed("Stream does not contain enough events", streamId, events, result.Events);
                }
                else
                {
                    for (var index = 0; index < events.Length; index++)
                    {
                        var parts     = events[index].Split(new char[] { ':' }, 2);
                        var eventType = parts[0];
                        var eventData = parts[1];

                        if (resultEventsReversed[index].Event.EventType != eventType)
                        {
                            DumpFailed("Invalid event type", streamId, events, resultEventsReversed);
                        }
                        else if (resultEventsReversed[index].Event.DebugDataView() != eventData)
                        {
                            DumpFailed("Invalid event body", streamId, events, resultEventsReversed);
                        }
                    }
                }

                break;
            }
#endif
        }
Exemplo n.º 19
0
        public async Task Delete(TId selector)
        {
            var id         = selector.ToString();
            var eventsTail = await connection.ReadStreamEventsBackwardAsync(id, 0, 1, false);

            var expectedVersion = eventsTail.LastEventNumber;
            await connection.DeleteStreamAsync(id, expectedVersion);
        }
Exemplo n.º 20
0
 public Func <Task <SerializedEvent> > ReadEventsBackwards(string stream, int batchSize, long maxToRead, Action <string> onNotFound, bool resolveLinks = true)
 {
     return(new EventReader(
                position => _connection.ReadStreamEventsBackwardAsync(stream, position, batchSize, resolveLinks),
                () => onNotFound(stream),
                StreamPosition.End,
                maxToRead).Next);
 }
 private static async Task <StreamEventsSlice> ReadStreamEventsBackwardsAsync(this IEventStoreConnection connection, string streamName, int lastEventNumber)
 {
     return(await connection.ReadStreamEventsBackwardAsync(
                streamName,
                lastEventNumber,
                PageSize, false,
                EventStoreCredentials.Default));
 }
        public async Task <StreamEventsSlice> GetCurrentSlice(IEventStoreConnection connection, long streamPosition, bool resolveLinkTos)
        {
            var currentSlice = await connection.ReadStreamEventsBackwardAsync(streamId, streamPosition, SliceSize, resolveLinkTos);

            nextSliceStart = currentSlice.NextEventNumber;

            return(currentSlice);
        }
Exemplo n.º 23
0
        private static long GetLatestStreamPosition(IEventStoreConnection connection, string streamId)
        {
            var position = connection
                           .ReadStreamEventsBackwardAsync(streamId, StreamPosition.End, 1, true)
                           .Result?.LastEventNumber ?? 0;

            return(position);
        }
        public ActionResult CardData()
        {
            string   NodeName = "none";
            CardVM   card     = null;
            int      cardNo;
            DateTime occurred;

            if (Session["Node"] != null)
            {
                NodeName = Session["Node"].ToString();
            }
            if (NodeName != "none")
            {
                using (IEventStoreConnection connection = EventStoreConnection.Create(new IPEndPoint(IPAddress.Loopback, 1113)))
                {
                    connection.ConnectAsync();

                    var result = Task.Run(() => connection.ReadStreamEventsBackwardAsync(NodeName, StreamPosition.End, 1, false));
                    Task.WaitAll();
                    var resultData = result.Result;
                    foreach (var Event in resultData.Events)
                    {
                        string strData           = Encoding.UTF8.GetString(Event.Event.Data);
                        string strMeta           = Encoding.UTF8.GetString(Event.Event.Metadata);
                        var    datetimeConverter = new IsoDateTimeConverter {
                            DateTimeFormat = "dd/MM/yyyy"
                        };
                        dynamic Eventdata     = JsonConvert.DeserializeObject(strData);
                        dynamic EventMetadata = JsonConvert.DeserializeObject(strMeta);
                        if (Event.Event.EventType == "checkIn" || Event.Event.EventType == "checkOut")
                        {
                            cardNo = (int)EventMetadata.SelectToken("CardNumber");
                            if (cardNo != 0)
                            {
                                using (DB db = new DB())
                                {
                                    card = new CardVM();
                                    var cardInfo = db.CardInfoes.Where(x => x.CardNumber == cardNo).FirstOrDefault();
                                    card.Id         = cardInfo.Id;
                                    card.Name       = cardInfo.Name;
                                    card.Title      = cardInfo.Title;
                                    card.Department = cardInfo.Department;
                                    card.ImageName  = cardInfo.ImageName;
                                    card.CardNumber = cardInfo.CardNumber;
                                }
                            }
                            else
                            {
                                card = null;
                            }
                            occurred = (DateTime)Eventdata.SelectToken("occurred");
                        }
                    }
                }
            }
            ViewBag.NodeName = NodeName;
            return(PartialView(card));
        }
        public static void EditJsonStream <T>(this IEventStoreConnection connection, IEventType <T> eventType) where T : class
        {
            var eventsStream = connection.ReadStreamEventsBackwardAsync(eventType.EditStreamName, 0, 1, true).Result;
            var lastEventNr  = eventsStream.LastEventNumber;
            var json         = Newtonsoft.Json.JsonConvert.SerializeObject(eventType.Data);
            var myEvent      = new EventData(Guid.NewGuid(), eventType.EventType, true, Encoding.UTF8.GetBytes(json), null);

            connection.AppendToStreamAsync(eventType.EditStreamName, lastEventNr, myEvent).Wait();
        }
 public EventStoreRepository(IEventStoreConnection connection)
 {
     this.connection    = connection;
     getStreamName      = (type, guid) => $"{type.Name}-{guid}"; // todo make this a delegate
     log                = LogManager.GetLogger(GetType());
     eventStreamReaders = new Dictionary <ReadDirection, Func <string, int, int, Task <StreamEventsSlice> > >()
     {
         [ReadDirection.Forwards]  = async(s, i, c) => await connection.ReadStreamEventsForwardAsync(s, i, c, resolveLinkTos : true).ConfigureAwait(false),
         [ReadDirection.Backwards] = async(s, i, c) => await connection.ReadStreamEventsBackwardAsync(s, i, c, true).ConfigureAwait(false)
     };
 }
        private async Task <ItemScanned> MostRecentItemScannedIntoBasket(Guid correlationId)
        {
            var slice = await
                        _eventStoreConnection.ReadStreamEventsBackwardAsync($"basket-{correlationId}", StreamPosition.End, 1, false);

            Assert.Equal(SliceReadStatus.Success, slice.Status);

            var returnedEvent = slice.Events[0].Event;

            return(JsonConvert.DeserializeObject <ItemScanned>(Encoding.UTF8.GetString(returnedEvent.Data)));
        }
Exemplo n.º 28
0
        /// <summary>
        /// Update a message to show it is dispatched
        /// </summary>
        /// <param name="id">The id of the message to update</param>
        /// <param name="dispatchedAt">When was the message dispatched, defaults to UTC now</param>
        /// <param name="args">Additional parameters required for search, if any</param>
        /// <param name="cancellationToken">Allows the sender to cancel the request pipeline. Optional</param>
        public async Task MarkDispatchedAsync(Guid id, DateTime?dispatchedAt      = null, Dictionary <string, object> args = null,
                                              CancellationToken cancellationToken = default)
        {
            var stream = GetStreamFromArgs(args);

            StreamEventsSlice slice;
            var           startPos        = (long)StreamPosition.End;
            long?         nextEventNumber = null;
            RecordedEvent resolvedEvent;
            bool          found = false;

            do
            {
                slice = await _eventStore.ReadStreamEventsBackwardAsync(stream, startPos, 100, true);

                startPos = slice.NextEventNumber;

                if (nextEventNumber is null)
                {
                    nextEventNumber = (await _eventStore.ReadStreamEventsBackwardAsync(stream, StreamPosition.End, 1, true)).LastEventNumber;
                }

                resolvedEvent = slice.Events.FirstOrDefault(e => e.Event.EventId == id).Event;

                if (resolvedEvent != null)
                {
                    found = true;
                }
            } while (!found && !slice.IsEndOfStream);

            if (resolvedEvent is null)
            {
                return;
            }

            var message = EventStoreMessageReader.ConvertEventToMessage(resolvedEvent, stream, dispatchedAt, nextEventNumber.Value);

            var eventData = EventStoreMessageWriter.CreateEventData(message);

            await _eventStore.AppendToStreamAsync(stream, nextEventNumber.Value, eventData);
        }
Exemplo n.º 29
0
        public static async Task <StreamSize> GetStreamSize(
            this IEventStoreConnection connection, string stream
            )
        {
            var last = await connection.ReadStreamEventsBackwardAsync(
                stream,
                StreamPosition.End,
                1,
                false
                ).ConfigureAwait(false);

            return(new StreamSize(last.LastEventNumber));
        }
        private async Task <Snapshot> GetSnapshot(string sku)
        {
            var streamName = GetSnapshotStreamName(sku);
            var slice      = await _connection.ReadStreamEventsBackwardAsync(streamName, StreamPosition.End, 1, false);

            if (slice.Events.Any())
            {
                var evnt = slice.Events.First();
                var json = Encoding.UTF8.GetString(evnt.Event.Data);
                return(JsonConvert.DeserializeObject <Snapshot>(json));
            }

            return(new Snapshot());
        }
Exemplo n.º 31
0
        private static void ReadStreamEventsBackward(IEventStoreConnection connection)
        {
            StreamEventsSlice slice = connection.ReadStreamEventsBackwardAsync("test-stream", StreamPosition.End, 10, false).Result;

            if (slice.Events.Length > 0)
            {
                Console.WriteLine("id: " + slice.Events[0].Event.EventId);

                string data = Encoding.UTF8.GetString(slice.Events[0].Event.Data);
                Console.WriteLine("data: " + data);

                string metadata = Encoding.UTF8.GetString(slice.Events[0].Event.Metadata);
                Console.WriteLine("metadata: " + metadata);
            }
        }