@@ -42,7 +42,7 @@ await cursor.ForEachAsync(change =>
42
42
. Match ( change => change . OperationType == ChangeStreamOperationType . Update ) ;
43
43
44
44
// Opens a change stream and prints the changes as they're received
45
- using ( var cursor = await _restaurantsCollection . WatchAsync ( pipeline ) )
45
+ using ( var cursor = await collection . WatchAsync ( pipeline ) )
46
46
{
47
47
await cursor . ForEachAsync ( change =>
48
48
{
@@ -56,7 +56,7 @@ await cursor.ForEachAsync(change =>
56
56
. Match ( change => change . OperationType == ChangeStreamOperationType . Update ) ;
57
57
58
58
// Opens a change streams and print the changes as they're received
59
- using ( var cursor = _restaurantsCollection . Watch ( pipeline ) )
59
+ using ( var cursor = collection . Watch ( pipeline ) )
60
60
{
61
61
foreach ( var change in cursor . ToEnumerable ( ) )
62
62
{
@@ -65,6 +65,115 @@ await cursor.ForEachAsync(change =>
65
65
}
66
66
// end-change-stream-pipeline
67
67
68
+ // start-split-event-helpers-sync
69
+ // Fetches the next complete change stream event
70
+ private static IEnumerable < ChangeStreamDocument < TDocument > > GetNextChangeStreamEvent < TDocument > (
71
+ IEnumerator < ChangeStreamDocument < TDocument > > changeStreamEnumerator )
72
+ {
73
+ while ( changeStreamEnumerator . MoveNext ( ) )
74
+ {
75
+ var changeStreamEvent = changeStreamEnumerator . Current ;
76
+ if ( changeStreamEvent . SplitEvent != null )
77
+ {
78
+ var fragment = changeStreamEvent ;
79
+ while ( fragment . SplitEvent . Fragment < fragment . SplitEvent . Of )
80
+ {
81
+ changeStreamEnumerator . MoveNext ( ) ;
82
+ fragment = changeStreamEnumerator . Current ;
83
+ MergeFragment ( changeStreamEvent , fragment ) ;
84
+ }
85
+ }
86
+ yield return changeStreamEvent ;
87
+ }
88
+ }
89
+
90
+ // Merges a fragment into the base event
91
+ private static void MergeFragment < TDocument > (
92
+ ChangeStreamDocument < TDocument > changeStreamEvent ,
93
+ ChangeStreamDocument < TDocument > fragment )
94
+ {
95
+ foreach ( var element in fragment . BackingDocument )
96
+ {
97
+ if ( element . Name != "_id" && element . Name != "splitEvent" )
98
+ {
99
+ changeStreamEvent . BackingDocument [ element . Name ] = element . Value ;
100
+ }
101
+ }
102
+ }
103
+ // end-split-event-helpers-sync
104
+
105
+ // start-split-event-helpers-async
106
+ // Fetches the next complete change stream event
107
+ private static async IAsyncEnumerable < ChangeStreamDocument < TDocument > > GetNextChangeStreamEventAsync < TDocument > (
108
+ IAsyncCursor < ChangeStreamDocument < TDocument > > changeStreamCursor )
109
+ {
110
+ var changeStreamEnumerator = GetNextChangeStreamEventFragmentAsync ( changeStreamCursor ) . GetAsyncEnumerator ( ) ;
111
+ while ( await changeStreamEnumerator . MoveNextAsync ( ) )
112
+ {
113
+ var changeStreamEvent = changeStreamEnumerator . Current ;
114
+ if ( changeStreamEvent . SplitEvent != null )
115
+ {
116
+ var fragment = changeStreamEvent ;
117
+ while ( fragment . SplitEvent . Fragment < fragment . SplitEvent . Of )
118
+ {
119
+ await changeStreamEnumerator . MoveNextAsync ( ) ;
120
+ fragment = changeStreamEnumerator . Current ;
121
+ MergeFragment ( changeStreamEvent , fragment ) ;
122
+ }
123
+ }
124
+ yield return changeStreamEvent ;
125
+ }
126
+ }
127
+
128
+ private static async IAsyncEnumerable < ChangeStreamDocument < TDocument > > GetNextChangeStreamEventFragmentAsync < TDocument > (
129
+ IAsyncCursor < ChangeStreamDocument < TDocument > > changeStreamCursor )
130
+ {
131
+ while ( await changeStreamCursor . MoveNextAsync ( ) )
132
+ {
133
+ foreach ( var changeStreamEvent in changeStreamCursor . Current )
134
+ {
135
+ yield return changeStreamEvent ;
136
+ }
137
+ }
138
+ }
139
+
140
+ // Merges a fragment into the base event
141
+ private static void MergeFragment < TDocument > (
142
+ ChangeStreamDocument < TDocument > changeStreamEvent ,
143
+ ChangeStreamDocument < TDocument > fragment )
144
+ {
145
+ foreach ( var element in fragment . BackingDocument )
146
+ {
147
+ if ( element . Name != "_id" && element . Name != "splitEvent" )
148
+ {
149
+ changeStreamEvent . BackingDocument [ element . Name ] = element . Value ;
150
+ }
151
+ }
152
+ }
153
+ // end-split-event-helpers-async
154
+
155
+ // start-split-change-event-sync
156
+ var pipeline = new EmptyPipelineDefinition < ChangeStreamDocument < Restaurant > > ( )
157
+ . ChangeStreamSplitLargeEvent ( ) ;
158
+
159
+ using var cursor = collection . Watch ( pipeline ) ;
160
+ foreach ( var completeEvent in GetNextChangeStreamEvent ( cursor . ToEnumerable ( ) . GetEnumerator ( ) ) )
161
+ {
162
+ Console . WriteLine ( "Received the following change: " + completeEvent . BackingDocument ) ;
163
+ }
164
+ // end-split-change-event-sync
165
+
166
+ // start-split-change-event-async
167
+ var pipeline = new EmptyPipelineDefinition < ChangeStreamDocument < Restaurant > > ( )
168
+ . ChangeStreamSplitLargeEvent ( ) ;
169
+
170
+ using var cursor = await collection . WatchAsync ( pipeline ) ;
171
+ await foreach ( var completeEvent in GetNextChangeStreamEventAsync ( cursor ) )
172
+ {
173
+ Console . WriteLine ( "Received the following change: " + completeEvent . BackingDocument ) ;
174
+ }
175
+ // end-split-change-event-async
176
+
68
177
// start-change-stream-post-image
69
178
var pipeline = new EmptyPipelineDefinition < ChangeStreamDocument < Restaurant > > ( )
70
179
. Match ( change => change . OperationType == ChangeStreamOperationType . Update ) ;
@@ -74,7 +183,7 @@ await cursor.ForEachAsync(change =>
74
183
FullDocument = ChangeStreamFullDocumentOption . UpdateLookup ,
75
184
} ;
76
185
77
- using ( var cursor = _restaurantsCollection . Watch ( pipeline , options ) )
186
+ using ( var cursor = collection . Watch ( pipeline , options ) )
78
187
{
79
188
foreach ( var change in cursor . ToEnumerable ( ) )
80
189
{
@@ -92,7 +201,7 @@ await cursor.ForEachAsync(change =>
92
201
FullDocument = ChangeStreamFullDocumentOption . UpdateLookup ,
93
202
} ;
94
203
95
- using var cursor = await _restaurantsCollection . WatchAsync ( pipeline , options ) ;
204
+ using var cursor = await collection . WatchAsync ( pipeline , options ) ;
96
205
await cursor . ForEachAsync ( change =>
97
206
{
98
207
Console . WriteLine ( change . FullDocument . ToBsonDocument ( ) ) ;
0 commit comments