public async Task Delete(IEnumerable <EventToDispatchRecordTableEntity> records, CancellationToken token) { var operation = new TableBatchOperation(); foreach (var record in records) { operation.Delete(record); } var table = await _table.GetOrCreate(); await table.ExecuteBatchAsync(operation, token); }
public async Task <bool> TryRead(StreamId streamId, IHydrateEvents events, CancellationToken token) { var table = await _table.GetOrCreate(); var hydratedAny = false; var query = table.CreateQuery <EventRecordTableEntity>() .Where(x => x.PartitionKey == streamId.Value) .Where(x => string.Compare(x.RowKey, "E^", StringComparison.Ordinal) > 0) .Where(x => string.Compare(x.RowKey, "F^", StringComparison.Ordinal) < 0) .AsTableQuery(); TableContinuationToken queryToken = null; do { var segment = await table.ExecuteQuerySegmentedAsync(query, queryToken, token); queryToken = segment.ContinuationToken; var eventsFromStore = segment .Select(x => { hydratedAny = true; var serializedEvent = new SerializedEvent(x.Type, x.Payload); return(_events.Deserialize(serializedEvent)); }); events.Hydrate(eventsFromStore); if (token.IsCancellationRequested) { throw new OperationCanceledException(token); } } while (queryToken != null); return(hydratedAny); }
private async Task WriteStream(Session session, CancellationToken token) { var table = await _table.GetOrCreate(); var operation = new TableBatchOperation(); var sessionTableEntity = new SessionRecordTableEntity { PartitionKey = session.StreamId.Value, RowKey = $"S^{session.SessionId.Value}", Created = $"{session.Created:O}", RecordCount = session.Records.Length }; operation.Add(TableOperation.Insert(sessionTableEntity)); foreach (var record in session.Records) { var eventTableEntity = new EventRecordTableEntity { PartitionKey = session.StreamId.Value, RowKey = $"E^{record.Version:x16}", Created = $"{record.Created:O}", SessionId = record.SessionId.Value, Type = record.Type, Payload = record.Payload }; operation.Add(TableOperation.Insert(eventTableEntity)); var dispatchTableEntity = new EventToDispatchRecordTableEntity { PartitionKey = session.StreamId.Value, RowKey = $"D^{record.Version:x16}", AggregateType = session.StreamId.AggregateType.Value, BucketId = session.StreamId.BucketId.Value, AggregateId = session.StreamId.AggregateId.Value, SessionId = session.SessionId.Value, Version = record.Version, Created = $"{record.Created:O}", Type = record.Type, Payload = record.Payload }; operation.Add(TableOperation.Insert(dispatchTableEntity)); } try { await table.ExecuteBatchAsync(operation, token); } catch (StorageException e) { switch (e.RequestInformation.HttpStatusCode) { case (int)HttpStatusCode.Conflict: throw new StreamConcurrentWriteException( $"The stream {session.StreamId.Value} was modified between read and write or the session {session.SessionId.Value} was already registered.", e ); default: throw; } } }