AzureStorageTableExtensions.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using Microsoft.Azure.Cosmos.Table;
  2. using Microsoft.Azure.Cosmos.Table.Queryable;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Linq;
  10. namespace TEAMModelOS.SDK.DI
  11. {
  12. public static class AzureStorageTableExtensions
  13. {
  14. #region CloudTable 批次操作
  15. /// <summary>
  16. /// 批次新增資料至Table儲存區。
  17. /// </summary>
  18. /// <param name="entities">欲快取的集合</param>
  19. /// <returns></returns>
  20. public static async Task<TableBatchResult> BatchInsertAsync<T>(this CloudTable table, IEnumerable<T> entities) where T : ITableEntity, new()
  21. {
  22. TableBatchOperation batchOperation = new TableBatchOperation();
  23. foreach (var cache in entities)
  24. {
  25. batchOperation.Insert(cache);
  26. }
  27. return await table.ExecuteBatchAsync(batchOperation);
  28. }
  29. #endregion
  30. #region CloudTable Get
  31. /// <summary>
  32. /// (同步)取得 table 中指定 PartitionKey 的所有集合。
  33. /// </summary>
  34. /// <returns></returns>
  35. public static IEnumerable<T> Get<T>(this CloudTable table, string partitionKey) where T : ITableEntity, new()
  36. {
  37. TableQuery<T> query = new TableQuery<T>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey));
  38. return table.ExecuteQuery(query);
  39. }
  40. /// <summary>
  41. /// (同步)取得指定 PartitionKey與 RowKey 的數據,效能:點查詢,最佳。
  42. /// </summary>
  43. /// <typeparam name="T">T</typeparam>
  44. /// <param name="partitionKey">PartitionKey</param>
  45. /// <param name="rowKey">RowKey</param>
  46. /// <returns></returns>
  47. public static T Get<T>(this CloudTable table, string partitionKey, string rowKey) where T : ITableEntity, new()
  48. {
  49. TableOperation retrieveOperation = TableOperation.Retrieve<T>(partitionKey, rowKey);
  50. var retrieveResult = table.Execute(retrieveOperation);
  51. if (retrieveResult.Result != null)
  52. {
  53. //DynamicTableEntityJsonSeria1lizer
  54. return (T)retrieveResult.Result;
  55. }
  56. else
  57. {
  58. return default(T);
  59. }
  60. }
  61. public static DynamicTableEntity Get(this CloudTable table, string partitionKey, string rowKey)
  62. {
  63. TableOperation retrieveOperation = TableOperation.Retrieve(partitionKey, rowKey);
  64. var retrieveResult = table.Execute(retrieveOperation);
  65. if (retrieveResult.Result != null)
  66. {
  67. return retrieveResult.Result as DynamicTableEntity;
  68. }
  69. else
  70. {
  71. return null;
  72. }
  73. }
  74. /// <summary>
  75. /// 取得指定 PartitionKey 的所有集合(分頁),效能:範圍查詢,次佳。
  76. /// </summary>
  77. /// <typeparam name="T">T</typeparam>
  78. /// <param name="partitionKey">PartitionKey</param>
  79. /// <param name="takeCount">指定每次返回數量</param>
  80. /// <param name="specifyPropertys">指定只須返回的屬性,效能:伺服器端預測,降低延遲和成本</param>
  81. /// <param name="onProgress">要返回集合的委派</param>
  82. /// <param name="ct">CancellationToken</param>
  83. /// <returns></returns>
  84. 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()
  85. {
  86. TableQuery<T> tableQuery = null;
  87. TableContinuationToken token = null;
  88. var filter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey);
  89. if (specifyPropertys != null)
  90. tableQuery = new TableQuery<T>().Where(filter).Select(specifyPropertys);
  91. else
  92. tableQuery = new TableQuery<T>().Where(filter);
  93. if (takeCount != null) tableQuery.TakeCount = takeCount;
  94. do
  95. {
  96. var queryResponse = await table.ExecuteQuerySegmentedAsync(tableQuery, token);
  97. token = queryResponse.ContinuationToken;
  98. onProgress?.Invoke(queryResponse.Results);
  99. } while (token != null && !ct.IsCancellationRequested);
  100. }
  101. /// <summary>
  102. /// 取得指定 PartitionKey 的所有集合,效能:範圍查詢,次佳。
  103. /// </summary>
  104. /// <typeparam name="T">T</typeparam>
  105. /// <param name="partitionKey">PartitionKey</param>
  106. /// <param name="takeCount">指定每次返回數量</param>
  107. /// <param name="specifyPropertys">指定只須返回的屬性,效能:伺服器端預測,降低延遲和成本</param>
  108. /// <param name="onProgress">要返回集合的委派</param>
  109. /// <param name="ct">CancellationToken</param>
  110. /// <returns></returns>
  111. 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()
  112. {
  113. TableQuery<T> tableQuery = null;
  114. TableContinuationToken token = null;
  115. var filter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey);
  116. if (specifyPropertys != null)
  117. tableQuery = new TableQuery<T>().Where(filter).Select(specifyPropertys);
  118. else
  119. tableQuery = new TableQuery<T>().Where(filter);
  120. if (takeCount != null) tableQuery.TakeCount = takeCount;
  121. var items = new List<T>();
  122. do
  123. {
  124. var queryResponse = await table.ExecuteQuerySegmentedAsync(tableQuery, token);
  125. token = queryResponse.ContinuationToken;
  126. items.AddRange(queryResponse.Results);
  127. } while (token != null && !ct.IsCancellationRequested);
  128. return items;
  129. }
  130. /// <summary>
  131. /// 傳回指定partitionKey的Count數量
  132. /// </summary>
  133. /// <param name="partitionKey"></param>
  134. /// <returns></returns>
  135. public static int GetCount(this CloudTable table, string partitionKey)
  136. {
  137. TableQuery query = new TableQuery().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey)).Select(new List<string> { "PartitionKey" });
  138. var queryResponse = table.ExecuteQuery(query).ToList();
  139. return queryResponse.Count();
  140. }
  141. #endregion
  142. #region CloudTable Get All
  143. /// <summary>
  144. /// (同步)取得 table 的所有集合,效能:資料表掃描,不佳。
  145. /// </summary>
  146. /// <returns></returns>
  147. public static IEnumerable<T> GetAll<T>(this CloudTable table) where T : ITableEntity, new()
  148. {
  149. return table.ExecuteQuery(new TableQuery<T>());
  150. }
  151. /// <summary>
  152. /// 取得 table 的所有集合(分頁),效能:資料表掃描,不佳。
  153. /// </summary>
  154. /// <param name="takeCount">指定每次返回數量</param>
  155. /// <param name="specifyPropertys">指定只須返回的屬性,效能:伺服器端預測,降低延遲和成本</param>
  156. /// <param name="onProgress">要返回集合的委派</param>
  157. /// <returns></returns>
  158. 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()
  159. {
  160. TableContinuationToken token = null;
  161. TableQuery<T> tableQuery = new TableQuery<T>();
  162. if (specifyPropertys != null)
  163. tableQuery = new TableQuery<T>().Select(specifyPropertys);
  164. else
  165. tableQuery = new TableQuery<T>();
  166. if (takeCount != null) tableQuery.TakeCount = takeCount;
  167. var items = new List<T>();
  168. do
  169. {
  170. var queryResponse = await table.ExecuteQuerySegmentedAsync(tableQuery, token);
  171. token = queryResponse.ContinuationToken;
  172. items.AddRange(queryResponse.Results);
  173. onProgress?.Invoke(queryResponse.Results);
  174. } while (token != null && !ct.IsCancellationRequested);
  175. }
  176. /// <summary>
  177. /// 取得 table 的所有集合,效能:資料表掃描,不佳。
  178. /// </summary>
  179. /// <param name="takeCount">指定每次返回數量</param>
  180. /// <param name="specifyPropertys">指定只須返回的屬性,效能:伺服器端預測,降低延遲和成本</param>
  181. /// <returns></returns>
  182. 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()
  183. {
  184. TableContinuationToken token = null;
  185. TableQuery<T> tableQuery = new TableQuery<T>();
  186. if (takeCount != null) tableQuery.TakeCount = takeCount;
  187. if (specifyPropertys != null)
  188. tableQuery = new TableQuery<T>().Select(specifyPropertys);
  189. else
  190. tableQuery = new TableQuery<T>();
  191. var items = new List<T>();
  192. do
  193. {
  194. var queryResponse = await table.ExecuteQuerySegmentedAsync(tableQuery, token);
  195. token = queryResponse.ContinuationToken;
  196. items.AddRange(queryResponse.Results);
  197. } while (token != null && !ct.IsCancellationRequested);
  198. return items;
  199. }
  200. #endregion
  201. }
  202. }