예제 #1
0
        public async Task <IReadOnlyCollection <StorageEvent> > ReadStreamForwards(string streamId, int startPosition, int numberOfEventsToRead)
        {
            int endPosition = numberOfEventsToRead == int.MaxValue ? int.MaxValue : startPosition + numberOfEventsToRead;

            var eventsQuery = this.client.CreateDocumentQuery <DocumentDbStorageEvent>(commitsLink)
                              .Where(x => x.StreamId == streamId && x.EventNumber >= startPosition && x.EventNumber <= endPosition)
                              .OrderBy(x => x.EventNumber)
                              .AsDocumentQuery();

            var events = new List <StorageEvent>();

            while (eventsQuery.HasMoreResults)
            {
                var response = await eventsQuery.ExecuteNextAsync <DocumentDbStorageEvent>();

                loggingOptions.OnSuccess(ResponseInformation.FromReadResponse(nameof(ReadStreamForwards), response));

                foreach (var e in response)
                {
                    events.Add(e.ToStorageEvent(this.typeMap));
                }
            }

            return(events.AsReadOnly());
        }
예제 #2
0
        public async Task AppendToStream(string streamId, IEnumerable <StorageEvent> events)
        {
            var documents = events
                            .Select(
                document => DocumentDbStorageEvent.FromStorageEvent(
                    document,
                    typeMap,
                    collectionOptions.DocumentTimeToLiveSeconds))
                            .ToList();

            try
            {
                var result = await client.ExecuteStoredProcedureAsync <dynamic>(
                    appendStoredProcedureLink,
                    new RequestOptions
                {
                    PartitionKey     = new PartitionKey(streamId),
                    ConsistencyLevel = collectionOptions.ConsistencyLevel
                },
                    documents);

                loggingOptions.OnSuccess(ResponseInformation.FromWriteResponse(nameof(AppendToStream), result));
            }
            catch (DocumentClientException exception)
            {
                if (exception.Error.Message.Contains(ConcurrencyConflictErrorKey))
                {
                    throw new ConcurrencyException(exception.Error.Message, exception);
                }

                throw;
            }
        }
예제 #3
0
        public async Task <IReadOnlyCollection <StorageEvent> > ReadStreamForwardsFromLast(string streamId, Predicate <StorageEvent> readFromHere)
        {
            using (var eventsQuery = client.CreateDocumentQuery <DocumentDbStorageEvent>(commitsLink)
                                     .Where(x => x.StreamId == streamId)
                                     .OrderByDescending(x => x.EventNumber)
                                     .AsDocumentQuery())
            {
                var eventsInReverseOrder = new List <StorageEvent>();
                var finished             = false;

                while (!finished && eventsQuery.HasMoreResults)
                {
                    var response = await eventsQuery.ExecuteNextAsync <DocumentDbStorageEvent>();

                    loggingOptions.OnSuccess(ResponseInformation.FromReadResponse(nameof(ReadStreamForwardsFromLast), response));

                    foreach (var e in response)
                    {
                        var storageEvent = e.ToStorageEvent(typeMap);
                        eventsInReverseOrder.Add(storageEvent);

                        if (readFromHere(storageEvent))
                        {
                            finished = true;
                            break;
                        }
                    }
                }

                return(eventsInReverseOrder
                       .Reverse <StorageEvent>()
                       .ToList()
                       .AsReadOnly());
            }
        }
예제 #4
0
        public async Task DeleteStream(string streamId)
        {
            while (true)
            {
                var result = await client.ExecuteStoredProcedureAsync <dynamic>(
                    deleteStoredProcedureLink,
                    new RequestOptions { PartitionKey = new PartitionKey(streamId), ConsistencyLevel = collectionOptions.ConsistencyLevel },
                    streamId);

                if ((bool)result.Response.continuation)
                {
                    continue;
                }

                loggingOptions.OnSuccess(ResponseInformation.FromWriteResponse(nameof(DeleteStream), result));

                break;
            }
        }
예제 #5
0
 internal void OnSuccess(ResponseInformation response)
 {
     Success?.Invoke(response);
 }