|
1 |
| -using Microsoft.Azure.Documents.ChangeFeedProcessor; |
2 |
| -using Microsoft.Azure.Documents; |
3 |
| -using Microsoft.Azure.Documents.Client; |
4 |
| -using System; |
5 |
| -using System.Collections.Generic; |
6 |
| -using System.Threading; |
7 |
| -using System.Threading.Tasks; |
| 1 | +//--------------------------------------------------------------------------------- |
| 2 | +// <copyright file="DocumentFeedObserver.cs" company="Microsoft"> |
| 3 | +// Microsoft (R) Azure SDK |
| 4 | +// Software Development Kit |
| 5 | +// |
| 6 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 7 | +// |
| 8 | +// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, |
| 9 | +// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES |
| 10 | +// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | +// </copyright> |
| 12 | +//--------------------------------------------------------------------------------- |
8 | 13 |
|
9 |
| -class DocumentFeedObserver : IChangeFeedObserver |
| 14 | +namespace ChangeFeedMigrationSample |
10 | 15 | {
|
11 |
| - private static int s_totalDocs = 0; |
12 |
| - private DocumentCollectionInfo collectionInfo; |
13 |
| - private DocumentClient client; |
| 16 | + using System; |
| 17 | + using System.Collections.Generic; |
| 18 | + using System.Threading; |
| 19 | + using System.Threading.Tasks; |
| 20 | + using Microsoft.Azure.Documents; |
| 21 | + using Microsoft.Azure.Documents.ChangeFeedProcessor; |
| 22 | + using Microsoft.Azure.Documents.Client; |
14 | 23 |
|
15 |
| - public DocumentFeedObserver(DocumentClient client, DocumentCollectionInfo destCollInfo) |
| 24 | + /// <summary> |
| 25 | + /// Cass to create instance of document feed observer. |
| 26 | + /// </summary> |
| 27 | + public class DocumentFeedObserver : IChangeFeedObserver |
16 | 28 | {
|
17 |
| - this.client = client; |
18 |
| - this.collectionInfo = destCollInfo; |
19 |
| - } |
| 29 | + private static int s_totalDocs = 0; |
| 30 | + private DocumentCollectionInfo collectionInfo; |
| 31 | + private DocumentClient client; |
| 32 | + private Uri destinationCollectionUri; |
20 | 33 |
|
21 |
| - public Task OpenAsync(ChangeFeedObserverContext context) |
22 |
| - { |
23 |
| - Console.WriteLine("Worker opened, {0}", context.PartitionKeyRangeId); |
24 |
| - return Task.CompletedTask; |
25 |
| - } |
| 34 | + public DocumentFeedObserver(DocumentClient client, DocumentCollectionInfo destCollInfo) |
| 35 | + { |
| 36 | + this.client = client; |
| 37 | + this.collectionInfo = destCollInfo; |
| 38 | + this.destinationCollectionUri = UriFactory.CreateDocumentCollectionUri(this.collectionInfo.DatabaseName, this.collectionInfo.CollectionName); |
| 39 | + } |
26 | 40 |
|
27 |
| - public Task CloseAsync(ChangeFeedObserverContext context, ChangeFeedObserverCloseReason reason) |
28 |
| - { |
29 |
| - Console.WriteLine("Worker closed, {0}", context.PartitionKeyRangeId); |
30 |
| - Console.WriteLine("Reason for shutdown, {0}", reason); |
31 |
| - return Task.CompletedTask; |
32 |
| - } |
| 41 | + public Task OpenAsync(ChangeFeedObserverContext context) |
| 42 | + { |
| 43 | + Console.WriteLine("Worker opened, {0}", context.PartitionKeyRangeId); |
| 44 | + return Task.CompletedTask; |
| 45 | + } |
33 | 46 |
|
34 |
| - public Task ProcessChangesAsync(ChangeFeedObserverContext context, IReadOnlyList<Document> docs) |
35 |
| - { |
| 47 | + public Task CloseAsync(ChangeFeedObserverContext context, ChangeFeedObserverCloseReason reason) |
| 48 | + { |
| 49 | + Console.WriteLine("Worker closed, {0}", context.PartitionKeyRangeId); |
| 50 | + Console.WriteLine("Reason for shutdown, {0}", reason); |
| 51 | + return Task.CompletedTask; |
| 52 | + } |
36 | 53 |
|
37 |
| - Console.WriteLine("Change feed: total {0} doc(s)", Interlocked.Add(ref s_totalDocs, docs.Count)); |
38 |
| - foreach (Document doc in docs) |
| 54 | + public Task ProcessChangesAsync(ChangeFeedObserverContext context, IReadOnlyList<Document> docs) |
39 | 55 | {
|
40 |
| - Console.WriteLine(doc.ToString()); |
41 |
| - this.client.UpsertDocumentAsync( |
42 |
| - UriFactory.CreateDocumentCollectionUri(this.collectionInfo.DatabaseName, this.collectionInfo.CollectionName), |
43 |
| - doc); |
| 56 | + Console.WriteLine("Change feed: total {0} doc(s)", Interlocked.Add(ref s_totalDocs, docs.Count)); |
| 57 | + foreach (Document doc in docs) |
| 58 | + { |
| 59 | + Console.WriteLine(doc.Id.ToString()); |
| 60 | + this.client.UpsertDocumentAsync(this.destinationCollectionUri, doc); |
| 61 | + } |
| 62 | + |
| 63 | + return Task.CompletedTask; |
44 | 64 | }
|
45 |
| - return Task.CompletedTask; |
46 | 65 | }
|
47 | 66 | }
|
48 |
| - |
|
0 commit comments