Ejemplo n.º 1
0
        private Task EventAppeared(ResolvedEvent e, CancellationToken token, Func <string, long, IFullEvent, Task> callback)
        {
            var metadata = e.Event.Metadata;
            var data     = e.Event.Data;

            var descriptor = _serializer.Deserialize <EventDescriptor>(metadata);

            if (descriptor.Compressed)
            {
                data = data.Decompress();
            }

            var eventType = Type.GetType(e.Event.EventType, false);

            // Not all types are detected and initialized by NSB - they do it in the pipeline, we have to do it here
            _mapper.Initialize(eventType);

            var payload = _serializer.Deserialize(eventType, data) as IEvent;

            return(callback(e.Event.EventStreamId, e.Event.EventNumber, new FullEvent
            {
                Descriptor = descriptor,
                Event = payload,
                EventId = e.Event.EventId
            }));
        }
Ejemplo n.º 2
0
        protected override JsonObjectContract CreateObjectContract(Type objectType)
        {
            var mappedTypeFor = objectType;

            _mapper.Initialize(objectType);
            mappedTypeFor = _mapper.GetMappedTypeFor(objectType);

            if (mappedTypeFor == null)
            {
                return(base.CreateObjectContract(objectType));
            }

            var objectContract = base.CreateObjectContract(objectType);

            objectContract.DefaultCreator = () => _factory.Create(objectType);

            return(objectContract);
        }
Ejemplo n.º 3
0
        public async Task <IFullEvent[]> GetEvents(string stream, long?start = null, int?count = null)
        {
            // Don't worry about start or count - if stream is in cache its not being written to very often and start/count are unlikely to change
            var cached = _cache.Retreive(stream) as IFullEvent[];

            if (cached != null)
            {
                return(cached);
            }


            var shard = Math.Abs(stream.GetHash() % _clients.Count());

            var sliceStart = start ?? StreamPosition.Start;
            StreamEventsSlice current;

            var events = new List <ResolvedEvent>();

            using (var ctx = _metrics.Begin("EventStore Read Time"))
            {
                do
                {
                    var readsize = _readsize;
                    if (count.HasValue)
                    {
                        readsize = Math.Min(count.Value - events.Count, _readsize);
                    }

                    current =
                        await _clients[shard].ReadStreamEventsForwardAsync(stream, sliceStart, readsize, false)
                        .ConfigureAwait(false);

                    events.AddRange(current.Events);
                    sliceStart = current.NextEventNumber;
                } while (!current.IsEndOfStream && (!count.HasValue || (events.Count != count.Value)));

                if (ctx.Elapsed > TimeSpan.FromSeconds(1))
                {
                    SlowLogger.InfoEvent("SlowRead", "{Events} events size {Size} stream [{Stream:l}] elapsed {Milliseconds} ms", events.Count, events.Sum(x => x.Event.Data.Length), stream, ctx.Elapsed.TotalMilliseconds);
                }
                Logger.DebugEvent("Read", "{Events} events size {Size} stream [{Stream:l}] elapsed {Milliseconds} ms", events.Count, events.Sum(x => x.Event.Data.Length), stream, ctx.Elapsed.TotalMilliseconds);
            }

            if (current.Status == SliceReadStatus.StreamNotFound)
            {
                throw new NotFoundException($"Stream [{stream}] does not exist on {_clients[shard].Settings.GossipSeeds[0].EndPoint.Address}!");
            }


            var translatedEvents = events.Select(e =>
            {
                var metadata = e.Event.Metadata;
                var data     = e.Event.Data;

                var descriptor = _serializer.Deserialize <EventDescriptor>(metadata);

                if (descriptor.Compressed)
                {
                    data = data.Decompress();
                }

                var eventType = Type.GetType(e.Event.EventType, false);
                _mapper.Initialize(eventType);

                var @event = _serializer.Deserialize(eventType, data);

                // Special case if event was written without a version - substitue the position from store
                if (descriptor.Version == 0)
                {
                    descriptor.Version = e.Event.EventNumber;
                }

                return((IFullEvent) new FullEvent
                {
                    Descriptor = descriptor,
                    Event = @event,
                    EventId = e.Event.EventId
                });
            }).ToArray();

            _cache.Cache(stream, translatedEvents);
            return(translatedEvents);
        }
Ejemplo n.º 4
0
        public async Task <IFullEvent[]> GetEvents(string stream, long?start = null, int?count = null)
        {
            var shard = Math.Abs(stream.GetHash() % _clients.Count());

            var sliceStart = start ?? StreamPosition.Start;
            StreamEventsSlice current;

            var events = new List <ResolvedEvent>();

            using (var ctx = _metrics.Begin("EventStore Read Time"))
            {
                do
                {
                    var readsize = _readsize;
                    if (count.HasValue)
                    {
                        readsize = Math.Min(count.Value - events.Count, _readsize);
                    }

                    current =
                        await _clients[shard].ReadStreamEventsForwardAsync(stream, sliceStart, readsize, false)
                        .ConfigureAwait(false);

                    events.AddRange(current.Events);
                    sliceStart = current.NextEventNumber;
                } while (!current.IsEndOfStream && (!count.HasValue || (events.Count != count.Value)));

                if (ctx.Elapsed > TimeSpan.FromSeconds(1))
                {
                    SlowLogger.InfoEvent("SlowRead", "{Events} events size {Size} stream [{Stream:l}] elapsed {Milliseconds} ms", events.Count, events.Sum(x => x.Event.Data.Length), stream, ctx.Elapsed.TotalMilliseconds);
                }
                Logger.DebugEvent("Read", "{Events} events size {Size} stream [{Stream:l}] elapsed {Milliseconds} ms", events.Count, events.Sum(x => x.Event.Data.Length), stream, ctx.Elapsed.TotalMilliseconds);
            }

            if (current.Status == SliceReadStatus.StreamNotFound)
            {
                throw new NotFoundException(stream, _clients[shard].Settings.GossipSeeds[0].EndPoint.Address);
            }


            var translatedEvents = events.Select(e =>
            {
                var metadata = e.Event.Metadata;
                var data     = e.Event.Data;

                var descriptor = _serializer.Deserialize <EventDescriptor>(metadata);

                if (descriptor == null || descriptor.EventId == Guid.Empty)
                {
                    // Assume we read from a children projection
                    // (theres no way to set metadata in projection states)

                    var children = _serializer.Deserialize <ChildrenProjection>(data);
                    if (!(children is IEvent))
                    {
                        throw new UnknownMessageException(e.Event.EventType);
                    }

                    return(new FullEvent
                    {
                        Descriptor = null,
                        Event = children as IEvent,
                        EventId = Guid.Empty
                    });
                }
                if (descriptor.Compressed)
                {
                    data = data.Decompress();
                }

                var eventType = _registrar.GetNamedType(e.Event.EventType);
                _mapper.Initialize(eventType);

                var @event = _serializer.Deserialize(eventType, data);

                if (!(@event is IEvent))
                {
                    throw new UnknownMessageException(e.Event.EventType);
                }

                // Special case if event was written without a version - substitue the position from store
                if (descriptor.Version == 0)
                {
                    descriptor.Version = e.Event.EventNumber;
                }

                return((IFullEvent) new FullEvent
                {
                    Descriptor = descriptor,
                    Event = @event as IEvent,
                    EventId = e.Event.EventId
                });
            }).ToArray();

            return(translatedEvents);
        }