Пример #1
0
        private static void ProcessAddList(List <CacheIndexInternal> internalIndexList,
                                           List <IndexItem> cappedDeleteItemList,
                                           CacheIndex clientIndex,
                                           IndexStoreContext storeContext,
                                           IndexTypeMapping indexTypeMapping)
        {
            #region Add new items to each internalIndex

            Index indexInfo;
            InternalItemComparer comparer;
            int searchIndex;

            PerformanceCounters.Instance.SetCounterValue(
                PerformanceCounterEnum.AddList,
                indexTypeMapping.TypeId,
                clientIndex.AddList.Count);

            foreach (CacheIndexInternal cacheIndexInternal in internalIndexList)
            {
                indexInfo = indexTypeMapping.IndexCollection[cacheIndexInternal.InDeserializationContext.IndexName];
                comparer  = new InternalItemComparer(indexInfo.PrimarySortInfo.IsTag, indexInfo.PrimarySortInfo.FieldName, indexInfo.PrimarySortInfo.SortOrderList);
                if (clientIndex.AddList.Count > 0)
                {
                    foreach (IndexDataItem addItem in clientIndex.AddList)
                    {
                        searchIndex = cacheIndexInternal.Search(addItem);
                        if (searchIndex > -1)
                        {
                            UpdateExistingIndexItem(clientIndex, cacheIndexInternal, indexInfo, addItem, searchIndex, storeContext, comparer);
                        }
                        else
                        {
                            AddNewItem(clientIndex, cacheIndexInternal, indexInfo, addItem, storeContext, comparer);

                            #region IndexSize Capping
                            if (indexInfo.MaxIndexSize > 0 && cacheIndexInternal.Count > indexInfo.MaxIndexSize)
                            {
                                int deleteIndex = indexInfo.TrimFromTail ? cacheIndexInternal.Count - 1 : 0;
                                //Add item to a list to delete it from the Data Store
                                cappedDeleteItemList.Add(InternalItemAdapter.ConvertToIndexItem(cacheIndexInternal.GetItem(deleteIndex),
                                                                                                cacheIndexInternal.InDeserializationContext));

                                //Delete from Index Store
                                cacheIndexInternal.DeleteItem(deleteIndex, false);
                            }
                            #endregion
                        }
                    }
                }

                //Update metadata
                if (clientIndex.UpdateMetadata && indexTypeMapping.IndexCollection[cacheIndexInternal.InDeserializationContext.IndexName].MetadataPresent)
                {
                    cacheIndexInternal.Metadata = clientIndex.Metadata;
                }
            }

            #endregion
        }
Пример #2
0
        /// <summary>
        /// Gets the tags.
        /// </summary>
        /// <param name="cacheIndexInternal">The cache index internal.</param>
        /// <param name="searchItem">The search item.</param>
        /// <param name="resultItem">The result item.</param>
        internal static void GetTags(CacheIndexInternal cacheIndexInternal, IndexItem searchItem, IndexItem resultItem)
        {
            int searchIndex = cacheIndexInternal.Search(searchItem);

            if (searchIndex > -1)
            {
                IndexItem tempIndexItem = InternalItemAdapter.ConvertToIndexItem(cacheIndexInternal.GetItem(searchIndex), cacheIndexInternal.InDeserializationContext);

                foreach (KeyValuePair <string, byte[]> kvp in tempIndexItem.Tags)
                {
                    if (!resultItem.Tags.ContainsKey(kvp.Key))
                    {
                        resultItem.Tags.Add(kvp.Key, kvp.Value);
                    }
                }
            }
        }
        /// <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);
        }