Skip to content

Commit

Permalink
add batch operation options that define insert method (insert and/or …
Browse files Browse the repository at this point in the history
…replace or merge)
  • Loading branch information
Marcin Wolnik committed Oct 12, 2017
1 parent e5eb7cb commit 06623dc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.WindowsAzure.Storage;
using System;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -135,11 +136,14 @@ public async Task<object> AddAsync(string tableName, ITableEntity entity)
/// </summary>
/// <param name="tableName">Name of the table.</param>
/// <param name="entities">Collection of entities.</param>
/// <param name="options">Batch operation options</param>
/// <returns></returns>
public async Task<IEnumerable<T>> AddBatchAsync<T>(string tableName, IEnumerable<ITableEntity> entities) where T : class, ITableEntity, new()
public async Task<IEnumerable<T>> AddBatchAsync<T>(string tableName, IEnumerable<ITableEntity> entities, BatchOperationOptions options = null) where T : class, ITableEntity, new()
{
var table = await EnsureTable(tableName);

options = options ?? new BatchOperationOptions();

var tasks = new List<Task<IList<TableResult>>>();

var entitiesOffset = 0;
Expand All @@ -148,10 +152,23 @@ public async Task<object> AddAsync(string tableName, ITableEntity entity)
var entitiesToAdd = entities.Skip(entitiesOffset).Take(100).ToList();
entitiesOffset += entitiesToAdd.Count;

Action<TableBatchOperation, ITableEntity> batchInsertOperation = null;
switch (options.BatchInsertMethod)
{
case BatchInsertMethod.Insert:
batchInsertOperation = (bo, entity) => bo.Insert(entity);
break;
case BatchInsertMethod.InsertOrReplace:
batchInsertOperation = (bo, entity) => bo.InsertOrReplace(entity);
break;
case BatchInsertMethod.InsertOrMerge:
batchInsertOperation = (bo, entity) => bo.InsertOrMerge(entity);
break;
}
TableBatchOperation batchOperation = new TableBatchOperation();
foreach (var entity in entitiesToAdd)
{
batchOperation.Insert(entity);
batchInsertOperation(batchOperation, entity);
}
tasks.Add(table.ExecuteBatchAsync(batchOperation));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Wolnik.Azure.TableStorage.Repository
{
public class BatchOperationOptions
{
public BatchInsertMethod BatchInsertMethod { get; set; }
}

public enum BatchInsertMethod
{
Insert,
InsertOrReplace,
InsertOrMerge
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface ITableStorage
Task<object> AddOrUpdateAsync(string tableName, ITableEntity entity);
Task<object> DeleteAsync(string tableName, ITableEntity entity);
Task<object> AddAsync(string tableName, ITableEntity entity);
Task<IEnumerable<T>> AddBatchAsync<T>(string tableName, IEnumerable<ITableEntity> entities) where T : class, ITableEntity, new();
Task<IEnumerable<T>> AddBatchAsync<T>(string tableName, IEnumerable<ITableEntity> entities, BatchOperationOptions options) where T : class, ITableEntity, new();
Task<object> UpdateAsync(string tableName, ITableEntity entity);
}
}

0 comments on commit 06623dc

Please sign in to comment.