|
@@ -12,6 +12,7 @@ using TEAMModelOS.SDK.Helper.Security.AESCrypt;
|
|
|
using TEAMModelOS.SDK.Context.Exception;
|
|
|
using System.Reflection;
|
|
|
using TEAMModelOS.SDK.Context.Attributes.Table;
|
|
|
+using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
|
|
|
|
|
|
namespace TEAMModelOS.SDK.Module.AzureTable.Implements
|
|
|
{
|
|
@@ -64,9 +65,8 @@ namespace TEAMModelOS.SDK.Module.AzureTable.Implements
|
|
|
object[] attributes = type.GetCustomAttributes(true);
|
|
|
foreach (object attribute in attributes) //2.通过映射,找到成员属性上关联的特性类实例,
|
|
|
{
|
|
|
- if (attribute is TableSpaceAttribute)
|
|
|
+ if (attribute is TableSpaceAttribute tableSpace)
|
|
|
{
|
|
|
- TableSpaceAttribute tableSpace = (TableSpaceAttribute)attribute;
|
|
|
Name = tableSpace.Name + Name;
|
|
|
}
|
|
|
}
|
|
@@ -75,12 +75,12 @@ namespace TEAMModelOS.SDK.Module.AzureTable.Implements
|
|
|
|
|
|
public async Task<List<T>> FindAll<T>() where T : TableEntity, new()
|
|
|
{
|
|
|
- string TableName= await InitializeTable<T>();
|
|
|
+ string TableName = await InitializeTable<T>();
|
|
|
var exQuery = new TableQuery<T>();
|
|
|
- return await QueryList<T>(exQuery , TableName);
|
|
|
+ return await QueryList<T>(exQuery, TableName);
|
|
|
}
|
|
|
|
|
|
- private async Task<List<T>> QueryList<T>(TableQuery<T> exQuery ,string TableName) where T : TableEntity, new()
|
|
|
+ private async Task<List<T>> QueryList<T>(TableQuery<T> exQuery, string TableName) where T : TableEntity, new()
|
|
|
{
|
|
|
TableContinuationToken continuationToken = null;
|
|
|
List<T> entitys = new List<T>();
|
|
@@ -95,9 +95,9 @@ namespace TEAMModelOS.SDK.Module.AzureTable.Implements
|
|
|
} while (continuationToken != null);
|
|
|
return entitys;
|
|
|
}
|
|
|
-
|
|
|
|
|
|
- private async Task<T> QueryObject<T>(TableQuery<T> exQuery ,string TableName) where T : TableEntity, new()
|
|
|
+
|
|
|
+ private async Task<T> QueryObject<T>(TableQuery<T> exQuery, string TableName) where T : TableEntity, new()
|
|
|
{
|
|
|
TableContinuationToken continuationToken = null;
|
|
|
T entity = new T();
|
|
@@ -113,7 +113,7 @@ namespace TEAMModelOS.SDK.Module.AzureTable.Implements
|
|
|
return entity;
|
|
|
}
|
|
|
|
|
|
- public async Task<int> Count<T>(TableContinuationToken continuationToken ) where T : TableEntity, new()
|
|
|
+ public async Task<int> Count<T>(TableContinuationToken continuationToken) where T : TableEntity, new()
|
|
|
{
|
|
|
string TableName = await InitializeTable<T>();
|
|
|
var exQuery = new TableQuery<T>();
|
|
@@ -132,7 +132,7 @@ namespace TEAMModelOS.SDK.Module.AzureTable.Implements
|
|
|
|
|
|
public async Task<int> Count<T>() where T : TableEntity, new()
|
|
|
{
|
|
|
- string TableName = await InitializeTable<T>();
|
|
|
+ string TableName = await InitializeTable<T>();
|
|
|
TableContinuationToken continuationToken = null;
|
|
|
var exQuery = new TableQuery<T>();
|
|
|
List<T> entitys = new List<T>();
|
|
@@ -148,26 +148,28 @@ namespace TEAMModelOS.SDK.Module.AzureTable.Implements
|
|
|
return entitys.Count;
|
|
|
}
|
|
|
|
|
|
- public async Task<T> FindById<T>(string id) where T : TableEntity, new()
|
|
|
+ public async Task<T> FindByRowKey<T>(string id) where T : TableEntity, new()
|
|
|
{
|
|
|
string TableName = await InitializeTable<T>();
|
|
|
var exQuery = new TableQuery<T>();
|
|
|
if (!string.IsNullOrEmpty(id))
|
|
|
{
|
|
|
- exQuery.Where(TableQuery.GenerateFilterCondition("Id", QueryComparisons.Equal, id));
|
|
|
- return await QueryObject<T>(exQuery , TableName);
|
|
|
+ string typeStr = SwitchType(id, "RowKey");
|
|
|
+ exQuery.Where(typeStr);
|
|
|
+ // exQuery.Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, id));
|
|
|
+ return await QueryObject<T>(exQuery, TableName);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- return null;
|
|
|
+ return null;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
|
|
|
}
|
|
|
|
|
|
public async Task<List<T>> FindListByDict<T>(Dictionary<string, object> dict) where T : TableEntity, new()
|
|
|
{
|
|
|
- string TableName= await InitializeTable<T>();
|
|
|
+ string TableName = await InitializeTable<T>();
|
|
|
var exQuery = new TableQuery<T>();
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
if (null != dict && dict.Count > 0)
|
|
@@ -178,13 +180,19 @@ namespace TEAMModelOS.SDK.Module.AzureTable.Implements
|
|
|
{
|
|
|
if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
|
|
|
{
|
|
|
+ string typeStr = SwitchType(dict[key], key);
|
|
|
+
|
|
|
if (index == 1)
|
|
|
{
|
|
|
- builder.Append(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
|
|
|
+ //builder.Append(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
|
|
|
+ builder.Append(typeStr);
|
|
|
+
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- builder.Append(" " + TableOperators.And + " " + TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
|
|
|
+ //builder.Append(" " + TableOperators.And + " " + TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
|
|
|
+ builder.Append(" " + TableOperators.And + " " + typeStr);
|
|
|
+
|
|
|
}
|
|
|
index++;
|
|
|
}
|
|
@@ -193,30 +201,35 @@ namespace TEAMModelOS.SDK.Module.AzureTable.Implements
|
|
|
exQuery.Where(builder.ToString());
|
|
|
return await QueryList<T>(exQuery, TableName);
|
|
|
}
|
|
|
- else {
|
|
|
- return null;
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public async Task<List<T>> FindListByKey<T>(string key, string value) where T : TableEntity, new()
|
|
|
+ public async Task<List<T>> FindListByKey<T>(string key, object value) where T : TableEntity, new()
|
|
|
{
|
|
|
string TableName = await InitializeTable<T>();
|
|
|
|
|
|
var exQuery = new TableQuery<T>();
|
|
|
- if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
|
|
|
+ if (!string.IsNullOrEmpty(key) && value != null && !string.IsNullOrEmpty(value.ToString()))
|
|
|
{
|
|
|
- exQuery.Where(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, value));
|
|
|
+
|
|
|
+ string typeStr = SwitchType(value, key);
|
|
|
+ exQuery.Where(typeStr);
|
|
|
+ //exQuery.Where(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, value));
|
|
|
return await QueryList<T>(exQuery, TableName);
|
|
|
}
|
|
|
- else {
|
|
|
- return null;
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return null;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
|
|
|
public async Task<T> FindOneByDict<T>(IDictionary<string, object> dict) where T : TableEntity, new()
|
|
|
{
|
|
|
- string TableName = await InitializeTable<T>();
|
|
|
+ string TableName = await InitializeTable<T>();
|
|
|
var exQuery = new TableQuery<T>();
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
if (null != dict && dict.Count > 0)
|
|
@@ -227,47 +240,78 @@ namespace TEAMModelOS.SDK.Module.AzureTable.Implements
|
|
|
{
|
|
|
if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
|
|
|
{
|
|
|
+
|
|
|
+
|
|
|
+ string typeStr = SwitchType(dict[key], key);
|
|
|
+
|
|
|
if (index == 1)
|
|
|
{
|
|
|
- builder.Append(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
|
|
|
+ //builder.Append(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
|
|
|
+ builder.Append(typeStr);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- builder.Append(" " + TableOperators.And + " " + TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
|
|
|
+ // builder.Append(" " + TableOperators.And + " " + TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
|
|
|
+ builder.Append(" " + TableOperators.And + " " + typeStr);
|
|
|
}
|
|
|
index++;
|
|
|
}
|
|
|
}
|
|
|
exQuery.Where(builder.ToString());
|
|
|
- return await QueryObject<T>(exQuery ,TableName);
|
|
|
+ return await QueryObject<T>(exQuery, TableName);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- return null;
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private static string SwitchType(object obj, string key)
|
|
|
+ {
|
|
|
+ Type s = obj.GetType();
|
|
|
+ TypeCode typeCode = Type.GetTypeCode(s);
|
|
|
+
|
|
|
+ switch (typeCode)
|
|
|
+ {
|
|
|
+
|
|
|
+ case TypeCode.String: return TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, obj.ToString());
|
|
|
+ case TypeCode.Int32: return TableQuery.GenerateFilterConditionForInt(key, QueryComparisons.Equal, (int)obj);
|
|
|
+ case TypeCode.Double: return TableQuery.GenerateFilterConditionForDouble(key, QueryComparisons.Equal, (double)obj);
|
|
|
+ case TypeCode.Byte: return TableQuery.GenerateFilterConditionForBinary(key, QueryComparisons.Equal, (byte[])obj);
|
|
|
+ case TypeCode.Boolean: return TableQuery.GenerateFilterConditionForBool(key, QueryComparisons.Equal, (bool)obj);
|
|
|
+ case TypeCode.DateTime: return TableQuery.GenerateFilterConditionForDate(key, QueryComparisons.Equal, (DateTimeOffset)obj);
|
|
|
+ case TypeCode.Int64: return TableQuery.GenerateFilterConditionForLong(key, QueryComparisons.Equal, (long)obj);
|
|
|
+
|
|
|
+ default: return null;
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- public async Task<T> FindOneByKey<T>(string key, string value) where T : TableEntity, new()
|
|
|
+ public async Task<T> FindOneByKey<T>(string key, object value) where T : TableEntity, new()
|
|
|
{
|
|
|
string TableName = await InitializeTable<T>();
|
|
|
var exQuery = new TableQuery<T>();
|
|
|
- if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
|
|
|
+ if (!string.IsNullOrEmpty(key) && value != null && !string.IsNullOrEmpty(value.ToString()))
|
|
|
{
|
|
|
- exQuery.Where(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal,
|
|
|
- value));
|
|
|
- return await QueryObject<T>(exQuery ,TableName );
|
|
|
+
|
|
|
+ string typeStr = SwitchType(value, key);
|
|
|
+ exQuery.Where(typeStr);
|
|
|
+ //exQuery.Where(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal,
|
|
|
+ // value));
|
|
|
+
|
|
|
+
|
|
|
+ return await QueryObject<T>(exQuery, TableName);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return null;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
|
|
|
public async Task<List<T>> GetEntities<T>(IDictionary<string, object> dict) where T : TableEntity, new()
|
|
|
{
|
|
|
- string TableName= await InitializeTable<T>();
|
|
|
+ string TableName = await InitializeTable<T>();
|
|
|
var exQuery = new TableQuery<T>();
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
if (null != dict && dict.Count > 0)
|
|
@@ -278,22 +322,29 @@ namespace TEAMModelOS.SDK.Module.AzureTable.Implements
|
|
|
{
|
|
|
if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
|
|
|
{
|
|
|
+ string typeStr = SwitchType(dict, key);
|
|
|
+
|
|
|
if (index == 1)
|
|
|
{
|
|
|
- builder.Append(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
|
|
|
+ //builder.Append(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
|
|
|
+ builder.Append(typeStr);
|
|
|
+
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- builder.Append(" " + TableOperators.And + " " + TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
|
|
|
+ // builder.Append(" " + TableOperators.And + " " + TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
|
|
|
+ builder.Append(" " + TableOperators.And + " " + typeStr);
|
|
|
+
|
|
|
}
|
|
|
index++;
|
|
|
}
|
|
|
}
|
|
|
exQuery.Where(builder.ToString());
|
|
|
- return await QueryList<T>(exQuery, TableName);
|
|
|
+ return await QueryList<T>(exQuery, TableName);
|
|
|
}
|
|
|
- else {
|
|
|
- return null;
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -301,73 +352,210 @@ namespace TEAMModelOS.SDK.Module.AzureTable.Implements
|
|
|
|
|
|
public async Task<List<T>> SaveAll<T>(List<T> entitys) where T : TableEntity, new()
|
|
|
{
|
|
|
- await InitializeTable<T>();
|
|
|
- IList<TableResult> result = null;
|
|
|
- Parallel.ForEach(Partitioner.Create(0, entitys.Count, 100),
|
|
|
- async range =>
|
|
|
- {
|
|
|
- TableBatchOperation batchOperation = new TableBatchOperation();
|
|
|
- for (Int32 i = range.Item1; i < range.Item2; i++)
|
|
|
- batchOperation.Insert(entitys[i]);
|
|
|
- result = await Table.ExecuteBatchAsync(batchOperation);
|
|
|
- });
|
|
|
+ if (entitys.IsEmpty())
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ string TableName = await InitializeTable<T>();
|
|
|
+ IList<Dictionary<string, List<T>>> listInfo = new List<Dictionary<string, List<T>>>();
|
|
|
+ foreach (IGrouping<string, T> group in entitys.GroupBy(c => c.PartitionKey))
|
|
|
+ {
|
|
|
+ Dictionary<string, List<T>> dictInfo = new Dictionary<string, List<T>>
|
|
|
+ {
|
|
|
+ { group.Key, group.ToList() }
|
|
|
+ };
|
|
|
+ listInfo.Add(dictInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach (Dictionary<string, List<T>> dict in listInfo)
|
|
|
+ {
|
|
|
+ IList<TableResult> result = null;
|
|
|
+ foreach (string key in dict.Keys)
|
|
|
+ {
|
|
|
+ List<T> values = dict[key];
|
|
|
+ //Parallel.ForEach(Partitioner.Create(0, values.Count, 100),
|
|
|
+ // async range =>
|
|
|
+ // {
|
|
|
+ // TableBatchOperation batchOperation = new TableBatchOperation();
|
|
|
+ // for (Int32 i = range.Item1; i < range.Item2; i++)
|
|
|
+ // batchOperation.Insert(values[i]);
|
|
|
+ // result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
|
|
|
+ // });
|
|
|
+ int pageSize = 100;
|
|
|
+ int pages = (int)Math.Ceiling((double)values.Count / pageSize);
|
|
|
+ for (int i = 0; i < pages; i++)
|
|
|
+ {
|
|
|
+ List<T> lists = values.Skip((i) * pageSize).Take(pageSize).ToList();
|
|
|
+ TableBatchOperation batchOperation = new TableBatchOperation();
|
|
|
+ for (int j = 0; j < lists.Count; j++)
|
|
|
+ {
|
|
|
+ batchOperation.Insert(lists[j]);
|
|
|
+ }
|
|
|
+ result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
return entitys;
|
|
|
}
|
|
|
|
|
|
public async Task<List<T>> UpdateAll<T>(List<T> entitys) where T : TableEntity, new()
|
|
|
{
|
|
|
- await InitializeTable<T>();
|
|
|
- IList<TableResult> result = null;
|
|
|
- Parallel.ForEach(Partitioner.Create(0, entitys.Count, 100),
|
|
|
- async range =>
|
|
|
- {
|
|
|
- TableBatchOperation batchOperation = new TableBatchOperation();
|
|
|
- for (Int32 i = range.Item1; i < range.Item2; i++)
|
|
|
- batchOperation.Replace(entitys[i]);
|
|
|
- result = await Table.ExecuteBatchAsync(batchOperation);
|
|
|
- });
|
|
|
+ if (entitys.IsEmpty())
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ string TableName = await InitializeTable<T>();
|
|
|
+ IList<Dictionary<string, List<T>>> listInfo = new List<Dictionary<string, List<T>>>();
|
|
|
+ foreach (IGrouping<string, T> group in entitys.GroupBy(c => c.PartitionKey))
|
|
|
+ {
|
|
|
+ Dictionary<string, List<T>> dictInfo = new Dictionary<string, List<T>>
|
|
|
+ {
|
|
|
+ { group.Key, group.ToList() }
|
|
|
+ };
|
|
|
+ listInfo.Add(dictInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach (Dictionary<string, List<T>> dict in listInfo)
|
|
|
+ {
|
|
|
+ IList<TableResult> result = null;
|
|
|
+ foreach (string key in dict.Keys)
|
|
|
+ {
|
|
|
+ List<T> values = dict[key];
|
|
|
+ //Parallel.ForEach(Partitioner.Create(0, values.Count, 100),
|
|
|
+ // async range =>
|
|
|
+ // {
|
|
|
+ // TableBatchOperation batchOperation = new TableBatchOperation();
|
|
|
+ // for (Int32 i = range.Item1; i < range.Item2; i++)
|
|
|
+ // batchOperation.Replace(values[i]);
|
|
|
+ // result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
|
|
|
+ // });
|
|
|
+ int pageSize = 100;
|
|
|
+ int pages = (int)Math.Ceiling((double)values.Count / pageSize);
|
|
|
+ for (int i = 0; i < pages; i++)
|
|
|
+ {
|
|
|
+ List<T> lists = values.Skip((i) * pageSize).Take(pageSize).ToList();
|
|
|
+ TableBatchOperation batchOperation = new TableBatchOperation();
|
|
|
+ for (int j = 0; j < lists.Count; j++)
|
|
|
+ {
|
|
|
+ batchOperation.Replace(lists[j]);
|
|
|
+ }
|
|
|
+ result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
return entitys;
|
|
|
}
|
|
|
|
|
|
- public async Task<List<T>> SaveOrUpdateAll<T>(List<T> entitys) where T : TableEntity, new() {
|
|
|
- await InitializeTable<T>();
|
|
|
- IList<TableResult> result = null;
|
|
|
- Parallel.ForEach(Partitioner.Create(0, entitys.Count, 100),
|
|
|
- async range =>
|
|
|
- {
|
|
|
- TableBatchOperation batchOperation = new TableBatchOperation();
|
|
|
- for (Int32 i = range.Item1; i < range.Item2; i++)
|
|
|
- batchOperation.InsertOrReplace(entitys[i]);
|
|
|
- result = await Table.ExecuteBatchAsync(batchOperation);
|
|
|
- });
|
|
|
+ public async Task<List<T>> SaveOrUpdateAll<T>(List<T> entitys) where T : TableEntity, new()
|
|
|
+ {
|
|
|
+ if (entitys.IsEmpty())
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ string TableName = await InitializeTable<T>();
|
|
|
+ IList<Dictionary<string, List<T>>> listInfo = new List<Dictionary<string, List<T>>>();
|
|
|
+ foreach (IGrouping<string, T> group in entitys.GroupBy(c => c.PartitionKey))
|
|
|
+ {
|
|
|
+ Dictionary<string, List<T>> dictInfo = new Dictionary<string, List<T>>
|
|
|
+ {
|
|
|
+ { group.Key, group.ToList() }
|
|
|
+ };
|
|
|
+ listInfo.Add(dictInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach (Dictionary<string, List<T>> dict in listInfo)
|
|
|
+ {
|
|
|
+ IList<TableResult> result = null;
|
|
|
+ foreach (string key in dict.Keys)
|
|
|
+ {
|
|
|
+ List<T> values = dict[key];
|
|
|
+ //Parallel.ForEach(Partitioner.Create(0, values.Count, 50),
|
|
|
+ // async range =>
|
|
|
+ // {
|
|
|
+ // TableBatchOperation batchOperation = new TableBatchOperation();
|
|
|
+ // for (Int32 i = range.Item1; i < range.Item2; i++)
|
|
|
+ // batchOperation.InsertOrReplace(values[i]);
|
|
|
+ // result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
|
|
|
+ // });
|
|
|
+
|
|
|
+ int pageSize = 100;
|
|
|
+ int pages = (int)Math.Ceiling((double)values.Count/ pageSize);
|
|
|
+ for (int i= 0; i < pages; i++) {
|
|
|
+ List<T> lists= values.Skip((i) * pageSize).Take(pageSize).ToList();
|
|
|
+ TableBatchOperation batchOperation = new TableBatchOperation();
|
|
|
+ for (int j = 0; j < lists.Count; j++)
|
|
|
+ {
|
|
|
+ batchOperation.InsertOrReplace(lists[j]);
|
|
|
+ }
|
|
|
+ result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
return entitys;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
public async Task<List<T>> DeleteAll<T>(List<T> entitys) where T : TableEntity, new()
|
|
|
{
|
|
|
+ if (entitys.IsEmpty())
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
string TableName = await InitializeTable<T>();
|
|
|
- IList<TableResult> result = null;
|
|
|
- Parallel.ForEach(Partitioner.Create(0, entitys.Count, 100),
|
|
|
- async range =>
|
|
|
- {
|
|
|
- TableBatchOperation batchOperation = new TableBatchOperation();
|
|
|
- for (Int32 i = range.Item1; i < range.Item2; i++)
|
|
|
- batchOperation.Delete(entitys[i]);
|
|
|
-
|
|
|
- result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
|
|
|
- });
|
|
|
- return (List<T>)result;
|
|
|
+ IList<Dictionary<string, List<T>>> listInfo = new List<Dictionary<string, List<T>>>();
|
|
|
+ foreach (IGrouping<string, T> group in entitys.GroupBy(c => c.PartitionKey))
|
|
|
+ {
|
|
|
+ Dictionary<string, List<T>> dictInfo = new Dictionary<string, List<T>>
|
|
|
+ {
|
|
|
+ { group.Key, group.ToList() }
|
|
|
+ };
|
|
|
+ listInfo.Add(dictInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach (Dictionary<string, List<T>> dict in listInfo)
|
|
|
+ {
|
|
|
+ IList<TableResult> result = null;
|
|
|
+ foreach (string key in dict.Keys)
|
|
|
+ {
|
|
|
+ List<T> values = dict[key];
|
|
|
+ //Parallel.ForEach(Partitioner.Create(0, values.Count, 100),
|
|
|
+ // async range =>
|
|
|
+ // {
|
|
|
+ // TableBatchOperation batchOperation = new TableBatchOperation();
|
|
|
+ // for (Int32 i = range.Item1; i < range.Item2; i++)
|
|
|
+ // batchOperation.Delete(values[i]);
|
|
|
+ // result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
|
|
|
+ // });
|
|
|
+
|
|
|
+ int pageSize = 100;
|
|
|
+ int pages = (int)Math.Ceiling((double)values.Count / pageSize);
|
|
|
+ for (int i = 0; i < pages; i++)
|
|
|
+ {
|
|
|
+ List<T> lists = values.Skip((i) * pageSize).Take(pageSize).ToList();
|
|
|
+ TableBatchOperation batchOperation = new TableBatchOperation();
|
|
|
+ for (int j = 0; j < lists.Count; j++)
|
|
|
+ {
|
|
|
+ batchOperation.Delete(lists[j]);
|
|
|
+ }
|
|
|
+ result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return entitys;
|
|
|
}
|
|
|
|
|
|
public async Task<T> Save<T>(TableEntity entity) where T : TableEntity, new()
|
|
|
{
|
|
|
- string TableName= await InitializeTable<T>();
|
|
|
+ string TableName = await InitializeTable<T>();
|
|
|
TableOperation operation = TableOperation.Insert(entity);
|
|
|
TableResult result = await tableClient.GetTableReference(TableName).ExecuteAsync(operation);
|
|
|
return (T)result.Result;
|
|
|
}
|
|
|
public async Task<T> SaveOrUpdate<T>(TableEntity entity) where T : TableEntity, new()
|
|
|
{
|
|
|
- string TableName = await InitializeTable<T>();
|
|
|
+ string TableName = await InitializeTable<T>();
|
|
|
TableOperation operation = TableOperation.InsertOrReplace(entity);
|
|
|
TableResult result = await tableClient.GetTableReference(TableName).ExecuteAsync(operation);
|
|
|
return (T)result.Result;
|
|
@@ -383,8 +571,7 @@ namespace TEAMModelOS.SDK.Module.AzureTable.Implements
|
|
|
|
|
|
public async Task<T> Delete<T>(TableEntity entity) where T : TableEntity, new()
|
|
|
{
|
|
|
-
|
|
|
- string TableName = await InitializeTable<T>();
|
|
|
+ string TableName = await InitializeTable<T>();
|
|
|
TableOperation operation = TableOperation.Delete(entity);
|
|
|
TableResult result = await tableClient.GetTableReference(TableName).ExecuteAsync(operation);
|
|
|
return (T)result.Result;
|
|
@@ -405,8 +592,9 @@ namespace TEAMModelOS.SDK.Module.AzureTable.Implements
|
|
|
//{
|
|
|
// throw new NotImplementedException();
|
|
|
//}
|
|
|
-
|
|
|
- public async Task<AzurePagination<T>> FindListByDict<T>(Dictionary<string, object> dict, AzureTableToken azureTableToken) where T : TableEntity, new() {
|
|
|
+
|
|
|
+ public async Task<AzurePagination<T>> FindListByDict<T>(Dictionary<string, object> dict, AzureTableToken azureTableToken) where T : TableEntity, new()
|
|
|
+ {
|
|
|
string TableName = await InitializeTable<T>();
|
|
|
var exQuery = new TableQuery<T>();
|
|
|
StringBuilder builder = new StringBuilder();
|
|
@@ -418,30 +606,35 @@ namespace TEAMModelOS.SDK.Module.AzureTable.Implements
|
|
|
{
|
|
|
if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
|
|
|
{
|
|
|
+ string typeStr = SwitchType(dict, key);
|
|
|
if (index == 1)
|
|
|
{
|
|
|
- builder.Append(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
|
|
|
+ // builder.Append(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
|
|
|
+ builder.Append(typeStr);
|
|
|
+
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- builder.Append(" " + TableOperators.And + " " + TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
|
|
|
+ builder.Append(" " + TableOperators.And + " " + typeStr);
|
|
|
+ //builder.Append(" " + TableOperators.And + " " + TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
|
|
|
}
|
|
|
index++;
|
|
|
}
|
|
|
}
|
|
|
exQuery.Where(builder.ToString());
|
|
|
- return await QueryList<T>(azureTableToken, exQuery , TableName);
|
|
|
+ return await QueryList<T>(azureTableToken, exQuery, TableName);
|
|
|
}
|
|
|
- else {
|
|
|
- return null;
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return null;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
- private async Task<AzurePagination<T>> QueryList<T>(AzureTableToken azureTableToken, TableQuery<T> exQuery,string TableName) where T : TableEntity, new()
|
|
|
+ private async Task<AzurePagination<T>> QueryList<T>(AzureTableToken azureTableToken, TableQuery<T> exQuery, string TableName) where T : TableEntity, new()
|
|
|
{
|
|
|
TableContinuationToken tableToken = new HaBookTableContinuationToken(azureTableToken).GetContinuationToken();
|
|
|
List<T> entitys = new List<T>();
|
|
|
-
|
|
|
+
|
|
|
var result = await tableClient.GetTableReference(TableName).ExecuteQuerySegmentedAsync(exQuery, tableToken);
|
|
|
if (result.Results.Count > 0)
|
|
|
{
|
|
@@ -455,6 +648,5 @@ namespace TEAMModelOS.SDK.Module.AzureTable.Implements
|
|
|
};
|
|
|
return pagination;
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
}
|