|
@@ -120,7 +120,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
{
|
|
|
foreach (ContainerProperties container in await resultSetIterator.ReadNextAsync())
|
|
|
{
|
|
|
- CosmosDict.nameCosmos.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), partitionKey= container.PartitionKeyPath.Replace("/",""), cache = false, monitor = false });
|
|
|
}
|
|
|
}
|
|
|
bool isMonitor = false;
|
|
@@ -172,7 +172,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
{
|
|
|
await CosmosClient.GetDatabase(DatabaseId).GetContainer(cosmosModelInfo.container.Id).ReplaceThroughputAsync(RU);
|
|
|
}
|
|
|
- CosmosModelInfo cosmos = new CosmosModelInfo { container = container, cache = cache, monitor = monitor, type = type };
|
|
|
+ CosmosModelInfo cosmos = new CosmosModelInfo { container = container, cache = cache, monitor = monitor, type = type,partitionKey=PartitionKey };
|
|
|
CosmosDict.nameCosmos[CollectionName] = cosmos;
|
|
|
CosmosDict.typeCosmos.Add(type.Name, cosmos);
|
|
|
}
|
|
@@ -187,7 +187,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
CollectionThroughput = RU;
|
|
|
}
|
|
|
Container containerWithConsistentIndexing = await database.CreateContainerIfNotExistsAsync(containerProperties, throughput: CollectionThroughput);
|
|
|
- CosmosModelInfo cosmos = new CosmosModelInfo { container = containerWithConsistentIndexing, cache = cache, monitor = monitor, type = type };
|
|
|
+ CosmosModelInfo cosmos = new CosmosModelInfo { container = containerWithConsistentIndexing, cache = cache, monitor = monitor, type = type, partitionKey = PartitionKey };
|
|
|
CosmosDict.nameCosmos[CollectionName] = cosmos;
|
|
|
CosmosDict.typeCosmos.Add(type.Name, cosmos);
|
|
|
}
|
|
@@ -196,7 +196,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
{
|
|
|
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 });
|
|
|
+ CosmosDict.nameCosmos.TryAdd(leaseId, new CosmosModelInfo { container = leaseContainer, cache = false, monitor = false, partitionKey = "/id" });
|
|
|
}
|
|
|
return CosmosDict;
|
|
|
}
|
|
@@ -970,6 +970,63 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
stopwatch.Stop();
|
|
|
return enyites;
|
|
|
}
|
|
|
+ public async Task<List<dynamic>> UpdateAll (string typeName,List<dynamic> enyites)
|
|
|
+ {
|
|
|
+ int pages = (int)Math.Ceiling((double)enyites.Count / pageSize);
|
|
|
+ if (CosmosDict.typeCosmos.TryGetValue(typeName, out CosmosModelInfo container)) {
|
|
|
+ }
|
|
|
+ bool flag = false;
|
|
|
+ if (RedisHelper.Exists(CacheCosmosPrefix + container.container.Id))
|
|
|
+ {
|
|
|
+ flag = true;
|
|
|
+ }
|
|
|
+ string partitionKey = container.partitionKey;
|
|
|
+
|
|
|
+ Stopwatch stopwatch = Stopwatch.StartNew();
|
|
|
+ for (int i = 0; i < pages; i++)
|
|
|
+ {
|
|
|
+ List<dynamic> lists = enyites.Skip((i) * pageSize).Take(pageSize).ToList();
|
|
|
+ List<Item> itemsToInsert = new List<Item>();
|
|
|
+ lists.ForEach(async x =>
|
|
|
+ {
|
|
|
+ x.pk = typeName;
|
|
|
+ x.ttl = null;
|
|
|
+ MemoryStream stream = new MemoryStream();
|
|
|
+ await JsonSerializer.SerializeAsync(stream, x, new JsonSerializerOptions { IgnoreNullValues = true });
|
|
|
+ object o = x[partitionKey];
|
|
|
+ Item keyValue = new Item { id = x.id, pk = o.ToString(), stream = stream };
|
|
|
+ itemsToInsert.Add(keyValue);
|
|
|
+ });
|
|
|
+ List<Task> tasks = new List<Task>(lists.Count);
|
|
|
+ itemsToInsert.ForEach(item =>
|
|
|
+ {
|
|
|
+ tasks.Add(container.container.ReplaceItemStreamAsync(item.stream, item.id, new PartitionKey(item.pk))
|
|
|
+ .ContinueWith((Task<ResponseMessage> task) =>
|
|
|
+ {
|
|
|
+ //using (ResponseMessage response = task.Result)
|
|
|
+ //{
|
|
|
+ // if (!response.IsSuccessStatusCode)
|
|
|
+ // {
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+ }
|
|
|
+ ));
|
|
|
+ });
|
|
|
+ await Task.WhenAll(tasks);
|
|
|
+ if (container.cache && RedisHelper.Instance != null)
|
|
|
+ {
|
|
|
+ lists.ForEach(async x => {
|
|
|
+ await RedisHelper.HSetAsync(CacheCosmosPrefix + container.container.Id, x.id, x);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (container.cache && RedisHelper.Instance != null && !flag)
|
|
|
+ {
|
|
|
+ await RedisHelper.ExpireAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds);
|
|
|
+ }
|
|
|
+ stopwatch.Stop();
|
|
|
+ return enyites;
|
|
|
+ }
|
|
|
public async Task<T> Update<T>(T entity) where T : ID
|
|
|
{
|
|
|
CosmosModelInfo container = await InitializeCollection<T>();
|