Beispiel #1
0
        public async Task WriteSnapshots <T>(IState snapshot, IDictionary <string, string> commitHeaders) where T : IEntity
        {
            var streamName = _streamGen(_registrar.GetVersionedName(typeof(T)), StreamTypes.Snapshot, snapshot.Bucket, snapshot.Id, snapshot.Parents);

            Logger.DebugEvent("Write", "[{Stream:l}]", streamName);

            // We don't need snapshots to store the previous snapshot
            // ideally this field would be [JsonIgnore] but we have no dependency on json.net
            snapshot.Snapshot = null;

            var e = new FullEvent
            {
                Descriptor = new EventDescriptor
                {
                    EntityType    = _registrar.GetVersionedName(typeof(T)),
                    StreamType    = StreamTypes.Snapshot,
                    Bucket        = snapshot.Bucket,
                    StreamId      = snapshot.Id,
                    Parents       = snapshot.Parents,
                    Timestamp     = DateTime.UtcNow,
                    Version       = snapshot.Version + 1,
                    Headers       = new Dictionary <string, string>(),
                    CommitHeaders = commitHeaders
                },
                Event = snapshot,
            };

            await _store.WriteEvents(streamName, new[] { e }, commitHeaders).ConfigureAwait(false);
        }
        public static IFullEvent Event(IVersionRegistrar versionRegistry, Aggregates.UnitOfWork.IDomain uow, IEntity entity, IEvent @event)
        {
            var eventId = UnitOfWork.NextEventId(uow.CommitId);

            var newEvent = new FullEvent
            {
                Descriptor = new EventDescriptor
                {
                    EntityType = versionRegistry.GetVersionedName(entity.GetType()),
                    StreamType = StreamTypes.Domain,
                    Bucket     = entity.Bucket,
                    StreamId   = entity.Id,
                    Parents    = getParents(versionRegistry, entity),
                    Timestamp  = DateTime.UtcNow,
                    Version    = entity.StateVersion,
                    Headers    = new Dictionary <string, string>()
                    {
                        [$"{Defaults.PrefixHeader}.{Defaults.MessageIdHeader}"]     = eventId.ToString(),
                        [$"{Defaults.PrefixHeader}.{Defaults.CorrelationIdHeader}"] = uow.CommitId.ToString(),
                    }
                },
                EventId = eventId,
                Event   = @event
            };

            return(newEvent);
        }
Beispiel #3
0
        public async Task WriteSnapshots <T>(string bucket, Id streamId, Id[] parents, long version, IState snapshot, IDictionary <string, string> commitHeaders) where T : IEntity
        {
            var streamName = _streamGen(typeof(T), StreamTypes.Snapshot, bucket, streamId, parents);

            Logger.Write(LogLevel.Debug, () => $"Writing snapshot to stream [{streamName}]");

            // We don't need snapshots to store the previous snapshot
            // ideally this field would be [JsonIgnore] but we have no dependency on json.net
            snapshot.Snapshot = null;

            var e = new FullEvent
            {
                Descriptor = new EventDescriptor
                {
                    EntityType    = typeof(T).AssemblyQualifiedName,
                    StreamType    = StreamTypes.Snapshot,
                    Bucket        = bucket,
                    StreamId      = streamId,
                    Parents       = parents,
                    Timestamp     = DateTime.UtcNow,
                    Version       = version + 1,
                    Headers       = new Dictionary <string, string>(),
                    CommitHeaders = commitHeaders
                },
                Event = snapshot,
            };

            await _store.WriteEvents(streamName, new[] { e }, commitHeaders).ConfigureAwait(false);
        }
Beispiel #4
0
        public static IFullEvent OOBEvent(IVersionRegistrar versionRegistry, Aggregates.UnitOfWork.IDomain uow, IEntity entity, IEvent @event, string id, bool transient, int?daysToLive)
        {
            var eventId = Internal.UnitOfWork.NextEventId(uow.CommitId);

            var newEvent = new FullEvent
            {
                Descriptor = new EventDescriptor
                {
                    EntityType = versionRegistry.GetVersionedName(entity.GetType()),
                    StreamType = StreamTypes.OOB,
                    Bucket     = entity.Bucket,
                    StreamId   = entity.Id,
                    Parents    = getParents(versionRegistry, entity),
                    Timestamp  = DateTime.UtcNow,
                    Version    = entity.StateVersion,
                    Headers    = new Dictionary <string, string>()
                    {
                        [$"{Defaults.PrefixHeader}.{Defaults.MessageIdHeader}"]     = eventId.ToString(),
                        [$"{Defaults.PrefixHeader}.{Defaults.CorrelationIdHeader}"] = uow.CommitId.ToString(),
                        [Defaults.OobHeaderKey]     = id,
                        [Defaults.OobTransientKey]  = transient.ToString(),
                        [Defaults.OobDaysToLiveKey] = daysToLive.ToString()
                    }
                },
                EventId = eventId,
                Event   = @event
            };

            return(newEvent);
        }
        public void AddEvent <TEntity>(string bucket, Id streamId, Id[] parents, Messages.IEvent @event) where TEntity : IEntity
        {
            var key       = $"{typeof(TEntity).FullName}.{bucket}.{streamId}.{parents.BuildParentsString()}";
            var fullEvent =
                new FullEvent
            {
                Descriptor = new EventDescriptor(),
                Event      = @event,
                EventId    = Guid.NewGuid()
            };

            if (!_events.ContainsKey(key))
            {
                _events[key] = new[] { fullEvent }
            }
            ;
            else
            {
                _events[key] = _events[key].Concat(new[] { fullEvent }).ToArray();
            }
        }
Beispiel #6
0
        public async Task Write <T>(Tuple <long, T> poco, string bucket, Id streamId, Id[] parents, IDictionary <string, string> commitHeaders)
        {
            var streamName = _streamGen(typeof(T), StreamTypes.Poco, bucket, streamId, parents);

            Logger.DebugEvent("Write", "[{Stream:l}]", streamName);

            var descriptor = new EventDescriptor
            {
                EntityType = typeof(T).AssemblyQualifiedName,
                StreamType = StreamTypes.Poco,
                Bucket     = bucket,
                StreamId   = streamId,
                Parents    = parents,
                Timestamp  = DateTime.UtcNow,
                // When reading version will be the stream position
                Version       = 0,
                Headers       = new Dictionary <string, string>(),
                CommitHeaders = commitHeaders
            };

            var @event = new FullEvent
            {
                Descriptor = descriptor,
                Event      = poco.Item2,
                EventId    = Guid.NewGuid()
            };

            if (await _store.WriteEvents(streamName, new IFullEvent[] { @event }, commitHeaders, expectedVersion: poco.Item1).ConfigureAwait(false) == 1)
            {
                await _store.WriteMetadata(streamName, maxCount : 5).ConfigureAwait(false);
            }

            if (_shouldCache)
            {
                _cache.Cache(streamName, poco);
            }
        }