|
@@ -0,0 +1,232 @@
|
|
|
|
+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 批次操作
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 批次新增資料至Table儲存區。
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="entities">欲快取的集合</param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static async Task<TableBatchResult> BatchInsertAsync<T>(this CloudTable table, IEnumerable<T> 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
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// (同步)取得 table 中指定 PartitionKey 的所有集合。
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static IEnumerable<T> Get<T>(this CloudTable table, string partitionKey) where T : ITableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ TableQuery<T> query = new TableQuery<T>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey));
|
|
|
|
+ return table.ExecuteQuery(query);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// (同步)取得指定 PartitionKey與 RowKey 的數據,效能:點查詢,最佳。
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <typeparam name="T">T</typeparam>
|
|
|
|
+ /// <param name="partitionKey">PartitionKey</param>
|
|
|
|
+ /// <param name="rowKey">RowKey</param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static T Get<T>(this CloudTable table, string partitionKey, string rowKey) where T : ITableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ TableOperation retrieveOperation = TableOperation.Retrieve<T>(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;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 取得指定 PartitionKey 的所有集合(分頁),效能:範圍查詢,次佳。
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <typeparam name="T">T</typeparam>
|
|
|
|
+ /// <param name="partitionKey">PartitionKey</param>
|
|
|
|
+ /// <param name="takeCount">指定每次返回數量</param>
|
|
|
|
+ /// <param name="specifyPropertys">指定只須返回的屬性,效能:伺服器端預測,降低延遲和成本</param>
|
|
|
|
+ /// <param name="onProgress">要返回集合的委派</param>
|
|
|
|
+ /// <param name="ct">CancellationToken</param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static async Task GetAsync<T>(this CloudTable table, string partitionKey, int? takeCount = null, List<string> specifyPropertys = null, Action<IList<T>> onProgress = null, CancellationToken ct = default(CancellationToken)) where T : ITableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ TableQuery<T> tableQuery = null;
|
|
|
|
+ TableContinuationToken token = null;
|
|
|
|
+ var filter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey);
|
|
|
|
+
|
|
|
|
+ if (specifyPropertys != null)
|
|
|
|
+ tableQuery = new TableQuery<T>().Where(filter).Select(specifyPropertys);
|
|
|
|
+ else
|
|
|
|
+ tableQuery = new TableQuery<T>().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);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 取得指定 PartitionKey 的所有集合,效能:範圍查詢,次佳。
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <typeparam name="T">T</typeparam>
|
|
|
|
+ /// <param name="partitionKey">PartitionKey</param>
|
|
|
|
+ /// <param name="takeCount">指定每次返回數量</param>
|
|
|
|
+ /// <param name="specifyPropertys">指定只須返回的屬性,效能:伺服器端預測,降低延遲和成本</param>
|
|
|
|
+ /// <param name="onProgress">要返回集合的委派</param>
|
|
|
|
+ /// <param name="ct">CancellationToken</param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static async Task<IEnumerable<T>> GetAsync<T>(this CloudTable table, string partitionKey, int? takeCount = null, List<string> specifyPropertys = null, CancellationToken ct = default(CancellationToken)) where T : ITableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ TableQuery<T> tableQuery = null;
|
|
|
|
+ TableContinuationToken token = null;
|
|
|
|
+ var filter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey);
|
|
|
|
+
|
|
|
|
+ if (specifyPropertys != null)
|
|
|
|
+ tableQuery = new TableQuery<T>().Where(filter).Select(specifyPropertys);
|
|
|
|
+ else
|
|
|
|
+ tableQuery = new TableQuery<T>().Where(filter);
|
|
|
|
+
|
|
|
|
+ if (takeCount != null) tableQuery.TakeCount = takeCount;
|
|
|
|
+ var items = new List<T>();
|
|
|
|
+
|
|
|
|
+ do
|
|
|
|
+ {
|
|
|
|
+ var queryResponse = await table.ExecuteQuerySegmentedAsync(tableQuery, token);
|
|
|
|
+ token = queryResponse.ContinuationToken;
|
|
|
|
+ items.AddRange(queryResponse.Results);
|
|
|
|
+ } while (token != null && !ct.IsCancellationRequested);
|
|
|
|
+ return items;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 傳回指定partitionKey的Count數量
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="partitionKey"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static int GetCount(this CloudTable table, string partitionKey)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ TableQuery query = new TableQuery().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey)).Select(new List<string> { "PartitionKey" });
|
|
|
|
+ var queryResponse = table.ExecuteQuery(query).ToList();
|
|
|
|
+ return queryResponse.Count();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ #endregion
|
|
|
|
+
|
|
|
|
+ #region CloudTable Get All
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// (同步)取得 table 的所有集合,效能:資料表掃描,不佳。
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static IEnumerable<T> GetAll<T>(this CloudTable table) where T : ITableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ return table.ExecuteQuery(new TableQuery<T>());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 取得 table 的所有集合(分頁),效能:資料表掃描,不佳。
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="takeCount">指定每次返回數量</param>
|
|
|
|
+ /// <param name="specifyPropertys">指定只須返回的屬性,效能:伺服器端預測,降低延遲和成本</param>
|
|
|
|
+ /// <param name="onProgress">要返回集合的委派</param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static async Task GetAllAsync<T>(this CloudTable table, int? takeCount = null, List<string> specifyPropertys = null, Action<IList<T>> onProgress = null, CancellationToken ct = default(CancellationToken)) where T : ITableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ TableContinuationToken token = null;
|
|
|
|
+ TableQuery<T> tableQuery = new TableQuery<T>();
|
|
|
|
+ if (specifyPropertys != null)
|
|
|
|
+ tableQuery = new TableQuery<T>().Select(specifyPropertys);
|
|
|
|
+ else
|
|
|
|
+ tableQuery = new TableQuery<T>();
|
|
|
|
+ if (takeCount != null) tableQuery.TakeCount = takeCount;
|
|
|
|
+ var items = new List<T>();
|
|
|
|
+
|
|
|
|
+ do
|
|
|
|
+ {
|
|
|
|
+ var queryResponse = await table.ExecuteQuerySegmentedAsync(tableQuery, token);
|
|
|
|
+ token = queryResponse.ContinuationToken;
|
|
|
|
+ items.AddRange(queryResponse.Results);
|
|
|
|
+ onProgress?.Invoke(queryResponse.Results);
|
|
|
|
+ } while (token != null && !ct.IsCancellationRequested);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 取得 table 的所有集合,效能:資料表掃描,不佳。
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="takeCount">指定每次返回數量</param>
|
|
|
|
+ /// <param name="specifyPropertys">指定只須返回的屬性,效能:伺服器端預測,降低延遲和成本</param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static async Task<IEnumerable<T>> GetAllAsync<T>(this CloudTable table, int? takeCount = null, List<string> specifyPropertys = null, CancellationToken ct = default(CancellationToken)) where T : ITableEntity, new()
|
|
|
|
+ {
|
|
|
|
+ TableContinuationToken token = null;
|
|
|
|
+ TableQuery<T> tableQuery = new TableQuery<T>();
|
|
|
|
+ if (takeCount != null) tableQuery.TakeCount = takeCount;
|
|
|
|
+ if (specifyPropertys != null)
|
|
|
|
+ tableQuery = new TableQuery<T>().Select(specifyPropertys);
|
|
|
|
+ else
|
|
|
|
+ tableQuery = new TableQuery<T>();
|
|
|
|
+ var items = new List<T>();
|
|
|
|
+
|
|
|
|
+ do
|
|
|
|
+ {
|
|
|
|
+ var queryResponse = await table.ExecuteQuerySegmentedAsync(tableQuery, token);
|
|
|
|
+ token = queryResponse.ContinuationToken;
|
|
|
|
+ items.AddRange(queryResponse.Results);
|
|
|
|
+ } while (token != null && !ct.IsCancellationRequested);
|
|
|
|
+ return items;
|
|
|
|
+ }
|
|
|
|
+ #endregion
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+}
|