Exemplo n.º 1
0
        public PagedIndexQueryResult()
        {
            this.resultList = new List <TItem>();
            ByteArrayEqualityComparer byteArrayEqualityComparer = new ByteArrayEqualityComparer();

            this.resultMetadata         = new Dictionary <byte[] /*IndexId*/, byte[] /*metadata*/>(byteArrayEqualityComparer);
            this.totalCount             = 0;
            this.sortDescriptor         = null;
            this.cacheDataReferenceType = CacheDataReferenceTypes.CacheData;
        }
        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 Merge partialResults into completeResultList
                    List <TItem> completeResultList = new List <TItem>();
                    ByteArrayEqualityComparer byteArrayEqualityComparer = new ByteArrayEqualityComparer();
                    Dictionary <byte[] /*IndexId*/, byte[] /*metadata*/> completeMetadata =
                        new Dictionary <byte[] /*IndexId*/, byte[] /*metadata*/>(byteArrayEqualityComparer);
                    int totalCount = 0;

                    foreach (TQueryResult partialResult in partialResults)
                    {
                        if (partialResult != null)
                        {
                            totalCount += partialResult.TotalCount;
                            Merge(
                                ref completeResultList,
                                partialResult.ResultList,
                                partialResult.SortDescriptor,
                                PageSize,
                                partialResult.CacheDataReferenceType == CacheDataReferenceTypes.CacheDataReference ? true : false);

                            if (metadataRequested)
                            {
                                foreach (KeyValuePair <byte[], byte[]> kvp in partialResult.ResultMetadata)
                                {
                                    if (!completeMetadata.ContainsKey(kvp.Key))
                                    {
                                        completeMetadata.Add(kvp.Key, kvp.Value);
                                    }
                                }
                            }
                        }
                    }
                    #endregion

                    #region Use page logic
                    // Paging required only in multicluster configuration on client side
                    // Paging for single clustered configuration performed on server side
                    if (pageNum != 0)
                    {
                        List <TItem> filteredResultList = new List <TItem>();
                        int          pageSize           = (completeResultList.Count < PageSize ? completeResultList.Count : PageSize);
                        filteredResultList = completeResultList.GetRange(0, pageSize);
                    }
                    #endregion

                    #region Create final result
                    finalResult            = new TQueryResult();
                    finalResult.ResultList = completeResultList;
                    finalResult.TotalCount = totalCount;

                    if (metadataRequested)
                    {
                        Dictionary <byte[] /*IndexId*/, byte[] /*metadata*/> filteredMetadata =
                            new Dictionary <byte[] /*IndexId*/, byte[] /*metadata*/>(byteArrayEqualityComparer);
                        foreach (TItem cdr in finalResult.ResultList)
                        {
                            if (!filteredMetadata.ContainsKey(cdr.IndexId))
                            {
                                filteredMetadata.Add(cdr.IndexId, completeMetadata[cdr.IndexId]);
                            }
                        }
                        finalResult.ResultMetadata = filteredMetadata;
                    }
                    #endregion
                }
            }
            return(finalResult);
        }