|
@@ -3,11 +3,14 @@ using Microsoft.Azure.Cosmos.Linq;
|
|
|
using System;
|
|
|
using System.Collections.Concurrent;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.Diagnostics;
|
|
|
+using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Linq.Expressions;
|
|
|
using System.Net;
|
|
|
using System.Reflection;
|
|
|
using System.Text;
|
|
|
+using System.Text.Json;
|
|
|
using System.Threading.Tasks;
|
|
|
using TEAMModelOS.SDK.Context.Attributes.Azure;
|
|
|
using TEAMModelOS.SDK.Context.Exception;
|
|
@@ -233,22 +236,79 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
|
|
|
public async Task DeleteAll<T>(List<KeyValuePair<string, string>> ids) where T : ID
|
|
|
{
|
|
|
- string partitionKey = GetPartitionKey<T>();
|
|
|
- await Task.Run(() => Parallel.ForEach(ids, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (item) =>
|
|
|
- {
|
|
|
- Task.WaitAll(DeleteAsync<T>(item.Value, item.Key));
|
|
|
- }));
|
|
|
+ Container container = await InitializeCollection<T>();
|
|
|
+ //string partitionKey = GetPartitionKey<T>();
|
|
|
+ //await Task.Run(() => Parallel.ForEach(ids, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (item) =>
|
|
|
+ //{
|
|
|
+ // Task.WaitAll(DeleteAsync<T>(item.Value, item.Key));
|
|
|
+ //}));
|
|
|
+
|
|
|
+
|
|
|
+ Stopwatch stopwatch = Stopwatch.StartNew();
|
|
|
+ List<Task> tasks = new List<Task>(ids.Count);
|
|
|
+ ids.ForEach(item => {
|
|
|
+ tasks.Add(container.DeleteItemStreamAsync(item.Value,new PartitionKey(item.Key))
|
|
|
+ .ContinueWith((Task<ResponseMessage> task) =>
|
|
|
+ {
|
|
|
+ //using (ResponseMessage response = task.Result)
|
|
|
+ //{
|
|
|
+ // if (!response.IsSuccessStatusCode)
|
|
|
+ // {
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+ }
|
|
|
+ ));
|
|
|
+ });
|
|
|
+ //await Task.Run(() => Parallel.ForEach(enyites, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (item) =>
|
|
|
+ //{
|
|
|
+ // Task.WaitAll(Save(item));
|
|
|
+ //}));
|
|
|
+ await Task.WhenAll(tasks);
|
|
|
+ stopwatch.Stop();
|
|
|
}
|
|
|
|
|
|
public async Task DeleteAll<T>(List<T> entities) where T : ID
|
|
|
{
|
|
|
- string partitionKey = GetPartitionKey<T>();
|
|
|
+ //string partitionKey = GetPartitionKey<T>();
|
|
|
+ //Type type = typeof(T);
|
|
|
+ //await Task.Run(() => Parallel.ForEach(entities, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (item) =>
|
|
|
+ //{
|
|
|
+ // object o = type.GetProperty(partitionKey).GetValue(item, null);
|
|
|
+ // Task.WaitAll(DeleteAsync<T>(item.id, o.ToString()));
|
|
|
+ //}));
|
|
|
+
|
|
|
+ Container container = await InitializeCollection<T>();
|
|
|
+ string pk = GetPartitionKey<T>();
|
|
|
Type type = typeof(T);
|
|
|
- await Task.Run(() => Parallel.ForEach(entities, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (item) =>
|
|
|
- {
|
|
|
- object o = type.GetProperty(partitionKey).GetValue(item, null);
|
|
|
- Task.WaitAll(DeleteAsync<T>(item.id, o.ToString()));
|
|
|
- }));
|
|
|
+ List<KeyValuePair<PartitionKey, string>> itemsToInsert = new List<KeyValuePair<PartitionKey, string>>();
|
|
|
+ entities.ForEach( x => {
|
|
|
+ //MemoryStream stream = new MemoryStream();
|
|
|
+ //await JsonSerializer.SerializeAsync(stream, x);
|
|
|
+ object o = type.GetProperty(pk).GetValue(x, null);
|
|
|
+ KeyValuePair<PartitionKey, string> keyValue = new KeyValuePair<PartitionKey, string>(new PartitionKey(o.ToString()), x.id);
|
|
|
+ itemsToInsert.Add(keyValue);
|
|
|
+ });
|
|
|
+ Stopwatch stopwatch = Stopwatch.StartNew();
|
|
|
+ List<Task> tasks = new List<Task>(entities.Count);
|
|
|
+ itemsToInsert.ForEach(item => {
|
|
|
+ tasks.Add(container.DeleteItemStreamAsync(item.Value, item.Key)
|
|
|
+ .ContinueWith((Task<ResponseMessage> task) =>
|
|
|
+ {
|
|
|
+ //using (ResponseMessage response = task.Result)
|
|
|
+ //{
|
|
|
+ // if (!response.IsSuccessStatusCode)
|
|
|
+ // {
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+ }
|
|
|
+ ));
|
|
|
+ });
|
|
|
+ //await Task.Run(() => Parallel.ForEach(enyites, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (item) =>
|
|
|
+ //{
|
|
|
+ // Task.WaitAll(Save(item));
|
|
|
+ //}));
|
|
|
+ await Task.WhenAll(tasks);
|
|
|
+ stopwatch.Stop();
|
|
|
}
|
|
|
public async Task<T> DeleteAsync<T>(string id, string pk) where T : ID
|
|
|
{
|
|
@@ -555,11 +615,38 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
|
|
|
public async Task<List<T>> SaveAll<T>(List<T> enyites) where T : ID
|
|
|
{
|
|
|
-
|
|
|
- await Task.Run(() => Parallel.ForEach(enyites, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (item) =>
|
|
|
- {
|
|
|
- Task.WaitAll(Save(item));
|
|
|
- }));
|
|
|
+ Container container = await InitializeCollection<T>();
|
|
|
+ string pk= GetPartitionKey<T>();
|
|
|
+ Type type = typeof(T);
|
|
|
+ List<KeyValuePair<PartitionKey, Stream>> itemsToInsert = new List<KeyValuePair<PartitionKey, Stream>>();
|
|
|
+ enyites.ForEach(async x => {
|
|
|
+ MemoryStream stream = new MemoryStream();
|
|
|
+ await JsonSerializer.SerializeAsync(stream, x);
|
|
|
+ object o = type.GetProperty(pk).GetValue(x, null);
|
|
|
+ KeyValuePair<PartitionKey, Stream> keyValue = new KeyValuePair<PartitionKey, Stream>(new PartitionKey(o.ToString()), stream);
|
|
|
+ itemsToInsert.Add(keyValue);
|
|
|
+ });
|
|
|
+ int count=0;
|
|
|
+ Stopwatch stopwatch = Stopwatch.StartNew();
|
|
|
+ List<Task> tasks = new List<Task>(enyites.Count);
|
|
|
+ itemsToInsert.ForEach(item => {
|
|
|
+ tasks.Add(container.CreateItemStreamAsync(item.Value, item.Key)
|
|
|
+ .ContinueWith((Task<ResponseMessage> task) =>
|
|
|
+ {
|
|
|
+ using (ResponseMessage response = task.Result)
|
|
|
+ {
|
|
|
+ if (!response.IsSuccessStatusCode)
|
|
|
+ {
|
|
|
+ count++;
|
|
|
+ Console.WriteLine(response.RequestMessage);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ));
|
|
|
+ });
|
|
|
+ await Task.WhenAll(tasks);
|
|
|
+ stopwatch.Stop();
|
|
|
+ Console.WriteLine(count);
|
|
|
return enyites;
|
|
|
}
|
|
|
|
|
@@ -572,10 +659,43 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
|
|
|
|
|
|
public async Task<List<T>> UpdateAll<T>(List<T> entities) where T : ID
|
|
|
{
|
|
|
- await Task.Run(() => Parallel.ForEach(entities, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (item) =>
|
|
|
- {
|
|
|
- Task.WaitAll(Update(item));
|
|
|
- }));
|
|
|
+ //await Task.Run(() => Parallel.ForEach(entities, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (item) =>
|
|
|
+ //{
|
|
|
+ // Task.WaitAll(Update(item));
|
|
|
+ //}));
|
|
|
+
|
|
|
+ Container container = await InitializeCollection<T>();
|
|
|
+ string pk = GetPartitionKey<T>();
|
|
|
+ Type type = typeof(T);
|
|
|
+ List<KeyValuePair<PartitionKey, Stream>> itemsToInsert = new List<KeyValuePair<PartitionKey, Stream>>();
|
|
|
+ entities.ForEach(async x => {
|
|
|
+ MemoryStream stream = new MemoryStream();
|
|
|
+ await JsonSerializer.SerializeAsync(stream, x);
|
|
|
+ object o = type.GetProperty(pk).GetValue(x, null);
|
|
|
+ KeyValuePair<PartitionKey, Stream> keyValue = new KeyValuePair<PartitionKey, Stream>(new PartitionKey(o.ToString()), stream);
|
|
|
+ itemsToInsert.Add(keyValue);
|
|
|
+ });
|
|
|
+ Stopwatch stopwatch = Stopwatch.StartNew();
|
|
|
+ List<Task> tasks = new List<Task>(entities.Count);
|
|
|
+ itemsToInsert.ForEach(item => {
|
|
|
+ tasks.Add(container.UpsertItemStreamAsync(item.Value, item.Key)
|
|
|
+ .ContinueWith((Task<ResponseMessage> task) =>
|
|
|
+ {
|
|
|
+ //using (ResponseMessage response = task.Result)
|
|
|
+ //{
|
|
|
+ // if (!response.IsSuccessStatusCode)
|
|
|
+ // {
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+ }
|
|
|
+ ));
|
|
|
+ });
|
|
|
+ //await Task.Run(() => Parallel.ForEach(enyites, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (item) =>
|
|
|
+ //{
|
|
|
+ // Task.WaitAll(Save(item));
|
|
|
+ //}));
|
|
|
+ await Task.WhenAll(tasks);
|
|
|
+ stopwatch.Stop();
|
|
|
return entities;
|
|
|
}
|
|
|
|