11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4+ using System . Threading ;
45using System . Threading . Tasks ;
56using Microsoft . Azure . Documents ;
67using Microsoft . Azure . Documents . Client ;
@@ -39,19 +40,28 @@ internal AzureDocumentDbStorageEngine(DocumentClient client, string databaseName
3940 this . databaseUri = UriFactory . CreateDatabaseUri ( databaseName ) ;
4041 }
4142
42- public async Task < IStorageEngine > Initialise ( )
43+ public async Task < IStorageEngine > Initialise ( CancellationToken cancellationToken = default )
4344 {
45+ cancellationToken . ThrowIfCancellationRequested ( ) ;
4446 await CreateDatabaseIfItDoesNotExist ( ) ;
47+
48+ cancellationToken . ThrowIfCancellationRequested ( ) ;
4549 await CreateCollectionIfItDoesNotExist ( ) ;
50+
51+ cancellationToken . ThrowIfCancellationRequested ( ) ;
4652 await CreateAppendStoredProcedureIfItDoesNotExist ( ) ;
4753
54+
55+ cancellationToken . ThrowIfCancellationRequested ( ) ;
4856 await SetDatabaseOfferThroughput ( ) ;
57+
58+ cancellationToken . ThrowIfCancellationRequested ( ) ;
4959 await SetCollectionOfferThroughput ( ) ;
5060
5161 return this ;
5262 }
5363
54- public async Task AppendToStream ( string streamId , IEnumerable < StorageEvent > events )
64+ public async Task AppendToStream ( string streamId , IEnumerable < StorageEvent > events , CancellationToken cancellationToken = default )
5565 {
5666 var docs = events . Select ( d => DocumentDbStorageEvent . FromStorageEvent ( d , this . typeMap , this . jsonSerializer ) ) . ToList ( ) ;
5767
@@ -60,6 +70,7 @@ public async Task AppendToStream(string streamId, IEnumerable<StorageEvent> even
6070 var result = await this . client . ExecuteStoredProcedureAsync < dynamic > (
6171 storedProcLink ,
6272 new RequestOptions { PartitionKey = new PartitionKey ( streamId ) , ConsistencyLevel = this . collectionOptions . ConsistencyLevel } ,
73+ cancellationToken ,
6374 docs ) ;
6475
6576 loggingOptions . OnSuccess ( ResponseInformation . FromWriteResponse ( nameof ( AppendToStream ) , result ) ) ;
@@ -75,7 +86,7 @@ public async Task AppendToStream(string streamId, IEnumerable<StorageEvent> even
7586 }
7687 }
7788
78- public async Task < IReadOnlyCollection < StorageEvent > > ReadStreamForwards ( string streamId , int startPosition , int numberOfEventsToRead )
89+ public async Task < IReadOnlyCollection < StorageEvent > > ReadStreamForwards ( string streamId , int startPosition , int numberOfEventsToRead , CancellationToken cancellationToken = default )
7990 {
8091 int endPosition = numberOfEventsToRead == int . MaxValue ? int . MaxValue : startPosition + numberOfEventsToRead ;
8192
@@ -88,7 +99,7 @@ public async Task<IReadOnlyCollection<StorageEvent>> ReadStreamForwards(string s
8899
89100 while ( eventsQuery . HasMoreResults )
90101 {
91- var response = await eventsQuery . ExecuteNextAsync < DocumentDbStorageEvent > ( ) ;
102+ var response = await eventsQuery . ExecuteNextAsync < DocumentDbStorageEvent > ( cancellationToken ) ;
92103 loggingOptions . OnSuccess ( ResponseInformation . FromReadResponse ( nameof ( ReadStreamForwards ) , response ) ) ;
93104
94105 foreach ( var e in response )
0 commit comments