Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cosmos DB: Adds Patch Support #2161

Merged
merged 38 commits into from Apr 30, 2024
Merged

Conversation

sourabh1007
Copy link
Contributor

@sourabh1007 sourabh1007 commented Apr 12, 2024

Why make this change?

Today, in cosmos DB, we have ability to update the item which is actually replace the item. Adding new operation i.e. patch where customer would have ability to "patch" an item.
PATCH would be available only for Cosmos DB.

What is this change?

Before going further, it is highly recommended to go through below docs:

  1. How patch works in Cosmos DB https://learn.microsoft.com/en-us/azure/cosmos-db/partial-document-update#supported-operations
  2. Limitations: https://learn.microsoft.com/en-us/azure/cosmos-db/partial-document-update#supported-modes
  3. How it works with SDK: https://learn.microsoft.com/en-us/azure/cosmos-db/partial-document-update-getting-started?tabs=dotnet#prerequisites

To Summarize, here is a simple example of patch:

List<PatchOperation> operations = new ()
{
    PatchOperation.Add("/color", "silver"),
    PatchOperation.Remove("/used"),
    PatchOperation.Increment("/price", 50.00),
    PatchOperation.Add("/tags/-", "featured-bikes")
};

ItemResponse<Product> response = await container.PatchItemAsync<Product>(
    id: "e379aea5-63f5-4623-9a9b-4cd9b33b91d5",
    partitionKey: new PartitionKey("road-bikes"),
    patchOperations: operations
);

You need to generate PatchOpertation which need below information:
a) Operation type i.e. Set, Add, Remove etc
b) Attribute Path where operation needs to be applied i.e "/color", "/used" in above example
c) New value

What DAB supports?

  1. We decided to support only Set operation, as it solves the purpose to update an item. It means, you cannot perform any specific operations like remove, move etc.
  2. There is no special handling for an array.
  3. If the target path specifies an element that doesn't exist, it's added.
  4. If the target path specifies an element that already exists, its value is replaced.

Changes as part of this PR:

  1. Generate patch operation for given entities
    image
  2. Generate PatchPlanetInput without id field as using patch operation you can not update an Id
    image
  3. Implement patch operation
    a) It translates the passed item into "patchoperation" by traversing the item.
    b) Checks if number of patch operations are less than 10 or more than 10 (as cosmsodb supports at max 10 patch operations at a time)
    c) If it is less than or equal to 10, it fires patch call with patch operations
    d) If it is greater than 10, then it creates a transaction batch of patch call, with 10 patch operations in each patch call. (RU exhaustive but functionally it works)

Pictorial Overview of the implementation

flowchart TD
    User[fa:fa-user User]-->| patch operation |DAB
    subgraph DAB[DAB]
        Authorization[Authorization]-->Patch
        subgraph Patch[Patch Operation]
        PatchOperation[Generate Patch 'Set' Operations with passed item] -->CheckCount{Number of Patch Operation > 10}
        CheckCount --> |No| SDKOperation[SDK Patch call]
        CheckCount --> |Yes| TransactionBatch[Create a batch of patch calls with max 10 patch operations ]-->SDKBatchOperation[SDK ExecuteBatch call]
        end
    end

Authorization for this new Mutation Operation:
update access would be applied for patch and update operations both.

How was this tested?

  • Integration Tests
  • Unit Tests

Sample Request(s)

Refer test cases.

@sourabh1007
Copy link
Contributor Author

/azp run

@sourabh1007 sourabh1007 force-pushed the users/sourabhjain/addpatchsupport branch from 8932d9f to df297ab Compare April 17, 2024 01:27
@sourabh1007
Copy link
Contributor Author

/azp run

3 similar comments
@sourabh1007
Copy link
Contributor Author

/azp run

@sourabh1007
Copy link
Contributor Author

/azp run

@sourabh1007
Copy link
Contributor Author

/azp run

@sourabh1007 sourabh1007 marked this pull request as ready for review April 17, 2024 04:04
@sourabh1007 sourabh1007 enabled auto-merge (squash) April 17, 2024 15:25
Copy link
Contributor

@Aniruddh25 Aniruddh25 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Waiting on clarification on the need to introduce a patch permission..

src/Service.Tests/dab-config.CosmosDb_NoSql.json Outdated Show resolved Hide resolved
src/Config/ObjectModel/EntityAction.cs Outdated Show resolved Hide resolved
@sourabh1007 sourabh1007 force-pushed the users/sourabhjain/addpatchsupport branch from 985c8bb to 4b85e65 Compare April 23, 2024 01:58
@sourabh1007
Copy link
Contributor Author

/azp run

1 similar comment
@sourabh1007
Copy link
Contributor Author

/azp run

@sourabh1007 sourabh1007 force-pushed the users/sourabhjain/addpatchsupport branch from cea72a8 to 8233fdd Compare April 26, 2024 19:17
@sourabh1007
Copy link
Contributor Author

/azp run

src/Cli/Utils.cs Outdated Show resolved Hide resolved
@sourabh1007
Copy link
Contributor Author

/azp run

@sourabh1007
Copy link
Contributor Author

/azp run

sourabh1007 and others added 2 commits April 30, 2024 07:02
Co-authored-by: Aniruddh Munde <anmunde@microsoft.com>
@Aniruddh25 Aniruddh25 self-requested a review April 30, 2024 01:32
@Aniruddh25
Copy link
Contributor

previous comments have been addressed, but I need more time to look at few other files.. so reset my review. If this gets approved before that, feel free to merge it in.

@sourabh1007 sourabh1007 dismissed Aniruddh25’s stale review April 30, 2024 01:55

Already addressed..

@sourabh1007
Copy link
Contributor Author

/azp run

@sourabh1007
Copy link
Contributor Author

/azp run

Copy link
Contributor

@neeraj-sharma2592 neeraj-sharma2592 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

src/Core/Resolvers/CosmosMutationEngine.cs Show resolved Hide resolved
@sourabh1007
Copy link
Contributor Author

/azp run

@sourabh1007 sourabh1007 merged commit e076d87 into main Apr 30, 2024
7 checks passed
@sourabh1007 sourabh1007 deleted the users/sourabhjain/addpatchsupport branch April 30, 2024 09:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants