Beispiel #1
0
        public void Save(IAggregate aggregate, Guid commitId, Action <IDictionary <string, object> > updateHeaders)
        {
            EnsureConnected();

            var commitHeaders = new Dictionary <string, object>
            {
                { CommitIdHeader, commitId },
                { AggregateClrTypeHeader, aggregate.GetType().AssemblyQualifiedName }
            };

            updateHeaders(commitHeaders);

            var streamName      = aggregate.Id;
            var newEvents       = aggregate.GetUncommittedEvents().Cast <object>().ToList();
            var originalVersion = aggregate.Version - newEvents.Count;
            var expectedVersion = originalVersion == 0 ? -1 : originalVersion;

            expectedVersion--;

            var transaction = _eventStoreConnection.StartTransaction(streamName, expectedVersion);

            var preparedEvents = _adapter.PrepareEvents(newEvents, commitHeaders).ToList();

            var position = 0;

            while (position < preparedEvents.Count)
            {
                var pageEvents = preparedEvents.Skip(position).Take(WritePageSize);
                transaction.Write(pageEvents);
                position += WritePageSize;
            }

            transaction.Commit();
        }
        public void Save(IAggregate aggregate, Guid commitId, Action <IDictionary <string, object> > updateHeaders)
        {
            var commitHeaders = new Dictionary <string, object>
            {
                { COMMIT_ID_HEADER, commitId },
                { AGGREGATE_CLR_TYPE_HEADER, aggregate.GetType().AssemblyQualifiedName }
            };

            updateHeaders(commitHeaders);

            var streamName      = m_AggregateIdToStreamName(aggregate.GetType(), aggregate.Id);
            var newEvents       = aggregate.GetUncommittedEvents().Cast <object>().ToList();
            var originalVersion = aggregate.Version - newEvents.Count;
            var expectedVersion = originalVersion == 0 ? ExpectedVersion.NoStream : originalVersion;
            var eventsToSave    = newEvents.Select(e => ToEventData(Guid.NewGuid(), e, commitHeaders)).ToList();


            var transaction = m_EventStoreConnection.StartTransaction(streamName, expectedVersion);

            try
            {
                if (eventsToSave.Count < WRITE_PAGE_SIZE)
                {
                    transaction.Write(eventsToSave);
                }
                else
                {
                    var position = 0;
                    while (position < eventsToSave.Count)
                    {
                        var pageEvents = eventsToSave.Skip(position).Take(WRITE_PAGE_SIZE);
                        transaction.Write(pageEvents);
                        position += WRITE_PAGE_SIZE;
                    }
                }
                //TODO: not prod code. Need to arrange serialization, data saved to ES should be same as sent to queue
                foreach (var @event in newEvents)
                {
                    m_EventsPublisher.PublishEvent(@event);
                }
                transaction.Commit();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                //TODO:logging
                transaction.Rollback();
            }
            aggregate.ClearUncommittedEvents();
        }
        public void Save(TAggregate aggregate, Action <IDictionary <string, object> > updateHeaders)
        {
            var commitId      = Guid.NewGuid();
            var commitHeaders = new Dictionary <string, object>
            {
                { CommitIdHeader, commitId },
                { AggregateClrTypeHeader, aggregate.GetType().AssemblyQualifiedName }
            };

            if (updateHeaders != null)
            {
                updateHeaders(commitHeaders);
            }

            var streamName      = _aggregateIdToStreamName(aggregate.GetType(), aggregate.Id);
            var newEvents       = aggregate.GetUncommittedChanges().ToList();
            var originalVersion = aggregate.Version - newEvents.Count;
            var eventsToSave    = newEvents.Select(e => ToEventData(aggregate.Id, e, commitHeaders)).ToList();


            if (eventsToSave.Count < WritePageSize)
            {
                _eventStoreConnection.AppendToStream(streamName, originalVersion, eventsToSave);
            }
            else
            {
                var transaction = _eventStoreConnection.StartTransaction(streamName, originalVersion);

                var position = 0;
                while (position < eventsToSave.Count)
                {
                    var pageEvents = eventsToSave.Skip(position).Take(WritePageSize);
                    transaction.Write(pageEvents);
                    position += WritePageSize;
                }

                transaction.Commit();
            }

            aggregate.MarkChangesAsCommitted();

            if (Enumerable.Range(aggregate.Version + 1, newEvents.Count).Any(v => v % SnapshotStep == 0))
            {
                this.SaveSnapshot(aggregate);
            }
        }
        public void Save(IAggregate aggregate, Guid commitId, Action <IDictionary <string, object> > updateHeaders)
        {
            var commitHeaders = new Dictionary <string, object>
            {
                { CommitIdHeader, commitId },
                { AggregateClrTypeHeader, aggregate.GetType().AssemblyQualifiedName }
            };

            updateHeaders(commitHeaders);

            var streamName      = _aggregateIdToStreamName(aggregate.GetType(), aggregate.Id);
            var newEvents       = aggregate.GetUncommittedEvents().Cast <object>().ToList();
            var originalVersion = aggregate.Version - newEvents.Count;
            var expectedVersion = originalVersion == 0 ? ExpectedVersion.NoStream : originalVersion - 1;
            var eventsToSave    = newEvents.Select(e => ToEventData(Guid.NewGuid(), e, commitHeaders)).ToList();

            if (eventsToSave.Count < WritePageSize)
            {
                _eventStoreConnection.AppendToStream(streamName, expectedVersion, eventsToSave);
            }
            else
            {
                var transaction = _eventStoreConnection.StartTransaction(streamName, expectedVersion);

                var position = 0;
                while (position < eventsToSave.Count)
                {
                    var pageEvents = eventsToSave.Skip(position).Take(WritePageSize);
                    transaction.Write(pageEvents);
                    position += WritePageSize;
                }

                transaction.Commit();
            }

            foreach (var newEvent in newEvents)
            {
                _bus.Publish(newEvent);
            }

            aggregate.ClearUncommittedEvents();
        }
Beispiel #5
0
        void SaveEventsInBatch(string stream_name, int expected_version,
                               IEnumerable <Event> events, IEventSerializer serializer)
        {
            EventStoreTransaction trasaction = connection_
                                               .StartTransaction(stream_name, expected_version);

            EventData[] serialized_events = Serialize(events, serializer).ToArray();

            int position = 0;

            while (position < serialized_events.Length)
            {
                var page_events = serialized_events
                                  .Skip(position)
                                  .Take(kWritePageSize);
                trasaction.Write(page_events);
                position += kWritePageSize;
            }
            trasaction.Commit();
        }
        public override void SetUp()
        {
            base.SetUp();
            _node = new MiniNode(PathName);
            _node.Start();

            _firstEvent = TestEvent.NewTestEvent();

            _connection = TestConnection.Create(_node.TcpEndPoint);
            _connection.Connect();

            Assert.AreEqual(2, _connection.AppendToStream("test-stream",
                                                          ExpectedVersion.NoStream,
                                                          _firstEvent,
                                                          TestEvent.NewTestEvent(),
                                                          TestEvent.NewTestEvent()).NextExpectedVersion);

            using (var transaction = _connection.StartTransaction("test-stream", 2))
            {
                Assert.AreEqual(2, transaction.Commit().NextExpectedVersion);
            }
        }
        public override void SetUp()
        {
            base.SetUp();
            _node = new MiniNode(PathName);
            _node.Start();

            _firstEvent = TestEvent.NewTestEvent();

            _connection = TestConnection.Create(_node.TcpEndPoint);
            _connection.Connect();

            Assert.AreEqual(2, _connection.AppendToStream("test-stream",
                                                          ExpectedVersion.NoStream,
                                                          _firstEvent,
                                                          TestEvent.NewTestEvent(),
                                                          TestEvent.NewTestEvent()).NextExpectedVersion);

            using (var transaction = _connection.StartTransaction("test-stream", 2))
            {
                Assert.AreEqual(2, transaction.Commit().NextExpectedVersion);
            }
        }
 protected EventStoreTransaction TransStart(string streamId, string login, string password)
 {
     return(Connection.StartTransaction(streamId, ExpectedVersion.Any,
                                        login == null && password == null ? null : new UserCredentials(login, password)));
 }
Beispiel #9
0
 public OngoingTransaction StartTransaction(int expectedVersion)
 {
     return(new OngoingTransaction(_store.StartTransaction(_stream, expectedVersion)));
 }