public void Save(IEventSource source) { try { using (var session = _documentStore.OpenSession()) { session.UseOptimisticConcurrency = true; foreach (var sourcedEvent in source.GetUncommittedEvents()) { session.Store(new StoredEvent { Data = sourcedEvent, EventSequence = sourcedEvent.EventSequence, EventSourceId = sourcedEvent.EventSourceId, Id = sourcedEvent.EventSourceId + "/" + sourcedEvent.EventSequence }); } session.SaveChanges(); } } catch (Raven.Database.Exceptions.ConcurrencyException) { throw new ConcurrencyException(source.EventSourceId, source.Version); } }
public void Save(IEventSource source) { try { using (var session = _documentStore.OpenSession()) { session.UseOptimisticConcurrency = true; foreach (var sourcedEvent in source.GetUncommittedEvents()) { session.Store(new StoredEvent { Data = sourcedEvent, EventSequence = sourcedEvent.EventSequence, EventSourceId = sourcedEvent.EventSourceId, Id = sourcedEvent.EventSourceId + "/" + sourcedEvent.EventSequence }); } session.SaveChanges(); } } catch (Raven.Database.Exceptions.ConcurrencyException) { throw new ConcurrencyException(source.Id, source.Version); } }
/// <summary> /// Save all uncommited events from a specific aggregate root. /// </summary> /// <param name = "source"> /// The aggregate root for which all uncommited events should be saved. /// </param> public IEnumerable <IEvent> Save(IEventSource source) { var uncommittedEvents = source.GetUncommittedEvents(); SaveEvents(source.Id, source.UncommittedVersion, uncommittedEvents); source.AcceptChanges(); return(uncommittedEvents); }
public void Save(IEventSource source) { FileInfo file = source.EventSourceId.GetEventStoreFileInfo(_path); if (!file.Exists && !file.Directory.Exists) { file.Directory.Create(); } try { source.EventSourceId.GetWriteLock(); if (file.Exists) { if (GetVersion(source.EventSourceId) > source.InitialVersion) { throw new ConcurrencyException(source.EventSourceId, source.Version); } } using (var writer = file.OpenWrite()) { writer.Seek(0, SeekOrigin.End); var indicies = new long[source.GetUncommittedEvents().Count()]; var i = 0; var index = writer.Position; foreach (SourcedEvent sourcedEvent in source.GetUncommittedEvents()) { StoredEvent <JObject> storedEvent = _formatter.Serialize(sourcedEvent); var bytes = storedEvent.GetBytes(); writer.Write(BitConverter.GetBytes(bytes.Length), 0, 4); writer.Write(bytes, 0, bytes.Length); indicies[i++] = index; index += bytes.Length; } UpdateEventSourceIndexFile(source.EventSourceId, indicies); writer.Flush(); } } finally { source.EventSourceId.ReleaseWriteLock(); } }
public void Save(IEventSource source) { Queue<ISourcedEvent> events; var eventsToCommit = source.GetUncommittedEvents(); if (!_events.TryGetValue(source.Id, out events)) { events = new Queue<ISourcedEvent>(); _events.Add(source.Id, events); } foreach (var evnt in eventsToCommit) { events.Enqueue(evnt); } }
public void Save(IEventSource source) { Queue <ISourcedEvent> events; var eventsToCommit = source.GetUncommittedEvents(); if (!_events.TryGetValue(source.EventSourceId, out events)) { events = new Queue <ISourcedEvent>(); _events.Add(source.EventSourceId, events); } foreach (var evnt in eventsToCommit) { events.Enqueue(evnt); } }
public virtual void Save(IEventSource source) { IEnumerable<ISourcedEvent> eventsToSave = source.GetUncommittedEvents(); IDBCollection sources = database.GetCollection("Events"); if (IsNewEventSource(source)) { InsertNewEventSource(source, eventsToSave, sources); VerifyInsertSuccessful(source); } else { PushOptimisticUpdate(source, eventsToSave, sources); VerifyUpdateSuccessful(source); } }
public virtual void Save(IEventSource source) { IEnumerable <ISourcedEvent> eventsToSave = source.GetUncommittedEvents(); IDBCollection sources = database.GetCollection("Events"); if (IsNewEventSource(source)) { InsertNewEventSource(source, eventsToSave, sources); VerifyInsertSuccessful(source); } else { PushOptimisticUpdate(source, eventsToSave, sources); VerifyUpdateSuccessful(source); } }
/// <summary> /// Saves all events from an event provider. /// </summary> /// <param name="eventSource">The eventsource.</param> public void Save(IEventSource eventSource) { // Get all events. IEnumerable <ISourcedEvent> events = eventSource.GetUncommittedEvents(); // Create new connection. using (var connection = new SqlConnection(_connectionString)) { // Open connection and begin a transaction so we can // commit or rollback all the changes that has been made. connection.Open(); using (SqlTransaction transaction = connection.BeginTransaction()) { try { // Get the current version of the event provider. int?currentVersion = GetVersion(eventSource.EventSourceId, transaction); // Create new event provider when it is not found. if (currentVersion == null) { AddEventSource(eventSource, transaction); } else if (currentVersion.Value != eventSource.InitialVersion) { throw new ConcurrencyException(eventSource.EventSourceId, eventSource.Version); } // Save all events to the store. SaveEvents(events, transaction); // Update the version of the provider. UpdateEventSourceVersion(eventSource, transaction); // Everything is handled, commint transaction. transaction.Commit(); } catch { // Something went wrong, rollback transaction. transaction.Rollback(); throw; } } } }
public void Save(IEventSource source) { FileInfo file = source.EventSourceId.GetEventStoreFileInfo(_path); if (!file.Exists && !file.Directory.Exists) file.Directory.Create(); try { source.EventSourceId.GetWriteLock(); if (file.Exists) { if (GetVersion(source.EventSourceId) > source.InitialVersion) { throw new ConcurrencyException(source.EventSourceId, source.Version); } } using (var writer = file.OpenWrite()) { writer.Seek(0, SeekOrigin.End); var indicies = new long[source.GetUncommittedEvents().Count()]; var i = 0; var index = writer.Position; foreach (SourcedEvent sourcedEvent in source.GetUncommittedEvents()) { StoredEvent<JObject> storedEvent = _formatter.Serialize(sourcedEvent); var bytes = storedEvent.GetBytes(); writer.Write(BitConverter.GetBytes(bytes.Length), 0, 4); writer.Write(bytes, 0, bytes.Length); indicies[i++] = index; index += bytes.Length; } UpdateEventSourceIndexFile(source.EventSourceId, indicies); writer.Flush(); } } finally { source.EventSourceId.ReleaseWriteLock(); } }
public void Save(IEventSource source) { var events = source.GetUncommittedEvents(); _context.WithConnection(connection => _context.WithTransaction(connection, transaction => { var currentVersion = GetVersion(source.EventSourceId, transaction); if (currentVersion == null) { AddEventSource(source, transaction); } else if (currentVersion.Value != source.InitialVersion) { throw new ConcurrencyException(source.EventSourceId, source.Version); } SaveEvents(events, source.EventSourceId, transaction); UpdateEventSourceVersion(source, transaction); })); }
/// <summary> /// Saves all events from an event provider. /// </summary> /// <param name="provider">The eventsource.</param> public void Save(IEventSource eventSource) { // Get all events. IEnumerable<ISourcedEvent> events = eventSource.GetUncommittedEvents(); // Create new connection. using (var connection = new SqlConnection(_connectionString)) { // Open connection and begin a transaction so we can // commit or rollback all the changes that has been made. connection.Open(); using (SqlTransaction transaction = connection.BeginTransaction()) { try { // Get the current version of the event provider. int? currentVersion = GetVersion(eventSource.Id, transaction); // Create new event provider when it is not found. if (currentVersion == null) { AddEventSource(eventSource, transaction); } else if (currentVersion.Value != eventSource.InitialVersion) { throw new ConcurrencyException(eventSource.Id, eventSource.Version); } // Save all events to the store. SaveEvents(events, eventSource.Id, transaction); // Update the version of the provider. UpdateEventSourceVersion(eventSource, transaction); // Everything is handled, commint transaction. transaction.Commit(); } catch { // Something went wrong, rollback transaction. transaction.Rollback(); throw; } } } }
/// <summary> /// Save all events from a specific event provider. /// </summary> /// <exception cref="T:Ncqrs.Eventing.Storage.ConcurrencyException">Occurs when there is already a newer version of the event provider stored in the event store.</exception><param name="source">The source that should be saved.</param><requires description="source cannot be null." exception="T:System.ArgumentNullException">source != null</requires><exception cref="T:System.ArgumentNullException">source == null</exception><ensures description="Return should never be null.">Contract.Result<IEnumerable<IEvent>>() != null</ensures> public void Save(IEventSource source) { var context = _tableClient.GetDataServiceContext(); var sourceInStore = GetSourceFromStore(context, source.EventSourceId); var uncommitedEvents = source.GetUncommittedEvents(); if (sourceInStore == null) { sourceInStore = InsertNewEventSource(context, source); } else { if (source.Version != sourceInStore.Version) throw new ConcurrencyException(source.EventSourceId, source.Version); } // Update version. sourceInStore.Version = source.Version + uncommitedEvents.Count(); // TODO: We can do this better. context.UpdateObject(sourceInStore); context.SaveChanges(SaveChangesOptions.Batch); // TODO: Validate response. PushEventToStore(context, source.EventSourceId, uncommitedEvents); // TODO: Validate response. var response = context.SaveChanges(SaveChangesOptions.Batch); }
public void Save(IEventSource source) { var events = source.GetUncommittedEvents(); using (var conn = new SQLiteConnection(_connectionString)) { conn.Open(); using (var transaction = conn.BeginTransaction()) try { var currentVersion = GetVersion(source.EventSourceId, transaction); if (currentVersion == null) AddEventSource(source, transaction); else if (currentVersion.Value != source.InitialVersion) throw new ConcurrencyException(source.EventSourceId, source.Version); SaveEvents(events, source.EventSourceId, transaction); UpdateEventSourceVersion(source, transaction); transaction.Commit(); } catch { transaction.Rollback(); throw; } } }
public void Save(IEventSource source) { var events = source.GetUncommittedEvents(); _context.WithConnection(connection => _context.WithTransaction(connection, transaction => { var currentVersion = GetVersion(source.EventSourceId, transaction); if (currentVersion == null) AddEventSource(source, transaction); else if (currentVersion.Value != source.InitialVersion) throw new ConcurrencyException(source.EventSourceId, source.Version); SaveEvents(events, source.EventSourceId, transaction); UpdateEventSourceVersion(source, transaction); })); }