示例#1
0
        public TQueryResult MergeResults(IList <TQueryResult> PartialResults)
        {
            TQueryResult finalResult = default(TQueryResult);

            if (PartialResults != null && PartialResults.Count > 0)
            {
                if (PartialResults.Count == 1)
                {
                    // no need to merge anything
                    finalResult = PartialResults[0];
                }
                else
                {
                    #region Need to merge

                    #region Iterate over PartialResults and create CompleteResults
                    List <CacheData> CompleteResults = new List <CacheData>();
                    int totalCount = 0;

                    foreach (TQueryResult partialResult in PartialResults)
                    {
                        if (partialResult != null)
                        {
                            totalCount += partialResult.TotalCount;
                            CompleteResults.AddRange(partialResult.CacheDataList);
                        }
                    }
                    #endregion

                    #region sort CompleteResults
                    CompleteResults.Sort(CacheDataComparison);
                    #endregion

                    #region apply cap restriction and get FilteredResults
                    List <CacheData> FilteredResults = new List <CacheData>();
                    if (Caps.Count > 0)
                    {
                        if (CompleteResults.Count > PageSize)
                        {
                            #region apply cap restrictions

                            Dictionary <int /*activity type*/, int /*count*/>          Occurance    = new Dictionary <int, int>();
                            List <Pair <int /*insert position*/, CacheData /*item*/> > SkippedItems = new List <Pair <int, CacheData> >();
                            int OccuranceCount;
                            int Cap;
                            for (int i = 0; i < CompleteResults.Count && FilteredResults.Count < PageSize; i++)
                            {
                                if (Caps.TryGetValue(CompleteResults[i].CacheTypeId, out Cap))
                                {
                                    #region Cap exists
                                    if (!Occurance.TryGetValue(CompleteResults[i].CacheTypeId, out OccuranceCount))
                                    {
                                        // first occurance, add it
                                        Occurance.Add(CompleteResults[i].CacheTypeId, 1);
                                        FilteredResults.Add(CompleteResults[i]);
                                    }
                                    else if (OccuranceCount < Cap)
                                    {
                                        // max not reached, add it
                                        Occurance[CompleteResults[i].CacheTypeId]++;
                                        FilteredResults.Add(CompleteResults[i]);
                                    }
                                    else
                                    {
                                        // skip it
                                        SkippedItems.Add(new Pair <int, CacheData>(FilteredResults.Count, CompleteResults[i]));
                                    }


                                    #endregion
                                }
                                else
                                {
                                    // No Cap - Just Add it
                                    FilteredResults.Add(CompleteResults[i]);
                                }
                            }

                            #region Expand FilteredCacheDataRefList to include SkippedItems if necessary
                            int pos = 0;
                            int skippedItemIndex = 0;
                            while (FilteredResults.Count < PageSize && skippedItemIndex < SkippedItems.Count)
                            {
                                FilteredResults.Insert(SkippedItems[skippedItemIndex].First + (pos++), SkippedItems[skippedItemIndex++].Second);
                            }
                            #endregion

                            #endregion
                        }
                        else
                        {
                            //else no need to cap
                            FilteredResults = CompleteResults;
                        }
                    }
                    else
                    {
                        #region use page logic
                        int pageSize = (CompleteResults.Count < PageSize ? CompleteResults.Count : PageSize);
                        FilteredResults = CompleteResults.GetRange(0, pageSize);
                        #endregion
                    }
                    #endregion

                    #region create finalResult
                    finalResult = new TQueryResult();
                    finalResult.CacheDataList = FilteredResults;
                    finalResult.TotalCount    = totalCount;
                    #endregion



                    #endregion
                }
            }

            return(finalResult);
        }