Exemple #1
0
        /// <summary>
        /// Copy the content of one appender to another. The bytestreams will be identical.
        /// Use AddEvents(appender, events) to compress the bytestreams.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="to"></param>
        /// <param name="version"></param>
        public static void CopyAppender(this IAppendOnlyStore source, IAppendOnlyStore to, int version)
        {
            var serializer = new BinaryFormatterSerializer();

            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (to == null)
            {
                throw new ArgumentNullException("to");
            }

            var versions = new Dictionary <string, int>();

            foreach (var record in source.ReadRecords(0, version))
            {
                if (!versions.ContainsKey(record.Name))
                {
                    versions[record.Name] = 0;
                }

                var events = serializer.DeserializeEvent(record.Data);
                to.Append(record.Name, record.Data, versions[record.Name]);
                versions[record.Name]++;
            }
        }
Exemple #2
0
        public void AppendToStream(IIdentity id, long originalVersion, ICollection <IEvent> events)
        {
            if (events.Count == 0)
            {
                return;
            }
            var name = IdentityToString(id);
            var data = SerializeEvent(events.ToArray());

            try
            {
                _appendOnlyStore.Append(name, data, originalVersion);
                if (NewEventsArrived != null)
                {
                    NewEventsArrived(events.Count);
                }
            }
            catch (AppendOnlyStoreConcurrencyException e)
            {
                // load server events
                var server = LoadEventStream(id, 0, int.MaxValue);
                // throw a real problem
                throw OptimisticConcurrencyException.Create(server.Version, e.ExpectedStreamVersion, id, server.Events);
            }

            // technically there should be a parallel process that queries new changes
            // from the event store and sends them via messages (avoiding 2PC problem).
            // however, for demo purposes, we'll just send them to the console from here
            //Console.ForegroundColor = ConsoleColor.DarkGreen;
            //foreach (var @event in events)
            //{
            //    Console.WriteLine("  {0} r{1} Event: {2}", id,originalVersion, @event);
            //}
            //Console.ForegroundColor = ConsoleColor.DarkGray;
        }
		/// <summary>
		/// Copy the content of one appender to another. The bytestreams will be identical.
		/// Use AddEvents(appender, events) to compress the bytestreams.
		/// </summary>
		/// <param name="source"></param>
		/// <param name="to"></param>
		/// <param name="version"></param>
        public static void CopyAppender(this IAppendOnlyStore source, IAppendOnlyStore to, int version)
        {
            var serializer = new BinaryFormatterSerializer();

            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (to == null)
            {
                throw new ArgumentNullException("to");
            }

            var versions = new Dictionary<string, int>();

            foreach (var record in source.ReadRecords(0, version))
            {
                if (!versions.ContainsKey(record.Name))
                {
                    versions[record.Name] = 0;
                }

                var events = serializer.DeserializeEvent(record.Data);
                to.Append(record.Name, record.Data, versions[record.Name]);
                versions[record.Name]++;
            }
        }
Exemple #4
0
        public StoredEvent Append(IDomainEvent domainEvent)
        {
            var storedEvent = new StoredEvent(
                domainEvent.GetType().FullName + ", " + domainEvent.GetType().Assembly.GetName().Name,
                domainEvent.OccurredOn,
                domainEvent.ToString(),
                domainEvent.EventId,
                StoredEvent.ToStorePayload(domainEvent),
                domainEvent.UserId,
                domainEvent.ProcessId,
                domainEvent.UserId);

            try
            {
                _appendOnlyStore.Append(storedEvent);

                return(storedEvent);
            }
            catch (Exception e)
            {
                CustomErrorSignal.Handle(e);
            }

            return(null);
        }
Exemple #5
0
        public void AppendToStream(IIdentity id, long originalVersion, IEnumerable <Event> events)
        {
            if (!events.Any())
            {
                return;
            }

            var name = IdentityToString(id);

            var expectedVersion = originalVersion;

            foreach (var @event in events)
            {
                var data = SerializeEvent(@event);

                try
                {
                    appendOnlyStore.Append(name, @event.Date, data, expectedVersion);

                    expectedVersion++;
                }
                catch (AppendOnlyStoreConcurrencyException ex)
                {
                    var serverEvents = LoadEvents(id, 0, long.MaxValue);

                    var lastEvent = serverEvents.Last();

                    throw OptimisticConcurrencyException.Create(lastEvent.Version, ex.ExpectedVersion, id, serverEvents);
                }
            }
        }
Exemple #6
0
        public void AppendToStream <TType>(Guid id, long version, ICollection <IEvent> events, params IDomainEvent[] domainEvents)
        {
            //salvar os dados em uma lista interna
            var aggregateType = typeof(TType).Name;

            _appendOnlyStore.Append(id, aggregateType, version, events);
            _eventBus.Publish(domainEvents);
        }
Exemple #7
0
        public void Handle(ClientMessage.AppendEvents message)
        {
            _store.Append(message.EventStream, message.Data, message.ExpectedVersion);


            Log.Info("Storage service got request");
            message.Envelope(new ClientMessage.AppendEventsCompleted());
        }
Exemple #8
0
        public void AppendToStream(string streamName, ICollection <IEvent> events, int originalVersion)
        {
            var data = JsonConvert.SerializeObject(events, new JsonSerializerSettings {
                TypeNameHandling = TypeNameHandling.Auto
            });

            _appendOnlyStore.Append(streamName, data, originalVersion);
            foreach (var @event in events)
            {
                _messaging.Publish(@event);
            }
        }
 public void AppendToStore(string name, ICollection <MessageAttribute> attribs, long streamVersion, ICollection <object> messages)
 {
     using (var mem = new MemoryStream())
     {
         _serializer.WriteAttributes(attribs, mem);
         _serializer.WriteCompactInt(messages.Count, mem);
         foreach (var message in messages)
         {
             _serializer.WriteMessage(message, message.GetType(), mem);
         }
         _appendOnlyStore.Append(name, mem.ToArray(), streamVersion);
     }
 }
Exemple #10
0
 public void AppendToStore(string name, long streamVersion, ICollection <object> messages)
 {
     using (var mem = new MemoryStream())
         using (var bin = new BinaryWriter(mem))
         {
             bin.Write(messages.Count);
             foreach (var message in messages)
             {
                 var contract = _typeToContract[message.GetType()];
                 bin.Write(contract);
                 using (var inner = new MemoryStream())
                 {
                     RuntimeTypeModel.Default.Serialize(inner, message);
                     bin.Write((int)inner.Position);
                     bin.Write(inner.ToArray());
                 }
             }
             _appendOnlyStore.Append(name, mem.ToArray(), streamVersion);
         }
 }
Exemple #11
0
        public void AppendToStream <TType>(Guid id, long version, ICollection <IEvent> events)
        {
            var aggregateType = typeof(TType).Name;

            _appendOnlyStore.Append(id, aggregateType, version, events);
        }