Ejemplo n.º 1
0
        /// <summary>
        /// Processes the specified intersection query.
        /// </summary>
        /// <param name="intersectionQuery">The intersection query.</param>
        /// <param name="messageContext">The message context.</param>
        /// <param name="storeContext">The store context.</param>
        /// <returns>IntersectionQueryResult</returns>
        internal static IntersectionQueryResult Process(IntersectionQuery intersectionQuery,
                                                        MessageContext messageContext,
                                                        IndexStoreContext storeContext)
        {
            //Fetch each index (assume all indexes are local) and perform intersection and return the results
            IntersectionQueryResult          intersectionQueryResult;
            List <IndexDataItem>             resultItemList            = null;
            Dictionary <byte[], IndexHeader> indexIdIndexHeaderMapping = new Dictionary <byte[], IndexHeader>(new ByteArrayEqualityComparer());
            List <string>    localIdentityTagNames = null;
            bool             isTagPrimarySort      = false;
            string           sortFieldName         = null;
            List <SortOrder> sortOrderList         = null;
            StringBuilder    exceptionInfo         = new StringBuilder();

            try
            {
                if (intersectionQuery.IndexIdList.Count > 0)
                {
                    IndexTypeMapping indexTypeMapping =
                        storeContext.StorageConfiguration.CacheIndexV3StorageConfig.IndexTypeMappingCollection[messageContext.TypeId];
                    ValidateQuery(indexTypeMapping, intersectionQuery);

                    #region Set sort vars

                    Index targetIndexInfo = indexTypeMapping.IndexCollection[intersectionQuery.TargetIndexName];
                    localIdentityTagNames = indexTypeMapping.IndexCollection[intersectionQuery.TargetIndexName].LocalIdentityTagList;
                    bool    sortFieldPartOfLocalId = IsSortFieldPartOfLocalId(localIdentityTagNames, targetIndexInfo.PrimarySortInfo);
                    TagSort itemIdTagSort          = new TagSort("ItemId", false, new SortOrder(DataType.Int32, SortBy.ASC));

                    if (!sortFieldPartOfLocalId)
                    {
                        //Set sort vars
                        isTagPrimarySort = itemIdTagSort.IsTag;
                        sortFieldName    = itemIdTagSort.TagName;
                        sortOrderList    = new List <SortOrder>(1)
                        {
                            itemIdTagSort.SortOrder
                        };
                    }
                    else
                    {
                        isTagPrimarySort = targetIndexInfo.PrimarySortInfo.IsTag;
                        sortFieldName    = targetIndexInfo.PrimarySortInfo.FieldName;
                        sortOrderList    = targetIndexInfo.PrimarySortInfo.SortOrderList;
                    }

                    #endregion

                    #region Fetch CacheIndexInternal and Intersect

                    CacheIndexInternal      targetIndex;
                    CacheIndexInternal      resultCacheIndexInternal = null;
                    IntersectionQueryParams indexIdParam;
                    byte[] indexId;
                    byte[] metadata;
                    MetadataPropertyCollection metadataPropertyCollection;

                    for (int i = 0; i < intersectionQuery.IndexIdList.Count; i++)
                    {
                        #region Extract index and apply criteria

                        indexId      = intersectionQuery.IndexIdList[i];
                        indexIdParam = intersectionQuery.GetIntersectionQueryParamForIndexId(indexId);
                        // Note: This should be changed later and just extracted once if it is also requested in GetIndexHeader
                        metadata = null;
                        metadataPropertyCollection = null;
                        if (indexTypeMapping.MetadataStoredSeperately)
                        {
                            IndexServerUtils.GetMetadataStoredSeperately(indexTypeMapping,
                                                                         messageContext.TypeId,
                                                                         messageContext.PrimaryId,
                                                                         indexId,
                                                                         storeContext,
                                                                         out metadata,
                                                                         out metadataPropertyCollection);
                        }

                        targetIndex = IndexServerUtils.GetCacheIndexInternal(storeContext,
                                                                             messageContext.TypeId,
                                                                             (intersectionQuery.PrimaryIdList != null && i < intersectionQuery.PrimaryIdList.Count) ?
                                                                             intersectionQuery.PrimaryIdList[i] :
                                                                             IndexCacheUtils.GeneratePrimaryId(indexId),
                                                                             indexId,
                                                                             targetIndexInfo.ExtendedIdSuffix,
                                                                             intersectionQuery.TargetIndexName,
                                                                             indexIdParam.Count,
                                                                             indexIdParam.Filter,
                                                                             true,
                                                                             indexIdParam.IndexCondition,
                                                                             false,
                                                                             false,
                                                                             targetIndexInfo.PrimarySortInfo,
                                                                             targetIndexInfo.LocalIdentityTagList,
                                                                             targetIndexInfo.StringHashCodeDictionary,
                                                                             null,
                                                                             targetIndexInfo.IsMetadataPropertyCollection,
                                                                             metadataPropertyCollection,
                                                                             intersectionQuery.DomainSpecificProcessingType,
                                                                             storeContext.DomainSpecificConfig,
                                                                             null,
                                                                             null,
                                                                             false);

                        #endregion

                        if (targetIndex != null)
                        {
                            if (targetIndex.Count <= 0)
                            {
                                // No items in one of the indexes. Stop Interestion !!
                                resultCacheIndexInternal = null;
                                break;
                            }

                            PerformanceCounters.Instance.SetCounterValue(
                                PerformanceCounterEnum.NumOfItemsInIndexPerIntersectionQuery,
                                messageContext.TypeId,
                                targetIndex.OutDeserializationContext.TotalCount);

                            PerformanceCounters.Instance.SetCounterValue(
                                PerformanceCounterEnum.NumOfItemsReadPerIntersectionQuery,
                                messageContext.TypeId,
                                targetIndex.OutDeserializationContext.ReadItemCount);

                            if (!sortFieldPartOfLocalId)
                            {
                                //Need to sort indexes by item id
                                targetIndex.Sort(itemIdTagSort);
                            }

                            #region Intersection

                            if (resultCacheIndexInternal == null)
                            {
                                // No need to perform intersection for first index
                                resultCacheIndexInternal = targetIndex;
                            }
                            else
                            {
                                IntersectionAlgo.Intersect(isTagPrimarySort,
                                                           sortFieldName,
                                                           localIdentityTagNames,
                                                           sortOrderList,
                                                           resultCacheIndexInternal.InternalItemList,
                                                           targetIndex.InternalItemList,
                                                           intersectionQuery.MaxResultItems,
                                                           i == intersectionQuery.IndexIdList.Count - 1 ? intersectionQuery.IsSingleClusterQuery : false);

                                if (resultCacheIndexInternal == null || resultCacheIndexInternal.Count < 1)
                                {
                                    // Unable to fetch one of the indexes. Stop Interestion !!
                                    resultCacheIndexInternal  = null;
                                    indexIdIndexHeaderMapping = null;
                                    break;
                                }
                            }

                            #endregion
                        }
                        else
                        {
                            // Unable to fetch one of the indexes. Stop Interestion !!
                            resultCacheIndexInternal  = null;
                            indexIdIndexHeaderMapping = null;
                            break;
                        }

                        #region Get MetaData

                        if (intersectionQuery.GetIndexHeader)
                        {
                            if (!indexIdIndexHeaderMapping.ContainsKey(indexId))
                            {
                                indexIdIndexHeaderMapping.Add(indexId,
                                                              IndexServerUtils.GetIndexHeader(targetIndex, indexTypeMapping, messageContext.TypeId, IndexCacheUtils.GeneratePrimaryId(indexId), storeContext));
                            }
                        }

                        #endregion
                    }
                    if (resultCacheIndexInternal != null && resultCacheIndexInternal.Count > 0)
                    {
                        resultItemList = CacheIndexInternalAdapter.GetIndexDataItemList(resultCacheIndexInternal, 1, int.MaxValue);
                    }

                    #endregion

                    #region Get data

                    if (!intersectionQuery.ExcludeData && resultItemList != null && resultItemList.Count > 0)
                    {
                        DataTierUtil.GetData(resultItemList,
                                             storeContext,
                                             messageContext,
                                             indexTypeMapping.FullDataIdFieldList,
                                             intersectionQuery.FullDataIdInfo);
                    }

                    #endregion
                }

                intersectionQueryResult = new IntersectionQueryResult(resultItemList,
                                                                      indexIdIndexHeaderMapping,
                                                                      localIdentityTagNames,
                                                                      isTagPrimarySort,
                                                                      sortFieldName,
                                                                      sortOrderList,
                                                                      exceptionInfo.ToString());

                // update performance counter
                PerformanceCounters.Instance.SetCounterValue(
                    PerformanceCounterEnum.IndexLookupAvgPerIntersectionQuery,
                    messageContext.TypeId,
                    intersectionQuery.IndexIdList.Count);
            }
            catch (Exception ex)
            {
                exceptionInfo.Append(" | " + ex.Message);
                intersectionQueryResult = new IntersectionQueryResult(null, null, null, false, null, null, exceptionInfo.ToString());
                LoggingUtil.Log.ErrorFormat("TypeId {0} -- Error processing IntersectionQuery : {1}", messageContext.TypeId, ex);
            }
            return(intersectionQueryResult);
        }
        public TQueryResult MergeResults(IList <TQueryResult> partialResults)
        {
            TQueryResult completeResult = null;

            if (partialResults != null && partialResults.Count > 0)
            {
                if (partialResults.Count == 1)
                {
                    // No need to merge anything
                    completeResult = partialResults[0];
                }
                else
                {
                    completeResult = new TQueryResult();
                    StringBuilder exceptionStringBuilder = new StringBuilder();

                    #region Merge partialResults into completeResultList
                    ByteArrayEqualityComparer byteArrayEqualityComparer = new ByteArrayEqualityComparer();
                    Dictionary <byte[] /*IndexId*/, IndexHeader /*IndexHeader*/> completeIndexIdIndexHeaderMapping =
                        new Dictionary <byte[], IndexHeader>(byteArrayEqualityComparer);

                    for (int i = 0; i < partialResults.Count; i++)
                    {
                        TQueryResult partialResult = partialResults[i];
                        if (partialResult != null && partialResult.ResultItemList != null &&
                            partialResult.ResultItemList.Count > 0)
                        {
                            if (completeResult.ResultItemList == null || completeResult.ResultItemList.Count == 0)
                            {
                                completeResult = partialResult;
                            }
                            else
                            {
                                IntersectionAlgo.Intersect(
                                    partialResult.IsTagPrimarySort,
                                    partialResult.SortFieldName,
                                    partialResult.LocalIdentityTagNames,
                                    partialResult.SortOrderList,
                                    completeResult,
                                    partialResult,
                                    MaxResultItems,
                                    i == partialResults.Count - 1 ? true : false);

                                if (completeResult.ResultItemList == null || completeResult.ResultItemList.Count < 1)
                                {
                                    completeIndexIdIndexHeaderMapping = null;
                                    break;
                                }
                            }
                            if (GetIndexHeader && partialResult.IndexIdIndexHeaderMapping != null)
                            {
                                foreach (
                                    KeyValuePair <byte[], IndexHeader> kvp in partialResult.IndexIdIndexHeaderMapping)
                                {
                                    completeIndexIdIndexHeaderMapping.Add(kvp.Key, kvp.Value);
                                }
                            }
                        }
                        else
                        {
                            // Unable to fetch one of the indexes. Stop Interestion !!
                            completeResult.ResultItemList     = null;
                            completeIndexIdIndexHeaderMapping = null;

                            if ((partialResult != null) && (!String.IsNullOrEmpty(partialResult.ExceptionInfo)))
                            {
                                exceptionStringBuilder.Append(partialResult.ExceptionInfo);
                                exceptionStringBuilder.Append(" ");
                            }

                            break;
                        }
                    }

                    #region Create final result
                    completeResult.IndexIdIndexHeaderMapping = completeIndexIdIndexHeaderMapping;

                    if (exceptionStringBuilder.Length > 0)
                    {
                        completeResult.ExceptionInfo = exceptionStringBuilder.ToString();
                    }

                    #endregion

                    #endregion
                }
            }
            return(completeResult);
        }
Ejemplo n.º 3
0
 private void StartIntersection(int amount)
 {
     arrivedAtIntersection?.Invoke(IntersectionAlgo.Yeet(amount));
 }
        public TQueryResult MergeResults(IList <TQueryResult> partialResults)
        {
            TQueryResult completeResult = null;

            if (partialResults != null && partialResults.Count > 0)
            {
                if (partialResults.Count == 1)
                {
                    // No need to merge anything
                    completeResult = partialResults[0];
                }
                else
                {
                    completeResult = new TQueryResult();
                    #region Merge partialResults into completeResultList
                    ByteArrayEqualityComparer byteArrayEqualityComparer = new ByteArrayEqualityComparer();
                    Dictionary <byte[] /*IndexId*/, IndexHeader /*IndexHeader*/> completeIndexIdIndexHeaderMapping =
                        new Dictionary <byte[], IndexHeader>(byteArrayEqualityComparer);

                    foreach (TQueryResult partialResult in partialResults)
                    {
                        if (partialResult != null && partialResult.ResultItemList != null && partialResult.ResultItemList.Count > 0)
                        {
                            if (completeResult.ResultItemList == null || completeResult.ResultItemList.Count == 0)
                            {
                                completeResult = partialResult;
                            }
                            else
                            {
                                IntersectionAlgo.Intersect(
                                    partialResult.IsTagPrimarySort,
                                    partialResult.SortFieldName,
                                    partialResult.LocalIdentityTagNames,
                                    partialResult.SortOrderList,
                                    completeResult,
                                    partialResult);

                                if (completeResult.ResultItemList == null || completeResult.ResultItemList.Count < 1)
                                {
                                    completeIndexIdIndexHeaderMapping = null;
                                    break;
                                }
                            }
                            if (getIndexHeader && partialResult.IndexIdIndexHeaderMapping != null)
                            {
                                foreach (KeyValuePair <byte[], IndexHeader> kvp in partialResult.IndexIdIndexHeaderMapping)
                                {
                                    completeIndexIdIndexHeaderMapping.Add(kvp.Key, kvp.Value);
                                }
                            }
                        }
                        else
                        {
                            // Unable to fetch one of the indexes. Stop Interestion !!
                            completeResult.ResultItemList     = null;
                            completeIndexIdIndexHeaderMapping = null;
                            break;
                        }
                    }

                    #region Create final result
                    completeResult.IndexIdIndexHeaderMapping = completeIndexIdIndexHeaderMapping;
                    #endregion

                    #endregion
                }
            }
            return(completeResult);
        }