-
Notifications
You must be signed in to change notification settings - Fork 945
Add ReadMany API to ContainerClient for batch item reads #25516
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
Draft
Copilot
wants to merge
5
commits into
main
Choose a base branch
from
copilot/add-readmany-api-to-cosmos-sdk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
027cbd6
Initial plan
Copilot 726b9d3
Add ReadMany API to ContainerClient
Copilot 92648e5
Fix error handling in ReadMany test
Copilot 8c6cd57
Address code review feedback for ReadMany API
Copilot 48a3b34
Move ReadMany API to cosmos_read_many.go
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package azcosmos | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "net/http" | ||
|
|
||
| "github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
| ) | ||
|
|
||
| // ItemIdentity represents the identity (ID and partition key) of an item. | ||
| // This is useful for bulk/read-many style operations that need to address multiple | ||
| // items under (potentially) different partition key values. | ||
| // | ||
| // ID must match the 'id' property of the stored item. PartitionKey is the value (or | ||
| // composite/hierarchical set of values) the item was written with. For hierarchical | ||
| // partition keys create the PartitionKey with NewPartitionKey* helpers (e.g. | ||
| // NewPartitionKeyString, NewPartitionKeyInt, or NewPartitionKeyArray) following the | ||
| // order defined in the container. | ||
| type ItemIdentity struct { | ||
| ID string | ||
| PartitionKey PartitionKey | ||
| } | ||
|
|
||
| // ReadManyOptions contains options for ReadMany operations. | ||
| type ReadManyOptions struct { | ||
| // SessionToken to be used when using Session consistency on the account. | ||
| // When working with Session consistency, each new write request to Azure Cosmos DB is assigned a new SessionToken. | ||
| // The client instance will use this token internally with each read/query request to ensure that the set consistency level is maintained. | ||
| SessionToken *string | ||
| // ConsistencyLevel overrides the account defined consistency level for this operation. | ||
| // Consistency can only be relaxed. | ||
| ConsistencyLevel *ConsistencyLevel | ||
| // Options for operations in the dedicated gateway. | ||
| DedicatedGatewayRequestOptions *DedicatedGatewayRequestOptions | ||
| } | ||
|
|
||
| // ReadManyResponse represents the response from a ReadMany operation. | ||
| type ReadManyResponse struct { | ||
| Items [][]byte | ||
| TotalRequestCharge float32 | ||
| } | ||
|
|
||
| // ReadMany reads multiple items from a Cosmos container. | ||
| // This operation may be equivalent to performing a sequence of Read operations for each item, | ||
| // but the SDK will apply optimizations when possible. In the worst case, this is no worse | ||
| // than sequential point reads, but may benefit from current and future optimizations. | ||
| // Items that do not exist in the container will be silently skipped and not included in the response. | ||
| // ctx - The context for the request. | ||
| // itemIdentities - The identities (ID and partition key) of the items to read. | ||
| // options - Options for the operation. | ||
| func (c *ContainerClient) ReadMany( | ||
| ctx context.Context, | ||
| itemIdentities []ItemIdentity, | ||
| options *ReadManyOptions) (ReadManyResponse, error) { | ||
| if options == nil { | ||
| options = &ReadManyOptions{} | ||
| } | ||
|
|
||
| items := make([][]byte, 0, len(itemIdentities)) | ||
| var totalCharge float32 | ||
|
|
||
| for _, identity := range itemIdentities { | ||
| itemOptions := &ItemOptions{ | ||
| SessionToken: options.SessionToken, | ||
| ConsistencyLevel: options.ConsistencyLevel, | ||
| DedicatedGatewayRequestOptions: options.DedicatedGatewayRequestOptions, | ||
| } | ||
| response, err := c.ReadItem(ctx, identity.PartitionKey, identity.ID, itemOptions) | ||
| if err != nil { | ||
| var responseErr *azcore.ResponseError | ||
| if errors.As(err, &responseErr) && responseErr.StatusCode == http.StatusNotFound { | ||
| continue | ||
| } | ||
| return ReadManyResponse{}, err | ||
| } | ||
| items = append(items, response.Value) | ||
| totalCharge += response.RequestCharge | ||
| } | ||
|
|
||
| return ReadManyResponse{ | ||
| Items: items, | ||
| TotalRequestCharge: totalCharge, | ||
| }, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.