本文整理汇总了C#中ConcurrentDictionary.ToDictionary方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentDictionary.ToDictionary方法的具体用法?C# ConcurrentDictionary.ToDictionary怎么用?C# ConcurrentDictionary.ToDictionary使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentDictionary
的用法示例。
在下文中一共展示了ConcurrentDictionary.ToDictionary方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: createPersonsLots
public Tuple<Dictionary<int, int>, Dictionary<string, int>, Dictionary<int, JObject>> createPersonsLots(Dictionary<string, Dictionary<string, NomValue>> noms)
{
Stopwatch timer = new Stopwatch();
timer.Start();
CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken ct = cts.Token;
ConcurrentQueue<int> personIds = new ConcurrentQueue<int>(this.getPersonIds());
ConcurrentDictionary<int, int> personIdToLotId = new ConcurrentDictionary<int, int>();
ConcurrentDictionary<string, int> personEgnToLotId = new ConcurrentDictionary<string, int>();
ConcurrentDictionary<int, JObject> personLotIdToPersonNom = new ConcurrentDictionary<int, JObject>();
Utils.RunParallel("ParallelMigrations", ct,
() => this.personLotCreatorFactory().Value,
(personLotCreator) =>
{
using (personLotCreator)
{
personLotCreator.StartCreating(noms, personIds, personIdToLotId, personEgnToLotId, personLotIdToPersonNom, cts, ct);
}
})
.Wait();
timer.Stop();
Console.WriteLine("Person lot creation time - {0}", timer.Elapsed.TotalMinutes);
return Tuple.Create(
personIdToLotId.ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
personEgnToLotId.ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
personLotIdToPersonNom.ToDictionary(kvp => kvp.Key, kvp => kvp.Value));
}
示例2: GetCompletedTasks
/// <summary>
/// Retrieve a dictionary of users and their amount of completed tasks in the given stage
/// </summary>
/// <param name="stage"></param>
/// <returns></returns>
private Dictionary<int, int> GetCompletedTasks(Stage stage)
{
var completedTasks = new ConcurrentDictionary<int, int>();
foreach (var task in stage.Tasks)
{
foreach (var user in task.Users)
{
if (task.IsFinished(user.ID))
{
completedTasks.AddOrUpdate(user.ID, 1, (id, count) => count + 1);
}
}
}
return completedTasks.ToDictionary(k => k.Key, k => k.Value);
}
示例3: GetYamlTestSpecification
public static YamlSpecification GetYamlTestSpecification(bool useCache = false)
{
var folders = GetTestFolders(useCache);
var yamlFiles = new ConcurrentDictionary<string, IList<YamlDefinition>>();
using (var pbar = new ProgressBar(folders.Count, "Finding all the yaml files"))
//Parallel.ForEach(folders, (folder) =>
foreach (var folder in folders)
{
var definitions = GetFolderFiles(folder, useCache);
yamlFiles.TryAdd(folder, definitions);
pbar.Tick(string.Format("Found {0} yaml test files in {1}", definitions.Count(), folder));
//});
}
return new YamlSpecification
{
Definitions = yamlFiles.ToDictionary(k => k.Key, v => v.Value)
};
}
示例4: GetLocalUtf8BomSignatures
private Dictionary<string, bool> GetLocalUtf8BomSignatures(IEnumerable<string> textFiles)
{
var bag = new ConcurrentDictionary<string, bool>();
Console.WriteLine("Checking if local files are using UTF-8 signatures...");
Parallel.ForEach(
textFiles,
file =>
{
var filePath = Path.Combine(Args.ProjectPath, file);
bag[file] = IsUsingUtf8BomSignature(filePath);
});
Console.WriteLine("Checked {0} local files, {1} are using UTF-8 signatures.", bag.Count, bag.Count(i => i.Value));
return bag.ToDictionary(i => i.Key, i => i.Value);
}
示例5: migrateBlobs
public Dictionary<int, string> migrateBlobs()
{
this.oracleConn.Open();
Stopwatch timer = new Stopwatch();
timer.Start();
var ids = this.oracleConn.CreateStoreCommand(@"SELECT DOC_ID FROM CAA_DOC.DOCLIB_DOCUMENTS")
.Materialize(r => r.Field<int>("DOC_ID"));
Dictionary<int, string> blobIdsToFileKeys;
bool isFakeBlobMigration;
if (!bool.TryParse(ConfigurationManager.AppSettings["FakeBlobMigration"], out isFakeBlobMigration))
{
isFakeBlobMigration = false;
}
if (isFakeBlobMigration)
{
blobIdsToFileKeys = ids.ToDictionary(id => id, id => DUMMY_FILE_KEY);
}
else
{
CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken ct = cts.Token;
ConcurrentDictionary<int, string> blobIdsToFileKeysConcurrent = new ConcurrentDictionary<int, string>();
BlockingCollection<Tuple<int, MemoryStream>> blobContents = new BlockingCollection<Tuple<int, MemoryStream>>();
ConcurrentQueue<int> blobIds = new ConcurrentQueue<int>(ids);
BlockingCollection<long> downloadedBytes = new BlockingCollection<long>();
BlockingCollection<long> uploadedBytes = new BlockingCollection<long>();
int maxUploadQueueSizeInMB;
if (!int.TryParse(ConfigurationManager.AppSettings["MaxUploadQueueSizeInMB"], out maxUploadQueueSizeInMB))
{
maxUploadQueueSizeInMB = 8 * 1024;//8GB default
}
RateLimiter rateLimiter = new RateLimiter(0, maxUploadQueueSizeInMB * 1024 * 1024L, ct);
Task.WaitAll(
Task.WhenAll(
Utils.RunParallel("ParallelDownloads", ct,
() => this.downloaderFactory().Value,
(downloader) =>
{
using (downloader)
{
downloader.StartDownloading(blobIds, rateLimiter, blobContents, downloadedBytes, cts, ct);
}
})
.ContinueWith((t) =>
{
blobContents.CompleteAdding();
if (t.IsFaulted)
{
throw t.Exception;
}
}),
Utils.RunParallel("ParallelUploads", ct,
() => this.uploaderFactory().Value,
(uploader) =>
{
using (uploader)
{
uploader.StartUploading(blobContents, rateLimiter, blobIdsToFileKeysConcurrent, uploadedBytes, cts, ct);
}
}))
.ContinueWith((t) =>
{
downloadedBytes.CompleteAdding();
uploadedBytes.CompleteAdding();
if (t.IsFaulted)
{
throw t.Exception;
}
}),
Task.Run(() => PrintInfo(Console.CursorTop, "Downloaded", downloadedBytes, rateLimiter)),
Task.Run(() => PrintInfo(Console.CursorTop + 1, "Uploaded", uploadedBytes, rateLimiter)));
blobIdsToFileKeys = blobIdsToFileKeysConcurrent.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
timer.Stop();
Console.WriteLine("Blob migration time - {0}", timer.Elapsed.TotalMinutes);
return blobIdsToFileKeys;
}
示例6: GenerateAzureFileInfoForMigration
private static bool GenerateAzureFileInfoForMigration(
string repositoryRoot,
RewriterToolArguments rewriterToolArguments,
string azureDocumentUriPrefix,
AzureFileInformationCollection azureFileInformationCollection)
{
var azureMarkdownFileInfoMapping = new ConcurrentDictionary<string, AzureFileInfo>();
var azureResourceFileInfoMapping = new ConcurrentDictionary<string, AzureFileInfo>();
var azureIncludeMarkdownFileInfoMapping = new ConcurrentDictionary<string, AzureFileInfo>();
var azureIncludeResourceFileInfoMapping = new ConcurrentDictionary<string, AzureFileInfo>();
bool hasDupliateMdFileName = false;
var files = Directory.GetFiles(repositoryRoot, "*", SearchOption.AllDirectories);
Parallel.ForEach(
files,
new ParallelOptions { MaxDegreeOfParallelism = 8 },
file =>
{
var relativePath = PathUtility.MakeRelativePath(repositoryRoot, file);
if (IsIgnoreFile(relativePath, rewriterToolArguments.IsMigration))
{
return;
}
var filePath = PathUtility.NormalizePath(file);
var fileName = Path.GetFileName(file);
var azureFileInfo = new AzureFileInfo
{
FileName = fileName,
FilePath = PathUtility.NormalizePath(file),
NeedTransformToAzureExternalLink = false,
UriPrefix = string.Empty
};
var isIncludeFile = filePath.Split(new[] { Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries)
.Any(folder => folder.Equals("includes", StringComparison.OrdinalIgnoreCase));
var isMarkdownFile = Path.GetExtension(relativePath).Equals(MarkdownExtension, StringComparison.OrdinalIgnoreCase);
AzureFileInfo conflictFile = null;
var isSucceed = true;
if (!isIncludeFile && isMarkdownFile)
{
isSucceed = azureMarkdownFileInfoMapping.TryAdd(fileName, azureFileInfo);
azureMarkdownFileInfoMapping.TryGetValue(fileName, out conflictFile);
}
else if (!isIncludeFile && !isMarkdownFile)
{
// For resource file, even if has conflicts, we regards that as succeed
azureResourceFileInfoMapping.TryAdd(fileName, azureFileInfo);
}
else if (isIncludeFile && isMarkdownFile)
{
isSucceed = azureIncludeMarkdownFileInfoMapping.TryAdd(fileName, azureFileInfo);
azureIncludeMarkdownFileInfoMapping.TryGetValue(fileName, out conflictFile);
}
else
{
// For resource file, even if has conflicts, we regards that as succeed
azureIncludeResourceFileInfoMapping.TryAdd(fileName, azureFileInfo);
}
if (!isSucceed)
{
hasDupliateMdFileName = true;
Logger.LogError($"Error: GenerateAzureFileInfo failed. File: {file} name confilicts with: {conflictFile?.FilePath}");
}
});
azureFileInformationCollection.AzureMarkdownFileInfoMapping = azureMarkdownFileInfoMapping.ToDictionary(m => m.Key, m => m.Value);
azureFileInformationCollection.AzureResourceFileInfoMapping = azureResourceFileInfoMapping.ToDictionary(m => m.Key, m => m.Value);
azureFileInformationCollection.AzureIncludeMarkdownFileInfoMapping = azureIncludeMarkdownFileInfoMapping.ToDictionary(m => m.Key, m => m.Value);
azureFileInformationCollection.AzureIncludeResourceFileInfoMapping = azureIncludeResourceFileInfoMapping.ToDictionary(m => m.Key, m => m.Value);
return !hasDupliateMdFileName;
}
示例7: GenerateAzureFileInfo
private static bool GenerateAzureFileInfo(
string repositoryRoot,
RewriterToolArguments rewriterToolArguments,
string azureDocumentUriPrefix,
AzureFileInformationCollection azureFileInformationCollection)
{
var azureMarkdownFileInfoMapping = new ConcurrentDictionary<string, AzureFileInfo>();
var azureResourceFileInfoMapping = new ConcurrentDictionary<string, AzureFileInfo>();
var files = Directory.GetFiles(repositoryRoot, "*", SearchOption.AllDirectories);
Parallel.ForEach(
files,
new ParallelOptions { MaxDegreeOfParallelism = 8 },
file =>
{
var relativePath = PathUtility.MakeRelativePath(repositoryRoot, file);
if (IsIgnoreFile(relativePath, rewriterToolArguments.IsMigration))
{
return;
}
var isSucceed = true;
var azureTransformArguments = rewriterToolArguments.AzureTransformArgumentsList.FirstOrDefault(a => PathUtility.IsPathUnderSpecificFolder(file, a.SourceDir));
// By default, all the link should be transformed to external link with azure uri prefix
// However, if we find that the file is under one of the folder that need to be transformed. Then the prefix uri should be docs but not auzre
var needTransformToAzureExternalLink = true;
var uriPrefix = azureDocumentUriPrefix;
if (azureTransformArguments != null)
{
needTransformToAzureExternalLink = false;
uriPrefix = azureTransformArguments.DocsHostUriPrefix;
}
var fileName = Path.GetFileName(file);
var azureFileInfo = new AzureFileInfo
{
FileName = fileName,
FilePath = PathUtility.NormalizePath(file),
NeedTransformToAzureExternalLink = needTransformToAzureExternalLink,
UriPrefix = uriPrefix
};
AzureFileInfo conflictFile;
var isMarkdownFile = Path.GetExtension(relativePath).Equals(MarkdownExtension, StringComparison.OrdinalIgnoreCase);
if (isMarkdownFile)
{
isSucceed = azureMarkdownFileInfoMapping.TryAdd(fileName, azureFileInfo);
azureMarkdownFileInfoMapping.TryGetValue(fileName, out conflictFile);
}
else
{
isSucceed = azureResourceFileInfoMapping.TryAdd(fileName, azureFileInfo);
azureResourceFileInfoMapping.TryGetValue(fileName, out conflictFile);
}
if (!isSucceed)
{
Console.WriteLine($"GenerateAzureFileInfo warning: can't insert file: {file}, confilicts with: {conflictFile?.FilePath}");
}
});
azureFileInformationCollection.AzureMarkdownFileInfoMapping = azureMarkdownFileInfoMapping.ToDictionary(m => m.Key, m => m.Value);
azureFileInformationCollection.AzureResourceFileInfoMapping = azureResourceFileInfoMapping.ToDictionary(m => m.Key, m => m.Value);
return true;
}
示例8: Perform
public void Perform(Random rand)
{
foreach (Document doc in Documents)
{
foreach (Word word in doc.Words)
{
//Dictionary<Topic, double> results = new Dictionary<Topic, double>();
//foreach (Topic topic in topics)
//{
// int WinTopic = doc.Words.Where(w => w.Topic == topic).Count();
// double propWordinDoc = (double)WinTopic / doc.Words.Count;
// double propWordTopic = (double)GetTimesWordsAssignedTopic(word,topic) / GetAmountOfWordsAssignedTopic(word.Topic);
// double res = propWordinDoc * propWordTopic;
// results.Add(topic,res);
//}
ConcurrentDictionary<Topic, double> results = new ConcurrentDictionary<Topic, double>();
Parallel.ForEach(topics, topic =>
{
int WinTopic = doc.Words.Count(w => w.Topic == topic);
double propWordinDoc = (double)WinTopic / doc.Words.Count;
if (topic.WordsInTopic == -1)
topic.WordsInTopic = GetAmountOfWordsAssignedTopic(word.Topic);
double propWordTopic = (double)GetTimesWordsAssignedTopic(word, topic) / topic.WordsInTopic;
double res = propWordinDoc * propWordTopic;
results.AddOrUpdate(topic, res, (key,oldValue) => res);
});
Topic newTopic = GetNewTopic(results.ToDictionary(kvp=>kvp.Key, kvp=>kvp.Value));
word.Topic.WordsInTopic--;
word.Topic = newTopic;
newTopic.WordsInTopic++;
}
}
}
示例9: createAircraftsLots
public Tuple<Dictionary<int, int>, Dictionary<string, int>, Dictionary<int, JObject>> createAircraftsLots(Dictionary<string, Dictionary<string, NomValue>> noms)
{
Stopwatch timer = new Stopwatch();
timer.Start();
CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken ct = cts.Token;
ConcurrentDictionary<string, int> fmIdtoLotId = new ConcurrentDictionary<string, int>();
ConcurrentQueue<string> aircraftFmIds = new ConcurrentQueue<string>(this.getAircraftFmIds());
ConcurrentDictionary<string, int> MSNtoLotId = new ConcurrentDictionary<string, int>();
ConcurrentDictionary<int, JObject> aircraftLotIdToAircraftNom = new ConcurrentDictionary<int, JObject>();
Utils.RunParallel("ParallelMigrations", ct,
() => this.aircraftFmLotCreatorFactory().Value,
(aircraftFmLotCreator) =>
{
using (aircraftFmLotCreator)
{
aircraftFmLotCreator.StartCreating(noms, aircraftFmIds, fmIdtoLotId, MSNtoLotId, aircraftLotIdToAircraftNom, cts, ct);
}
})
.Wait();
ConcurrentDictionary<int, int> apexIdtoLotId = new ConcurrentDictionary<int, int>();
ConcurrentQueue<int> aircraftApexIds = new ConcurrentQueue<int>(this.getAircraftApexIds());
ConcurrentDictionary<string, int> unusedMSNs =
new ConcurrentDictionary<string, int>(MSNtoLotId.ToDictionary(kvp => kvp.Key, kvp => 0));//using the keys only, as there is no ConcurrentSet
Utils.RunParallel("ParallelMigrations", ct,
() => this.aircraftApexLotCreatorFactory().Value,
(aircraftApexLotCreator) =>
{
using (aircraftApexLotCreator)
{
aircraftApexLotCreator.StartCreating(noms, MSNtoLotId.ToDictionary(kvp => kvp.Key, kvp => kvp.Value), aircraftApexIds, unusedMSNs, apexIdtoLotId, cts, ct);
}
})
.Wait();
timer.Stop();
Console.WriteLine("Aircraft lot creation time - {0}", timer.Elapsed.TotalMinutes);
return Tuple.Create(
apexIdtoLotId.ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
fmIdtoLotId.ToDictionary(kvp => kvp.Key, kvp => kvp.Value),
aircraftLotIdToAircraftNom.ToDictionary(kvp => kvp.Key, kvp => kvp.Value));
}
示例10: UpdateBucketCacheAndPerformActionOnBucket
/// <summary>
/// Update BucketCache and perform action on each of the bucket while doing so.
/// </summary>
/// <param name="action">Action to be performed on each bucket.</param>
/// <returns>Returns a dictionary where key is bucket name and value is the bucket.</returns>
private Dictionary<string, Bucket> UpdateBucketCacheAndPerformActionOnBucket(Action<Bucket> action)
{
BlockingCollection<Bucket> bucketCollections = new BlockingCollection<Bucket>();
ConcurrentDictionary<string, Bucket> bucketDict = new ConcurrentDictionary<string, Bucket>();
IEnumerable<Project> projects = ListAllProjects();
// In each of these tasks, the buckets will be added to the blocking collection bucketCollections.
IEnumerable<Task> taskWithActions = projects.Select(project => ListBucketsAsync(project, bucketCollections));
// Once all the tasks are done, we signal to the blocking collection that there is nothing to be added.
Task.Factory.ContinueWhenAll(taskWithActions.ToArray(), result => { bucketCollections.CompleteAdding(); });
// bucketCollections.IsCompleted is true if CompleteAdding is called.
while (!bucketCollections.IsCompleted)
{
// bucketCollections.Take() will block until something is added.
try
{
Bucket bucket = bucketCollections.Take();
action(bucket);
bucketDict[bucket.Name] = bucket;
}
// This exception is thrown if a thread call CompleteAdding before we call bucketCollections.Take()
// and after we passed the IsCompleted check. We can just swallow it.
catch (InvalidOperationException ex)
{
WriteVerbose(ex.Message);
}
}
return bucketDict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
示例11: BulkGetHistoricPricesFromCarbon
private static IReadOnlyDictionary<string, double> BulkGetHistoricPricesFromCarbon(ICarbonClient client, IReadOnlyCollection<AnalyticsBondStaticData> bondDatas, DateTime date)
{
var dict = new ConcurrentDictionary<string, double>();
var ids = new Dictionary<string, HashSet<string>>();
foreach (var analyticsBondStaticData in bondDatas)
{
var d = GetBondPricesFromCache(analyticsBondStaticData.Id, date);
if (d.HasValue)
{
dict[analyticsBondStaticData.Id] = d.Value;
}
else
{
var snapSource = GetBondPriceSnapSource(analyticsBondStaticData);
HashSet<string> bondIds;
if (ids.TryGetValue(snapSource, out bondIds) && !bondIds.Contains(analyticsBondStaticData.Id))
{
bondIds.Add(analyticsBondStaticData.Id);
}
else
{
ids[snapSource] = new HashSet<string> { analyticsBondStaticData.Id };
}
}
}
if (ids.Count > 0)
{
Parallel.ForEach(ids, kvp =>
{
Parallel.ForEach(Helper.BatchItems(kvp.Value, 32), identfiers =>
{
SLog.log.InfoFormat("Request bond price for {0} ids", identfiers.Length);
var datas = client.GetTimeSeriesAsync(identfiers, kvp.Key,
new DateTime(date.Year, date.Month, date.Day, 0, 0, 0, DateTimeKind.Utc),
new DateTime(date.Year, date.Month, date.Day, 0, 0, 0, DateTimeKind.Utc)).Result;
if (datas == null || datas.Count <= 0) return;
foreach (var data in datas)
{
if (data.Series == null || data.Series.Count == 0) continue;
var o = client.GetValueFromTimeSeriesList(new List<ITimeSeries> { data }, "close", null);
if (!(o is double)) continue;
var close = (double)o;
dict.AddOrUpdate(data.Identifier, close, (s, d) => close);
AddBondPricesToCache(data.Identifier, date, close);
}
});
});
}
return dict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
示例12: BulkGetBondStaticDataFromCarbon
private static IReadOnlyDictionary<string, AnalyticsBondStaticData> BulkGetBondStaticDataFromCarbon(ICarbonClient client,
IReadOnlyCollection<string> bondIds)
{
if (bondIds == null || bondIds.Count == 0) return new Dictionary<string, AnalyticsBondStaticData>();
var dict = new ConcurrentDictionary<string, AnalyticsBondStaticData>();
var ids = new List<string>();
foreach (var id in bondIds)
{
AnalyticsBondStaticData data;
if (BondStaticDataCache.TryGetValue(id, out data))
{
dict.TryAdd(id, data);
}
else
{
var carbonBond = client.GetCachedCarbonBond(id);
if (carbonBond == null)
{
ids.Add(id);
}
else
{
var bond = ConvertCarbonBondToAnalyticsBondStaticData(carbonBond);
if (bond != null)
{
dict.TryAdd(id, bond);
BondStaticDataCache.TryAdd(id, bond);
BondStaticDataCache.TryAdd(bond.Id, bond);
}
else
{
ids.Add(id);
}
}
}
}
if (ids.Count == 0) return dict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
var datas = client.BulkGetSecurityMetadataInternal(ids, "static-bond");
if (datas == null || datas.Count == 0) return dict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
Parallel.ForEach(datas, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount },
data =>
{
var bond = ConvertCarbonBondStaticToAnalyticBondStatic(client, data);
if (bond != null)
{
BondStaticDataCache.TryAdd(bond.Id, bond);
BondStaticDataCache.TryAdd(data.Identifier, bond);
dict.TryAdd(bond.Id, bond);
}
});
return dict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
示例13: BulkGetHistoricEodDiscountCurvesFromCarbon
public static IReadOnlyDictionary<DateTime, DiscountCurve> BulkGetHistoricEodDiscountCurvesFromCarbon(ICarbonClient client,
List<DateTime> settleDates, string ccy, bool bOis)
{
var inputDateKind = settleDates.FirstOrDefault().Kind;
var dict = new ConcurrentDictionary<DateTime, DiscountCurve>();
var dates = new List<DateTime>();
foreach (var settleDate in settleDates)
{
DiscountCurve data;
var key = GetKeyForDiscountCurveCache(settleDate, ccy, bOis);
if (DicountCurveDataCache.TryGetValue(key, out data))
{
dict[settleDate] = data;
}
else if (settleDate <= DateTime.Today)
{
dates.Add(settleDate);
}
}
if (dates.Count == 0) return dict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
var curveName = GetCurveName(ccy, bOis);
if (string.IsNullOrEmpty(curveName)) return new Dictionary<DateTime, DiscountCurve>();
try
{
var start = dates.Min();
var end = dates.Max();
if (dates.Count > 16)
{
start = new DateTime(start.Year, start.Month, start.Day, 0, 0, 0, DateTimeKind.Utc);
end = new DateTime(end.Year, end.Month, end.Day, 0, 0, 0, DateTimeKind.Utc);
var datas = client.GetTimeSeriesData(curveName.ToUpperInvariant(), "interestcurves-ot", start, end, "dfs");
if (datas != null)
{
Parallel.ForEach(datas, messagePackObject =>
{
try
{
var curve = messagePackObject.Value.ConvertMessagePackObjectToDiscountFactors();
if (curve == null) return;
var dicountCurve = new DiscountCurve(new FinCADTable(curve, false));
var date = new DateTime(messagePackObject.Key.Year, messagePackObject.Key.Month, messagePackObject.Key.Day, 0, 0, 0, inputDateKind);
dict.TryAdd(date, dicountCurve);
var key = GetKeyForDiscountCurveCache(messagePackObject.Key, ccy, bOis);
DicountCurveDataCache.TryAdd(key, dicountCurve);
}
catch (Exception ex)
{
Log.ErrorFormat("Failed to convert curve {0} : {1}", curveName, ex);
}
});
}
}
else
{
Parallel.ForEach(dates, date =>
{
try
{
var curve = GetHistoricEodDiscountCurveFromCarbon(client, date, ccy, bOis);
if (curve == null) return;
dict.TryAdd(date, curve);
var key = GetKeyForDiscountCurveCache(date, ccy, bOis);
DicountCurveDataCache.TryAdd(key, curve);
}
catch (Exception ex)
{
SLog.log.ErrorFormat("Failed to get curve {0} : {1}", curveName, ex);
}
});
}
}
catch (Exception ex)
{
SLog.log.ErrorFormat("Failed to get curve {0} : {1}", curveName, ex);
}
return dict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
示例14: GetResAndDesAsync
public static Dictionary<string, ServerInfo> GetResAndDesAsync(IEnumerable<string> urls, double timeoutInMS)
{
ConcurrentDictionary<string, ServerInfo> serverInfosD = new ConcurrentDictionary<string, ServerInfo>();
if (urls != null)
{
using (var client = new HttpClient())
{
client.Timeout = TimeSpan.FromMilliseconds(timeoutInMS);
//do it all in parallel so we dont have to wait on eachother
//wait time becomes max specified timeout, typically 1000 - 3000 ms
Task.WaitAll(urls.Select(url =>
{
return client.GetAsync(HTTP_PROTOCOL + url).ContinueWith(response =>
{
var resp = "";
try
{
resp = response.Result.Content.ReadAsStringAsync().Result;
}
catch (AggregateException ae)
{
//?
}
var si = new ServerInfo();
if (!String.IsNullOrEmpty(resp))
{
si = JsonConvert.DeserializeObject<ServerInfo>(resp);
// Debug.WriteLine("{0} -> {1}", url, resp);
}
//var content = JsonConvert.DeserializeObject<IEnumerable<ServerInfo>>(resp
serverInfosD.AddOrUpdate(url, si, (key, existingValue) => si);
});
}).ToArray());
}
}
return serverInfosD.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
}
示例15: GetLocalOnlyAnsiCharacters
private Dictionary<string, bool> GetLocalOnlyAnsiCharacters(Dictionary<string, int> tfsEncodings)
{
var bag = new ConcurrentDictionary<string, bool>();
Console.WriteLine("Checking if local files are using non-ANSI characters...");
Parallel.ForEach(
tfsEncodings,
item =>
{
var filePath = Path.Combine(Args.ProjectPath, item.Key);
bag[item.Key] = IsUsingOnlyAnsiCharacters(filePath, item.Value);
});
Console.WriteLine("Checked {0} local files, {1} are using non-ANSI characters.", bag.Count, bag.Count(i => !i.Value));
return bag.ToDictionary(i => i.Key, i => i.Value);
}