/// <summary> /// Determines whether repositioning of index item is required or not. /// </summary> /// <param name="indexInfo">The index info.</param> /// <param name="addItem">The add item.</param> /// <param name="internalItem">The internal item.</param> /// <returns> /// <c>true</c> if repositioning of index item is required; otherwise, <c>false</c>. /// </returns> private static bool IsRepositioningOfIndexItemRequired(Index indexInfo, IndexDataItem addItem, InternalItem internalItem) { byte[] sortTagValueInAddItem; if (indexInfo.PrimarySortInfo.IsTag && addItem.Tags.TryGetValue(indexInfo.PrimarySortInfo.FieldName, out sortTagValueInAddItem)) { // Index is sorted by tag and tag might have been updated byte[] sortTagValueInIndex; internalItem.TryGetTagValue(indexInfo.PrimarySortInfo.FieldName, out sortTagValueInIndex); if (!ByteArrayComparerUtil.CompareByteArrays(sortTagValueInAddItem, sortTagValueInIndex)) { return(true); } } return(false); }
/// <summary> /// Adds the new item. /// </summary> /// <param name="clientIndex">Index from the client.</param> /// <param name="cacheIndexInternal">The cache index internal.</param> /// <param name="indexInfo">The index info.</param> /// <param name="addItem">The add item.</param> /// <param name="storeContext">The store context.</param> /// <param name="comparer">The comparer.</param> private static void AddNewItem(CacheIndex clientIndex, CacheIndexInternal cacheIndexInternal, Index indexInfo, IndexDataItem addItem, IndexStoreContext storeContext, InternalItemComparer comparer) { int searchIndex; List <KeyValuePair <int, byte[]> > kvpList; searchIndex = cacheIndexInternal.GetInsertPosition(addItem, indexInfo.PrimarySortInfo.SortOrderList[0].SortBy, comparer); //Add item to CacheIndexInternal kvpList = null; if (addItem.Tags != null && addItem.Tags.Count > 0) { kvpList = new List <KeyValuePair <int, byte[]> >(); if (clientIndex.TargetIndexName != null) //Save to single index { foreach (KeyValuePair <string, byte[]> kvp in addItem.Tags) { //Add to tagHashCollection storeContext.TagHashCollection.AddTag(cacheIndexInternal.InDeserializationContext.TypeId, kvp.Key); kvpList.Add(new KeyValuePair <int, byte[]>(TagHashCollection.GetTagHashCode(kvp.Key), kvp.Value)); } } else //Save to multiple indexes { List <string> tagNameList; clientIndex.IndexTagMapping.TryGetValue(cacheIndexInternal.InDeserializationContext.IndexName, out tagNameList); foreach (string tagName in tagNameList) { //Add to tagHashCollection storeContext.TagHashCollection.AddTag(cacheIndexInternal.InDeserializationContext.TypeId, tagName); kvpList.Add(new KeyValuePair <int, byte[]>(TagHashCollection.GetTagHashCode(tagName), addItem.Tags[tagName])); } } } cacheIndexInternal.InsertItem(new InternalItem { ItemId = addItem.ItemId, TagList = kvpList }, searchIndex, true); }
/// <summary> /// Repositions the index item. /// </summary> /// <param name="cacheIndexInternal">The cache index internal.</param> /// <param name="indexInfo">The index info.</param> /// <param name="addItem">The add item.</param> /// <param name="searchIndex">Index of the search.</param> /// <param name="internalItem">The internal item.</param> /// <param name="comparer">The comparer.</param> private static void RepositionIndexItem(CacheIndexInternal cacheIndexInternal, Index indexInfo, IndexDataItem addItem, int searchIndex, InternalItem internalItem, InternalItemComparer comparer) { // Remove the Item from current position cacheIndexInternal.DeleteItem(searchIndex, false); // Determine where to insert the Item int newSearchIndex = cacheIndexInternal.GetInsertPosition(addItem, indexInfo.PrimarySortInfo.SortOrderList[0].SortBy, comparer); // insert the item at new position cacheIndexInternal.InsertItem(internalItem, newSearchIndex, false); }
/// <summary> /// Updates the existing index item. /// </summary> /// <param name="clientIndex">Index from the client.</param> /// <param name="cacheIndexInternal">The cache index internal.</param> /// <param name="indexInfo">The index info.</param> /// <param name="addItem">The add item.</param> /// <param name="searchIndex">Index to search.</param> /// <param name="storeContext">The store context.</param> /// <param name="comparer">The comparer.</param> private static void UpdateExistingIndexItem(CacheIndex clientIndex, CacheIndexInternal cacheIndexInternal, Index indexInfo, IndexDataItem addItem, int searchIndex, IndexStoreContext storeContext, InternalItemComparer comparer) { InternalItem internalItem = cacheIndexInternal.InternalItemList[searchIndex]; if (clientIndex.TargetIndexName != null) //Save to single index { if (addItem.Tags != null && addItem.Tags.Count > 0) { bool reposition = IsRepositioningOfIndexItemRequired(indexInfo, addItem, internalItem); // Update all tags on the internal item foreach (KeyValuePair <string, byte[]> kvp in addItem.Tags) { storeContext.TagHashCollection.AddTag(cacheIndexInternal.InDeserializationContext.TypeId, kvp.Key); internalItem.UpdateTag(TagHashCollection.GetTagHashCode(kvp.Key), kvp.Value); } // Reposition index item if required if (reposition) { RepositionIndexItem(cacheIndexInternal, indexInfo, addItem, searchIndex, internalItem, comparer); } } } else //Save to multiple indexes { if (addItem.Tags != null && addItem.Tags.Count > 0) { List <string> tagNameList; byte[] tagValue; clientIndex.IndexTagMapping.TryGetValue(cacheIndexInternal.InDeserializationContext.IndexName, out tagNameList); bool reposition = IsRepositioningOfIndexItemRequired(indexInfo, addItem, internalItem); // Update all tags on the internal item foreach (string tagName in tagNameList) { //Add to tagHashCollection storeContext.TagHashCollection.AddTag(cacheIndexInternal.InDeserializationContext.TypeId, tagName); addItem.TryGetTagValue(tagName, out tagValue); internalItem.UpdateTag(TagHashCollection.GetTagHashCode(tagName), tagValue); } // Reposition index item if required if (reposition) { RepositionIndexItem(cacheIndexInternal, indexInfo, addItem, searchIndex, internalItem, comparer); } } } }
/// <summary> /// Processes the specified contains index query. /// </summary> /// <param name="containsIndexQuery">The contains index query.</param> /// <param name="messageContext">The message context.</param> /// <param name="storeContext">The store context.</param> /// <returns>ContainsIndexQueryResult</returns> internal static ContainsIndexQueryResult Process(ContainsIndexQuery containsIndexQuery, MessageContext messageContext, IndexStoreContext storeContext) { ContainsIndexQueryResult containsIndexQueryResult; MultiItemResult multiItemResult = null; byte[] metadata = null; MetadataPropertyCollection metadataPropertyCollection = null; bool indexExists = false; int indexSize = -1; int virtualCount = -1; try { IndexTypeMapping indexTypeMapping = storeContext.StorageConfiguration.CacheIndexV3StorageConfig.IndexTypeMappingCollection[messageContext.TypeId]; #region Check TargetIndexName if (string.IsNullOrEmpty(containsIndexQuery.TargetIndexName)) { containsIndexQuery.TargetIndexName = IndexServerUtils.CheckQueryTargetIndexName(indexTypeMapping); } if (!indexTypeMapping.IndexCollection.Contains(containsIndexQuery.TargetIndexName)) { throw new Exception("Invalid TargetIndexName - " + containsIndexQuery.TargetIndexName); } #endregion Index targetIndexInfo = indexTypeMapping.IndexCollection[containsIndexQuery.TargetIndexName]; int indexCap = targetIndexInfo.MaxIndexSize; List <CacheIndexInternal> internalCacheIndexList = new List <CacheIndexInternal>(); #region Get TargetIndex CacheIndexInternal cacheIndexInternal = IndexServerUtils.GetCacheIndexInternal(storeContext, messageContext.TypeId, messageContext.PrimaryId, containsIndexQuery.IndexId, targetIndexInfo.ExtendedIdSuffix, containsIndexQuery.TargetIndexName, 0, null, true, null, false, false, targetIndexInfo.PrimarySortInfo, targetIndexInfo.LocalIdentityTagList, targetIndexInfo.StringHashCodeDictionary, null, targetIndexInfo.IsMetadataPropertyCollection, null, containsIndexQuery.DomainSpecificProcessingType, storeContext.DomainSpecificConfig, null, null, false); #endregion if (cacheIndexInternal != null) { internalCacheIndexList.Add(cacheIndexInternal); indexExists = true; indexSize = cacheIndexInternal.OutDeserializationContext.TotalCount; virtualCount = cacheIndexInternal.VirtualCount; // update the performance counter PerformanceCounters.Instance.SetCounterValue( PerformanceCounterEnum.NumOfItemsInIndexPerContainsIndexQuery, messageContext.TypeId, indexSize); PerformanceCounters.Instance.SetCounterValue( PerformanceCounterEnum.NumOfItemsReadPerContainsIndexQuery, messageContext.TypeId, cacheIndexInternal.OutDeserializationContext.ReadItemCount); int searchIndex; IndexDataItem indexDataItem; foreach (IndexItem queryIndexItem in containsIndexQuery.IndexItemList) { #region Search item in index searchIndex = internalCacheIndexList[0].Search(queryIndexItem); #endregion if (searchIndex > -1) { if (multiItemResult == null) { multiItemResult = new MultiItemResult(containsIndexQuery.IndexId); } indexDataItem = new IndexDataItem(InternalItemAdapter.ConvertToIndexItem(internalCacheIndexList[0].GetItem(searchIndex), internalCacheIndexList[0].InDeserializationContext)); #region Get extra tags if (containsIndexQuery.TagsFromIndexes != null && containsIndexQuery.TagsFromIndexes.Count != 0) { foreach (string indexName in containsIndexQuery.TagsFromIndexes) { Index indexInfo = indexTypeMapping.IndexCollection[indexName]; CacheIndexInternal indexInternal = IndexServerUtils.GetCacheIndexInternal(storeContext, messageContext.TypeId, messageContext.PrimaryId, containsIndexQuery.IndexId, indexInfo.ExtendedIdSuffix, indexName, 0, null, true, null, false, false, indexInfo.PrimarySortInfo, indexInfo.LocalIdentityTagList, indexInfo.StringHashCodeDictionary, null, indexInfo.IsMetadataPropertyCollection, null, containsIndexQuery.DomainSpecificProcessingType, storeContext.DomainSpecificConfig, null, null, false); if (indexInternal != null) { // update the performance counter PerformanceCounters.Instance.SetCounterValue( PerformanceCounterEnum.NumOfItemsInIndexPerContainsIndexQuery, messageContext.TypeId, indexInternal.OutDeserializationContext.TotalCount); PerformanceCounters.Instance.SetCounterValue( PerformanceCounterEnum.NumOfItemsReadPerContainsIndexQuery, messageContext.TypeId, indexInternal.OutDeserializationContext.ReadItemCount); internalCacheIndexList.Add(indexInternal); IndexServerUtils.GetTags(indexInternal, queryIndexItem, indexDataItem); } } } #endregion multiItemResult.Add(indexDataItem); } } #region Get data if (!containsIndexQuery.ExcludeData && multiItemResult != null) { byte[] extendedId; List <RelayMessage> dataStoreMessages = new List <RelayMessage>(multiItemResult.Count); short relatedTypeId; if (containsIndexQuery.FullDataIdInfo != null && containsIndexQuery.FullDataIdInfo.RelatedTypeName != null) { if (!storeContext.TryGetTypeId(containsIndexQuery.FullDataIdInfo.RelatedTypeName, out relatedTypeId)) { LoggingUtil.Log.ErrorFormat("Invalid RelatedCacheTypeName - {0}", containsIndexQuery.FullDataIdInfo.RelatedTypeName); throw new Exception("Invalid RelatedTypeId for TypeId - " + containsIndexQuery.FullDataIdInfo.RelatedTypeName); } } else if (!storeContext.TryGetRelatedIndexTypeId(messageContext.TypeId, out relatedTypeId)) { LoggingUtil.Log.ErrorFormat("Invalid RelatedTypeId for TypeId - {0}", messageContext.TypeId); throw new Exception("Invalid RelatedTypeId for TypeId - " + messageContext.TypeId); } foreach (IndexDataItem resultItem in multiItemResult) { extendedId = DataTierUtil.GetFullDataId(containsIndexQuery.IndexId, resultItem, containsIndexQuery.FullDataIdInfo != null && containsIndexQuery.FullDataIdInfo.RelatedTypeName != null ? containsIndexQuery.FullDataIdInfo.FullDataIdFieldList : indexTypeMapping.FullDataIdFieldList); dataStoreMessages.Add(new RelayMessage(relatedTypeId, IndexCacheUtils.GeneratePrimaryId(extendedId), extendedId, MessageType.Get)); } storeContext.ForwarderComponent.HandleMessages(dataStoreMessages); int i = 0; foreach (IndexDataItem resultItem in multiItemResult) { if (dataStoreMessages[i].Payload != null) { resultItem.Data = dataStoreMessages[i].Payload.ByteArray; } i++; } } #endregion #region Get metadata if (containsIndexQuery.GetMetadata) { if (indexTypeMapping.MetadataStoredSeperately) { IndexServerUtils.GetMetadataStoredSeperately(indexTypeMapping, messageContext.TypeId, messageContext.PrimaryId, containsIndexQuery.IndexId, storeContext, out metadata, out metadataPropertyCollection); } else { IndexServerUtils.GetMetadataStoredWithIndex(indexTypeMapping, internalCacheIndexList, out metadata, out metadataPropertyCollection); } } #endregion } containsIndexQueryResult = new ContainsIndexQueryResult(multiItemResult, metadata, metadataPropertyCollection, indexSize, indexExists, virtualCount, indexCap, null); } catch (Exception ex) { containsIndexQueryResult = new ContainsIndexQueryResult(null, null, null, -1, false, -1, 0, ex.Message); LoggingUtil.Log.ErrorFormat("TypeId {0} -- Error processing ContainsIndexQuery : {1}", messageContext.TypeId, ex); } return(containsIndexQueryResult); }
internal static MultiIndexContainsQueryResult Process(MultiIndexContainsQuery multiIndexContainsQuery, MessageContext messageContext, IndexStoreContext storeContext) { MultiIndexContainsQueryResult multiIndexContainsQueryResult; List <Pair <MultiIndexContainsQueryResultItem, IndexHeader> > multiIndexContainsQueryResultItemIndexHeaderList = new List <Pair <MultiIndexContainsQueryResultItem, IndexHeader> >(multiIndexContainsQuery.IndexIdList.Count); int indexSize = -1; int indexCap = 0; StringBuilder exceptionInfo = new StringBuilder(); try { if (multiIndexContainsQuery.IndexIdList.Count > 0) { IndexTypeMapping indexTypeMapping = storeContext.StorageConfiguration.CacheIndexV3StorageConfig.IndexTypeMappingCollection[messageContext.TypeId]; short relatedTypeId; ValidateQuery(multiIndexContainsQuery, indexTypeMapping, storeContext, messageContext.TypeId, out relatedTypeId); Index targetIndexInfo = indexTypeMapping.IndexCollection[multiIndexContainsQuery.TargetIndexName]; indexCap = targetIndexInfo.MaxIndexSize; MultiIndexContainsQueryParams multiIndexContainsQueryParam; int searchIndex; IndexDataItem indexDataItem; MultiIndexContainsQueryResultItem multiIndexContainsQueryResultItem; IndexHeader indexHeader; List <RelayMessage> dataStoreMessages = new List <RelayMessage>(); byte[] extendedId; byte[] metadata; MetadataPropertyCollection metadataPropertyCollection; foreach (byte[] indexId in multiIndexContainsQuery.IndexIdList) { #region Get TargetIndex // 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, IndexCacheUtils.GeneratePrimaryId(indexId), indexId, storeContext, out metadata, out metadataPropertyCollection); } multiIndexContainsQueryParam = multiIndexContainsQuery.GetMultiIndexContainsQueryParamForIndexId(indexId); CacheIndexInternal cacheIndexInternal = IndexServerUtils.GetCacheIndexInternal(storeContext, messageContext.TypeId, IndexCacheUtils.GeneratePrimaryId(indexId), indexId, targetIndexInfo.ExtendedIdSuffix, multiIndexContainsQuery.TargetIndexName, multiIndexContainsQueryParam.Count, multiIndexContainsQueryParam.Filter, true, multiIndexContainsQueryParam.IndexCondition, false, false, targetIndexInfo.PrimarySortInfo, targetIndexInfo.LocalIdentityTagList, targetIndexInfo.StringHashCodeDictionary, null, targetIndexInfo.IsMetadataPropertyCollection, metadataPropertyCollection, multiIndexContainsQuery.DomainSpecificProcessingType, storeContext.DomainSpecificConfig, null, null, false); #endregion if (cacheIndexInternal != null) { indexSize = cacheIndexInternal.OutDeserializationContext.TotalCount; multiIndexContainsQueryResultItem = null; indexHeader = null; // update the performance counter PerformanceCounters.Instance.SetCounterValue(PerformanceCounterEnum.NumOfItemsInIndexPerMultiIndexContainsQuery, messageContext.TypeId, cacheIndexInternal.OutDeserializationContext.TotalCount); PerformanceCounters.Instance.SetCounterValue(PerformanceCounterEnum.NumOfItemsReadPerMultiIndexContainsQuery, messageContext.TypeId, cacheIndexInternal.OutDeserializationContext.ReadItemCount); #region MultiIndexContainsQueryResultItem foreach (IndexItem queryIndexItem in multiIndexContainsQuery.IndexItemList) { #region Search item in index searchIndex = cacheIndexInternal.Search(queryIndexItem); #endregion if (searchIndex > -1) { if (multiIndexContainsQueryResultItem == null) { multiIndexContainsQueryResultItem = new MultiIndexContainsQueryResultItem { IndexId = indexId, IndexCap = indexCap, IndexExists = true, IndexSize = indexSize }; } indexDataItem = new IndexDataItem( InternalItemAdapter.ConvertToIndexItem(cacheIndexInternal.GetItem(searchIndex), cacheIndexInternal.InDeserializationContext)); multiIndexContainsQueryResultItem.Add(indexDataItem); // Data if (!multiIndexContainsQuery.ExcludeData) { extendedId = DataTierUtil.GetFullDataId(indexId, indexDataItem, multiIndexContainsQuery.FullDataIdInfo != null && multiIndexContainsQuery.FullDataIdInfo.RelatedTypeName != null ? multiIndexContainsQuery.FullDataIdInfo.FullDataIdFieldList : indexTypeMapping.FullDataIdFieldList); dataStoreMessages.Add(new RelayMessage(relatedTypeId, IndexCacheUtils.GeneratePrimaryId(extendedId), extendedId, MessageType.Get)); } } } #endregion #region Add indexHeader to Result if (multiIndexContainsQueryResultItem != null && multiIndexContainsQueryResultItem.Count > 0) { if (multiIndexContainsQuery.GetIndexHeader) { indexHeader = IndexServerUtils.GetIndexHeader(cacheIndexInternal, indexTypeMapping, messageContext.TypeId, IndexCacheUtils.GeneratePrimaryId(indexId), storeContext); } multiIndexContainsQueryResultItemIndexHeaderList.Add(new Pair <MultiIndexContainsQueryResultItem, IndexHeader> { First = multiIndexContainsQueryResultItem, Second = indexHeader }); } #endregion } } #region Get data if (!multiIndexContainsQuery.ExcludeData) { storeContext.ForwarderComponent.HandleMessages(dataStoreMessages); int i = 0; foreach ( Pair <MultiIndexContainsQueryResultItem, IndexHeader> pair in multiIndexContainsQueryResultItemIndexHeaderList) { foreach (IndexDataItem resultItem in pair.First) { if (dataStoreMessages[i].Payload != null) { resultItem.Data = dataStoreMessages[i].Payload.ByteArray; } i++; } } } #endregion } multiIndexContainsQueryResult = new MultiIndexContainsQueryResult(multiIndexContainsQueryResultItemIndexHeaderList, exceptionInfo.ToString()); // update performance counter PerformanceCounters.Instance.SetCounterValue(PerformanceCounterEnum.IndexLookupAvgPerMultiIndexContainsQuery, messageContext.TypeId, multiIndexContainsQuery.IndexIdList.Count); } catch (Exception ex) { multiIndexContainsQueryResult = new MultiIndexContainsQueryResult(null, ex.Message); LoggingUtil.Log.ErrorFormat("TypeId {0} -- Error processing MultiIndexContainsQuery : {1}", messageContext.TypeId, ex); } return(multiIndexContainsQueryResult); }