/// <summary> /// Performs the query override. /// </summary> /// <param name="indexTypeMapping">The index type mapping.</param> /// <param name="query">The query.</param> /// <param name="messageContext">The message context.</param> private void PerformQueryOverride(IndexTypeMapping indexTypeMapping, BaseMultiIndexIdQuery <PagedIndexQueryResult> query, MessageContext messageContext) { PagedIndexQuery pagedQuery = query as PagedIndexQuery; if (indexTypeMapping.QueryOverrideSettings != null) { // override MaxItemsPerIndexThreshold if required if (indexTypeMapping.QueryOverrideSettings.MaxItemsPerIndexThreshold != 0 && pagedQuery.IndexIdList.Count > 1 && pagedQuery.PageNum == 0 && (pagedQuery.IndexIdParamsMapping == null || pagedQuery.IndexIdParamsMapping.Count == 0) && pagedQuery.MaxItemsPerIndex == 0 && pagedQuery.Filter == null) { LoggingUtil.Log.ErrorFormat( "Encountered Potentially Bad Paged Query. Overriding MaxItemsPerIndex to {0}. AddressHistory {1}. Original Query Info: {2}", indexTypeMapping.QueryOverrideSettings.MaxItemsPerIndexThreshold, IndexServerUtils.FormatAddressHistory(messageContext.AddressHistory), FormatQueryInfo(pagedQuery)); pagedQuery.MaxItemsPerIndex = indexTypeMapping.QueryOverrideSettings.MaxItemsPerIndexThreshold; } // override PageNum if required if (indexTypeMapping.QueryOverrideSettings.DisableFullPageQuery && pagedQuery.PageNum == 0 && pagedQuery.PageSize != 0) { LoggingUtil.Log.InfoFormat( "Configuration rules require overriding PageNum to 1. AddressHistory {0}. Original Query Info: {1}", IndexServerUtils.FormatAddressHistory(messageContext.AddressHistory), FormatQueryInfo(pagedQuery)); pagedQuery.PageNum = 1; } } }
/// <summary> /// Processes the specified query. /// </summary> /// <param name="query">The query.</param> /// <param name="messageContext">The message context.</param> /// <param name="storeContext">The store context.</param> /// <returns>Query Result</returns> internal TQueryResult Process(BaseMultiIndexIdQuery <TQueryResult> query, MessageContext messageContext, IndexStoreContext storeContext) { TQueryResult result; List <ResultItem> resultItemList = new List <ResultItem>(); Dictionary <byte[], IndexHeader> indexIdIndexHeaderMapping = null; bool isTagPrimarySort = false; string sortFieldName = null; List <SortOrder> sortOrderList = null; int totalCount = 0; int additionalAvailableItemCount = 0; GroupByResult groupByResult = null; StringBuilder exceptionInfo = new StringBuilder(); int indexCap = 0; IndexTypeMapping indexTypeMapping = storeContext.StorageConfiguration.CacheIndexV3StorageConfig.IndexTypeMappingCollection[messageContext.TypeId]; try { if (query.IndexIdList.Count > 0) { #region Validate Query ValidateQuery(indexTypeMapping, query, messageContext); #endregion #region Set sort vars Index targetIndexInfo = indexTypeMapping.IndexCollection[query.TargetIndexName]; indexCap = targetIndexInfo.MaxIndexSize; if (query.TagSort != null) { isTagPrimarySort = query.TagSort.IsTag; sortFieldName = query.TagSort.TagName; sortOrderList = new List <SortOrder>(1) { query.TagSort.SortOrder }; } else { isTagPrimarySort = targetIndexInfo.PrimarySortInfo.IsTag; sortFieldName = targetIndexInfo.PrimarySortInfo.FieldName; sortOrderList = targetIndexInfo.PrimarySortInfo.SortOrderList; } BaseComparer baseComparer = new BaseComparer(isTagPrimarySort, sortFieldName, sortOrderList); groupByResult = new GroupByResult(baseComparer); #endregion #region Prepare ResultList CacheIndexInternal targetIndex; IndexIdParams indexIdParam; int maxExtractCount; byte[] metadata; MetadataPropertyCollection metadataPropertyCollection; Dictionary <KeyValuePair <byte[], string>, CacheIndexInternal> internalIndexDictionary = new Dictionary <KeyValuePair <byte[], string>, CacheIndexInternal>(); int maxMergeCount = query.MaxMergeCount; IndexCondition queryIndexCondition = query.IndexCondition; for (int i = 0; i < query.IndexIdList.Count; i++) { #region Extract index and apply criteria indexIdParam = query.GetParamsForIndexId(query.IndexIdList[i]); maxExtractCount = ComputeMaxExtractCount(indexIdParam.MaxItems, query.GetAdditionalAvailableItemCount, indexIdParam.Filter, query.MaxMergeCount); // 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, query.IndexIdList[i], storeContext, out metadata, out metadataPropertyCollection); } targetIndex = IndexServerUtils.GetCacheIndexInternal(storeContext, messageContext.TypeId, (query.PrimaryIdList != null && i < query.PrimaryIdList.Count) ? query.PrimaryIdList[i] : IndexCacheUtils.GeneratePrimaryId(query.IndexIdList[i]), query.IndexIdList[i], targetIndexInfo.ExtendedIdSuffix, query.TargetIndexName, maxExtractCount, indexIdParam.Filter, true, queryIndexCondition, false, false, targetIndexInfo.PrimarySortInfo, targetIndexInfo.LocalIdentityTagList, targetIndexInfo.StringHashCodeDictionary, query.CapCondition, targetIndexInfo.IsMetadataPropertyCollection, metadataPropertyCollection, query.DomainSpecificProcessingType, storeContext.DomainSpecificConfig, null, query.GroupBy, false); #endregion if (targetIndex != null) { totalCount += targetIndex.OutDeserializationContext.TotalCount; additionalAvailableItemCount += targetIndex.Count; internalIndexDictionary.Add(new KeyValuePair <byte[], string>(query.IndexIdList[i], query.TargetIndexName), targetIndex); SetItemCounter(messageContext.TypeId, targetIndex.OutDeserializationContext); #region Dynamic tag sort if (query.TagSort != null) { targetIndex.Sort(query.TagSort); } #endregion #region Get items from index and merge if (query.GroupBy == null) { MergeAlgo.MergeItemLists(ref resultItemList, CacheIndexInternalAdapter.GetResultItemList(targetIndex, 1, int.MaxValue), query.MaxMergeCount, baseComparer); } else { MergeAlgo.MergeGroupResult(ref groupByResult, targetIndex.GroupByResult, query.MaxMergeCount, baseComparer); } if ((i != query.IndexIdList.Count - 1) && (resultItemList.Count == maxMergeCount)) { AdjustIndexCondition(GetConditionBoundaryBytes(resultItemList, groupByResult, query.GroupBy, isTagPrimarySort, sortFieldName), ref queryIndexCondition, baseComparer); } #endregion } } #endregion #region Subset Processing ProcessSubsets(query, ref resultItemList, ref groupByResult, baseComparer); #endregion #region Get Extra Tags for IndexIds in the list //Note: Getting extra tags from GroupByResult not supported for now if (query.TagsFromIndexes != null && query.TagsFromIndexes.Count != 0) { KeyValuePair <byte[] /*IndexId */, string /*IndexName*/> kvp; CacheIndexInternal additionalCacheIndexInternal; #region Form IndexId - PrimaryId Mapping Dictionary <byte[] /*IndexId */, int /*PrimaryId*/> indexIdPrimaryIdMapping = new Dictionary <byte[] /*IndexId */, int /*PrimaryId*/>(query.IndexIdList.Count, new ByteArrayEqualityComparer()); if (query.PrimaryIdList != null && query.PrimaryIdList.Count > 0) { //Form dictionary of IndexIdPrimaryIdMapping for (int i = 0; i < query.IndexIdList.Count && i < query.PrimaryIdList.Count; i++) { indexIdPrimaryIdMapping.Add(query.IndexIdList[i], query.PrimaryIdList[i]); } } #endregion int indexPrimaryId; foreach (ResultItem resultItem in resultItemList) { foreach (string indexName in query.TagsFromIndexes) { Index indexInfo = indexTypeMapping.IndexCollection[indexName]; kvp = new KeyValuePair <byte[], string>(resultItem.IndexId, indexName); if (!internalIndexDictionary.ContainsKey(kvp)) { additionalCacheIndexInternal = IndexServerUtils.GetCacheIndexInternal(storeContext, messageContext.TypeId, indexIdPrimaryIdMapping.TryGetValue(resultItem.IndexId, out indexPrimaryId) ? indexPrimaryId : IndexCacheUtils.GeneratePrimaryId(resultItem.IndexId), resultItem.IndexId, indexInfo.ExtendedIdSuffix, indexName, 0, null, true, null, false, false, indexInfo.PrimarySortInfo, indexInfo.LocalIdentityTagList, indexInfo.StringHashCodeDictionary, null, indexInfo.IsMetadataPropertyCollection, null, query.DomainSpecificProcessingType, storeContext.DomainSpecificConfig, null, null, false); if (additionalCacheIndexInternal != null) { SetItemCounter(messageContext.TypeId, additionalCacheIndexInternal.OutDeserializationContext); internalIndexDictionary.Add(kvp, additionalCacheIndexInternal); try { IndexServerUtils.GetTags(additionalCacheIndexInternal, resultItem, resultItem); } catch (Exception ex) { LoggingUtil.Log.Error(ex.ToString()); exceptionInfo.Append(" | " + ex.Message); } } } } } } #endregion #region Get IndexHeader if (query.GetIndexHeaderType == GetIndexHeaderType.AllIndexIds) { //Get IndexHeader for all IndexIds indexIdIndexHeaderMapping = new Dictionary <byte[], IndexHeader>(new ByteArrayEqualityComparer()); for (int i = 0; i < query.IndexIdList.Count; i++) { byte[] indexId = query.IndexIdList[i]; CacheIndexInternal targetIndexCacheIndexInternal; if (!indexIdIndexHeaderMapping.ContainsKey(indexId) && internalIndexDictionary.TryGetValue(new KeyValuePair <byte[], string>(indexId, query.TargetIndexName), out targetIndexCacheIndexInternal)) { indexIdIndexHeaderMapping.Add(indexId, GetIndexHeader(internalIndexDictionary, targetIndexCacheIndexInternal, indexId, query, indexTypeMapping, messageContext.TypeId, storeContext, i)); } } } else if (query.GetIndexHeaderType == GetIndexHeaderType.ResultItemsIndexIds) { //Get IndexHeader just for IndexIds present in the result indexIdIndexHeaderMapping = new Dictionary <byte[], IndexHeader>(new ByteArrayEqualityComparer()); if (query.GroupBy == null) { for (int i = 0; i < resultItemList.Count; i++) { ResultItem resultItem = resultItemList[i]; if (!indexIdIndexHeaderMapping.ContainsKey(resultItem.IndexId)) { CacheIndexInternal targetIndexCacheIndexInternal; internalIndexDictionary.TryGetValue(new KeyValuePair <byte[], string>(resultItem.IndexId, query.TargetIndexName), out targetIndexCacheIndexInternal); indexIdIndexHeaderMapping.Add(resultItem.IndexId, GetIndexHeader(internalIndexDictionary, targetIndexCacheIndexInternal, resultItem.IndexId, query, indexTypeMapping, messageContext.TypeId, storeContext, i)); } } } else { foreach (ResultItemBag resultItemBag in groupByResult) { for (int i = 0; i < resultItemBag.Count; i++) { ResultItem resultItem = resultItemBag[i]; if (!indexIdIndexHeaderMapping.ContainsKey(resultItem.IndexId)) { CacheIndexInternal targetIndexCacheIndexInternal; internalIndexDictionary.TryGetValue(new KeyValuePair <byte[], string>(resultItem.IndexId, query.TargetIndexName), out targetIndexCacheIndexInternal); indexIdIndexHeaderMapping.Add(resultItem.IndexId, GetIndexHeader(internalIndexDictionary, targetIndexCacheIndexInternal, resultItem.IndexId, query, indexTypeMapping, messageContext.TypeId, storeContext, i)); } } } } } #endregion #region Get data if (!query.ExcludeData) { DataTierUtil.GetData(resultItemList, groupByResult, storeContext, messageContext, indexTypeMapping.FullDataIdFieldList, query.FullDataIdInfo); } #endregion } result = new TQueryResult { ResultItemList = resultItemList, IndexIdIndexHeaderMapping = indexIdIndexHeaderMapping, TotalCount = totalCount, AdditionalAvailableItemCount = additionalAvailableItemCount, IsTagPrimarySort = isTagPrimarySort, SortFieldName = sortFieldName, SortOrderList = sortOrderList, IndexCap = indexCap, GroupByResult = groupByResult, ExceptionInfo = exceptionInfo.ToString() }; #region Log Potentially Bad Queries if (indexTypeMapping.QueryOverrideSettings != null && indexTypeMapping.QueryOverrideSettings.MaxResultItemsThresholdLog > 0 && resultItemList != null && resultItemList.Count > indexTypeMapping.QueryOverrideSettings.MaxResultItemsThresholdLog) { LoggingUtil.Log.ErrorFormat("Encountered potentially Bad Paged Query with Large Result Set of {0}. AddressHistory: {1}. Query Info: {2}", resultItemList.Count, IndexServerUtils.FormatAddressHistory(messageContext.AddressHistory), FormatQueryInfo(query)); } LoggingUtil.Log.DebugFormat("QueryInfo: {0}, AddressHistory: {1}", FormatQueryInfo(query), IndexServerUtils.FormatAddressHistory(messageContext.AddressHistory)); #endregion SetIndexIdListCounter(messageContext.TypeId, query); } catch (Exception ex) { exceptionInfo.Append(" | " + ex.Message); result = new TQueryResult { ExceptionInfo = exceptionInfo.ToString() }; LoggingUtil.Log.ErrorFormat("TypeId {0} -- Error processing PagedIndexQuery : {1}", messageContext.TypeId, ex); } return(result); }