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

refactor batch #20706

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public TableClient(System.Uri endpoint, string tableName, Azure.Data.Tables.Tabl
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Data.Tables.Models.TableItem>> CreateIfNotExistsAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static string CreateQueryFilter(System.FormattableString filter) { throw null; }
public static string CreateQueryFilter<T>(System.Linq.Expressions.Expression<System.Func<T, bool>> filter) { throw null; }
public virtual Azure.Data.Tables.TableTransactionalBatch CreateTransactionalBatch(string partitionKey) { throw null; }
public virtual Azure.Response Delete(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response> DeleteAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response DeleteEntity(string partitionKey, string rowKey, Azure.ETag ifMatch = default(Azure.ETag), System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
Expand All @@ -43,6 +42,8 @@ public TableClient(System.Uri endpoint, string tableName, Azure.Data.Tables.Tabl
public virtual Azure.Pageable<T> Query<T>(string filter = null, int? maxPerPage = default(int?), System.Collections.Generic.IEnumerable<string> select = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) where T : class, Azure.Data.Tables.ITableEntity, new() { throw null; }
public virtual Azure.Response SetAccessPolicy(System.Collections.Generic.IEnumerable<Azure.Data.Tables.Models.SignedIdentifier> tableAcl, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response> SetAccessPolicyAsync(System.Collections.Generic.IEnumerable<Azure.Data.Tables.Models.SignedIdentifier> tableAcl, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual Azure.Response<Azure.Data.Tables.Models.TableBatchResponse> SubmitTransaction(Azure.Data.Tables.TableTransactionalBatch transactionalBatch, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Data.Tables.Models.TableBatchResponse>> SubmitTransactionAsync(Azure.Data.Tables.TableTransactionalBatch transactionalBatch, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response> UpdateEntityAsync<T>(T entity, Azure.ETag ifMatch, Azure.Data.Tables.TableUpdateMode mode = Azure.Data.Tables.TableUpdateMode.Merge, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) where T : class, Azure.Data.Tables.ITableEntity, new() { throw null; }
public virtual Azure.Response UpdateEntity<T>(T entity, Azure.ETag ifMatch, Azure.Data.Tables.TableUpdateMode mode = Azure.Data.Tables.TableUpdateMode.Merge, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) where T : class, Azure.Data.Tables.ITableEntity, new() { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response> UpsertEntityAsync<T>(T entity, Azure.Data.Tables.TableUpdateMode mode = Azure.Data.Tables.TableUpdateMode.Merge, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) where T : class, Azure.Data.Tables.ITableEntity, new() { throw null; }
Expand Down Expand Up @@ -137,14 +138,18 @@ public static partial class TablesModelFactory
public partial class TableTransactionalBatch
{
protected TableTransactionalBatch() { }
public TableTransactionalBatch(string partitionKey) { }
public int OperationCount { get { throw null; } }
public virtual void AddEntities<T>(System.Collections.Generic.IEnumerable<T> entities) where T : class, Azure.Data.Tables.ITableEntity, new() { }
public virtual void AddEntity<T>(T entity) where T : class, Azure.Data.Tables.ITableEntity, new() { }
public virtual void AddEntity(Azure.Data.Tables.ITableEntity entity) { }
public void ClearOperations() { }
public virtual void DeleteEntity(string rowKey, Azure.ETag ifMatch = default(Azure.ETag)) { }
public virtual Azure.Response<Azure.Data.Tables.Models.TableBatchResponse> SubmitBatch(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.Data.Tables.Models.TableBatchResponse>> SubmitBatchAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public System.Collections.Generic.IReadOnlyList<Azure.Data.Tables.ITableEntity> GetEntities() { throw null; }
public bool RemoveEntities(System.Collections.Generic.IEnumerable<Azure.Data.Tables.ITableEntity> entities) { throw null; }
public bool RemoveEntity(Azure.Data.Tables.ITableEntity entity) { throw null; }
public virtual bool TryGetFailedEntityFromException(Azure.RequestFailedException exception, out Azure.Data.Tables.ITableEntity failedEntity) { throw null; }
public virtual void UpdateEntity<T>(T entity, Azure.ETag ifMatch, Azure.Data.Tables.TableUpdateMode mode = Azure.Data.Tables.TableUpdateMode.Merge) where T : class, Azure.Data.Tables.ITableEntity, new() { }
public virtual void UpsertEntity<T>(T entity, Azure.Data.Tables.TableUpdateMode mode = Azure.Data.Tables.TableUpdateMode.Merge) where T : class, Azure.Data.Tables.ITableEntity, new() { }
public virtual void UpdateEntity(Azure.Data.Tables.ITableEntity entity, Azure.ETag ifMatch, Azure.Data.Tables.TableUpdateMode mode = Azure.Data.Tables.TableUpdateMode.Merge) { }
public virtual void UpsertEntity(Azure.Data.Tables.ITableEntity entity, Azure.Data.Tables.TableUpdateMode mode = Azure.Data.Tables.TableUpdateMode.Merge) { }
Copy link
Contributor

Choose a reason for hiding this comment

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

How do we choose when to provide only singular overload and when the IEnumerable one?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's mostly an assumption that add is the common bulk scenario. We can always add more later if needed.

}
public enum TableUpdateMode
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ List<TableEntity> entityList = new List<TableEntity>{
};

// Create the batch.
TableTransactionalBatch addEntitiesBatch = client.CreateTransactionalBatch(partitionKey);
TableTransactionalBatch addEntitiesBatch = new TableTransactionalBatch(partitionKey);

// Add the entities to be added to the batch.
addEntitiesBatch.AddEntities(entityList);

// Submit the batch.
TableBatchResponse response = await addEntitiesBatch.SubmitBatchAsync().ConfigureAwait(false);
TableBatchResponse response = await client.SubmitTransactionAsync(addEntitiesBatch).ConfigureAwait(false);

foreach (TableEntity entity in entityList)
{
Expand All @@ -84,7 +84,7 @@ This example assumes we already have added the entities from the previous add en

```C# Snippet:BatchMixed
// Create a new batch.
TableTransactionalBatch mixedBatch = client.CreateTransactionalBatch(partitionKey);
TableTransactionalBatch mixedBatch = new TableTransactionalBatch(partitionKey);

// Add an entity for deletion to the batch.
mixedBatch.DeleteEntity(entityList[0].RowKey);
Expand All @@ -111,7 +111,7 @@ updateEntity["Brand"] = "Generic";
mixedBatch.UpsertEntity(updateEntity, TableUpdateMode.Replace);

// Submit the batch.
await mixedBatch.SubmitBatchAsync().ConfigureAwait(false);
await client.SubmitTransactionAsync(mixedBatch).ConfigureAwait(false);
```

## Deleting entities with a transactional batch
Expand All @@ -120,7 +120,7 @@ Let's clean up the rest of the entities remaining in the table with a batch dele

```C# Snippet:BatchDelete
// Create a new batch.
TableTransactionalBatch deleteEntitiesBatch = client.CreateTransactionalBatch(partitionKey);
TableTransactionalBatch deleteEntitiesBatch = new TableTransactionalBatch(partitionKey);

// Add the entities for deletion to the batch.
foreach (TableEntity entity in entityList)
Expand All @@ -129,7 +129,7 @@ foreach (TableEntity entity in entityList)
}

// Submit the batch.
await deleteEntitiesBatch.SubmitBatchAsync().ConfigureAwait(false);
await client.SubmitTransactionAsync(deleteEntitiesBatch).ConfigureAwait(false);
```

---
Expand Down
21 changes: 21 additions & 0 deletions sdk/tables/Azure.Data.Tables/src/BatchItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Core;

namespace Azure.Data.Tables
{
internal class BatchItem
{
public RequestType RequestType { get; set; }
public ITableEntity Entity { get; set; }
public HttpMessage Message { get; set; }

public BatchItem(RequestType requestType, ITableEntity entity, HttpMessage message)
{
RequestType = requestType;
Entity = entity;
Message = message;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal static Dictionary<string, object> ToOdataAnnotatedDictionary<T>(this T
return dictEntity.ToOdataAnnotatedDictionary();
}

var properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public);
var properties = entity.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
var annotatedDictionary = new Dictionary<string, object>(properties.Length * 2);

foreach (var prop in properties)
Expand Down
6 changes: 4 additions & 2 deletions sdk/tables/Azure.Data.Tables/src/RequestType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ namespace Azure.Data.Tables
internal enum RequestType
{
Create,
Update,
UpdateMerge,
UpdateReplace,
Delete,
Upsert
UpsertMerge,
UpsertReplace
}
}
10 changes: 5 additions & 5 deletions sdk/tables/Azure.Data.Tables/src/TableBatchResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
namespace Azure.Data.Tables.Models
{
/// <summary>
/// The response from <see cref="TableTransactionalBatch.SubmitBatch(System.Threading.CancellationToken)"/> or <see cref="TableTransactionalBatch.SubmitBatchAsync(System.Threading.CancellationToken)"/>.
/// The response from <see cref="TableClient.SubmitTransaction"/> or <see cref="TableClient.SubmitTransactionAsync"/>.
/// </summary>
public class TableBatchResponse
{
internal IDictionary<string, (HttpMessage Message, RequestType RequestType)> _requestLookup;
private readonly IDictionary<string, BatchItem> _requestLookup;

internal TableBatchResponse(ConcurrentDictionary<string, (HttpMessage Message, RequestType RequestType)> requestLookup)
internal TableBatchResponse(ConcurrentDictionary<string, BatchItem> requestLookup)
{
_requestLookup = requestLookup;
}
Expand All @@ -32,12 +32,12 @@ internal TableBatchResponse(ConcurrentDictionary<string, (HttpMessage Message, R
/// <returns></returns>
Copy link
Member

Choose a reason for hiding this comment

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

nit: Since this is a public member, should we consider adding content for <returns> or removing the tag?

public Response GetResponseForEntity(string rowKey)
{
if (!_requestLookup.TryGetValue(rowKey, out (HttpMessage Message, RequestType RequestType) tuple))
if (!_requestLookup.TryGetValue(rowKey, out BatchItem item))
{
throw new InvalidOperationException("The batch operation did not contain an entity with the specified rowKey");
}

return tuple.Message.Response;
return item.Message.Response;
}
}
}
Loading