From a5469f22c932e9ac376881dd8aa0b5a205dea4fb Mon Sep 17 00:00:00 2001 From: Justine Cocchi Date: Mon, 16 Dec 2024 12:47:40 -0800 Subject: [PATCH] [Internal] Samples: Fixes all versions and deletes change feed samples (#4923) # Pull Request Template ## Description Update samples for all versions and deletes pull model and change feed processor to handle id for delete operations. ## Type of change Please delete options that are not relevant. - [] Bug fix (non-breaking change which fixes an issue) - [] New feature (non-breaking change which adds functionality) - [] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [x] This change requires a documentation update ## Closing issues To automatically close an issue: closes #IssueNumber Co-authored-by: Kiran Kumar Kolli --- .../Program.cs | 28 ++++++++----------- .../Program.cs | 2 +- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/Microsoft.Azure.Cosmos.Samples/Usage/CFPullModelAllVersionsAndDeletesMode/Program.cs b/Microsoft.Azure.Cosmos.Samples/Usage/CFPullModelAllVersionsAndDeletesMode/Program.cs index e6f86b9ed4..676e344256 100644 --- a/Microsoft.Azure.Cosmos.Samples/Usage/CFPullModelAllVersionsAndDeletesMode/Program.cs +++ b/Microsoft.Azure.Cosmos.Samples/Usage/CFPullModelAllVersionsAndDeletesMode/Program.cs @@ -60,12 +60,12 @@ static async Task CreateAllVersionsAndDeletesChangeFeedIterator(Containe Console.WriteLine("Creating ChangeFeedIterator to read the change feed in All Versions and Deletes mode."); // - using (FeedIterator> allVersionsIterator = container - .GetChangeFeedIterator>(ChangeFeedStartFrom.Now(), ChangeFeedMode.AllVersionsAndDeletes)) + using (FeedIterator allVersionsIterator = container + .GetChangeFeedIterator(ChangeFeedStartFrom.Now(), ChangeFeedMode.AllVersionsAndDeletes)) { while (allVersionsIterator.HasMoreResults) { - FeedResponse> response = await allVersionsIterator.ReadNextAsync(); + FeedResponse response = await allVersionsIterator.ReadNextAsync(); if (response.StatusCode == HttpStatusCode.NotModified) { @@ -134,11 +134,11 @@ static async Task ReadAllVersionsAndDeletesChangeFeed(Container container, strin Console.WriteLine("Press any key to stop."); // - using (FeedIterator> allVersionsIterator = container.GetChangeFeedIterator>(ChangeFeedStartFrom.ContinuationToken(allVersionsContinuationToken), ChangeFeedMode.AllVersionsAndDeletes, new ChangeFeedRequestOptions { PageSizeHint = 10 })) + using (FeedIterator allVersionsIterator = container.GetChangeFeedIterator(ChangeFeedStartFrom.ContinuationToken(allVersionsContinuationToken), ChangeFeedMode.AllVersionsAndDeletes, new ChangeFeedRequestOptions { PageSizeHint = 10 })) { while (allVersionsIterator.HasMoreResults) { - FeedResponse> response = await allVersionsIterator.ReadNextAsync(); + FeedResponse response = await allVersionsIterator.ReadNextAsync(); if (response.StatusCode == HttpStatusCode.NotModified) { @@ -147,25 +147,21 @@ static async Task ReadAllVersionsAndDeletesChangeFeed(Container container, strin } else { - foreach (ChangeFeedItem item in response) + foreach (dynamic item in response) { // if operaiton is delete - if (item.Metadata.OperationType == ChangeFeedOperationType.Delete) + if (item.metadata.operationType == "delete") { - if (item.Metadata.IsTimeToLiveExpired == true) - { - Console.WriteLine($"Operation: {item.Metadata.OperationType} (due to TTL). Item id: {item.Previous.Id}. Previous value: {item.Previous.Value}"); - } - else - { - Console.WriteLine($"Operation: {item.Metadata.OperationType} (not due to TTL). Item id: {item.Previous.Id}. Previous value: {item.Previous.Value}"); - } + bool isTTL = (item.metadata?.timeToLiveExpired == null) ? false : true; + Console.WriteLine($"Operation: {item.metadata.operationType}. Item id: {item.metadata.id}. Due to ttl: {isTTL}"); } // if operation is create or replace else { - Console.WriteLine($"Operation: {item.Metadata.OperationType}. Item id: {item.Current.Id}. Current value: {item.Current.Value}"); + Console.WriteLine($"Operation: {item.metadata.operationType}. Item id: {item.current.Id}. Current value: {item.current.Value}"); } + + Console.WriteLine($"{item}"); } } diff --git a/Microsoft.Azure.Cosmos.Samples/Usage/ChangeFeedAllVersionsAndDeletes/Program.cs b/Microsoft.Azure.Cosmos.Samples/Usage/ChangeFeedAllVersionsAndDeletes/Program.cs index 309316997b..9cb88d133e 100644 --- a/Microsoft.Azure.Cosmos.Samples/Usage/ChangeFeedAllVersionsAndDeletes/Program.cs +++ b/Microsoft.Azure.Cosmos.Samples/Usage/ChangeFeedAllVersionsAndDeletes/Program.cs @@ -350,7 +350,7 @@ static async Task HandleChangesAsync(ChangeFeedProcessorContext context, IReadOn { if (item.Metadata.OperationType == ChangeFeedOperationType.Delete) { - Console.WriteLine($"\tDetected {item.Metadata.OperationType} operation for item with id {item.Previous.id}."); + Console.WriteLine($"\tDetected {item.Metadata.OperationType} operation for item."); } else {