Skip to content

Commit

Permalink
[Internal] Samples: Fixes all versions and deletes change feed samples (
Browse files Browse the repository at this point in the history
#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 <kirankk@microsoft.com>
  • Loading branch information
jcocchi and kirankumarkolli authored Dec 16, 2024
1 parent bf16585 commit a5469f2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ static async Task<string> CreateAllVersionsAndDeletesChangeFeedIterator(Containe
Console.WriteLine("Creating ChangeFeedIterator to read the change feed in All Versions and Deletes mode.");

// <InitializeFeedIterator>
using (FeedIterator<ChangeFeedItem<Item>> allVersionsIterator = container
.GetChangeFeedIterator<ChangeFeedItem<Item>>(ChangeFeedStartFrom.Now(), ChangeFeedMode.AllVersionsAndDeletes))
using (FeedIterator<dynamic> allVersionsIterator = container
.GetChangeFeedIterator<dynamic>(ChangeFeedStartFrom.Now(), ChangeFeedMode.AllVersionsAndDeletes))
{
while (allVersionsIterator.HasMoreResults)
{
FeedResponse<ChangeFeedItem<Item>> response = await allVersionsIterator.ReadNextAsync();
FeedResponse<dynamic> response = await allVersionsIterator.ReadNextAsync();

if (response.StatusCode == HttpStatusCode.NotModified)
{
Expand Down Expand Up @@ -134,11 +134,11 @@ static async Task ReadAllVersionsAndDeletesChangeFeed(Container container, strin
Console.WriteLine("Press any key to stop.");

// <ReadAllVersionsAndDeletesChanges>
using (FeedIterator<ChangeFeedItem<Item>> allVersionsIterator = container.GetChangeFeedIterator<ChangeFeedItem<Item>>(ChangeFeedStartFrom.ContinuationToken(allVersionsContinuationToken), ChangeFeedMode.AllVersionsAndDeletes, new ChangeFeedRequestOptions { PageSizeHint = 10 }))
using (FeedIterator<dynamic> allVersionsIterator = container.GetChangeFeedIterator<dynamic>(ChangeFeedStartFrom.ContinuationToken(allVersionsContinuationToken), ChangeFeedMode.AllVersionsAndDeletes, new ChangeFeedRequestOptions { PageSizeHint = 10 }))
{
while (allVersionsIterator.HasMoreResults)
{
FeedResponse<ChangeFeedItem<Item>> response = await allVersionsIterator.ReadNextAsync();
FeedResponse<dynamic> response = await allVersionsIterator.ReadNextAsync();

if (response.StatusCode == HttpStatusCode.NotModified)
{
Expand All @@ -147,25 +147,21 @@ static async Task ReadAllVersionsAndDeletesChangeFeed(Container container, strin
}
else
{
foreach (ChangeFeedItem<Item> 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}");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit a5469f2

Please sign in to comment.