public async Task WriteSnapshots <T>(string bucket, string streamId, ISnapshot snapshot, IDictionary <string, string> commitHeaders) where T : class, IEventSource
        {
            var streamName = _streamGen(typeof(T), StreamTypes.Snapshot, bucket, streamId);

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

            var e = new WritableEvent
            {
                Descriptor = new EventDescriptor
                {
                    EntityType    = typeof(T).AssemblyQualifiedName,
                    StreamType    = StreamTypes.Snapshot,
                    Bucket        = bucket,
                    StreamId      = streamId,
                    Timestamp     = snapshot.Timestamp,
                    Version       = snapshot.Version,
                    Headers       = new Dictionary <string, string>(),
                    CommitHeaders = commitHeaders
                },
                Event = snapshot.Payload,
            };

            Saved.Mark();
            await _store.WriteSnapshot(streamName, e, commitHeaders).ConfigureAwait(false);
        }
Exemple #2
0
        public void Add(Object @event, IDictionary <String, String> headers)
        {
            IWritableEvent writable = new WritableEvent
            {
                Descriptor = new EventDescriptor
                {
                    EntityType = typeof(T).AssemblyQualifiedName,
                    Timestamp  = DateTime.UtcNow,
                    Version    = ++this._version,
                    Headers    = headers
                },
                Event   = @event,
                EventId = Guid.NewGuid()
            };

            var mutators = _builder.BuildAll <IEventMutator>();

            if (mutators != null && mutators.Any())
            {
                foreach (var mutate in mutators)
                {
                    Logger.DebugFormat("Mutating outgoing event {0} with mutator {1}", @event.GetType().FullName, mutate.GetType().FullName);
                    writable = mutate.MutateOutgoing(writable);
                }
            }

            _uncommitted.Add(writable);
        }
        private IWritableEvent MakeWritableEvent(IEvent @event, IDictionary <string, string> headers, bool version = true)
        {
            var writable = new WritableEvent
            {
                Descriptor = new EventDescriptor
                {
                    EntityType = typeof(T).AssemblyQualifiedName,
                    Timestamp  = DateTime.UtcNow,
                    Version    = version ? StreamVersion + 1 : StreamVersion,
                    Headers    = headers
                },
                Event = @event
            };

            var mutators = _builder.BuildAll <IEventMutator>();

            if (mutators == null)
            {
                return(writable);
            }

            foreach (var mutate in mutators)
            {
                Logger.Write(LogLevel.Debug, () => $"Mutating outgoing event {@event.GetType().FullName} with mutator {mutate.GetType().FullName}");
                writable.Event = mutate.MutateOutgoing((IEvent)writable.Event);
            }
            return(writable);
        }
        public async Task Write <T>(T poco, string bucket, string stream, IDictionary <string, string> commitHeaders)
        {
            var streamName = $"{_streamGen(typeof(T), bucket + ".POCO", stream)}";

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

            var descriptor = new EventDescriptor
            {
                EntityType = typeof(T).AssemblyQualifiedName,
                Timestamp  = DateTime.UtcNow,
                Version    = -1,
                Headers    = commitHeaders
            };

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

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

            if (_shouldCache)
            {
                _cache.Cache(streamName, poco);
            }
        }
Exemple #5
0
        private IWritableEvent MakeWritableEvent(IEvent @event, IDictionary <string, string> headers, bool version = true)
        {
            var writable = new WritableEvent
            {
                Descriptor = new EventDescriptor
                {
                    EntityType = typeof(T).AssemblyQualifiedName,
                    StreamType = StreamType,
                    Bucket     = Bucket,
                    StreamId   = StreamId,
                    Timestamp  = DateTime.UtcNow,
                    Version    = version ? StreamVersion + 1 : StreamVersion,
                    Headers    = headers ?? new Dictionary <string, string>()
                },
                EventId = UnitOfWork.NextEventId(_commitId),
                Event   = @event
            };

            var mutators = _builder.BuildAll <IEventMutator>();

            if (!mutators.Any())
            {
                return(writable);
            }

            IMutating mutated = new Mutating(writable.Event, writable.Descriptor.Headers);

            foreach (var mutate in mutators)
            {
                Logger.Write(LogLevel.Debug, () => $"Mutating outgoing event {@event.GetType().FullName} with mutator {mutate.GetType().FullName}");
                mutated = mutate.MutateOutgoing(mutated);
            }

            foreach (var header in mutated.Headers)
            {
                writable.Descriptor.Headers[header.Key] = header.Value;
            }
            writable.Event = mutated.Message;

            return(writable);
        }
        private IFullEvent MakeWritableEvent(string streamType, IEvent @event, IDictionary <string, string> headers)
        {
            var version  = Interlocked.Increment(ref _streamVersion);
            var writable = new WritableEvent
            {
                Descriptor = new EventDescriptor
                {
                    EntityType = typeof(T).AssemblyQualifiedName,
                    StreamType = streamType,
                    Bucket     = Bucket,
                    StreamId   = StreamId,
                    Parents    = Parents,
                    Timestamp  = DateTime.UtcNow,
                    Version    = version,
                    Headers    = headers ?? new Dictionary <string, string>()
                },
                Event = @event
            };

            return(writable);
        }
Exemple #7
0
        public async Task Write <T>(Tuple <long, T> poco, string bucket, Id streamId, IEnumerable <Id> parents, IDictionary <string, string> commitHeaders)
        {
            var streamName = _streamGen(typeof(T), StreamTypes.Poco, bucket, streamId, parents);

            Logger.Write(LogLevel.Debug, () => $"Writing poco to stream id [{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 WritableEvent
            {
                Descriptor = descriptor,
                Event      = poco.Item2,
                EventId    = Guid.NewGuid()
            };

            Saved.Mark();
            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);
            }
        }
        public async Task <int> AddToQueue(string channel, object queued)
        {
            var streamName = $"DELAY.{Assembly.GetEntryAssembly().FullName}.{channel}";

            Logger.Write(LogLevel.Debug, () => $"Appending delayed object to channel [{channel}]");

            var @event = new WritableEvent
            {
                Descriptor = new EventDescriptor
                {
                    EntityType = "DELAY",
                    Timestamp  = DateTime.UtcNow,
                },
                Event = queued,
            };

            var start = StreamPosition.Start;
            var read  = await _store.GetEventsBackwards($"{streamName}.SNAP", StreamPosition.End, 1).ConfigureAwait(false);

            if (read != null && read.Any())
            {
                var snapshot = read.Single().Event as Snapshot;
                start = snapshot.Position + 1;
            }

            read = await _store.GetEventsBackwards(streamName, StreamPosition.End, 1).ConfigureAwait(false);

            _uncommitted.Add(new Tuple <string, WritableEvent>(streamName, @event));
            var existing = _uncommitted.Count(c => c.Item1 == streamName);

            if (read == null || !read.Any())
            {
                return(existing);
            }


            return(existing + (read.Single().Descriptor.Version - start) + 1);
        }