using Microsoft.Azure.Cosmos.Table; using Microsoft.Azure.Cosmos.Table.Queryable; using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Linq; namespace TEAMModelOS.SDK.DI { public static class AzureStorageTableExtensions { #region CloudTable 批次操作 /// /// 批次新增資料至Table儲存區。 /// /// 欲快取的集合 /// public static async Task BatchInsertAsync(this CloudTable table, IEnumerable entities) where T : ITableEntity, new() { TableBatchOperation batchOperation = new TableBatchOperation(); foreach (var cache in entities) { batchOperation.Insert(cache); } return await table.ExecuteBatchAsync(batchOperation); } #endregion #region CloudTable Get /// /// (同步)取得 table 中指定 PartitionKey 的所有集合。 /// /// public static IEnumerable Get(this CloudTable table, string partitionKey) where T : ITableEntity, new() { TableQuery query = new TableQuery().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey)); return table.ExecuteQuery(query); } /// /// (同步)取得指定 PartitionKey與 RowKey 的數據,效能:點查詢,最佳。 /// /// T /// PartitionKey /// RowKey /// public static T Get(this CloudTable table, string partitionKey, string rowKey) where T : ITableEntity, new() { TableOperation retrieveOperation = TableOperation.Retrieve(partitionKey, rowKey); var retrieveResult = table.Execute(retrieveOperation); if (retrieveResult.Result != null) { //DynamicTableEntityJsonSeria1lizer return (T)retrieveResult.Result; } else { return default(T); } } public static DynamicTableEntity Get(this CloudTable table, string partitionKey, string rowKey) { TableOperation retrieveOperation = TableOperation.Retrieve(partitionKey, rowKey); var retrieveResult = table.Execute(retrieveOperation); if (retrieveResult.Result != null) { return retrieveResult.Result as DynamicTableEntity; } else { return null; } } /// /// 取得指定 PartitionKey 的所有集合(分頁),效能:範圍查詢,次佳。 /// /// T /// PartitionKey /// 指定每次返回數量 /// 指定只須返回的屬性,效能:伺服器端預測,降低延遲和成本 /// 要返回集合的委派 /// CancellationToken /// public static async Task GetAsync(this CloudTable table, string partitionKey, int? takeCount = null, List specifyPropertys = null, Action> onProgress = null, CancellationToken ct = default(CancellationToken)) where T : ITableEntity, new() { TableQuery tableQuery = null; TableContinuationToken token = null; var filter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey); if (specifyPropertys != null) tableQuery = new TableQuery().Where(filter).Select(specifyPropertys); else tableQuery = new TableQuery().Where(filter); if (takeCount != null) tableQuery.TakeCount = takeCount; do { var queryResponse = await table.ExecuteQuerySegmentedAsync(tableQuery, token); token = queryResponse.ContinuationToken; onProgress?.Invoke(queryResponse.Results); } while (token != null && !ct.IsCancellationRequested); } /// /// 取得指定 PartitionKey 的所有集合,效能:範圍查詢,次佳。 /// /// T /// PartitionKey /// 指定每次返回數量 /// 指定只須返回的屬性,效能:伺服器端預測,降低延遲和成本 /// 要返回集合的委派 /// CancellationToken /// public static async Task> GetAsync(this CloudTable table, string partitionKey, int? takeCount = null, List specifyPropertys = null, CancellationToken ct = default(CancellationToken)) where T : ITableEntity, new() { TableQuery tableQuery = null; TableContinuationToken token = null; var filter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey); if (specifyPropertys != null) tableQuery = new TableQuery().Where(filter).Select(specifyPropertys); else tableQuery = new TableQuery().Where(filter); if (takeCount != null) tableQuery.TakeCount = takeCount; var items = new List(); do { var queryResponse = await table.ExecuteQuerySegmentedAsync(tableQuery, token); token = queryResponse.ContinuationToken; items.AddRange(queryResponse.Results); } while (token != null && !ct.IsCancellationRequested); return items; } /// /// 傳回指定partitionKey的Count數量 /// /// /// public static int GetCount(this CloudTable table, string partitionKey) { TableQuery query = new TableQuery().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey)).Select(new List { "PartitionKey" }); var queryResponse = table.ExecuteQuery(query).ToList(); return queryResponse.Count(); } #endregion #region CloudTable Get All /// /// (同步)取得 table 的所有集合,效能:資料表掃描,不佳。 /// /// public static IEnumerable GetAll(this CloudTable table) where T : ITableEntity, new() { return table.ExecuteQuery(new TableQuery()); } /// /// 取得 table 的所有集合(分頁),效能:資料表掃描,不佳。 /// /// 指定每次返回數量 /// 指定只須返回的屬性,效能:伺服器端預測,降低延遲和成本 /// 要返回集合的委派 /// public static async Task GetAllAsync(this CloudTable table, int? takeCount = null, List specifyPropertys = null, Action> onProgress = null, CancellationToken ct = default(CancellationToken)) where T : ITableEntity, new() { TableContinuationToken token = null; TableQuery tableQuery = new TableQuery(); if (specifyPropertys != null) tableQuery = new TableQuery().Select(specifyPropertys); else tableQuery = new TableQuery(); if (takeCount != null) tableQuery.TakeCount = takeCount; var items = new List(); do { var queryResponse = await table.ExecuteQuerySegmentedAsync(tableQuery, token); token = queryResponse.ContinuationToken; items.AddRange(queryResponse.Results); onProgress?.Invoke(queryResponse.Results); } while (token != null && !ct.IsCancellationRequested); } /// /// 取得 table 的所有集合,效能:資料表掃描,不佳。 /// /// 指定每次返回數量 /// 指定只須返回的屬性,效能:伺服器端預測,降低延遲和成本 /// public static async Task> GetAllAsync(this CloudTable table, int? takeCount = null, List specifyPropertys = null, CancellationToken ct = default(CancellationToken)) where T : ITableEntity, new() { TableContinuationToken token = null; TableQuery tableQuery = new TableQuery(); if (takeCount != null) tableQuery.TakeCount = takeCount; if (specifyPropertys != null) tableQuery = new TableQuery().Select(specifyPropertys); else tableQuery = new TableQuery(); var items = new List(); do { var queryResponse = await table.ExecuteQuerySegmentedAsync(tableQuery, token); token = queryResponse.ContinuationToken; items.AddRange(queryResponse.Results); } while (token != null && !ct.IsCancellationRequested); return items; } #endregion } }