|
@@ -1,5 +1,6 @@
|
|
using Microsoft.Azure.Cosmos;
|
|
using Microsoft.Azure.Cosmos;
|
|
using Microsoft.Azure.Cosmos.Linq;
|
|
using Microsoft.Azure.Cosmos.Linq;
|
|
|
|
+using OpenXmlPowerTools;
|
|
using System;
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
@@ -21,31 +22,48 @@ using TEAMModelOS.SDK.Module.AzureCosmosDB.Configuration;
|
|
|
|
|
|
namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
{
|
|
{
|
|
- public class CosmosModelInfo
|
|
|
|
|
|
+
|
|
|
|
+ public class AzureCosmosDBV3Repository : IAzureCosmosDBV3Repository
|
|
{
|
|
{
|
|
- public Container container { get; set; }
|
|
|
|
- public bool cache { get; set; }
|
|
|
|
- public bool monitor { get; set; } = false;
|
|
|
|
- public Type type { get; set; }
|
|
|
|
- }
|
|
|
|
|
|
|
|
- public class AzureCosmosDBV3Repository : IAzureCosmosDBV3Repository
|
|
|
|
- {
|
|
|
|
|
|
|
|
private CosmosClient CosmosClient { get; set; }
|
|
private CosmosClient CosmosClient { get; set; }
|
|
|
|
+ public CosmosDict CosmosDict { get; set; } = new CosmosDict();
|
|
/// <summary>
|
|
/// <summary>
|
|
- /// 线程安全的dict类型
|
|
|
|
|
|
+ /// 数据库名
|
|
/// </summary>
|
|
/// </summary>
|
|
- private Dictionary<string, CosmosModelInfo> DocumentCollectionDict { get; set; } = new Dictionary<string, CosmosModelInfo>();
|
|
|
|
-
|
|
|
|
private string DatabaseId { get; set; }
|
|
private string DatabaseId { get; set; }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// RU
|
|
|
|
+ /// </summary>
|
|
private int CollectionThroughput { get; set; }
|
|
private int CollectionThroughput { get; set; }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 数据库对象
|
|
|
|
+ /// </summary>
|
|
private Database database { get; set; }
|
|
private Database database { get; set; }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 分页大小
|
|
|
|
+ /// </summary>
|
|
int pageSize = 200;
|
|
int pageSize = 200;
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 缓存前缀
|
|
|
|
+ /// </summary>
|
|
private const string CacheCosmosPrefix = "cosmos:";
|
|
private const string CacheCosmosPrefix = "cosmos:";
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 扫描类
|
|
|
|
+ /// </summary>
|
|
private string[] ScanModel { get; set; }
|
|
private string[] ScanModel { get; set; }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 超时时间
|
|
|
|
+ /// </summary>
|
|
private const int timeoutSeconds = 86400;
|
|
private const int timeoutSeconds = 86400;
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// ttl时长 1秒
|
|
|
|
+ /// </summary>
|
|
private const int ttl = 1;
|
|
private const int ttl = 1;
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 更新源通知容器
|
|
|
|
+ /// </summary>
|
|
private string leaseId = "AleaseContainer";
|
|
private string leaseId = "AleaseContainer";
|
|
public AzureCosmosDBV3Repository(AzureCosmosDBOptions options, CosmosSerializer cosmosSerializer)
|
|
public AzureCosmosDBV3Repository(AzureCosmosDBOptions options, CosmosSerializer cosmosSerializer)
|
|
{
|
|
{
|
|
@@ -62,11 +80,9 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
DatabaseId = options.Database;
|
|
DatabaseId = options.Database;
|
|
CollectionThroughput = options.CollectionThroughput;
|
|
CollectionThroughput = options.CollectionThroughput;
|
|
ScanModel = options.ScanModel;
|
|
ScanModel = options.ScanModel;
|
|
- // InitializeDatabase().GetAwaiter().GetResult();
|
|
|
|
}
|
|
}
|
|
catch (CosmosException e)
|
|
catch (CosmosException e)
|
|
{
|
|
{
|
|
- // Dispose(true);
|
|
|
|
throw new BizException(e.Message, 500, e.StackTrace);
|
|
throw new BizException(e.Message, 500, e.StackTrace);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -86,118 +102,105 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
DatabaseId = options.Database;
|
|
DatabaseId = options.Database;
|
|
CollectionThroughput = options.CollectionThroughput;
|
|
CollectionThroughput = options.CollectionThroughput;
|
|
ScanModel = options.ScanModel;
|
|
ScanModel = options.ScanModel;
|
|
- // InitializeDatabase().GetAwaiter().GetResult();
|
|
|
|
}
|
|
}
|
|
catch (CosmosException e)
|
|
catch (CosmosException e)
|
|
{
|
|
{
|
|
- // Dispose(true);
|
|
|
|
throw new BizException(e.Message, 500, e.StackTrace);
|
|
throw new BizException(e.Message, 500, e.StackTrace);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
- public async Task<Dictionary<string, CosmosModelInfo>> InitializeDatabase()
|
|
|
|
- {
|
|
|
|
- try
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 初始化CosmosDB数据库
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public async Task<CosmosDict> InitializeDatabase() {
|
|
|
|
+ database = await CosmosClient.CreateDatabaseIfNotExistsAsync(DatabaseId, CollectionThroughput);
|
|
|
|
+ FeedIterator<ContainerProperties> resultSetIterator = database.GetContainerQueryIterator<ContainerProperties>();
|
|
|
|
+ while (resultSetIterator.HasMoreResults)
|
|
{
|
|
{
|
|
- database = await CosmosClient.CreateDatabaseIfNotExistsAsync(DatabaseId, CollectionThroughput);
|
|
|
|
- FeedIterator<ContainerProperties> resultSetIterator = database.GetContainerQueryIterator<ContainerProperties>();
|
|
|
|
- while (resultSetIterator.HasMoreResults)
|
|
|
|
|
|
+ foreach (ContainerProperties container in await resultSetIterator.ReadNextAsync())
|
|
{
|
|
{
|
|
- foreach (ContainerProperties container in await resultSetIterator.ReadNextAsync())
|
|
|
|
- {
|
|
|
|
- DocumentCollectionDict.TryAdd(container.Id, new CosmosModelInfo { container = database.GetContainer(container.Id), cache = false, monitor = false });
|
|
|
|
- }
|
|
|
|
|
|
+ CosmosDict.nameCosmos.TryAdd(container.Id, new CosmosModelInfo { container = database.GetContainer(container.Id), cache = false, monitor = false });
|
|
}
|
|
}
|
|
- bool isMonitor = false;
|
|
|
|
- //获取数据库所有的表
|
|
|
|
- List<Type> types = ReflectorExtensions.GetAllTypeAsAttribute<CosmosDBAttribute>(ScanModel);
|
|
|
|
- foreach (Type type in types)
|
|
|
|
|
|
+ }
|
|
|
|
+ bool isMonitor = false;
|
|
|
|
+ //获取数据库所有的表
|
|
|
|
+ List<Type> types = ReflectorExtensions.GetAllTypeAsAttribute<CosmosDBAttribute>(ScanModel);
|
|
|
|
+ foreach (Type type in types)
|
|
|
|
+ {
|
|
|
|
+ string PartitionKey = GetPartitionKey(type);
|
|
|
|
+ string CollectionName = "";
|
|
|
|
+ int RU = 0;
|
|
|
|
+ bool cache = false;
|
|
|
|
+ bool monitor = false;
|
|
|
|
+ IEnumerable<CosmosDBAttribute> attributes = type.GetCustomAttributes<CosmosDBAttribute>(true);
|
|
|
|
+ if (attributes != null && !string.IsNullOrEmpty(attributes.First<CosmosDBAttribute>().Name))
|
|
{
|
|
{
|
|
- string PartitionKey = GetPartitionKey(type);
|
|
|
|
- string CollectionName = "";
|
|
|
|
- int RU = 0;
|
|
|
|
- bool cache = false;
|
|
|
|
- bool monitor = false;
|
|
|
|
- IEnumerable<CosmosDBAttribute> attributes = type.GetCustomAttributes<CosmosDBAttribute>(true);
|
|
|
|
- if (!string.IsNullOrEmpty(attributes.First<CosmosDBAttribute>().Name))
|
|
|
|
- {
|
|
|
|
- CollectionName = attributes.First<CosmosDBAttribute>().Name;
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- CollectionName = type.Name;
|
|
|
|
- }
|
|
|
|
- if (attributes.First<CosmosDBAttribute>().Cache)
|
|
|
|
- {
|
|
|
|
- cache = attributes.First<CosmosDBAttribute>().Cache;
|
|
|
|
- }
|
|
|
|
- if (attributes.First<CosmosDBAttribute>().Monitor)
|
|
|
|
|
|
+ CollectionName = attributes.First<CosmosDBAttribute>().Name;
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ throw new BizException("必须指定容器名",ResponseCode.PARAMS_ERROR);
|
|
|
|
+ }
|
|
|
|
+ if (attributes.First<CosmosDBAttribute>().Cache)
|
|
|
|
+ {
|
|
|
|
+ cache = attributes.First<CosmosDBAttribute>().Cache;
|
|
|
|
+ }
|
|
|
|
+ if (attributes.First<CosmosDBAttribute>().Monitor)
|
|
|
|
+ {
|
|
|
|
+ monitor = attributes.First<CosmosDBAttribute>().Monitor;
|
|
|
|
+ if (monitor)
|
|
{
|
|
{
|
|
- monitor = attributes.First<CosmosDBAttribute>().Monitor;
|
|
|
|
- if (monitor)
|
|
|
|
- {
|
|
|
|
- isMonitor = true;
|
|
|
|
- }
|
|
|
|
|
|
+ isMonitor = true;
|
|
}
|
|
}
|
|
- //else
|
|
|
|
- //{
|
|
|
|
- // cache = false;
|
|
|
|
- //}
|
|
|
|
- if (attributes.First<CosmosDBAttribute>().RU > 400)
|
|
|
|
|
|
+ }
|
|
|
|
+ if (attributes.First<CosmosDBAttribute>().RU > 400)
|
|
|
|
+ {
|
|
|
|
+ RU = attributes.First<CosmosDBAttribute>().RU;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ RU = CollectionThroughput;
|
|
|
|
+ }
|
|
|
|
+ //如果表存在于数据则检查RU是否变动,如果不存在则执行创建DocumentCollection
|
|
|
|
+ if (CosmosDict.nameCosmos.TryGetValue(CollectionName, out CosmosModelInfo cosmosModelInfo))
|
|
|
|
+ { //更新RU
|
|
|
|
+
|
|
|
|
+ cosmosModelInfo.cache = cache;
|
|
|
|
+ Container container = CosmosClient.GetDatabase(DatabaseId).GetContainer(cosmosModelInfo.container.Id);
|
|
|
|
+ int? throughputResponse = await container.ReadThroughputAsync();
|
|
|
|
+ if (throughputResponse < RU)
|
|
{
|
|
{
|
|
- RU = attributes.First<CosmosDBAttribute>().RU;
|
|
|
|
|
|
+ await CosmosClient.GetDatabase(DatabaseId).GetContainer(cosmosModelInfo.container.Id).ReplaceThroughputAsync(RU);
|
|
}
|
|
}
|
|
- else
|
|
|
|
|
|
+ CosmosModelInfo cosmos = new CosmosModelInfo { container = container, cache = cache, monitor = monitor, type = type };
|
|
|
|
+ CosmosDict.nameCosmos[CollectionName] = cosmos;
|
|
|
|
+ CosmosDict.typeCosmos.Add(type.Name, cosmos);
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ ContainerProperties containerProperties = new ContainerProperties { Id = CollectionName, DefaultTimeToLive = -1 };
|
|
|
|
+ if (!string.IsNullOrEmpty(PartitionKey))
|
|
{
|
|
{
|
|
- RU = CollectionThroughput;
|
|
|
|
- }
|
|
|
|
- //如果表存在于数据则检查RU是否变动,如果不存在则执行创建DocumentCollection
|
|
|
|
- if (DocumentCollectionDict.TryGetValue(CollectionName, out CosmosModelInfo cosmosModelInfo))
|
|
|
|
- { //更新RU
|
|
|
|
-
|
|
|
|
- cosmosModelInfo.cache = cache;
|
|
|
|
- Container container = CosmosClient.GetDatabase(DatabaseId).GetContainer(cosmosModelInfo.container.Id);
|
|
|
|
- int? throughputResponse = await container.ReadThroughputAsync();
|
|
|
|
- if (throughputResponse < RU)
|
|
|
|
- {
|
|
|
|
- await CosmosClient.GetDatabase(DatabaseId).GetContainer(cosmosModelInfo.container.Id).ReplaceThroughputAsync(RU);
|
|
|
|
- }
|
|
|
|
- DocumentCollectionDict[CollectionName] = new CosmosModelInfo { container = container, cache = cache, monitor = monitor, type = type };
|
|
|
|
|
|
+ containerProperties.PartitionKeyPath = "/" + PartitionKey;
|
|
}
|
|
}
|
|
- else
|
|
|
|
|
|
+ if (RU > CollectionThroughput)
|
|
{
|
|
{
|
|
- ContainerProperties containerProperties = new ContainerProperties { Id = CollectionName ,DefaultTimeToLive=-1 };
|
|
|
|
-
|
|
|
|
- if (!string.IsNullOrEmpty(PartitionKey))
|
|
|
|
- {
|
|
|
|
- containerProperties.PartitionKeyPath = "/" + PartitionKey;
|
|
|
|
- }
|
|
|
|
- if (RU > CollectionThroughput)
|
|
|
|
- {
|
|
|
|
- CollectionThroughput = RU;
|
|
|
|
- }
|
|
|
|
- Container containerWithConsistentIndexing = await database.CreateContainerIfNotExistsAsync(containerProperties, throughput: CollectionThroughput);
|
|
|
|
- DocumentCollectionDict.TryAdd(CollectionName, new CosmosModelInfo { container = containerWithConsistentIndexing, cache = cache, monitor = monitor, type = type });
|
|
|
|
|
|
+ CollectionThroughput = RU;
|
|
}
|
|
}
|
|
|
|
+ Container containerWithConsistentIndexing = await database.CreateContainerIfNotExistsAsync(containerProperties, throughput: CollectionThroughput);
|
|
|
|
+ CosmosModelInfo cosmos = new CosmosModelInfo { container = containerWithConsistentIndexing, cache = cache, monitor = monitor, type = type };
|
|
|
|
+ CosmosDict.nameCosmos[CollectionName] = cosmos;
|
|
|
|
+ CosmosDict.typeCosmos.Add(type.Name, cosmos);
|
|
}
|
|
}
|
|
- if (isMonitor)
|
|
|
|
- {
|
|
|
|
- ContainerProperties leaseProperties = new ContainerProperties { Id = leaseId, PartitionKeyPath = "/id", DefaultTimeToLive = -1 };
|
|
|
|
- Container leaseContainer = await database.CreateContainerIfNotExistsAsync(leaseProperties, throughput: CollectionThroughput);
|
|
|
|
- DocumentCollectionDict.TryAdd(leaseId, new CosmosModelInfo { container = leaseContainer, cache = false, monitor = false });
|
|
|
|
- }
|
|
|
|
- return DocumentCollectionDict;
|
|
|
|
}
|
|
}
|
|
- catch (CosmosException e)
|
|
|
|
|
|
+ if (isMonitor)
|
|
{
|
|
{
|
|
- throw new BizException(e.Message, 500, e.StackTrace);
|
|
|
|
|
|
+ ContainerProperties leaseProperties = new ContainerProperties { Id = leaseId, PartitionKeyPath = "/id", DefaultTimeToLive = -1 };
|
|
|
|
+ Container leaseContainer = await database.CreateContainerIfNotExistsAsync(leaseProperties, throughput: CollectionThroughput);
|
|
|
|
+ CosmosDict.nameCosmos.TryAdd(leaseId, new CosmosModelInfo { container = leaseContainer, cache = false, monitor = false });
|
|
}
|
|
}
|
|
|
|
+ return CosmosDict;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
private string GetPartitionKey<T>()
|
|
private string GetPartitionKey<T>()
|
|
{
|
|
{
|
|
Type type = typeof(T);
|
|
Type type = typeof(T);
|
|
@@ -225,7 +228,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
}
|
|
}
|
|
if (attrProperties.Count <= 0)
|
|
if (attrProperties.Count <= 0)
|
|
{
|
|
{
|
|
- throw new BizException(type.Name + "has no PartitionKey !");
|
|
|
|
|
|
+ throw new BizException(type.Name + " has no PartitionKey !");
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
@@ -237,41 +240,41 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
private async Task<CosmosModelInfo> InitializeCollection<T>()
|
|
private async Task<CosmosModelInfo> InitializeCollection<T>()
|
|
{
|
|
{
|
|
Type type = typeof(T);
|
|
Type type = typeof(T);
|
|
string partitionKey = GetPartitionKey<T>();
|
|
string partitionKey = GetPartitionKey<T>();
|
|
- string CollectionName;
|
|
|
|
|
|
+ CosmosDBAttribute cosmosDBAttribute = null;
|
|
IEnumerable<CosmosDBAttribute> attributes = type.GetCustomAttributes<CosmosDBAttribute>(true);
|
|
IEnumerable<CosmosDBAttribute> attributes = type.GetCustomAttributes<CosmosDBAttribute>(true);
|
|
- if (!string.IsNullOrEmpty(attributes.First<CosmosDBAttribute>().Name))
|
|
|
|
|
|
+ if (attributes != null && !string.IsNullOrEmpty(attributes.First<CosmosDBAttribute>().Name))
|
|
{
|
|
{
|
|
- CollectionName = attributes.First<CosmosDBAttribute>().Name;
|
|
|
|
|
|
+ cosmosDBAttribute = attributes.First<CosmosDBAttribute>();
|
|
}
|
|
}
|
|
- else
|
|
|
|
- {
|
|
|
|
- CollectionName = type.Name;
|
|
|
|
|
|
+ else {
|
|
|
|
+ throw new BizException(type.Name+"未指定CosmosDB表名",ResponseCode.PARAMS_ERROR);
|
|
}
|
|
}
|
|
- return await InitializeCollection(CollectionName, partitionKey);
|
|
|
|
|
|
+
|
|
|
|
+ return await InitializeCollection(cosmosDBAttribute, type.Name, partitionKey);
|
|
}
|
|
}
|
|
-
|
|
|
|
- private async Task<CosmosModelInfo> InitializeCollection(string CollectionName, string PartitionKey)
|
|
|
|
|
|
+ private async Task<CosmosModelInfo> InitializeCollection(CosmosDBAttribute cosmosDBAttribute, string typeName, string PartitionKey)
|
|
{
|
|
{
|
|
/////内存中已经存在这个表则直接返回
|
|
/////内存中已经存在这个表则直接返回
|
|
- if (DocumentCollectionDict.TryGetValue(CollectionName, out CosmosModelInfo cosmosModelInfo))
|
|
|
|
|
|
+ if (CosmosDict.typeCosmos.TryGetValue(typeName, out CosmosModelInfo cosmosModelInfo))
|
|
{
|
|
{
|
|
return cosmosModelInfo;
|
|
return cosmosModelInfo;
|
|
}///如果没有则尝试默认创建
|
|
}///如果没有则尝试默认创建
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- ContainerProperties containerProperties = new ContainerProperties { Id = CollectionName };
|
|
|
|
|
|
+ ContainerProperties containerProperties = new ContainerProperties { Id = cosmosDBAttribute.Name };
|
|
if (!string.IsNullOrEmpty(PartitionKey))
|
|
if (!string.IsNullOrEmpty(PartitionKey))
|
|
{
|
|
{
|
|
- containerProperties.PartitionKeyPath = "/" + PartitionKey;
|
|
|
|
|
|
+ containerProperties.PartitionKeyPath = "/"+ PartitionKey;
|
|
}
|
|
}
|
|
- Container containerWithConsistentIndexing = await database.CreateContainerIfNotExistsAsync(containerProperties, throughput: CollectionThroughput);
|
|
|
|
- CosmosModelInfo cosmosModel = new CosmosModelInfo { container = containerWithConsistentIndexing, cache = false };
|
|
|
|
- DocumentCollectionDict.TryAdd(CollectionName, cosmosModel);
|
|
|
|
|
|
+
|
|
|
|
+ Container containerWithConsistentIndexing = await database.CreateContainerIfNotExistsAsync(containerProperties, throughput: cosmosDBAttribute.RU);
|
|
|
|
+ CosmosModelInfo cosmosModel = new CosmosModelInfo { container = containerWithConsistentIndexing, cache = cosmosDBAttribute.Cache,monitor= cosmosDBAttribute.Monitor };
|
|
|
|
+ CosmosDict. nameCosmos.TryAdd(cosmosDBAttribute.Name, cosmosModel);
|
|
|
|
+ CosmosDict.typeCosmos.TryAdd(typeName, cosmosModel);
|
|
return cosmosModel;
|
|
return cosmosModel;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -281,10 +284,11 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="list"></param>
|
|
/// <param name="list"></param>
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
- private async Task<List<T>> DeleteTTL<T>(List<T> list)where T:ID {
|
|
|
|
|
|
+ private async Task<List<T>> DeleteTTL<T>(List<T> list) where T : ID
|
|
|
|
+ {
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
- list.ForEach(x=> { x.ttl = ttl; });
|
|
|
|
- list= await SaveOrUpdateAll(list);
|
|
|
|
|
|
+ list.ForEach(x => { x.ttl = ttl; });
|
|
|
|
+ list = await SaveOrUpdateAll(list);
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
{
|
|
{
|
|
list.ForEach(async x => {
|
|
list.ForEach(async x => {
|
|
@@ -293,17 +297,22 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
}
|
|
}
|
|
return list;
|
|
return list;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+
|
|
public async Task<List<IdPk>> DeleteAll<T>(List<IdPk> ids) where T : ID
|
|
public async Task<List<IdPk>> DeleteAll<T>(List<IdPk> ids) where T : ID
|
|
{
|
|
{
|
|
|
|
+
|
|
|
|
+ // string pk = GetPartitionKey<T>();
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
List<IdPk> idPks = new List<IdPk>();
|
|
List<IdPk> idPks = new List<IdPk>();
|
|
if (container.monitor)
|
|
if (container.monitor)
|
|
{
|
|
{
|
|
- List<T> list= await FindByDict<T>(new Dictionary<string, object>() { { "id", ids.Select(x => x.id).ToArray() } });
|
|
|
|
- list= await DeleteTTL(list);
|
|
|
|
|
|
+ List<T> list = await FindByDict<T>(new Dictionary<string, object>() { { "id", ids.Select(x => x.id).ToArray() } });
|
|
|
|
+ list = await DeleteTTL(list);
|
|
return ids;
|
|
return ids;
|
|
}
|
|
}
|
|
- else {
|
|
|
|
|
|
+ else
|
|
|
|
+ {
|
|
int pages = (int)Math.Ceiling((double)ids.Count / pageSize);
|
|
int pages = (int)Math.Ceiling((double)ids.Count / pageSize);
|
|
Stopwatch stopwatch = Stopwatch.StartNew();
|
|
Stopwatch stopwatch = Stopwatch.StartNew();
|
|
for (int i = 0; i < pages; i++)
|
|
for (int i = 0; i < pages; i++)
|
|
@@ -317,11 +326,11 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
{
|
|
{
|
|
using (ResponseMessage response = task.Result)
|
|
using (ResponseMessage response = task.Result)
|
|
{
|
|
{
|
|
- idPks.Add(new IdPk { id = item.id, pk = item.pk.ToString(), StatusCode = response.StatusCode });
|
|
|
|
- // if (!response.IsSuccessStatusCode)
|
|
|
|
- // {
|
|
|
|
- // }
|
|
|
|
- }
|
|
|
|
|
|
+ idPks.Add(new IdPk { id = item.id, pk = item.pk, StatusCode = response.StatusCode });
|
|
|
|
+ // if (!response.IsSuccessStatusCode)
|
|
|
|
+ // {
|
|
|
|
+ // }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
));
|
|
));
|
|
});
|
|
});
|
|
@@ -336,10 +345,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
stopwatch.Stop();
|
|
stopwatch.Stop();
|
|
return idPks;
|
|
return idPks;
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
-
|
|
|
|
public async Task<List<IdPk>> DeleteAll<T>(Dictionary<string, object> dict) where T : ID
|
|
public async Task<List<IdPk>> DeleteAll<T>(Dictionary<string, object> dict) where T : ID
|
|
{
|
|
{
|
|
if (dict.Keys.Count > 0)
|
|
if (dict.Keys.Count > 0)
|
|
@@ -347,10 +353,11 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
List<T> list = await FindByDict<T>(dict);
|
|
List<T> list = await FindByDict<T>(dict);
|
|
return await DeleteAll(list);
|
|
return await DeleteAll(list);
|
|
}
|
|
}
|
|
- else {
|
|
|
|
- throw new BizException("参数为空", 500);
|
|
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ throw new BizException("参数为空", 500);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
}
|
|
}
|
|
public async Task<List<IdPk>> DeleteAll<T>(List<T> enyites) where T : ID
|
|
public async Task<List<IdPk>> DeleteAll<T>(List<T> enyites) where T : ID
|
|
{
|
|
{
|
|
@@ -362,13 +369,15 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
if (container.monitor)
|
|
if (container.monitor)
|
|
{
|
|
{
|
|
enyites = await DeleteTTL(enyites);
|
|
enyites = await DeleteTTL(enyites);
|
|
- foreach (T t in enyites) {
|
|
|
|
|
|
+ foreach (T t in enyites)
|
|
|
|
+ {
|
|
object o = type.GetProperty(pk).GetValue(t, null);
|
|
object o = type.GetProperty(pk).GetValue(t, null);
|
|
- idPks.Add(new IdPk { id = t.id, pk =o.ToString(), StatusCode = HttpStatusCode .NoContent});
|
|
|
|
|
|
+ idPks.Add(new IdPk { id = t.id, pk = o.ToString(), StatusCode = HttpStatusCode.NoContent });
|
|
}
|
|
}
|
|
return idPks;
|
|
return idPks;
|
|
}
|
|
}
|
|
- else {
|
|
|
|
|
|
+ else
|
|
|
|
+ {
|
|
int pages = (int)Math.Ceiling((double)enyites.Count / pageSize);
|
|
int pages = (int)Math.Ceiling((double)enyites.Count / pageSize);
|
|
Stopwatch stopwatch = Stopwatch.StartNew();
|
|
Stopwatch stopwatch = Stopwatch.StartNew();
|
|
for (int i = 0; i < pages; i++)
|
|
for (int i = 0; i < pages; i++)
|
|
@@ -408,27 +417,30 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
return idPks;
|
|
return idPks;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
public async Task<IdPk> DeleteAsync<T>(IdPk idPk) where T : ID
|
|
public async Task<IdPk> DeleteAsync<T>(IdPk idPk) where T : ID
|
|
{
|
|
{
|
|
return await DeleteAsync<T>(idPk.id, idPk.pk);
|
|
return await DeleteAsync<T>(idPk.id, idPk.pk);
|
|
}
|
|
}
|
|
public async Task<IdPk> DeleteAsync<T>(string id, string pk) where T : ID
|
|
public async Task<IdPk> DeleteAsync<T>(string id, string pk) where T : ID
|
|
{
|
|
{
|
|
|
|
+
|
|
|
|
+ // pk = GetPartitionKey<T>();
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
if (container.monitor)
|
|
if (container.monitor)
|
|
{
|
|
{
|
|
- List<T> list= await FindByDict<T>(new Dictionary<string, object>() { { "id", id } });
|
|
|
|
|
|
+ List<T> list = await FindByDict<T>(new Dictionary<string, object>() { { "id", id } });
|
|
if (list.Count > 0)
|
|
if (list.Count > 0)
|
|
{
|
|
{
|
|
await DeleteTTL<T>(list);
|
|
await DeleteTTL<T>(list);
|
|
return new IdPk { id = id, pk = pk, StatusCode = HttpStatusCode.NoContent };
|
|
return new IdPk { id = id, pk = pk, StatusCode = HttpStatusCode.NoContent };
|
|
}
|
|
}
|
|
- else {
|
|
|
|
|
|
+ else
|
|
|
|
+ {
|
|
throw new BizException("未找到ID匹配的数据,删除失败");
|
|
throw new BizException("未找到ID匹配的数据,删除失败");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- else {
|
|
|
|
|
|
+ else
|
|
|
|
+ {
|
|
ResponseMessage response = await container.container.DeleteItemStreamAsync(id: id, partitionKey: new PartitionKey(pk));
|
|
ResponseMessage response = await container.container.DeleteItemStreamAsync(id: id, partitionKey: new PartitionKey(pk));
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
{
|
|
{
|
|
@@ -436,7 +448,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
}
|
|
}
|
|
return new IdPk { id = id, pk = pk, StatusCode = response.StatusCode };
|
|
return new IdPk { id = id, pk = pk, StatusCode = response.StatusCode };
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
public async Task<IdPk> DeleteAsync<T>(T entity) where T : ID
|
|
public async Task<IdPk> DeleteAsync<T>(T entity) where T : ID
|
|
@@ -444,33 +456,29 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
string partitionKey = GetPartitionKey<T>();
|
|
string partitionKey = GetPartitionKey<T>();
|
|
Type type = typeof(T);
|
|
Type type = typeof(T);
|
|
- object o = type.GetProperty(partitionKey).GetValue(entity, null);
|
|
|
|
|
|
+ object o = type.GetProperty(partitionKey).GetValue(entity, null);
|
|
if (container.monitor)
|
|
if (container.monitor)
|
|
{
|
|
{
|
|
- await DeleteTTL<T>(new List<T>() { entity});
|
|
|
|
|
|
+ await DeleteTTL<T>(new List<T>() { entity });
|
|
return new IdPk { id = entity.id, pk = o.ToString(), StatusCode = HttpStatusCode.NoContent };
|
|
return new IdPk { id = entity.id, pk = o.ToString(), StatusCode = HttpStatusCode.NoContent };
|
|
}
|
|
}
|
|
- else {
|
|
|
|
|
|
+ else
|
|
|
|
+ {
|
|
ResponseMessage response = await container.container.DeleteItemStreamAsync(id: entity.id, partitionKey: new PartitionKey(o.ToString()));
|
|
ResponseMessage response = await container.container.DeleteItemStreamAsync(id: entity.id, partitionKey: new PartitionKey(o.ToString()));
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
{
|
|
{
|
|
await RedisHelper.HDelAsync(CacheCosmosPrefix + container.container.Id, entity.id);
|
|
await RedisHelper.HDelAsync(CacheCosmosPrefix + container.container.Id, entity.id);
|
|
}
|
|
}
|
|
- return new IdPk { id = entity.id, pk = o.ToString(), StatusCode = response.StatusCode };
|
|
|
|
|
|
+ return new IdPk { id = entity.id, pk = partitionKey, StatusCode = response.StatusCode };
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- //public async Task<T> DeleteAsync<T>(string id) where T : ID
|
|
|
|
- //{
|
|
|
|
- // Container container = await InitializeCollection<T>();
|
|
|
|
- // ItemResponse<T> response = await container.DeleteItemAsync<T>(id: id, partitionKey: new PartitionKey(GetPartitionKey<T>()));
|
|
|
|
- // return response.Resource;
|
|
|
|
- //}
|
|
|
|
-
|
|
|
|
public async Task<List<T>> FindAll<T>(List<string> propertys = null) where T : ID
|
|
public async Task<List<T>> FindAll<T>(List<string> propertys = null) where T : ID
|
|
{
|
|
{
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
|
|
+ string pk = typeof(T).Name;
|
|
StringBuilder sql;
|
|
StringBuilder sql;
|
|
sql = SQLHelperParametric.GetSQLSelect(propertys);
|
|
sql = SQLHelperParametric.GetSQLSelect(propertys);
|
|
|
|
+ sql.Append(" where c.pk = " + pk);
|
|
CosmosDbQuery cosmosDbQuery = new CosmosDbQuery { QueryText = sql.ToString() };
|
|
CosmosDbQuery cosmosDbQuery = new CosmosDbQuery { QueryText = sql.ToString() };
|
|
FeedIterator<T> query = container.container.GetItemQueryIterator<T>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition);
|
|
FeedIterator<T> query = container.container.GetItemQueryIterator<T>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition);
|
|
return await ResultsFromFeedIterator(query);
|
|
return await ResultsFromFeedIterator(query);
|
|
@@ -514,9 +522,9 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
|
|
|
return results;
|
|
return results;
|
|
}
|
|
}
|
|
- public async Task<List<dynamic>> FindByDict(string CollectionName, Dictionary<string, object> dict, string partitionKey = null, List<string> propertys = null)
|
|
|
|
|
|
+ public async Task<List<dynamic>> FindByDict(string CollectionName, Dictionary<string, object> dict, List<string> propertys = null)
|
|
{
|
|
{
|
|
- if (DocumentCollectionDict.TryGetValue(CollectionName, out CosmosModelInfo container))
|
|
|
|
|
|
+ if (CosmosDict.typeCosmos.TryGetValue(CollectionName, out CosmosModelInfo container))
|
|
{
|
|
{
|
|
//StringBuilder sql = new StringBuilder("select value(c) from c");
|
|
//StringBuilder sql = new StringBuilder("select value(c) from c");
|
|
//SQLHelper.GetSQL(dict, ref sql);
|
|
//SQLHelper.GetSQL(dict, ref sql);
|
|
@@ -525,9 +533,11 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
// QueryText = sql.ToString()
|
|
// QueryText = sql.ToString()
|
|
|
|
|
|
//};
|
|
//};
|
|
|
|
+
|
|
|
|
+ string pk = container.type.Name;
|
|
StringBuilder sql;
|
|
StringBuilder sql;
|
|
sql = SQLHelperParametric.GetSQLSelect(propertys);
|
|
sql = SQLHelperParametric.GetSQLSelect(propertys);
|
|
- CosmosDbQuery cosmosDbQuery = SQLHelperParametric.GetSQL(dict, sql);
|
|
|
|
|
|
+ CosmosDbQuery cosmosDbQuery = SQLHelperParametric.GetSQL(dict, sql, pk);
|
|
QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(-1, null));
|
|
QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(-1, null));
|
|
FeedIterator<dynamic> query = container.container.GetItemQueryIterator<dynamic>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: queryRequestOptions);
|
|
FeedIterator<dynamic> query = container.container.GetItemQueryIterator<dynamic>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: queryRequestOptions);
|
|
return await ResultsFromFeedIterator(query);
|
|
return await ResultsFromFeedIterator(query);
|
|
@@ -539,16 +549,27 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- public async Task<List<dynamic>> FindCountByDict(string CollectionName, Dictionary<string, object> dict, string partitionKey = null)
|
|
|
|
|
|
+ public async Task<List<int>> FindCountByDict<T>(Dictionary<string, object> dict) {
|
|
|
|
+ CosmosModelInfo container = await InitializeCollection<T>();
|
|
|
|
+ string pk = typeof(T).Name;
|
|
|
|
+ StringBuilder sql = new StringBuilder("select value count(c) from c");
|
|
|
|
+ CosmosDbQuery cosmosDbQuery = SQLHelperParametric.GetSQL(dict, sql, pk);
|
|
|
|
+ QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(-1, null));
|
|
|
|
+ FeedIterator<int> query = container.container.GetItemQueryIterator<int>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: queryRequestOptions);
|
|
|
|
+ return await ResultsFromFeedIterator(query);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<List<dynamic>> FindCountByDict(string CollectionName, Dictionary<string, object> dict)
|
|
{
|
|
{
|
|
- if (DocumentCollectionDict.TryGetValue(CollectionName, out CosmosModelInfo container))
|
|
|
|
|
|
+ if (CosmosDict.typeCosmos.TryGetValue(CollectionName, out CosmosModelInfo container))
|
|
{
|
|
{
|
|
|
|
+ string pk = container.type.Name;
|
|
dict.Remove("@CURRPAGE");
|
|
dict.Remove("@CURRPAGE");
|
|
dict.Remove("@PAGESIZE");
|
|
dict.Remove("@PAGESIZE");
|
|
dict.Remove("@ASC");
|
|
dict.Remove("@ASC");
|
|
dict.Remove("@DESC");
|
|
dict.Remove("@DESC");
|
|
StringBuilder sql = new StringBuilder("select value count(c) from c");
|
|
StringBuilder sql = new StringBuilder("select value count(c) from c");
|
|
- CosmosDbQuery cosmosDbQuery = SQLHelperParametric.GetSQL(dict, sql);
|
|
|
|
|
|
+ CosmosDbQuery cosmosDbQuery = SQLHelperParametric.GetSQL(dict, sql, pk);
|
|
QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(-1, null));
|
|
QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(-1, null));
|
|
FeedIterator<dynamic> query = container.container.GetItemQueryIterator<dynamic>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: queryRequestOptions);
|
|
FeedIterator<dynamic> query = container.container.GetItemQueryIterator<dynamic>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: queryRequestOptions);
|
|
return await ResultsFromFeedIterator(query);
|
|
return await ResultsFromFeedIterator(query);
|
|
@@ -559,16 +580,16 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public async Task<List<T>> FindByParams<T>(Dictionary<string, object> dict, string partitionKey = null, List<string> propertys = null) where T : ID
|
|
|
|
|
|
+ public async Task<List<T>> FindByParams<T>(Dictionary<string, object> dict, List<string> propertys = null) where T : ID
|
|
{
|
|
{
|
|
- return await FindByDict<T>(dict, partitionKey, propertys);
|
|
|
|
|
|
+ return await FindByDict<T>(dict, propertys);
|
|
}
|
|
}
|
|
- public async Task<List<T>> FindByDict<T>(Dictionary<string, object> dict, string partitionKey = null, List<string> propertys = null) where T : ID
|
|
|
|
|
|
+ public async Task<List<T>> FindByDict<T>(Dictionary<string, object> dict, List<string> propertys = null) where T : ID
|
|
{
|
|
{
|
|
StringBuilder sql;
|
|
StringBuilder sql;
|
|
sql = SQLHelperParametric.GetSQLSelect(propertys);
|
|
sql = SQLHelperParametric.GetSQLSelect(propertys);
|
|
-
|
|
|
|
- CosmosDbQuery cosmosDbQuery = SQLHelperParametric.GetSQL(dict, sql);
|
|
|
|
|
|
+ string pk = typeof(T).Name;
|
|
|
|
+ CosmosDbQuery cosmosDbQuery = SQLHelperParametric.GetSQL(dict, sql, pk);
|
|
QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(-1, null));
|
|
QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(-1, null));
|
|
return await ResultsFromQueryAndOptions<T>(cosmosDbQuery, queryRequestOptions);
|
|
return await ResultsFromQueryAndOptions<T>(cosmosDbQuery, queryRequestOptions);
|
|
}
|
|
}
|
|
@@ -623,6 +644,9 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
}
|
|
}
|
|
public async Task<List<T>> FindLinq<T>(Expression<Func<T, bool>> query = null, Expression<Func<T, object>> order = null, bool isDesc = false) where T : ID
|
|
public async Task<List<T>> FindLinq<T>(Expression<Func<T, bool>> query = null, Expression<Func<T, object>> order = null, bool isDesc = false) where T : ID
|
|
{
|
|
{
|
|
|
|
+
|
|
|
|
+ string pk = typeof(T).Name;
|
|
|
|
+ query = query.And(x => x.pk == pk);
|
|
//QueryRequestOptions queryRequestOptions = GetQueryRequestOptions(itemsPerPage);
|
|
//QueryRequestOptions queryRequestOptions = GetQueryRequestOptions(itemsPerPage);
|
|
QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(-1, null));
|
|
QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(-1, null));
|
|
FeedIterator<T> feedIterator;
|
|
FeedIterator<T> feedIterator;
|
|
@@ -684,33 +708,38 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
|
|
|
public async Task<List<T>> FindSQL<T>(string sql, Dictionary<string, object> Parameters = null) where T : ID
|
|
public async Task<List<T>> FindSQL<T>(string sql, Dictionary<string, object> Parameters = null) where T : ID
|
|
{
|
|
{
|
|
- CosmosModelInfo container = await InitializeCollection<T>();
|
|
|
|
- QueryRequestOptions queryOptions = GetQueryRequestOptions(GetEffectivePageSize(-1, null));
|
|
|
|
- if (Parameters != null)
|
|
|
|
|
|
+ if (sql.Contains(".pk"))
|
|
{
|
|
{
|
|
- CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
|
|
|
|
|
|
+ CosmosModelInfo container = await InitializeCollection<T>();
|
|
|
|
+ QueryRequestOptions queryOptions = GetQueryRequestOptions(GetEffectivePageSize(-1, null));
|
|
|
|
+ if (Parameters != null)
|
|
|
|
+ {
|
|
|
|
+ CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
|
|
|
|
+ {
|
|
|
|
+ QueryText = sql,
|
|
|
|
+ Parameters = Parameters
|
|
|
|
+ };
|
|
|
|
+ FeedIterator<T> feedIterator = container.container
|
|
|
|
+ .GetItemQueryIterator<T>(cosmosDbQuery.CosmosQueryDefinition, requestOptions: queryOptions);
|
|
|
|
+ return await ResultsFromFeedIterator(feedIterator);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
{
|
|
{
|
|
- QueryText = sql,
|
|
|
|
- Parameters = Parameters
|
|
|
|
- };
|
|
|
|
- FeedIterator<T> feedIterator = container.container
|
|
|
|
- .GetItemQueryIterator<T>(cosmosDbQuery.CosmosQueryDefinition, requestOptions: queryOptions);
|
|
|
|
- return await ResultsFromFeedIterator(feedIterator);
|
|
|
|
|
|
+ QueryDefinition queryDefinition = new QueryDefinition(sql);
|
|
|
|
+ return await ResultsFromFeedIterator<T>(container.container.GetItemQueryIterator<T>(queryDefinition));
|
|
|
|
+ }
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- QueryDefinition queryDefinition = new QueryDefinition(sql);
|
|
|
|
- return await ResultsFromFeedIterator<T>(container.container.GetItemQueryIterator<T>(queryDefinition));
|
|
|
|
|
|
+ throw new BizException("查询参数必须设置 .pk ", ResponseCode.PARAMS_ERROR);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
public async Task<T> Save<T>(T entity) where T : ID
|
|
public async Task<T> Save<T>(T entity) where T : ID
|
|
{
|
|
{
|
|
try
|
|
try
|
|
{
|
|
{
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
|
|
+ entity.pk = container.type.Name;
|
|
ItemResponse<T> response = await container.container.CreateItemAsync<T>(entity);
|
|
ItemResponse<T> response = await container.container.CreateItemAsync<T>(entity);
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
{
|
|
{
|
|
@@ -742,7 +771,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
{
|
|
{
|
|
flag = true;
|
|
flag = true;
|
|
}
|
|
}
|
|
- string pk = GetPartitionKey<T>();
|
|
|
|
|
|
+ string partitionKey = GetPartitionKey<T>();
|
|
Type type = typeof(T);
|
|
Type type = typeof(T);
|
|
Stopwatch stopwatch = Stopwatch.StartNew();
|
|
Stopwatch stopwatch = Stopwatch.StartNew();
|
|
for (int i = 0; i < pages; i++)
|
|
for (int i = 0; i < pages; i++)
|
|
@@ -751,9 +780,10 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
List<KeyValuePair<PartitionKey, Stream>> itemsToInsert = new List<KeyValuePair<PartitionKey, Stream>>();
|
|
List<KeyValuePair<PartitionKey, Stream>> itemsToInsert = new List<KeyValuePair<PartitionKey, Stream>>();
|
|
lists.ForEach(async x =>
|
|
lists.ForEach(async x =>
|
|
{
|
|
{
|
|
|
|
+ x.pk = type.Name;
|
|
MemoryStream stream = new MemoryStream();
|
|
MemoryStream stream = new MemoryStream();
|
|
await JsonSerializer.SerializeAsync(stream, x, new JsonSerializerOptions { IgnoreNullValues = true });
|
|
await JsonSerializer.SerializeAsync(stream, x, new JsonSerializerOptions { IgnoreNullValues = true });
|
|
- object o = type.GetProperty(pk).GetValue(x, null);
|
|
|
|
|
|
+ object o = type.GetProperty(partitionKey).GetValue(x, null);
|
|
KeyValuePair<PartitionKey, Stream> keyValue = new KeyValuePair<PartitionKey, Stream>(new PartitionKey(o.ToString()), stream);
|
|
KeyValuePair<PartitionKey, Stream> keyValue = new KeyValuePair<PartitionKey, Stream>(new PartitionKey(o.ToString()), stream);
|
|
itemsToInsert.Add(keyValue);
|
|
itemsToInsert.Add(keyValue);
|
|
});
|
|
});
|
|
@@ -790,6 +820,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
public async Task<T> SaveOrUpdate<T>(T entity) where T : ID
|
|
public async Task<T> SaveOrUpdate<T>(T entity) where T : ID
|
|
{
|
|
{
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
|
|
+ entity.pk = container.type.Name;
|
|
ItemResponse<T> response = await container.container.UpsertItemAsync(item: entity);
|
|
ItemResponse<T> response = await container.container.UpsertItemAsync(item: entity);
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
{
|
|
{
|
|
@@ -819,7 +850,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
{
|
|
{
|
|
flag = true;
|
|
flag = true;
|
|
}
|
|
}
|
|
- string pk = GetPartitionKey<T>();
|
|
|
|
|
|
+ string partitionKey = GetPartitionKey<T>();
|
|
Type type = typeof(T);
|
|
Type type = typeof(T);
|
|
Stopwatch stopwatch = Stopwatch.StartNew();
|
|
Stopwatch stopwatch = Stopwatch.StartNew();
|
|
for (int i = 0; i < pages; i++)
|
|
for (int i = 0; i < pages; i++)
|
|
@@ -828,9 +859,10 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
List<KeyValuePair<PartitionKey, Stream>> itemsToInsert = new List<KeyValuePair<PartitionKey, Stream>>();
|
|
List<KeyValuePair<PartitionKey, Stream>> itemsToInsert = new List<KeyValuePair<PartitionKey, Stream>>();
|
|
lists.ForEach(async x =>
|
|
lists.ForEach(async x =>
|
|
{
|
|
{
|
|
|
|
+ x.pk = type.Name;
|
|
MemoryStream stream = new MemoryStream();
|
|
MemoryStream stream = new MemoryStream();
|
|
await JsonSerializer.SerializeAsync(stream, x, new JsonSerializerOptions { IgnoreNullValues = true });
|
|
await JsonSerializer.SerializeAsync(stream, x, new JsonSerializerOptions { IgnoreNullValues = true });
|
|
- object o = type.GetProperty(pk).GetValue(x, null);
|
|
|
|
|
|
+ object o = type.GetProperty(partitionKey).GetValue(x, null);
|
|
KeyValuePair<PartitionKey, Stream> keyValue = new KeyValuePair<PartitionKey, Stream>(new PartitionKey(o.ToString()), stream);
|
|
KeyValuePair<PartitionKey, Stream> keyValue = new KeyValuePair<PartitionKey, Stream>(new PartitionKey(o.ToString()), stream);
|
|
itemsToInsert.Add(keyValue);
|
|
itemsToInsert.Add(keyValue);
|
|
});
|
|
});
|
|
@@ -864,12 +896,13 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
stopwatch.Stop();
|
|
stopwatch.Stop();
|
|
return enyites;
|
|
return enyites;
|
|
}
|
|
}
|
|
-
|
|
|
|
public async Task<T> Update<T>(T entity) where T : ID
|
|
public async Task<T> Update<T>(T entity) where T : ID
|
|
{
|
|
{
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
- string pk = GetPartitionKey<T>();
|
|
|
|
- object o = typeof(T).GetProperty(pk).GetValue(entity, null);
|
|
|
|
|
|
+ string partitionKey = GetPartitionKey<T>();
|
|
|
|
+ Type type = typeof(T);
|
|
|
|
+ entity.pk = type.Name;
|
|
|
|
+ object o = type.GetProperty(partitionKey).GetValue(entity, null);
|
|
ItemResponse<T> response = await container.container.ReplaceItemAsync(entity, entity.id, new PartitionKey(o.ToString()));
|
|
ItemResponse<T> response = await container.container.ReplaceItemAsync(entity, entity.id, new PartitionKey(o.ToString()));
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
{
|
|
{
|
|
@@ -885,15 +918,12 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
}
|
|
}
|
|
return response.Resource;
|
|
return response.Resource;
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
internal class Item
|
|
internal class Item
|
|
{
|
|
{
|
|
public string id { get; set; }
|
|
public string id { get; set; }
|
|
public string pk { get; set; }
|
|
public string pk { get; set; }
|
|
public MemoryStream stream { get; set; }
|
|
public MemoryStream stream { get; set; }
|
|
}
|
|
}
|
|
-
|
|
|
|
public async Task<List<T>> UpdateAll<T>(List<T> enyites) where T : ID
|
|
public async Task<List<T>> UpdateAll<T>(List<T> enyites) where T : ID
|
|
{
|
|
{
|
|
//await Task.Run(() => Parallel.ForEach(entities, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (item) =>
|
|
//await Task.Run(() => Parallel.ForEach(entities, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (item) =>
|
|
@@ -908,7 +938,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
{
|
|
{
|
|
flag = true;
|
|
flag = true;
|
|
}
|
|
}
|
|
- string pk = GetPartitionKey<T>();
|
|
|
|
|
|
+ string partitionKey = GetPartitionKey<T>();
|
|
Type type = typeof(T);
|
|
Type type = typeof(T);
|
|
Stopwatch stopwatch = Stopwatch.StartNew();
|
|
Stopwatch stopwatch = Stopwatch.StartNew();
|
|
for (int i = 0; i < pages; i++)
|
|
for (int i = 0; i < pages; i++)
|
|
@@ -917,9 +947,10 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
List<Item> itemsToInsert = new List<Item>();
|
|
List<Item> itemsToInsert = new List<Item>();
|
|
lists.ForEach(async x =>
|
|
lists.ForEach(async x =>
|
|
{
|
|
{
|
|
|
|
+ x.pk = type.Name;
|
|
MemoryStream stream = new MemoryStream();
|
|
MemoryStream stream = new MemoryStream();
|
|
await JsonSerializer.SerializeAsync(stream, x, new JsonSerializerOptions { IgnoreNullValues = true });
|
|
await JsonSerializer.SerializeAsync(stream, x, new JsonSerializerOptions { IgnoreNullValues = true });
|
|
- object o = type.GetProperty(pk).GetValue(x, null);
|
|
|
|
|
|
+ object o = type.GetProperty(partitionKey).GetValue(x, null);
|
|
Item keyValue = new Item { id = x.id, pk = o.ToString(), stream = stream };
|
|
Item keyValue = new Item { id = x.id, pk = o.ToString(), stream = stream };
|
|
itemsToInsert.Add(keyValue);
|
|
itemsToInsert.Add(keyValue);
|
|
});
|
|
});
|
|
@@ -953,26 +984,15 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
stopwatch.Stop();
|
|
stopwatch.Stop();
|
|
return enyites;
|
|
return enyites;
|
|
}
|
|
}
|
|
-
|
|
|
|
- //public void Dispose()
|
|
|
|
- //{
|
|
|
|
- // Dispose(true);
|
|
|
|
- //}
|
|
|
|
- //protected virtual void Dispose(bool disposing)
|
|
|
|
- //{
|
|
|
|
- // if (disposing)
|
|
|
|
- // {
|
|
|
|
- // CosmosClient?.Dispose();
|
|
|
|
- // }
|
|
|
|
- //}
|
|
|
|
- private async Task<T> FindByIdAsSql<T>(string id) where T : ID
|
|
|
|
|
|
+ private async Task<List<T>> FindByIdAsSql<T>(string id) where T : ID
|
|
{
|
|
{
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
|
|
+ string pk = container.type.Name;
|
|
CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
|
|
CosmosDbQuery cosmosDbQuery = new CosmosDbQuery
|
|
{
|
|
{
|
|
QueryText = @"SELECT *
|
|
QueryText = @"SELECT *
|
|
FROM c
|
|
FROM c
|
|
- WHERE c.id = @id",
|
|
|
|
|
|
+ WHERE c.pk='" + pk + "' and c.id = @id",
|
|
Parameters = new Dictionary<string, object>
|
|
Parameters = new Dictionary<string, object>
|
|
{
|
|
{
|
|
|
|
|
|
@@ -981,21 +1001,18 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
};
|
|
};
|
|
FeedIterator<T> feedIterator = container.container
|
|
FeedIterator<T> feedIterator = container.container
|
|
.GetItemQueryIterator<T>(cosmosDbQuery.CosmosQueryDefinition);
|
|
.GetItemQueryIterator<T>(cosmosDbQuery.CosmosQueryDefinition);
|
|
- return (await ResultsFromFeedIterator(feedIterator)).SingleOrDefault();
|
|
|
|
|
|
+ return (await ResultsFromFeedIterator(feedIterator)).ToList();
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
public async Task<T> FindByIdPk<T>(string id, string pk) where T : ID
|
|
public async Task<T> FindByIdPk<T>(string id, string pk) where T : ID
|
|
{
|
|
{
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
ItemResponse<T> response = await container.container.ReadItemAsync<T>(id: id, partitionKey: new PartitionKey(pk));
|
|
ItemResponse<T> response = await container.container.ReadItemAsync<T>(id: id, partitionKey: new PartitionKey(pk));
|
|
return response.Resource;
|
|
return response.Resource;
|
|
}
|
|
}
|
|
-
|
|
|
|
- public async Task<T> FindById<T>(string id, bool cache = true) where T : ID
|
|
|
|
|
|
+ public async Task<List<T>> FindById<T>(string id, bool cache = true) where T : ID
|
|
{
|
|
{
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
- if (container.cache && RedisHelper.Instance != null && cache==true)
|
|
|
|
|
|
+ if (container.cache && RedisHelper.Instance != null && cache == true)
|
|
{
|
|
{
|
|
|
|
|
|
return await RedisHelper.CacheShellAsync(CacheCosmosPrefix + container.container.Id, id, timeoutSeconds, () => { return FindByIdAsSql<T>(id); });
|
|
return await RedisHelper.CacheShellAsync(CacheCosmosPrefix + container.container.Id, id, timeoutSeconds, () => { return FindByIdAsSql<T>(id); });
|
|
@@ -1007,7 +1024,6 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
-
|
|
|
|
public async Task<List<T>> FindByIds<T>(List<string> ids) where T : ID
|
|
public async Task<List<T>> FindByIds<T>(List<string> ids) where T : ID
|
|
{
|
|
{
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
@@ -1040,46 +1056,44 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
return await FindByDict<T>(new Dictionary<string, object> { { "id", ids.ToArray() } });
|
|
return await FindByDict<T>(new Dictionary<string, object> { { "id", ids.ToArray() } });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- public async Task<dynamic> FindById(string CollectionName, string id)
|
|
|
|
|
|
+ public async Task<dynamic> FindById(string typeName, string id)
|
|
{
|
|
{
|
|
- if (DocumentCollectionDict.TryGetValue(CollectionName, out CosmosModelInfo container))
|
|
|
|
|
|
+ if (CosmosDict.typeCosmos.TryGetValue(typeName, out CosmosModelInfo container))
|
|
{
|
|
{
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
{
|
|
{
|
|
|
|
|
|
- return await RedisHelper.CacheShellAsync(CacheCosmosPrefix + container.container.Id, id, timeoutSeconds, () => { return FindByDict(CollectionName, new Dictionary<string, object> { { "id", id } }); });
|
|
|
|
|
|
+ return await RedisHelper.CacheShellAsync(CacheCosmosPrefix + container.container.Id, id, timeoutSeconds, () => { return FindByDict(typeName, new Dictionary<string, object> { { "id", id } }); });
|
|
|
|
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- return await FindByDict(CollectionName, new Dictionary<string, object> { { "id", id } });
|
|
|
|
|
|
+ return await FindByDict(typeName, new Dictionary<string, object> { { "id", id } });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- throw new BizException("CollectionName named:" + CollectionName + " dose not exsit in Database!");
|
|
|
|
|
|
+ throw new BizException("CollectionName named:" + typeName + " dose not exsit in Database!");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- public async Task<List<dynamic>> FindByIds(string CollectionName, List<string> ids)
|
|
|
|
|
|
+ public async Task<List<dynamic>> FindByIds(string typeName, List<string> ids)
|
|
{
|
|
{
|
|
- if (DocumentCollectionDict.TryGetValue(CollectionName, out CosmosModelInfo container))
|
|
|
|
|
|
+ if (CosmosDict. typeCosmos.TryGetValue(typeName, out CosmosModelInfo container))
|
|
{
|
|
{
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
{
|
|
{
|
|
|
|
|
|
- return await RedisHelper.CacheShellAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds, () => { return FindByDict(CollectionName, new Dictionary<string, object> { { "id", ids.ToArray() } }); });
|
|
|
|
|
|
+ return await RedisHelper.CacheShellAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds, () => { return FindByDict(typeName, new Dictionary<string, object> { { "id", ids.ToArray() } }); });
|
|
|
|
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- return await FindByDict(CollectionName, new Dictionary<string, object> { { "id", ids.ToArray() } });
|
|
|
|
|
|
+ return await FindByDict(typeName, new Dictionary<string, object> { { "id", ids.ToArray() } });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- throw new BizException("CollectionName named:" + CollectionName + " dose not exsit in Database!");
|
|
|
|
|
|
+ throw new BizException("CollectionName named:" + typeName + " dose not exsit in Database!");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|