public IEnumerable <IEvent> GetEvents(EventFilter filter)
        {
            var filterDefinition = filter.GetFilter();

            IEnumerable <IEvent> events = new List <IEvent>();

            if (filterDefinition.StreamIdentifier != null)
            {
                if (store.ContainsKey(filterDefinition.StreamIdentifier.Value))
                {
                    events = store[filterDefinition.StreamIdentifier.Value];
                }
            }
            else
            {
                events = store.Values.SelectMany(e => e);
            }

            if (filterDefinition.Types.Any())
            {
                events = events.Join(filterDefinition.Types, @event => @event.GetType(), type => type.Key, (@event, _) => @event);
            }

            if (filter.Timestamp.HasValue)
            {
                events = events.Where(e => e.Timestamp <= filter.Timestamp.Value);
            }

            if (filterDefinition.Version != null && filterDefinition.Version.VersionMode == VersionMode.Max)
            {
                events = events.Where(e => e.Version <= filterDefinition.Version.Version);
            }
            else if (filterDefinition.Version != null && filterDefinition.Version.VersionMode == VersionMode.Min)
            {
                events = events.Where(e => e.Version >= filterDefinition.Version.Version);
            }

            events = events.OrderBy(e => e.Version);

            return(events);
        }
예제 #2
0
        public IEnumerable <IEvent> GetEvents(EventFilter filter)
        {
            var collection = GetMongoDbCollection(EventCollection);

            var builder = Builders <EventPersistance> .Filter;
            FilterDefinition <EventPersistance> mongofilter = null;

            var filterDefinition = filter.GetFilter();

            foreach (var type in filterDefinition.Types)
            {
                if (mongofilter == null)
                {
                    mongofilter = builder.Eq("EventClass", type.Key.AssemblyQualifiedName);
                    //if (type.Value != null)
                    //    mongofilter = builder.And(mongofilter, builder.Eq("AggregateId", type.Value.Value));
                }
                else
                {
                    var innerFilter = builder.Eq("EventClass", type.Key.AssemblyQualifiedName);
                    //if (type.Value != null)
                    //    innerFilter = builder.And(innerFilter, builder.Eq("AggregateId", type.Value.Value));

                    mongofilter = mongofilter | innerFilter;
                }
            }

            if (filterDefinition.StreamIdentifier != null)
            {
                if (mongofilter == null)
                {
                    mongofilter = builder.Eq("AggregateId", filterDefinition.StreamIdentifier.Value);
                }
                else
                {
                    mongofilter = builder.And(mongofilter, builder.Eq("AggregateId", filterDefinition.StreamIdentifier.Value));
                }
            }

            if (filterDefinition.Version != null)
            {
                if (filterDefinition.Version.VersionMode == VersionMode.Max)
                {
                    if (mongofilter == null)
                    {
                        mongofilter = builder.Lte("version", filterDefinition.Version.Version);
                    }
                    else
                    {
                        mongofilter = builder.And(mongofilter, builder.Lte("version", filterDefinition.Version.Version));
                    }
                }
                else if (filterDefinition.Version.VersionMode == VersionMode.Min)
                {
                    if (mongofilter == null)
                    {
                        mongofilter = builder.Gte("version", filterDefinition.Version.Version);
                    }
                    else
                    {
                        mongofilter = builder.And(mongofilter, builder.Gte("version", filterDefinition.Version.Version));
                    }
                }
            }

            if (filterDefinition.Timestamp.HasValue)
            {
                if (mongofilter == null)
                {
                    mongofilter = builder.Eq("Timestamp", filterDefinition.Timestamp.Value);
                }
                else
                {
                    mongofilter = builder.And(mongofilter, builder.Lte("Timestamp", filterDefinition.Timestamp.Value));
                }
            }

            var allRawEvents = collection
                               .Find(mongofilter)
                               .ToList();

            var allEvents = allRawEvents
                            .Select((@event, i) =>
            {
                var evt       = (Event)JsonConvert.DeserializeObject(@event.Data, typeResolver.Lookup(@event.EventClass));
                evt.Timestamp = @event.Timestamp;
                evt.Version   = @event.Version;
                return(evt);
            })
                            .ToList();

            return(allEvents);
        }