|
@@ -21,8 +21,9 @@ using TEAMModelOS.SDK.Module.AzureCosmosDB.Configuration;
|
|
|
|
|
|
namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
{
|
|
|
- public class CosmosModelInfo {
|
|
|
- public Container container{ get; set; }
|
|
|
+ public class CosmosModelInfo
|
|
|
+ {
|
|
|
+ public Container container { get; set; }
|
|
|
public bool cache { get; set; }
|
|
|
public bool monitor { get; set; } = false;
|
|
|
public Type type { get; set; }
|
|
@@ -45,7 +46,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
private string[] ScanModel { get; set; }
|
|
|
private const int timeoutSeconds = 86400;
|
|
|
|
|
|
- private string leaseId = "leaseContainer";
|
|
|
+ private string leaseId = "AleaseContainer";
|
|
|
public AzureCosmosDBV3Repository(AzureCosmosDBOptions options, CosmosSerializer cosmosSerializer)
|
|
|
{
|
|
|
try
|
|
@@ -105,7 +106,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
{
|
|
|
foreach (ContainerProperties container in await resultSetIterator.ReadNextAsync())
|
|
|
{
|
|
|
- DocumentCollectionDict.TryAdd(container.Id, new CosmosModelInfo { container = database.GetContainer(container.Id), cache = false,monitor=false });
|
|
|
+ DocumentCollectionDict.TryAdd(container.Id, new CosmosModelInfo { container = database.GetContainer(container.Id), cache = false, monitor = false });
|
|
|
}
|
|
|
}
|
|
|
bool isMonitor = false;
|
|
@@ -127,14 +128,15 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
{
|
|
|
CollectionName = type.Name;
|
|
|
}
|
|
|
- if ( attributes.First<CosmosDBAttribute>().Cache)
|
|
|
+ if (attributes.First<CosmosDBAttribute>().Cache)
|
|
|
{
|
|
|
cache = attributes.First<CosmosDBAttribute>().Cache;
|
|
|
}
|
|
|
if (attributes.First<CosmosDBAttribute>().Monitor)
|
|
|
{
|
|
|
monitor = attributes.First<CosmosDBAttribute>().Monitor;
|
|
|
- if (monitor) {
|
|
|
+ if (monitor)
|
|
|
+ {
|
|
|
isMonitor = true;
|
|
|
}
|
|
|
}
|
|
@@ -155,13 +157,13 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
{ //更新RU
|
|
|
|
|
|
cosmosModelInfo.cache = cache;
|
|
|
- Container container = CosmosClient.GetDatabase(DatabaseId).GetContainer(cosmosModelInfo.container.Id);
|
|
|
+ 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 } ;
|
|
|
+ DocumentCollectionDict[CollectionName] = new CosmosModelInfo { container = container, cache = cache, monitor = monitor, type = type };
|
|
|
}
|
|
|
else
|
|
|
{
|
|
@@ -176,10 +178,11 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
CollectionThroughput = RU;
|
|
|
}
|
|
|
Container containerWithConsistentIndexing = await database.CreateContainerIfNotExistsAsync(containerProperties, throughput: CollectionThroughput);
|
|
|
- DocumentCollectionDict.TryAdd(CollectionName, new CosmosModelInfo { container= containerWithConsistentIndexing ,cache=cache , monitor = monitor ,type=type });
|
|
|
+ DocumentCollectionDict.TryAdd(CollectionName, new CosmosModelInfo { container = containerWithConsistentIndexing, cache = cache, monitor = monitor, type = type });
|
|
|
}
|
|
|
}
|
|
|
- if (isMonitor) {
|
|
|
+ if (isMonitor)
|
|
|
+ {
|
|
|
ContainerProperties leaseProperties = new ContainerProperties { Id = leaseId, PartitionKeyPath = "/id" };
|
|
|
Container leaseContainer = await database.CreateContainerIfNotExistsAsync(leaseProperties, throughput: CollectionThroughput);
|
|
|
DocumentCollectionDict.TryAdd(leaseId, new CosmosModelInfo { container = leaseContainer, cache = false, monitor = false });
|
|
@@ -191,10 +194,11 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public Dictionary<string, CosmosModelInfo> GetCosmosModelInfo() {
|
|
|
+ public Dictionary<string, CosmosModelInfo> GetCosmosModelInfo()
|
|
|
+ {
|
|
|
return this.DocumentCollectionDict;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
private string GetPartitionKey<T>()
|
|
@@ -297,8 +301,8 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
tasks.Add(container.container.DeleteItemStreamAsync(item.id, new PartitionKey(item.pk))
|
|
|
.ContinueWith((Task<ResponseMessage> task) =>
|
|
|
{
|
|
|
- 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)
|
|
|
// {
|
|
@@ -307,21 +311,21 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
}
|
|
|
));
|
|
|
});
|
|
|
- await Task.WhenAll(tasks);
|
|
|
+ await Task.WhenAll(tasks);
|
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
|
{
|
|
|
lists.ForEach(async x => {
|
|
|
- await RedisHelper.HDelAsync(CacheCosmosPrefix + container.container.Id,x.id );
|
|
|
+ await RedisHelper.HDelAsync(CacheCosmosPrefix + container.container.Id, x.id);
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
stopwatch.Stop();
|
|
|
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
|
|
|
{
|
|
|
- List<T> list= await FindByDict<T>(dict);
|
|
|
- return await DeleteAll(list);
|
|
|
+ List<T> list = await FindByDict<T>(dict);
|
|
|
+ return await DeleteAll(list);
|
|
|
}
|
|
|
public async Task<List<IdPk>> DeleteAll<T>(List<T> enyites) where T : ID
|
|
|
{
|
|
@@ -348,14 +352,14 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
{
|
|
|
tasks.Add(container.container.DeleteItemStreamAsync(item.Value, item.Key)
|
|
|
.ContinueWith((Task<ResponseMessage> task) =>
|
|
|
+ {
|
|
|
+ using (ResponseMessage response = task.Result)
|
|
|
{
|
|
|
- using (ResponseMessage response = task.Result)
|
|
|
- {
|
|
|
-
|
|
|
- idPks.Add(new IdPk { id = item.Value, pk = item.Key.ToString(), StatusCode = response.StatusCode });
|
|
|
-
|
|
|
- }
|
|
|
+
|
|
|
+ idPks.Add(new IdPk { id = item.Value, pk = item.Key.ToString(), StatusCode = response.StatusCode });
|
|
|
+
|
|
|
}
|
|
|
+ }
|
|
|
));
|
|
|
});
|
|
|
await Task.WhenAll(tasks); if (container.cache && RedisHelper.Instance != null)
|
|
@@ -369,18 +373,19 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
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);
|
|
|
}
|
|
|
public async Task<IdPk> DeleteAsync<T>(string id, string pk) where T : ID
|
|
|
{
|
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
|
- 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)
|
|
|
{
|
|
|
await RedisHelper.HDelAsync(CacheCosmosPrefix + container.container.Id, id);
|
|
|
}
|
|
|
- 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
|
|
@@ -389,7 +394,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
string partitionKey = GetPartitionKey<T>();
|
|
|
Type type = typeof(T);
|
|
|
object o = type.GetProperty(partitionKey).GetValue(entity, null);
|
|
|
- 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)
|
|
|
{
|
|
|
await RedisHelper.HDelAsync(CacheCosmosPrefix + container.container.Id, entity.id);
|
|
@@ -409,7 +414,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
|
StringBuilder sql;
|
|
|
sql = SQLHelperParametric.GetSQLSelect(propertys);
|
|
|
- CosmosDbQuery cosmosDbQuery = new CosmosDbQuery {QueryText = sql.ToString() };
|
|
|
+ CosmosDbQuery cosmosDbQuery = new CosmosDbQuery { QueryText = sql.ToString() };
|
|
|
FeedIterator<T> query = container.container.GetItemQueryIterator<T>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition);
|
|
|
return await ResultsFromFeedIterator(query);
|
|
|
|
|
@@ -466,7 +471,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
StringBuilder sql;
|
|
|
sql = SQLHelperParametric.GetSQLSelect(propertys);
|
|
|
CosmosDbQuery cosmosDbQuery = SQLHelperParametric.GetSQL(dict, sql);
|
|
|
- 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);
|
|
|
return await ResultsFromFeedIterator(query);
|
|
|
}
|
|
@@ -487,7 +492,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
dict.Remove("@DESC");
|
|
|
StringBuilder sql = new StringBuilder("select value count(c) from c");
|
|
|
CosmosDbQuery cosmosDbQuery = SQLHelperParametric.GetSQL(dict, sql);
|
|
|
- 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);
|
|
|
return await ResultsFromFeedIterator(query);
|
|
|
}
|
|
@@ -499,9 +504,9 @@ 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
|
|
|
{
|
|
|
- return await FindByDict<T>(dict,partitionKey, propertys);
|
|
|
+ return await FindByDict<T>(dict, partitionKey, 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, string partitionKey = null, List<string> propertys = null) where T : ID
|
|
|
{
|
|
|
StringBuilder sql;
|
|
|
sql = SQLHelperParametric.GetSQLSelect(propertys);
|
|
@@ -510,7 +515,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(-1, null));
|
|
|
return await ResultsFromQueryAndOptions<T>(cosmosDbQuery, queryRequestOptions);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
|
|
|
private async Task<List<T>> ResultsFromQueryAndOptions<T>(CosmosDbQuery cosmosDbQuery, QueryRequestOptions queryOptions)
|
|
|
{
|
|
@@ -562,7 +567,7 @@ 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
|
|
|
{
|
|
|
//QueryRequestOptions queryRequestOptions = GetQueryRequestOptions(itemsPerPage);
|
|
|
- QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(-1,null));
|
|
|
+ QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(-1, null));
|
|
|
FeedIterator<T> feedIterator;
|
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
|
|
|
@@ -650,16 +655,18 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
{
|
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|
|
|
ItemResponse<T> response = await container.container.CreateItemAsync<T>(entity);
|
|
|
- if (container.cache && RedisHelper.Instance!=null) {
|
|
|
+ if (container.cache && RedisHelper.Instance != null)
|
|
|
+ {
|
|
|
if (!RedisHelper.Exists(CacheCosmosPrefix + container.container.Id))
|
|
|
{
|
|
|
await RedisHelper.HSetAsync(CacheCosmosPrefix + container.container.Id, entity.id, entity);
|
|
|
}
|
|
|
- else {
|
|
|
+ else
|
|
|
+ {
|
|
|
await RedisHelper.HSetAsync(CacheCosmosPrefix + container.container.Id, entity.id, entity);
|
|
|
await RedisHelper.ExpireAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
return response.Resource;
|
|
|
}
|
|
@@ -688,7 +695,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
lists.ForEach(async x =>
|
|
|
{
|
|
|
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);
|
|
|
KeyValuePair<PartitionKey, Stream> keyValue = new KeyValuePair<PartitionKey, Stream>(new PartitionKey(o.ToString()), stream);
|
|
|
itemsToInsert.Add(keyValue);
|
|
@@ -712,11 +719,12 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
|
{
|
|
|
lists.ForEach(async x => {
|
|
|
- await RedisHelper.HSetAsync(CacheCosmosPrefix+container.container.Id, x.id, x);
|
|
|
+ await RedisHelper.HSetAsync(CacheCosmosPrefix + container.container.Id, x.id, x);
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
- if (container.cache && RedisHelper.Instance != null&&!flag) {
|
|
|
+ if (container.cache && RedisHelper.Instance != null && !flag)
|
|
|
+ {
|
|
|
await RedisHelper.ExpireAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds);
|
|
|
}
|
|
|
stopwatch.Stop();
|
|
@@ -792,7 +800,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
- if (container.cache && RedisHelper.Instance != null&&!flag)
|
|
|
+ if (container.cache && RedisHelper.Instance != null && !flag)
|
|
|
{
|
|
|
await RedisHelper.ExpireAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds);
|
|
|
}
|
|
@@ -881,7 +889,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
- if (container.cache && RedisHelper.Instance != null&&!flag)
|
|
|
+ if (container.cache && RedisHelper.Instance != null && !flag)
|
|
|
{
|
|
|
await RedisHelper.ExpireAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds);
|
|
|
}
|
|
@@ -933,33 +941,37 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
|
{
|
|
|
|
|
|
- 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); });
|
|
|
+
|
|
|
}
|
|
|
- else {
|
|
|
+ else
|
|
|
+ {
|
|
|
return await FindByIdAsSql<T>(id);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- 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>();
|
|
|
-
|
|
|
+
|
|
|
if (container.cache && RedisHelper.Instance != null)
|
|
|
{
|
|
|
List<T> list = new List<T>();
|
|
|
List<string> NotIn = new List<string>();
|
|
|
- foreach (string id in ids) {
|
|
|
+ foreach (string id in ids)
|
|
|
+ {
|
|
|
if (!await RedisHelper.HExistsAsync(CacheCosmosPrefix + container.container.Id, id))
|
|
|
{
|
|
|
NotIn.Add(id);
|
|
|
}
|
|
|
- else {
|
|
|
+ else
|
|
|
+ {
|
|
|
list.Add(await RedisHelper.HGetAsync<T>(CacheCosmosPrefix + container.container.Id, id));
|
|
|
}
|
|
|
}
|
|
|
- if (NotIn.IsNotEmpty()) {
|
|
|
+ if (NotIn.IsNotEmpty())
|
|
|
+ {
|
|
|
List<T> noInList = await FindByDict<T>(new Dictionary<string, object> { { "id", NotIn.ToArray() } });
|
|
|
noInList.ForEach(x => { RedisHelper.HSet(CacheCosmosPrefix + container.container.Id, x.id, x); RedisHelper.Expire(CacheCosmosPrefix + container.container.Id, timeoutSeconds); });
|
|
|
list.AddRange(noInList);
|
|
@@ -979,7 +991,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
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(CollectionName, new Dictionary<string, object> { { "id", id } }); });
|
|
|
|
|
|
}
|
|
|
else
|