public static long[] GetGoals() { System.Collections.Generic.KeyValuePair <int, NobilityDonation>[] OrderDonations = Donations.OrderBy(don => don.Value.Donation).ToArray(); Array.Reverse(OrderDonations); long KingDonation = 0; long PrinceDonation = 0; long DukeDonation = 0; for (int i = 0; i < OrderDonations.Length; i++) { if (i < 3) { KingDonation = OrderDonations[i].Value.Donation; PrinceDonation = KingDonation - 1; DukeDonation = PrinceDonation - 1; } else if (i < 15) { PrinceDonation = OrderDonations[i].Value.Donation; DukeDonation = PrinceDonation - 1; } else if (i < 50) { DukeDonation = OrderDonations[i].Value.Donation; } } return(new long[] { KingDonation, PrinceDonation, DukeDonation }); }
static async Task DumpCurrentState(CancellationToken token) { while (!token.IsCancellationRequested) { Console.Clear(); Console.WriteLine("--- Current state ---"); if (!sentAndReceived.IsEmpty) { foreach (var entry in sentAndReceived.OrderBy(e => e.Value)) { Console.WriteLine($"'{entry.Key}' to be received at '{entry.Value.ToString("yyyy-MM-dd HH:mm:ss.ffffff", CultureInfo.InvariantCulture)}'"); } } else { Console.WriteLine("empty."); } Console.WriteLine("--- Current state ---"); await WriteStats(); try { await Task.Delay(TimeSpan.FromSeconds(20), token); } catch (OperationCanceledException) { } } }
/// <summary> /// 按最少使用策略清理 /// </summary> private void ClearCacheByLFU() { List <string> clearKey = _DicCacheValue.OrderBy(m => m.Value.CacheReferencesCount).Take(_ClearCacheCount).Select(m => m.Key).ToList(); foreach (string key in clearKey) { RemoveCache(key); } }
/// <summary> /// Splits a chunk into two chunks. /// </summary> /// <returns>The new chunk created from the split.</returns> public DatabaseChunk Split() { var newData = new ConcurrentDictionary <ObjectId, Document>(_data.OrderBy(e => e.Key).Skip(_data.Count / 2).ToDictionary(e => e.Key, e => e.Value)); _data = new ConcurrentDictionary <ObjectId, Document>(_data.OrderBy(e => e.Key).Take(_data.Count / 2)); var oldEnd = _end; _end = new ChunkMarker(newData.Keys.Min().ToString()); return(new DatabaseChunk(_end, oldEnd, newData)); }
private static async Task FetchData(double lat, double lng, bool silent = false) { if (lastUpdated > DateTime.Now.AddSeconds(-30) && !silent) { return; } if (lastUpdated < DateTime.Now.AddSeconds(-30) && silent && rarePokemons != null && rarePokemons.Count > 0) { foreach (var p in rarePokemons.Select(p => p.Value)) { CalculateDistanceAndEstTime(p); } var ordered = rarePokemons.OrderBy(p => p.Value.Setting.Priority).ThenBy(p => p.Value.Distance); _session.EventDispatcher.Send(new HumanWalkSnipeEvent() { Type = HumanWalkSnipeEventTypes.ClientRequestUpdate, Pokemons = ApplyFilter(ordered.ToList()), }); } List <Task <List <SnipePokemonInfo> > > allTasks = new List <Task <List <SnipePokemonInfo> > >() { FetchFromPokeradar(lat, lng), FetchFromSkiplagged(lat, lng), FetchFromPokecrew(lat, lng), FetchFromPokesnipers(lat, lng), FetchFromPokeZZ(lat, lng), FetchFromFastPokemap(lat, lng), FetchFromPokeWatcher(lat, lng) }; if (_setting.HumanWalkingSnipeIncludeDefaultLocation && LocationUtils.CalculateDistanceInMeters(lat, lng, _session.Settings.DefaultLatitude, _session.Settings.DefaultLongitude) > 1000) { allTasks.Add(FetchFromPokeradar(_session.Settings.DefaultLatitude, _session.Settings.DefaultLongitude)); allTasks.Add(FetchFromSkiplagged(_session.Settings.DefaultLatitude, _session.Settings.DefaultLongitude)); allTasks.Add(FetchFromPokecrew(_session.Settings.DefaultLatitude, _session.Settings.DefaultLongitude)); allTasks.Add(FetchFromPokesnipers(_session.Settings.DefaultLatitude, _session.Settings.DefaultLongitude)); allTasks.Add(FetchFromPokeZZ(_session.Settings.DefaultLatitude, _session.Settings.DefaultLongitude)); allTasks.Add(FetchFromFastPokemap(_session.Settings.DefaultLatitude, _session.Settings.DefaultLongitude)); allTasks.Add(FetchFromPokeWatcher(_session.Settings.DefaultLatitude, _session.Settings.DefaultLongitude)); } Task.WaitAll(allTasks.ToArray()); lastUpdated = DateTime.Now; var fetchedPokemons = allTasks.SelectMany(p => p.Result); await PostProcessDataFetched(fetchedPokemons, !silent); }
void LoadSymbols() { MarketSymbols = new ConcurrentDictionary<string, string>(); resourceName = new [] { "Windows.Controls.Resources.NYSE.csv", "Windows.Controls.Resources.NASDAQ.csv", "Windows.Controls.Resources.AMEX.csv" }; var asm = Assembly.GetExecutingAssembly(); Parallel.ForEach(resourceName, (s) => ReadResourceSymbolFile(s, asm)); if (!MarketSymbols.Any()) return; Symbols = MarketSymbols.OrderBy(x => x.Key).ToDictionary(k => k.Key, v => v.Value); for (int i = 0; i < Symbols.Count; i++) { var itm = Symbols.ElementAt(i); var idx = int.Parse(itm.Value.Substring(0, 1)); // Stripping the market index char var val = itm.Value.Substring(1); Symbols[itm.Key] = val; this.AppendValues(itm.Key, val, icon[idx]); } }
// does NOT enter the teamCacheLock, caller MUST protected void EnsureTeamCacheCapacity(bool overrideCountCheck = false) { if (overrideCountCheck || cachedTeamInformations.Count >= MaxCachedTeamDetails) { // start by purging all old items // ToArray to avoid enumeration + modification foreach (var oldTeamInfoId in cachedTeamInformations.Where(kvp => kvp.Value.Age >= MaxTeamLifespan).Select(kvp => kvp.Key).ToArray()) { // TODO i don't think we care about tryremove failure cachedTeamInformations.TryRemove(oldTeamInfoId, out var _); } } if (cachedTeamInformations.Count > MaxCachedTeamDetails) { // we've purged old ones but it's not good enough // keep the most commonly accessed ones (hits per time) // untested algorithm, but this is a low volume cache anyway // clears cache to half capacity (not max) to avoid constantly calling this // that also attempts to avoid the common request of an item which is always new and getting purged from cache TeamId[] destroy = cachedTeamInformations.OrderBy(tInf => Math.Round((20.0 * tInf.Value.HitCount) / Math.Min(tInf.Value.Age.TotalSeconds, MaxTeamLifespan.TotalSeconds * 1.5))).ThenByDescending(tInf => tInf.Value.Age).Take(cachedTeamInformations.Count - (MaxCachedTeamDetails / 2)).Select(tInf => tInf.Key).ToArray(); foreach (var oldTeamInfoId in destroy) { // TODO i don't think we care about tryremove failure cachedTeamInformations.TryRemove(oldTeamInfoId, out var _); } } }
public Task <IAsyncEnumerable <KeyValuePair <TKey, TValue> > > CreateEnumerableAsync(ITransaction txn, EnumerationMode enumerationMode) { return(Task.FromResult <IAsyncEnumerable <KeyValuePair <TKey, TValue> > >(new MockAsyncEnumerable <KeyValuePair <TKey, TValue> >( enumerationMode == EnumerationMode.Unordered ? (IEnumerable <KeyValuePair <TKey, TValue> >)dictionary : dictionary.OrderBy(x => x.Key)))); }
static async Task Main(string[] args) { var address = "https://mail.ru/"; var uri = new Uri(address); var domain = $"{uri.Scheme}://{uri.Host}"; var strategy = Factory.GetSearchStrategy("First"); //var strategy = Factory.GetSearchStrategy("Second"); //var strategy = Factory.GetSearchStrategy("Third"); var parser = new Parser(strategy); var returnedLinks = new ConcurrentDictionary <string, string>(); Console.WriteLine("Search started"); await parser.Parse(address, returnedLinks, domain); foreach (var link in returnedLinks.OrderBy(it => it.Key)) { Console.WriteLine($"{link.Key} --- {link.Value}"); } Console.WriteLine("Search finished"); }
public ElevatorModel TakeClosestElevator(int floor) { if (!routeValidationService.IsFloorNumberCorrect(floor)) { throw new ArgumentOutOfRangeException(nameof(floor)); } lock (locker) { var orderedElevators = freeElevators .OrderBy(p => Math.Abs(p.Value.CurrentFloor - floor)); if (!orderedElevators.Any()) { return(null); } var closestElevatorEntry = orderedElevators.First(); freeElevators.TryRemove(closestElevatorEntry.Key, out var closestElevator); occupiedElevators.TryAdd(closestElevatorEntry.Key, closestElevator); elevatorEventLogService.LogEvent(closestElevator, "Called elevator"); return(closestElevator); } }
private static List <OverlappingSegmentsRegion> GetSegmentSets(int defaultAlleleCountThreshold, Dictionary <string, List <BedEntry> > commonRegions, Dictionary <string, IReadOnlyList <SampleGenomicBin> > genomicBinsByChromosome, Dictionary <string, List <BedInterval> > segmentIntervalsByChromosome, Dictionary <string, List <Balleles> > allelesByChromosomeCommonSegs, Segments segments) { var segmentsSetByChromosome = new ConcurrentDictionary <string, List <OverlappingSegmentsRegion> >(); Parallel.ForEach( segments.GetChromosomes(), chr => { var segmentsByChromosome = segments.GetSegmentsForChromosome(chr).ToList(); if (commonRegions.Keys.Any(chromosome => chromosome == chr)) { var commonCnvCanvasSegments = CanvasSegment.CreateSegmentsFromCommonCnvs(genomicBinsByChromosome[chr], segmentIntervalsByChromosome[chr], allelesByChromosomeCommonSegs[chr]); segmentsSetByChromosome[chr] = CanvasSegment.MergeCommonCnvSegments(segmentsByChromosome, commonCnvCanvasSegments, defaultAlleleCountThreshold); } else { segmentsSetByChromosome[chr] = segmentsByChromosome.Select( segment => new OverlappingSegmentsRegion(new List <CanvasSegment> { segment }, null)).ToList(); } }); return(segmentsSetByChromosome.OrderBy(i => i.Key).Select(x => x.Value).SelectMany(x => x).ToList()); }
/// <summary> /// Orders the clients map /// </summary> public Dictionary <UInt64, string> order(ConcurrentDictionary <UInt64, string> cDict) { IEnumerable <KeyValuePair <UInt64, string> > orderedMap = cDict.OrderBy(element => element.Key); Dictionary <UInt64, string> dict = orderedMap.ToDictionary(el => el.Key, el => el.Value); return(dict); }
public async Task FlushAsync(bool disposeMode) { this.logger.LogTrace("({0}:{1})", nameof(disposeMode), disposeMode); int count = this.items.Count; if (disposeMode || (count > this.threshold)) { // Push to store all items that are not already persisted. ICollection <StakeItem> entries = this.items.Values; await this.dBreezeCoinView.PutStakeAsync(entries.Where(w => !w.InStore)).ConfigureAwait(false); if (disposeMode) { return; } // Pop some items to remove a window of 10 % of the threshold. ConcurrentDictionary <uint256, StakeItem> select = this.items; IEnumerable <KeyValuePair <uint256, StakeItem> > oldes = select.OrderBy(o => o.Value.Height).Take(this.thresholdWindow); StakeItem unused; foreach (KeyValuePair <uint256, StakeItem> olde in oldes) { this.items.TryRemove(olde.Key, out unused); } } this.logger.LogTrace("(-)"); }
/// <summary> /// Generates the execution result, a list of individual results for each resolver tracked. /// </summary> /// <returns>IDictionary<System.String, System.Object>.</returns> private IResponseFieldSet GenerateExecutionResult() { // apollo tracing does not specifiy providing startOffset and duration keys for the execution // phase, just output the resolvers array var list = new ResponseList(); foreach (var timeEntry in _resolverEntries.OrderBy(x => x.Value.StartOffsetTicks)) { var resolverEntry = new ResponseFieldSet(); resolverEntry.Add("path", new ResponseList(timeEntry .Key .Request .Origin .Path .ToArray() .Select(x => new ResponseSingleValue(x)))); var parentType = _schema.KnownTypes.FindGraphType(timeEntry.Key.Request?.DataSource?.Value); if (parentType != null) { resolverEntry.AddSingleValue("parentType", parentType.Name); } resolverEntry.AddSingleValue("fieldName", timeEntry.Key.Request.Field.Name); resolverEntry.AddSingleValue("returnType", timeEntry.Key.Request.Field.TypeExpression.ToString()); resolverEntry.AddSingleValue("startOffset", timeEntry.Value.StartOffsetNanoseconds); resolverEntry.AddSingleValue("duration", timeEntry.Value.DurationNanoSeconds); list.Add(resolverEntry); } var dictionary = new ResponseFieldSet(); dictionary.Add("resolvers", list); return(dictionary); }
/// <summary> /// Processes the pending queue. /// </summary> private static void ProcessPendingQueue() { foreach (var item in QueuedTest.OrderBy(x => x.Value.CreatedOn)) { SendTestToHub(item.Value, item.Key); } }
public IEnumerable <Playlist> FetchProviderPlaylists(IEnumerable <PlaylistProvider> providers) { ConcurrentDictionary <int, Playlist> playlists = new ConcurrentDictionary <int, Playlist>(); logger.Info(MyOperation.PlaylistFetching, OperationStatus.Started, "Fetching provider playlists"); List <Task> tasks = new List <Task>(); foreach (PlaylistProvider provider in providers) { Task task = Task.Run(async() => { Playlist playlist = await FetchProviderPlaylistAsync(provider); if (!Playlist.IsNullOrEmpty(playlist)) { playlists.AddOrUpdate( provider.Priority, playlist, (key, oldValue) => playlist); } }); tasks.Add(task); } Task.WaitAll(tasks.ToArray()); return(playlists .OrderBy(x => x.Key) .Select(x => x.Value)); }
private void DeleteQueueMessages() { if (Interlocked.CompareExchange(ref _isDeletingQueueMessage, 1, 0) == 0) { try { var queues = _queueDict.OrderBy(x => x.Key).Select(x => x.Value).ToList(); foreach (var queue in queues) { try { queue.DeleteMessages(_messageStore.MinMessagePosition); } catch (Exception ex) { _logger.Error(string.Format("Delete queue (topic: {0}, queueId: {1}) messages has exception.", queue.Topic, queue.QueueId), ex); } } } finally { Interlocked.Exchange(ref _isDeletingQueueMessage, 0); } } }
public void Flush(bool disposeMode) { int count = this.items.Count; if (disposeMode || (count > this.threshold)) { // Push to store all items that are not already persisted. ICollection <StakeItem> entries = this.items.Values; this.dBreezeCoinView.PutStake(entries.Where(w => !w.InStore)); if (disposeMode) { return; } // Pop some items to remove a window of 10 % of the threshold. ConcurrentDictionary <uint256, StakeItem> select = this.items; IEnumerable <KeyValuePair <uint256, StakeItem> > oldes = select.OrderBy(o => o.Value.Height).Take(this.thresholdWindow); StakeItem unused; foreach (KeyValuePair <uint256, StakeItem> olde in oldes) { this.items.TryRemove(olde.Key, out unused); } } }
public int[] CalculateOverlapDist(int[] inputVector) { ConcurrentDictionary <int, List <KeyPair> > overlapList = new ConcurrentDictionary <int, List <KeyPair> >(); ParallelOptions opts = new ParallelOptions(); opts.MaxDegreeOfParallelism = Environment.ProcessorCount; // Run overlap calculation on all actors(in all partitions) Parallel.ForEach(this.ActorMap, opts, (placement) => { var partitionOverlaps = ((ActorReference)placement.ActorRef).Ask <List <KeyPair> >(new CalculateOverlapMsg { InputVector = inputVector }, this.Config.ConnectionTimeout, placement.NodePath).Result; overlapList.TryAdd(placement.PartitionIndx, partitionOverlaps); }); List <int> overlaps = new List <int>(); int cnt = 0; foreach (var item in overlapList.OrderBy(i => i.Key)) { foreach (var keyPair in item.Value) { cnt++; overlaps.Add((int)(long)keyPair.Value); } Debug.WriteLine($"cnt: {cnt} - key:{item.Key} - cnt:{item.Value.Count}"); } return(overlaps.ToArray()); }
/// <summary> /// Get blocks that are labelled as decoration for each page in the document, using a content and a geometric similarity measure. /// <para>Decoration blocks are blocks that contains information such as author names, publication titles, page numbers, etc. /// They are printed repeatedly at the border of each page, usually placed inside headers or footers, but sometimes also at the /// left or right edge of the page.</para> /// </summary> /// <param name="pages">The <see cref="Page"/>s in the document. All of them are needed for the algorithm to work.</param> /// <param name="wordExtractor"></param> /// <param name="pageSegmenter"></param> /// <param name="minimumEditDistanceNormalised">Minimum edit distance normalised. A value of 0 means both strings are exactly equal.</param> /// <param name="similarityThreshold">Minimum similarity score to decide wether a block is labelled as decoration or not.</param> /// <param name="n">Number of blocks in a page to be considered when looking for decoration blocks.</param> /// <param name="maxDegreeOfParallelism">Sets the maximum number of concurrent tasks enabled. /// <para>A positive property value limits the number of concurrent operations to the set value. /// If it is -1, there is no limit on the number of concurrently running operations.</para></param> public static IReadOnlyList <IReadOnlyList <TextBlock> > Get(IReadOnlyList <Page> pages, IWordExtractor wordExtractor, IPageSegmenter pageSegmenter, Func <string, string, double> minimumEditDistanceNormalised, double similarityThreshold = 0.25, int n = 5, int maxDegreeOfParallelism = -1) { if (pages.Count < 2) { throw new ArgumentException("The algorithm cannot be used with a document of less than 2 pages.", nameof(pages)); } ConcurrentDictionary <int, IReadOnlyList <TextBlock> > pagesBlocks = new ConcurrentDictionary <int, IReadOnlyList <TextBlock> >(); ParallelOptions parallelOptions = new ParallelOptions() { MaxDegreeOfParallelism = maxDegreeOfParallelism }; Parallel.For(0, pages.Count(), parallelOptions, p => { var words = pages[p].GetWords(wordExtractor); var blocks = pageSegmenter.GetBlocks(words); if (!pagesBlocks.TryAdd(p, blocks)) { throw new ArgumentException("Cannot add element with index " + p + " in ConcurrentDictionary."); } }); return(Get(pagesBlocks.OrderBy(x => x.Key).Select(x => x.Value).ToList(), minimumEditDistanceNormalised, similarityThreshold, n, maxDegreeOfParallelism)); }
/// <summary> /// To do. Create a clever search algorithm. /// </summary> /// <param name="Word"></param> /// <param name="Collection"></param> /// <returns></returns> public string GetSimilar(string Word, List <string> Collection) { if (Word.IsNullOrEmpty()) { return(Word); } ConcurrentDictionary <string, int> keys = new ConcurrentDictionary <string, int>(); /*foreach (string s in words.Keys) * { * if (s.EndsWith(Word)) * { * keys.Add(s, s.Length); * } * }*/ Collection.AsParallel().ForAll(s => { if (s.EndsWith(Word)) { keys.TryAdd(s, s.Length); } }); if (!keys.Any() && Word.Length > 2) { return(this.GetSimilar(Word.Substring(1), Collection)); } string key = keys.OrderBy(val => val.Value).FirstOrDefault().Key; return(key); }
public void Start() { Task.Run(async() => { while (!_disposed) { await Task.Delay(TimeSpan.FromSeconds(1)); var buffer = new List <QueuedFileSystemEventArgs>(); //copy to local buffer from another thread await Dispatcher.InvokeAsync(() => { buffer.AddRange(_eventBuffer); }, DispatcherPriority.Normal); ClearOldEvents(); CopyToQueue(buffer); await Dispatcher.InvokeAsync(() => _eventBuffer.RemoveAll(a => _queue.Values.Contains(a)), DispatcherPriority.Normal); if (_queue.Count == 0) { //wait for incoming events continue; } OnBulkEvent?.Invoke(_queue.Select(x => x.Value)); foreach (var eventArg in _queue.OrderBy(x => x.Key.Split('\\').Length)) { //Logger.WriteLine($"WindowsFileSystemWatcherFiber dispatch real event: {eventArg.Value.ChangeType} {eventArg}"); _queue.TryRemove(eventArg.Key, out var dummy); } } }); }
public int SaveChunks() { int count = 0; try { lock (_chunkCache) { SaveLevelInfo(new LevelInfo()); foreach (var chunkColumn in _chunkCache.OrderBy(pair => pair.Key.X >> 5).ThenBy(pair => pair.Key.Z >> 5)) { if (chunkColumn.Value != null && chunkColumn.Value.NeedSave) { SaveChunk(chunkColumn.Value, BasePath, WaterOffsetY); count++; } } } } catch (Exception e) { Log.Error("saving chunks", e); } return(count); }
internal void Freeze() { var wasFrozen = Interlocked.Exchange(ref _frozen, 1); if (wasFrozen != 0) { throw new InvalidOperationException(); } // Sort fields. ArrayBuilder <SynthesizedStaticField> fieldsBuilder = ArrayBuilder <SynthesizedStaticField> .GetInstance(_mappedFields.Count + (_mvidField != null ? 1 : 0)); fieldsBuilder.AddRange(_mappedFields.Values); if (_mvidField != null) { fieldsBuilder.Add(_mvidField); } fieldsBuilder.AddRange(_instrumentationPayloadRootFields.Values); fieldsBuilder.Sort(FieldComparer.Instance); _orderedSynthesizedFields = fieldsBuilder.ToImmutableAndFree(); // Sort methods. _orderedSynthesizedMethods = _synthesizedMethods.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value).AsImmutable(); // Sort proxy types. _orderedProxyTypes = _proxyTypes.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value).AsImmutable(); }
public int SortAndWriteToFile() { var sortedCollection = _collection.OrderBy(x => x.Key); File.WriteAllLines(Common.Common.OutputFileName, sortedCollection.Select(s => s.Value.ToString()).ToList()); return(sortedCollection.Count()); }
public void AddMessage(MultipartMessage message) { if (message.MultipartMessageId != _multipartMessageId) { return; } if (_receivedLength >= _totalLength) { return; } if (!_messages.TryAdd(message.Offset, message)) { return; } if (Interlocked.Add(ref _receivedLength, message.Length) >= _totalLength) { var buffer = new GrowingSpanBuffer(stackalloc byte[(int)_totalLength]); foreach (var kvp in _messages.OrderBy(kvp => kvp.Key)) { buffer.WriteBytes(kvp.Value.Data); } var bufferReader = new SpanBufferReader(buffer.Data); var fullMessage = _service._messageReader.ReadFrom(ref bufferReader, 0x00); Complete(fullMessage); } }
private static void AddResultToCache(string source, Type result, bool shouldUpdateIfExists = false) { var cacheEntry = new CacheEntry { Source = source, Type = result, Usages = 1 }; if (shouldUpdateIfExists) { cacheEntries.AddOrUpdate(source, cacheEntry, (key, existingValue) => cacheEntry); } else { cacheEntries.TryAdd(source, cacheEntry); } if (cacheEntries.Count > 256) { var kvp = cacheEntries.OrderBy(x => x.Value.Usages).FirstOrDefault(); if (kvp.Key != null) { CacheEntry _; cacheEntries.TryRemove(kvp.Key, out _); } } }
public ActionResult <List <SeriesSearchResult> > StartsWith(string query, int limit = int.MaxValue) { query = query.ToLowerInvariant(); List <SeriesSearchResult> seriesList = new List <SeriesSearchResult>(); ConcurrentDictionary <SVR_AnimeSeries, string> tempSeries = new ConcurrentDictionary <SVR_AnimeSeries, string>(); ParallelQuery <SVR_AnimeSeries> allSeries = RepoFactory.AnimeSeries.GetAll() .Where(a => a?.Contract?.AniDBAnime?.AniDBAnime != null && !a.Contract.AniDBAnime.Tags.Select(b => b.TagName) .FindInEnumerable(User.GetHideCategories())) .AsParallel(); #region Search_TitlesOnly allSeries.ForAll(a => CheckTitlesStartsWith(a, query, ref tempSeries, limit)); Dictionary <SVR_AnimeSeries, string> series = tempSeries.OrderBy(a => a.Value).ToDictionary(a => a.Key, a => a.Value); foreach (KeyValuePair <SVR_AnimeSeries, string> ser in series) { seriesList.Add(new SeriesSearchResult(HttpContext, ser.Key, ser.Value, 0)); if (seriesList.Count >= limit) { break; } } #endregion return(seriesList); }
// Природній відбір private int Survive() { // Безпечний словник не впорядкований, тому відбір найкращих дещо складніший topTours = population.OrderBy(pair => pair.Key).Take(popuSize).ToArray(); population.Clear(); return(population.Count); }
/// <summary> /// Random search optimizer initializes random parameters between min and max of the provided bounds. /// Returns all results, chronologically ordered. /// Note that the order of results might be affected if running parallel. /// </summary> /// <param name="functionToMinimize"></param> /// <returns></returns> public OptimizerResult[] Optimize(Func <double[], OptimizerResult> functionToMinimize) { // Generate parameter sets. var parameterSets = SampleRandomParameterSets(m_iterations, m_parameters, m_sampler); // Run parameter sets. var parameterIndexToResult = new ConcurrentDictionary <int, OptimizerResult>(); if (!m_runParallel) { for (int index = 0; index < parameterSets.Length; index++) { RunParameterSet(index, parameterSets, functionToMinimize, parameterIndexToResult); } } else { Parallel.For(0, parameterSets.Length, m_parallelOptions, (index, loopState) => { RunParameterSet(index, parameterSets, functionToMinimize, parameterIndexToResult); }); } var results = parameterIndexToResult.OrderBy(v => v.Key) .Select(v => v.Value) .ToArray(); return(results); }
public int SaveChunks() { int count = 0; try { lock (_chunkCache) { SaveLevelInfo(new LevelInfo()); Dictionary <Tuple <int, int>, List <ChunkColumn> > regions = new Dictionary <Tuple <int, int>, List <ChunkColumn> >(); foreach (var chunkColumn in _chunkCache.OrderBy(pair => pair.Key.X >> 5).ThenBy(pair => pair.Key.Z >> 5)) { var regionKey = new Tuple <int, int>(chunkColumn.Key.X >> 5, chunkColumn.Key.Z >> 5); if (!regions.ContainsKey(regionKey)) { regions.Add(regionKey, new List <ChunkColumn>()); } regions[regionKey].Add(chunkColumn.Value); } List <Task> tasks = new List <Task>(); foreach (var region in regions.OrderBy(pair => pair.Key.Item1).ThenBy(pair => pair.Key.Item2)) { Task task = new Task(delegate { List <ChunkColumn> chunks = region.Value; foreach (var chunkColumn in chunks) { if (chunkColumn != null && chunkColumn.NeedSave) { SaveChunk(chunkColumn, BasePath); count++; } } }); task.Start(); tasks.Add(task); } Task.WaitAll(tasks.ToArray()); //foreach (var chunkColumn in _chunkCache.OrderBy(pair => pair.Key.X >> 5).ThenBy(pair => pair.Key.Z >> 5)) //{ // if (chunkColumn.Value != null && chunkColumn.Value.NeedSave) // { // SaveChunk(chunkColumn.Value, BasePath); // count++; // } //} } } catch (Exception e) { Log.Error("saving chunks", e); } return(count); }
public async Task<HttpResponseMessage> Count() { // For each partition client, keep track of partition information and the number of words ConcurrentDictionary<Int64RangePartitionInformation, long> totals = new ConcurrentDictionary<Int64RangePartitionInformation, long>(); IList<Task> tasks = new List<Task>(); foreach (Int64RangePartitionInformation partition in await this.GetServicePartitionKeysAsync()) { try { ServicePartitionClient<HttpCommunicationClient> partitionClient = new ServicePartitionClient<HttpCommunicationClient>(communicationFactory, serviceUri, new ServicePartitionKey(partition.LowKey)); await partitionClient.InvokeWithRetryAsync( async (client) => { HttpResponseMessage response = await client.HttpClient.GetAsync(new Uri(client.Url, "Count")); string content = await response.Content.ReadAsStringAsync(); totals[partition] = Int64.Parse(content.Trim()); }); } catch (Exception ex) { // Sample code: print exception ServiceEventSource.Current.OperationFailed(ex.Message, "Count - run web request"); } } StringBuilder sb = new StringBuilder(); sb.Append("<h1> Total:"); sb.Append(totals.Aggregate<KeyValuePair<Int64RangePartitionInformation, long>, long>(0, (total, next) => next.Value + total)); sb.Append("</h1>"); sb.Append("<table><tr><td>Partition ID</td><td>Key Range</td><td>Total</td></tr>"); foreach (KeyValuePair<Int64RangePartitionInformation, long> partitionData in totals.OrderBy(partitionData => partitionData.Key.LowKey)) { sb.Append("<tr><td>"); sb.Append(partitionData.Key.Id); sb.Append("</td><td>"); sb.AppendFormat("{0} - {1}", partitionData.Key.LowKey, partitionData.Key.HighKey); sb.Append("</td><td>"); sb.Append(partitionData.Value); sb.Append("</td></tr>"); } sb.Append("</table>"); return new HttpResponseMessage() { Content = new StringContent(sb.ToString(), Encoding.UTF8, "text/html") }; }
public void HandleLocalFileChanges(ConcurrentDictionary<string, string> localFileChanges) { string localChangesFilePath = $"{ConfigurationManager.AppSettings["LocalWikiRootPath"].ToString()}Changes.txt"; Dictionary<string, string> orderedLocalFileChanges = localFileChanges.OrderBy(x => x.Key).ToDictionary(d => d.Key, d => d.Value); _consoleManager.WritePaddedText("The following changes were made to the local wiki files: "); using (StreamWriter streamWriter = File.CreateText(localChangesFilePath)) { foreach (string change in orderedLocalFileChanges.Values) { _consoleManager.WriteTextLine(change); streamWriter.WriteLine(change); } } }
/// <summary> /// Orders the clients map /// </summary> public Dictionary<UInt64, string> order(ConcurrentDictionary<UInt64, string> cDict) { IEnumerable<KeyValuePair<UInt64, string>> orderedMap = cDict.OrderBy(element => element.Key); Dictionary<UInt64, string> dict = orderedMap.ToDictionary(el => el.Key, el => el.Value); return dict; }
/// <summary> /// Manipulates the input strings to existing or new entities. /// Then it passes those entities to SuggestEntities to generate suggestions. /// <note>This method is only used for the initial step of the Import. Once entites are generated; use SuggestEntities</note> /// </summary> /// <param name="rowsWithHeaders">List of string[]. The first string[] is the headers, the rest are data to be imported</param> /// <returns>Entites with suggestions</returns> public Suggestions ValidateThenSuggestEntities(List<string[]> rowsWithHeaders) { var headers = rowsWithHeaders[0]; rowsWithHeaders.RemoveAt(0); var rows = rowsWithHeaders; #region Assign Column placement //Client var clientNameCol = Array.IndexOf(headers, "Client Name"); //Location var addressLineOneCol = Array.IndexOf(headers, "Address Line One"); var addressLineTwoCol = Array.IndexOf(headers, "Address Line Two"); var cityCol = Array.IndexOf(headers, "AdminDistrictTwo"); var stateCol = Array.IndexOf(headers, "AdminDistrictOne"); var zipCodeCol = Array.IndexOf(headers, "PostalCode"); var countryCodeCol = Array.IndexOf(headers, "Country Code"); var regionNameCol = Array.IndexOf(headers, "Region Name"); var latitudeCol = Array.IndexOf(headers, "Latitude"); var longitudeCol = Array.IndexOf(headers, "Longitude"); //Repeat var frequencyCol = Array.IndexOf(headers, "Frequency"); var repeatEveryCol = Array.IndexOf(headers, "Repeat Every"); var startDateCol = Array.IndexOf(headers, "Start Date"); var endDateCol = Array.IndexOf(headers, "End Date"); var endAfterCol = Array.IndexOf(headers, "End After Times"); var frequencyDetailCol = Array.IndexOf(headers, "Frequency Detail"); //Contact Info var phoneNumberValueCols = headers.Where(h => h.Contains("Phone Value")).ToArray(); var phoneNumberLabelCols = headers.Where(h => h.Contains("Phone Label")).ToArray(); var emailValueCols = headers.Where(h => h.Contains("Email Value")).ToArray(); var emailLabelCols = headers.Where(h => h.Contains("Email Label")).ToArray(); var websiteValueCols = headers.Where(h => h.Contains("Website Value")).ToArray(); var websiteLabelCols = headers.Where(h => h.Contains("Website Label")).ToArray(); var otherValueCols = headers.Where(h => h.Contains("Other Value")).ToArray(); var otherLabelCols = headers.Where(h => h.Contains("Other Label")).ToArray(); #endregion var importRowsConcurrentDictionary = new ConcurrentDictionary<int, ImportRow>(); var rowCount = rows.Count(); Parallel.For((long)0, rowCount, rowIndex => { var row = rows[(int)rowIndex]; var importRow = new ImportRow(); #region Location var importedLocation = new Api.Models.Location(); string clientName; //Checks if at least one location column is passed if (new[] { addressLineOneCol, addressLineTwoCol, cityCol, stateCol, countryCodeCol, zipCodeCol, latitudeCol, longitudeCol }.Any(col => col != -1)) { var latitude = latitudeCol != -1 ? row[latitudeCol] : null; var longitude = longitudeCol != -1 ? row[longitudeCol] : null; var addressLineOne = addressLineOneCol != -1 ? row[addressLineOneCol] : null; var addressLineTwo = addressLineTwoCol != -1 ? row[addressLineTwoCol] : null; var adminDistrictTwo = cityCol != -1 ? row[cityCol] : null; var adminDistrictOne = stateCol != -1 ? row[stateCol] : null; var postalCode = zipCodeCol != -1 ? row[zipCodeCol] : null; var regionName = regionNameCol != -1 ? row[regionNameCol] : null; //If lat/lon dont have values, try to Geocode if (latitude == null && longitude == null) { var address = new Address { AddressLineOne = addressLineOne, City = adminDistrictTwo, State = adminDistrictOne, ZipCode = postalCode }; //Attempt to Geocode the address var geocodeResult = BingLocationServices.TryGeocode(address).FirstOrDefault(gc => gc != null); latitude = geocodeResult != null ? geocodeResult.Latitude : null; longitude = geocodeResult != null ? geocodeResult.Longitude : null; //If they still do not have values, throw error on the location if (latitude == null && longitude == null) importedLocation.StatusInt = (int)ImportStatus.Error; //Matched the entire address to a location (AddressLineOne, AddressLineTwo, City, State and ZipCode) var matchedLocation = _locations.FirstOrDefault(l => l.Value.AddressLineOne == addressLineOne && l.Value.AddressLineTwo == addressLineTwo && l.Value.AdminDistrictTwo == adminDistrictTwo && l.Value.AdminDistrictOne == adminDistrictOne && l.Value.PostalCode == postalCode); if (matchedLocation.Key != Guid.Empty) importedLocation = ConvertLocationSetRegionAndStatus(matchedLocation.Value, regionName, ImportStatus.Linked); else { clientName = clientNameCol != -1 ? row[clientNameCol] : null; //Matched the address entered and client name to a location matchedLocation = _locations.FirstOrDefault(l => clientName != null && l.Value.ClientId != null && (addressLineTwo != null && (l.Value.AddressLineOne == addressLineOne && l.Value.AddressLineTwo == addressLineTwo && _clients.First(c => c.Key == l.Value.ClientId).Value.Name == clientName))); if (matchedLocation.Key != Guid.Empty) importedLocation = ConvertLocationSetRegionAndStatus(matchedLocation.Value, regionName, ImportStatus.Linked); } } //If lat/lon exist, try and match based on that if (latitude != null && longitude != null && importedLocation.StatusInt != (int)ImportStatus.Error) { var setError = false; //check for invalid deciamls Decimal convertedLatitude = 0; Decimal convertedLongitude = 0; Decimal tempdeciaml; if (Decimal.TryParse(latitude, out tempdeciaml)) convertedLatitude = tempdeciaml; else setError = true; if (Decimal.TryParse(longitude, out tempdeciaml)) convertedLongitude = tempdeciaml; else setError = true; if (!setError) { var roundedLatitude = Math.Round(convertedLatitude, 6); var roundedLongitude = Math.Round(convertedLongitude, 6); //Try and match a location to one in the FoundOPS system var matchedLocation = addressLineTwo != null //Use this statement if Address Line Two is not null ? _locations.FirstOrDefault(l => l.Value.AddressLineTwo == addressLineTwo && (l.Value.Latitude != null && l.Value.Longitude != null) && (decimal.Round(l.Value.Latitude.Value, 6) == roundedLatitude && decimal.Round(l.Value.Longitude.Value, 6) == roundedLongitude)) //Use this statement if Address Line Two is null : _locations.FirstOrDefault(l => (l.Value.Latitude != null && l.Value.Longitude != null) && (decimal.Round(l.Value.Latitude.Value, 6) == roundedLatitude && decimal.Round(l.Value.Longitude.Value, 6) == roundedLongitude)); //If a match is found, assign Linked status to all cells if (matchedLocation.Key != Guid.Empty) importedLocation = ConvertLocationSetRegionAndStatus(matchedLocation.Value, regionName, ImportStatus.Linked); } else importedLocation.StatusInt = (int) ImportStatus.Error; } //If no linked location is found, it means that the location is new if (importedLocation.StatusInt != (int)ImportStatus.Linked) { var status = (int) ImportStatus.New; if (importedLocation.StatusInt == (int) ImportStatus.Error) status = (int) ImportStatus.Error; importedLocation = new Api.Models.Location { Id = Guid.NewGuid(), AddressLineOne = addressLineOne, AddressLineTwo = addressLineTwo ?? null, AdminDistrictTwo = adminDistrictTwo, AdminDistrictOne = adminDistrictOne, PostalCode = postalCode, CountryCode = "US", ContactInfoSet = new List<ContactInfo>(), Latitude = latitude, Longitude = longitude, StatusInt = status, Region = SetRegion(regionName) }; //Since the location is new, we add it to the list of locations to be searched next time we try and match _locations.GetOrAdd(importedLocation.Id, FoundOps.Api.Models.Location.ConvertBack(importedLocation)); } importRow.Location = importedLocation; } #endregion #region Client clientName = clientNameCol != -1 ? row[clientNameCol] : null; //Make sure a Client is being Imported if (clientName != null) { //Find an existing Client based on the Name var existingClient = _clients.FirstOrDefault(c => c.Value.Name == clientName); Models.Client importedClient; //If a Client was found, set it to be imported with a Linked Status if (existingClient.Key != Guid.Empty) { importedClient = FoundOps.Api.Models.Client.ConvertModel(existingClient.Value); importedClient.StatusInt = (int)ImportStatus.Linked; } //If not, create a new Client and add it to _clients for matching later else { importedClient = new FoundOps.Api.Models.Client { Id = Guid.NewGuid(), Name = clientName, ContactInfoSet = new List<ContactInfo>(), StatusInt = (int)ImportStatus.New }; //Since the client is new, we add it to the list of clients to be searched next time we try and match _clients.GetOrAdd(importedClient.Id, FoundOps.Api.Models.Client.ConvertBack(importedClient)); } importRow.Client = importedClient; } #endregion #region Repeat if (new[] { startDateCol, endDateCol, endAfterCol, repeatEveryCol, frequencyCol, frequencyDetailCol }.Any(col => col != -1)) { var setError = false; //check for invalid DateTime's DateTime startDate = DateTime.UtcNow.Date; DateTime? endDate = null; DateTime tempDate; if (startDateCol != -1 && !String.IsNullOrEmpty(row[startDateCol])) { if (DateTime.TryParse(row[startDateCol], out tempDate)) startDate = tempDate; else { startDate = DateTime.UtcNow.Date; setError = true; } } if (endDateCol != -1 && !String.IsNullOrEmpty(row[endDateCol])) { if (DateTime.TryParse(row[endDateCol], out tempDate)) endDate = tempDate; else { endDate = null; setError = true; } } //Check for invalid Int's int? endAfterTimes = null; int? repeatEveryTimes = null; int tempInt; if (endAfterCol != -1 && !String.IsNullOrEmpty(row[endAfterCol])) { if (Int32.TryParse(row[endAfterCol], out tempInt)) endAfterTimes = tempInt; else { endAfterTimes = null; setError = true; } } if (repeatEveryCol != -1 && !String.IsNullOrEmpty(row[repeatEveryCol])) { if (Int32.TryParse(row[repeatEveryCol], out tempInt)) repeatEveryTimes = tempInt; else { repeatEveryTimes = null; setError = true; } } //Create the Repeat object var repeat = new Repeat { Id = Guid.NewGuid(), StartDate = startDate, EndDate = endDate, EndAfterTimes = endAfterTimes, RepeatEveryTimes = repeatEveryTimes }; #region Frequency var val = frequencyCol != -1 ? row[frequencyCol].ToLower() : String.Empty; switch (val) { case "o": case "once": repeat.FrequencyInt = (int)Frequency.Once; break; case "d": case "daily": repeat.FrequencyInt = (int)Frequency.Daily; break; case "w": case "weekly": repeat.FrequencyInt = (int)Frequency.Weekly; break; case "m": case "monthly": repeat.FrequencyInt = (int)Frequency.Monthly; break; case "y": case "yearly": repeat.FrequencyInt = (int)Frequency.Yearly; break; default: repeat.FrequencyInt = null; break; } #endregion #region Frequency Detail val = frequencyDetailCol != -1 ? row[frequencyDetailCol].ToLower() : String.Empty; val = val.Replace(" ", ""); if (repeat.Frequency == Frequency.Weekly) { var startDayOfWeek = repeat.StartDate.DayOfWeek; //If it is empty assume the Start Date if (string.IsNullOrEmpty(val)) repeat.FrequencyDetailAsWeeklyFrequencyDetail = new[] { startDayOfWeek }; else { var dayStrings = val.Split(','); var daysOfWeek = new List<DayOfWeek>(); if (dayStrings.Any(s => s == "s" || s == "su" || s == "sun" || s == "sunday")) daysOfWeek.Add(DayOfWeek.Sunday); if (dayStrings.Any(s => s == "m" || s == "mo" || s == "mon" || s == "monday")) daysOfWeek.Add(DayOfWeek.Monday); if (dayStrings.Any(s => s == "t" || s == "tu" || s == "tue" || s == "tues" || s == "tuesday")) daysOfWeek.Add(DayOfWeek.Tuesday); if (dayStrings.Any(s => s == "w" || s == "we" || s == "wed" || s == "wednesday")) daysOfWeek.Add(DayOfWeek.Wednesday); if (dayStrings.Any(s => s == "r" || s == "th" || s == "tr" || s == "thur" || s == "thurs" || s == "thursday")) daysOfWeek.Add(DayOfWeek.Thursday); if (dayStrings.Any(s => s == "f" || s == "fr" || s == "fri" || s == "friday")) daysOfWeek.Add(DayOfWeek.Friday); if (dayStrings.Any(s => s == "s" || s == "sa" || s == "sat" || s == "saturday")) daysOfWeek.Add(DayOfWeek.Saturday); //Make sure the days include the startdate if (!daysOfWeek.Contains(startDayOfWeek)) daysOfWeek.Add(startDayOfWeek); repeat.FrequencyDetailAsWeeklyFrequencyDetail = daysOfWeek.OrderBy(e => (int)e).ToArray(); } } if (repeat.Frequency == Frequency.Monthly) { if (string.IsNullOrEmpty(val) || val == "date") { repeat.FrequencyDetailAsMonthlyFrequencyDetail = MonthlyFrequencyDetail.OnDayInMonth; } else if (val == "day") { var detailsAvailable = repeat.AvailableMonthlyFrequencyDetailTypes.ToList(); if (detailsAvailable.Count() > 1) detailsAvailable.Remove(MonthlyFrequencyDetail.OnDayInMonth); repeat.FrequencyDetailAsMonthlyFrequencyDetail = detailsAvailable.First(); } } #endregion repeat.StatusInt = repeat.RepeatEveryTimes == null || repeat.FrequencyInt == null || setError ? (int)ImportStatus.Error : (int)ImportStatus.New; importRow.Repeat = repeat; } #endregion #region Contact Info //Create label and value dictionaries for Phone Number contact information var phoneValueDictionary = phoneNumberValueCols.ToDictionary(p => Convert.ToInt32(p.Split(' ').ElementAt(2)), p => row[Array.IndexOf(headers, p)]); var phoneLabelDictionary = phoneNumberLabelCols.ToDictionary(p => Convert.ToInt32(p.Split(' ').ElementAt(2)), p => row[Array.IndexOf(headers, p)]); //Create label and value dictionaries for Email Address contact information var emailValueDictionary = emailValueCols.ToDictionary(e => Convert.ToInt32(e.Split(' ').ElementAt(2)), e => row[Array.IndexOf(headers, e)]); var emailLabelDictionary = emailLabelCols.ToDictionary(e => Convert.ToInt32(e.Split(' ').ElementAt(2)), e => row[Array.IndexOf(headers, e)]); //Create label and value dictionaries for Website contact information var websiteValueDictionary = websiteValueCols.ToDictionary(w => Convert.ToInt32(w.Split(' ').ElementAt(2)), w => row[Array.IndexOf(headers, w)]); var websiteLabelDictionary = websiteLabelCols.ToDictionary(w => Convert.ToInt32(w.Split(' ').ElementAt(2)), w => row[Array.IndexOf(headers, w)]); //Create label and value dictionaries for any other types of contact information var otherValueDictionary = otherValueCols.ToDictionary(o => Convert.ToInt32(o.Split(' ').ElementAt(2)), o => row[Array.IndexOf(headers, o)]); var otherLabelDictionary = otherLabelCols.ToDictionary(o => Convert.ToInt32(o.Split(' ').ElementAt(2)), o => row[Array.IndexOf(headers, o)]); //Find which type of contact info is being imported the most //This way we only have one loop var maxLabel = Math.Max(Math.Max(phoneLabelDictionary.Count, emailLabelDictionary.Count), Math.Max(websiteLabelDictionary.Count, otherLabelDictionary.Count)); var maxValue = Math.Max(Math.Max(phoneValueDictionary.Count, emailValueDictionary.Count), Math.Max(websiteValueDictionary.Count, otherValueDictionary.Count)); var max = Math.Max(maxLabel, maxValue); //Dictionary of Cantact Information to be imported for the row var concurrentContactInfoDictionary = new ConcurrentDictionary<Guid, ContactInfo>(); Parallel.For((long)1, max + 1, contactIndex => { //Phone if (phoneLabelDictionary.Count >= contactIndex || phoneValueDictionary.Count >= contactIndex) MatchContactInfo(phoneLabelDictionary, phoneValueDictionary, contactIndex, concurrentContactInfoDictionary, "Phone Number"); //Email if (emailLabelDictionary.Count >= contactIndex || emailValueDictionary.Count >= contactIndex) MatchContactInfo(emailLabelDictionary, emailValueDictionary, contactIndex, concurrentContactInfoDictionary, "Email Address"); //Website if (websiteLabelDictionary.Count >= contactIndex || websiteValueDictionary.Count >= contactIndex) MatchContactInfo(websiteLabelDictionary, websiteValueDictionary, contactIndex, concurrentContactInfoDictionary, "Website"); //Other if (otherLabelDictionary.Count >= contactIndex || otherValueDictionary.Count >= contactIndex) MatchContactInfo(otherLabelDictionary, otherValueDictionary, contactIndex, concurrentContactInfoDictionary, "Other"); }); //Once all the contact info sets are made or matched, add them to the ImportRow importRow.ContactInfoSet.AddRange(concurrentContactInfoDictionary.Select(ci => ci.Value)); #endregion //Add the ImportRow to the concurrent dictionary of ImportRows importRowsConcurrentDictionary.GetOrAdd((int)rowIndex, importRow); }); //Order the ImportRows by rowIndex var importRows = importRowsConcurrentDictionary.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value).ToArray(); //Send the validated ImportRows to get suggestions and return return SuggestEntites(importRows); }
private string[] SortFilesByLineNumbers( ConcurrentDictionary<string, int> files) { var sortedFiles = new string[files.Count]; var count = 0; foreach(var file in files.OrderBy(x => x.Value)) sortedFiles[count++] = file.Key; return sortedFiles; }
/// <summary> /// Suggests other potential options based on the entities passed /// </summary> /// <param name="rows">The rows being imported</param> /// <returns>Entites with suggestions</returns> public Suggestions SuggestEntites(ImportRow[] rows) { var rowSuggestionsConcurrentDictionary = new ConcurrentDictionary<int, RowSuggestions>(); var suggestionsToReturn = new Suggestions(); var clients = new ConcurrentDictionary<Guid, FoundOps.Api.Models.Client>(); var locations = new ConcurrentDictionary<Guid, FoundOps.Api.Models.Location>(); var contactInfoSets = new ConcurrentDictionary<Guid, FoundOps.Api.Models.ContactInfo>(); Parallel.For((long)0, rows.Count(), rowIndex => { var row = rows[rowIndex]; var rowSuggestions = new RowSuggestions(); #region Location if (row.Location != null) { //Find all the Locations to be suggested by finding all Locations for the Client of the row var locationSuggestions = row.Client != null ? _locations.Where(l => l.Value.ClientId == row.Client.Id).ToArray() : null; if (locationSuggestions != null) { //Add any of the suggestions to the rows suggestions rowSuggestions.LocationSuggestions.AddRange(locationSuggestions.Select(l => l.Key)); var convertedLocationSuggestions = locationSuggestions.Select(l => l.Value).Select(FoundOps.Api.Models.Location.ConvertModel); //Add all suggested Locations to the list of Locations to be returned foreach (var location in convertedLocationSuggestions) locations.GetOrAdd(location.Id, location); } //Add the matched/new location as the first suggestion rowSuggestions.LocationSuggestions.Add(row.Location.Id); //Add the location passed to the list of location entites locations.GetOrAdd(row.Location.Id, row.Location); } #endregion #region Client if (row.Client != null) { //Find all the Clients to be suggested by finding all Clients for the Location of the row var clientSuggestions = row.Location != null ? _clients.Where(c => c.Key == row.Location.ClientId).ToArray() : null; if (clientSuggestions != null) { //Add any of the suggestions to the rows suggestions rowSuggestions.ClientSuggestions.AddRange(clientSuggestions.Select(c => c.Key)); var convertedClientSuggestions = clientSuggestions.Select(c => c.Value).Select(FoundOps.Api.Models.Client.ConvertModel); //Add all suggested Clients to the list of Clients to be returned foreach (var client in convertedClientSuggestions) clients.GetOrAdd(client.Id, client); } //Add the matched/new client as the first suggestion rowSuggestions.ClientSuggestions.Add(row.Client.Id); //Add the Client passed to the list of client entites clients.GetOrAdd(row.Client.Id, row.Client); } #endregion //Repeat if (row.Repeat != null) rowSuggestions.Repeats.Add(row.Repeat); //Contact Info if (row.ContactInfoSet.Count != 0) { rowSuggestions.ContactInfoSuggestions.AddRange(row.ContactInfoSet.Select(ci => ci.Id)); foreach (var contactInfoSet in row.ContactInfoSet) contactInfoSets.GetOrAdd(contactInfoSet.Id, contactInfoSet); } //Add this row's suggestions to the list to be returned rowSuggestionsConcurrentDictionary.GetOrAdd((int)rowIndex, rowSuggestions); }); //Order the row suggestions by rowIndex suggestionsToReturn.RowSuggestions.AddRange(rowSuggestionsConcurrentDictionary.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value)); //Only add distinct Clients var distinctClients = clients.Distinct(); suggestionsToReturn.Clients.AddRange(distinctClients.Select(dc => dc.Value)); //Only add distinct Locations var distinctLocations = locations.Distinct(); suggestionsToReturn.Locations.AddRange(distinctLocations.Select(dl => dl.Value)); //Only add distinct ContactInfo var distinctContactInfo = contactInfoSets.Select(ci => ci.Value).Distinct(); suggestionsToReturn.ContactInfoSet.AddRange(distinctContactInfo); return suggestionsToReturn; }
public async Task<HitsCountResponse> GetHitsCount() { // Get the list of representative service partition clients. var partitionClients = await this.GetServicePartitionClientsAsync(); // For each partition client, keep track of partition information and the number of words var totals = new ConcurrentDictionary<Int64RangePartitionInformation, Task<long>>(); IList<Task> tasks = new List<Task>(partitionClients.Count); foreach (var partitionClient in partitionClients) { // partitionClient internally resolves the address and retries on transient errors based on the configured retry policy. Task<long> tt = partitionClient.beanCache.GetHitsCount(); tasks.Add(tt); totals[partitionClient.part as Int64RangePartitionInformation] = tt; } try { await Task.WhenAll(tasks); } catch (Exception ex) { // Sample code: print exception ServiceEventSource.Current.Message(ex.Message, "Count - run web request"); throw ex; } var response = new HitsCountResponse(); response.Total = totals.Aggregate(0, (total, next) => (int)next.Value.Result + total); foreach (var partitionData in totals.OrderBy(partitionData => partitionData.Key.LowKey)) { var cachInfo = new CacheInfo(); cachInfo.Id = partitionData.Key.Id; cachInfo.LowKey = partitionData.Key.LowKey; cachInfo.HighKey = partitionData.Key.HighKey; cachInfo.Hits = partitionData.Value.Result; response.CacheInfo.Add(cachInfo); } return response; }
public async Task<CountResponse> Count() { // Get the list of representative service partition clients. IList<ServicePartitionClient<CommunicationClient>> partitionClients = await this.GetServicePartitionClientsAsync(); // For each partition client, keep track of partition information and the number of words ConcurrentDictionary<Int64RangePartitionInformation, long> totals = new ConcurrentDictionary<Int64RangePartitionInformation, long>(); IList<Task> tasks = new List<Task>(partitionClients.Count); foreach (ServicePartitionClient<CommunicationClient> partitionClient in partitionClients) { // partitionClient internally resolves the address and retries on transient errors based on the configured retry policy. tasks.Add( partitionClient.InvokeWithRetryAsync( client => { Uri serviceAddress = new Uri(client.BaseAddress, "Count"); HttpWebRequest request = WebRequest.CreateHttp(serviceAddress); request.Method = "GET"; request.Timeout = (int)client.OperationTimeout.TotalMilliseconds; request.ReadWriteTimeout = (int)client.ReadWriteTimeout.TotalMilliseconds; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) using (StreamReader reader = new StreamReader(response.GetResponseStream())) { totals[client.ResolvedServicePartition.Info as Int64RangePartitionInformation] = Int64.Parse(reader.ReadToEnd().Trim()); } return Task.FromResult(true); })); } try { await Task.WhenAll(tasks); } catch (Exception ex) { // Sample code: print exception ServiceEventSource.Current.OperationFailed(ex.Message, "Count - run web request"); } var retVal = new CountResponse(); retVal.Total = totals.Aggregate<KeyValuePair<Int64RangePartitionInformation, long>, long>(0, (total, next) => next.Value + total); foreach (KeyValuePair<Int64RangePartitionInformation, long> partitionData in totals.OrderBy(partitionData => partitionData.Key.LowKey)) { var info = new Info(); info.Id = partitionData.Key.Id; info.LowKey = partitionData.Key.LowKey; info.HighKey = partitionData.Key.HighKey; info.Hits = partitionData.Value; retVal.Infos.Add(info); } return retVal; }
public async Task<HttpResponseMessage> Count() { // Get the list of representative service partition clients. IList<ServicePartitionClient<CommunicationClient>> partitionClients = await this.GetServicePartitionClientsAsync(); // For each partition client, keep track of partition information and the number of words ConcurrentDictionary<Int64RangePartitionInformation, long> totals = new ConcurrentDictionary<Int64RangePartitionInformation, long>(); IList<Task> tasks = new List<Task>(partitionClients.Count); foreach (ServicePartitionClient<CommunicationClient> partitionClient in partitionClients) { // partitionClient internally resolves the address and retries on transient errors based on the configured retry policy. tasks.Add( partitionClient.InvokeWithRetryAsync( client => { Uri serviceAddress = new Uri(client.BaseAddress, "Count"); HttpWebRequest request = WebRequest.CreateHttp(serviceAddress); request.Method = "GET"; request.Timeout = (int) client.OperationTimeout.TotalMilliseconds; request.ReadWriteTimeout = (int) client.ReadWriteTimeout.TotalMilliseconds; using (HttpWebResponse response = (HttpWebResponse) request.GetResponse()) using (StreamReader reader = new StreamReader(response.GetResponseStream())) { totals[client.ResolvedServicePartition.Info as Int64RangePartitionInformation] = Int64.Parse(reader.ReadToEnd().Trim()); } return Task.FromResult(true); })); } try { await Task.WhenAll(tasks); } catch (Exception ex) { // Sample code: print exception ServiceEventSource.Current.OperationFailed(ex.Message, "Count - run web request"); } StringBuilder sb = new StringBuilder(); sb.Append("<h1> Total:"); sb.Append(totals.Aggregate<KeyValuePair<Int64RangePartitionInformation, long>, long>(0, (total, next) => next.Value + total)); sb.Append("</h1>"); sb.Append("<table><tr><td>Partition ID</td><td>Key Range</td><td>Total</td></tr>"); foreach (KeyValuePair<Int64RangePartitionInformation, long> partitionData in totals.OrderBy(partitionData => partitionData.Key.LowKey)) { sb.Append("<tr><td>"); sb.Append(partitionData.Key.Id); sb.Append("</td><td>"); sb.AppendFormat("{0} - {1}", partitionData.Key.LowKey, partitionData.Key.HighKey); sb.Append("</td><td>"); sb.Append(partitionData.Value); sb.Append("</td></tr>"); } sb.Append("</table>"); HttpResponseMessage message = new HttpResponseMessage(); message.Content = new StringContent(sb.ToString(), Encoding.UTF8, "text/html"); return message; }