コード例 #1
0
        /// <summary>
        /// Gets the data into resultItemList items.
        /// </summary>
        /// <param name="resultItemList">The result item list.</param>
        /// <param name="storeContext">The store context.</param>
        /// <param name="messageContext">The message context.</param>
        /// <param name="fullDataIdFieldList">The full data id field list.</param>
        /// <param name="fullDataIdInfo">The full data id info.</param>
        internal static void GetData(List <ResultItem> resultItemList,
                                     IndexStoreContext storeContext,
                                     MessageContext messageContext,
                                     FullDataIdFieldList fullDataIdFieldList,
                                     FullDataIdInfo fullDataIdInfo)
        {
            byte[] extendedId;
            List <RelayMessage> dataStoreMessages = new List <RelayMessage>();
            short relatedTypeId;

            if (fullDataIdInfo != null && fullDataIdInfo.RelatedTypeName != null)
            {
                if (!storeContext.TryGetTypeId(fullDataIdInfo.RelatedTypeName, out relatedTypeId))
                {
                    LoggingUtil.Log.ErrorFormat("Invalid RelatedCacheTypeName - {0}", fullDataIdInfo.RelatedTypeName);
                    throw new Exception("Invalid RelatedTypeId for TypeId - " + 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);
            }

            if (resultItemList != null && resultItemList.Count > 0)
            {
                foreach (ResultItem resultItem in resultItemList)
                {
                    extendedId = GetFullDataId(resultItem.IndexId,
                                               resultItem,
                                               fullDataIdInfo != null && fullDataIdInfo.RelatedTypeName != null ? fullDataIdInfo.FullDataIdFieldList : fullDataIdFieldList);
                    dataStoreMessages.Add(new RelayMessage(relatedTypeId,
                                                           IndexCacheUtils.GeneratePrimaryId(extendedId),
                                                           extendedId,
                                                           MessageType.Get));
                }

                storeContext.ForwarderComponent.HandleMessages(dataStoreMessages);

                int i = 0;
                foreach (ResultItem resultItem in resultItemList)
                {
                    if (dataStoreMessages[i].Payload != null)
                    {
                        resultItem.Data = dataStoreMessages[i].Payload.ByteArray;
                    }
                    else
                    {
                        LoggingUtil.Log.DebugFormat("Fetched Null Data for TypeId: {0}, IndexId: {1}, ItemId: {2}, FullDataId: {3}, PrimaryId: {4}",
                                                    relatedTypeId,
                                                    IndexCacheUtils.GetReadableByteArray(resultItem.IndexId),
                                                    IndexCacheUtils.GetReadableByteArray(resultItem.ItemId),
                                                    IndexCacheUtils.GetReadableByteArray(dataStoreMessages[i].ExtendedId),
                                                    IndexCacheUtils.GeneratePrimaryId(dataStoreMessages[i].ExtendedId)
                                                    );
                    }
                    i++;
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Processes the specified message context.
        /// </summary>
        /// <param name="messageContext">The message context.</param>
        /// <param name="storeContext">The store context.</param>
        internal static void Process(MessageContext messageContext, IndexStoreContext storeContext)
        {
            // TBD : Support concurrent DeleteAllInType for different types
            lock (LockingUtil.Instance.LockerObjects)
            {
                RelayMessage msg;

                short typeId = messageContext.TypeId;

                if (DataTierUtil.ShouldForwardToDataTier(messageContext.RelayTTL,
                                                         messageContext.SourceZone,
                                                         storeContext.MyZone,
                                                         storeContext.StorageConfiguration.CacheIndexV3StorageConfig.IndexTypeMappingCollection[typeId].IndexServerMode))
                {
                    // Send DeleteAll to Data Store
                    short relatedTypeId;
                    if (!storeContext.TryGetRelatedIndexTypeId(typeId, out relatedTypeId))
                    {
                        LoggingUtil.Log.ErrorFormat("Invalid RelatedTypeId for TypeId - {0}", typeId);
                        throw new Exception("Invalid RelatedTypeId for TypeId - " + typeId);
                    }
                    msg = new RelayMessage(relatedTypeId, 0, MessageType.DeleteAllInType);
                    storeContext.ForwarderComponent.HandleMessage(msg);
                }

                // Send DeleteAll to local Index storage
                BinaryStorageAdapter.Clear(storeContext.IndexStorageComponent, typeId);


                // Delete hash mapping entries in file
                storeContext.RemoveType(typeId);
            }
        }
コード例 #3
0
        private static void ValidateQuery(MultiIndexContainsQuery multiIndexContainsQuery,
                                          IndexTypeMapping indexTypeMapping,
                                          IndexStoreContext storeContext,
                                          short typeId,
                                          out short relatedTypeId)
        {
            relatedTypeId = -1;
            if (string.IsNullOrEmpty(multiIndexContainsQuery.TargetIndexName))
            {
                multiIndexContainsQuery.TargetIndexName = IndexServerUtils.CheckQueryTargetIndexName(indexTypeMapping);
            }

            if (!indexTypeMapping.IndexCollection.Contains(multiIndexContainsQuery.TargetIndexName))
            {
                throw new Exception("Invalid TargetIndexName - " + multiIndexContainsQuery.TargetIndexName);
            }

            if (!multiIndexContainsQuery.ExcludeData)
            {
                if (multiIndexContainsQuery.FullDataIdInfo != null && multiIndexContainsQuery.FullDataIdInfo.RelatedTypeName != null)
                {
                    if (!storeContext.TryGetTypeId(multiIndexContainsQuery.FullDataIdInfo.RelatedTypeName, out relatedTypeId))
                    {
                        LoggingUtil.Log.ErrorFormat("Invalid RelatedCacheTypeName - {0}", multiIndexContainsQuery.FullDataIdInfo.RelatedTypeName);
                        throw new Exception("Invalid RelatedTypeId for TypeId - " + multiIndexContainsQuery.FullDataIdInfo.RelatedTypeName);
                    }
                }
                else if (!storeContext.TryGetRelatedIndexTypeId(typeId, out relatedTypeId))
                {
                    LoggingUtil.Log.ErrorFormat("Invalid RelatedTypeId for TypeId - {0}", typeId);
                    throw new Exception("Invalid RelatedTypeId for TypeId - " + typeId);
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Processes the specified filtered index delete command.
        /// </summary>
        /// <param name="filteredIndexDeleteCommand">The filtered index delete command.</param>
        /// <param name="messageContext">The message context.</param>
        /// <param name="storeContext">The store context.</param>
        internal static void Process(
            FilteredIndexDeleteCommand filteredIndexDeleteCommand,
            MessageContext messageContext,
            IndexStoreContext storeContext)
        {
            if (filteredIndexDeleteCommand != null)
            {
                IndexTypeMapping indexTypeMapping =
                    storeContext.StorageConfiguration.CacheIndexV3StorageConfig.IndexTypeMappingCollection[messageContext.TypeId];
                Index  indexInfo = indexTypeMapping.IndexCollection[filteredIndexDeleteCommand.TargetIndexName];
                byte[] metadata  = null;
                MetadataPropertyCollection metadataPropertyCollection = null;

                #region Get CacheIndexInternal for TargetIndexName

                if (indexTypeMapping.MetadataStoredSeperately)
                {
                    IndexServerUtils.GetMetadataStoredSeperately(indexTypeMapping,
                                                                 messageContext.TypeId,
                                                                 messageContext.PrimaryId,
                                                                 filteredIndexDeleteCommand.IndexId,
                                                                 storeContext,
                                                                 out metadata,
                                                                 out metadataPropertyCollection);
                }

                CacheIndexInternal cacheIndexInternal = IndexServerUtils.GetCacheIndexInternal(storeContext,
                                                                                               messageContext.TypeId,
                                                                                               filteredIndexDeleteCommand.PrimaryId,
                                                                                               filteredIndexDeleteCommand.IndexId,
                                                                                               indexInfo.ExtendedIdSuffix,
                                                                                               filteredIndexDeleteCommand.TargetIndexName,
                                                                                               0,
                                                                                               filteredIndexDeleteCommand.DeleteFilter,
                                                                                               false,
                                                                                               null,
                                                                                               false,
                                                                                               true,
                                                                                               indexInfo.PrimarySortInfo,
                                                                                               indexInfo.LocalIdentityTagList,
                                                                                               indexInfo.StringHashCodeDictionary,
                                                                                               null,
                                                                                               indexInfo.IsMetadataPropertyCollection,
                                                                                               metadataPropertyCollection,
                                                                                               DomainSpecificProcessingType.None,
                                                                                               null,
                                                                                               null,
                                                                                               null,
                                                                                               false);

                #endregion

                if (cacheIndexInternal != null)
                {
                    #region Increment perf counters' number

                    PerformanceCounters.Instance.SetCounterValue(
                        PerformanceCounterEnum.NumOfItemsInIndexPerFilterDeleteRequest,
                        messageContext.TypeId,
                        cacheIndexInternal.OutDeserializationContext.TotalCount);

                    PerformanceCounters.Instance.SetCounterValue(
                        PerformanceCounterEnum.NumOfItemsReadPerFilterDeleteRequest,
                        messageContext.TypeId,
                        cacheIndexInternal.OutDeserializationContext.ReadItemCount);

                    PerformanceCounters.Instance.SetCounterValue(
                        PerformanceCounterEnum.NumOfItemsFilteredPerFilterDeleteRequest,
                        messageContext.TypeId,
                        (cacheIndexInternal.OutDeserializationContext.TotalCount - cacheIndexInternal.OutDeserializationContext.FilteredInternalItemList.Count));

                    #endregion

                    #region Update VirtualCount

                    cacheIndexInternal.VirtualCount -= (cacheIndexInternal.OutDeserializationContext.TotalCount - cacheIndexInternal.Count);

                    #endregion

                    #region Save CacheIndexInternal to local storage since item which pass delete filter are pruned in it

                    byte[] extendedId = IndexServerUtils.FormExtendedId(filteredIndexDeleteCommand.IndexId,
                                                                        indexTypeMapping.IndexCollection[cacheIndexInternal.InDeserializationContext.IndexName].ExtendedIdSuffix);

                    // compose a bdb entry header
                    bool isCompress = storeContext.GetCompressOption(messageContext.TypeId);

                    PayloadStorage bdbEntryHeader = new PayloadStorage
                    {
                        Compressed       = isCompress,
                        TTL              = -1,
                        LastUpdatedTicks = DateTime.Now.Ticks,
                        ExpirationTicks  = -1,
                        Deactivated      = false
                    };

                    BinaryStorageAdapter.Save(
                        storeContext.MemoryPool,
                        storeContext.IndexStorageComponent,
                        messageContext.TypeId,
                        filteredIndexDeleteCommand.PrimaryId,
                        extendedId,
                        bdbEntryHeader,
                        Serializer.Serialize <CacheIndexInternal>(cacheIndexInternal, isCompress, RelayMessage.RelayCompressionImplementation));

                    #endregion

                    #region Data store deletes

                    if (DataTierUtil.ShouldForwardToDataTier(messageContext.RelayTTL,
                                                             messageContext.SourceZone,
                                                             storeContext.MyZone,
                                                             indexTypeMapping.IndexServerMode) &&
                        cacheIndexInternal.OutDeserializationContext.FilteredInternalItemList != null &&
                        cacheIndexInternal.OutDeserializationContext.FilteredInternalItemList.Count > 0)
                    {
                        List <RelayMessage> dataStorageMessageList = new List <RelayMessage>();

                        short relatedTypeId;
                        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);
                        }
                        cacheIndexInternal.InternalItemList = cacheIndexInternal.OutDeserializationContext.FilteredInternalItemList;
                        List <byte[]> fullDataIdList = DataTierUtil.GetFullDataIds(cacheIndexInternal.InDeserializationContext.IndexId,
                                                                                   cacheIndexInternal.InternalItemList,
                                                                                   indexTypeMapping.FullDataIdFieldList);

                        foreach (byte[] fullDataId in fullDataIdList)
                        {
                            if (fullDataId != null)
                            {
                                dataStorageMessageList.Add(new RelayMessage(relatedTypeId,
                                                                            IndexCacheUtils.GeneratePrimaryId(fullDataId),
                                                                            fullDataId,
                                                                            MessageType.Delete));
                            }
                        }

                        if (dataStorageMessageList.Count > 0)
                        {
                            storeContext.ForwarderComponent.HandleMessages(dataStorageMessageList);
                        }
                    }

                    #endregion
                }
            }
        }
コード例 #5
0
        /// <summary>
        /// Processes the specified message context.
        /// </summary>
        /// <param name="messageContext">The message context.</param>
        /// <param name="storeContext">The store context.</param>
        internal static void Process(MessageContext messageContext, IndexStoreContext storeContext)
        {
            lock (LockingUtil.Instance.GetLock(messageContext.PrimaryId))
            {
                IndexTypeMapping indexTypeMapping =
                    storeContext.StorageConfiguration.CacheIndexV3StorageConfig.IndexTypeMappingCollection[messageContext.TypeId];
                List <RelayMessage> indexStorageMessageList = new List <RelayMessage>(indexTypeMapping.IndexCollection.Count);
                List <RelayMessage> dataStorageMessageList  = new List <RelayMessage>();
                CacheIndexInternal  internalIndex;
                List <byte[]>       fullDataIdList;

                if (indexTypeMapping.MetadataStoredSeperately)
                {
                    BinaryStorageAdapter.Delete(
                        storeContext.IndexStorageComponent,
                        messageContext.TypeId,
                        messageContext.PrimaryId,
                        messageContext.ExtendedId);
                }

                foreach (Index index in indexTypeMapping.IndexCollection)
                {
                    internalIndex = IndexServerUtils.GetCacheIndexInternal(storeContext,
                                                                           messageContext.TypeId,
                                                                           messageContext.PrimaryId,
                                                                           messageContext.ExtendedId,
                                                                           index.ExtendedIdSuffix,
                                                                           index.IndexName,
                                                                           0,
                                                                           null,
                                                                           true,
                                                                           null,
                                                                           false,
                                                                           false,
                                                                           index.PrimarySortInfo,
                                                                           index.LocalIdentityTagList,
                                                                           index.StringHashCodeDictionary,
                                                                           null,
                                                                           indexTypeMapping.IndexCollection[index.IndexName].IsMetadataPropertyCollection,
                                                                           null,
                                                                           DomainSpecificProcessingType.None,
                                                                           null,
                                                                           null,
                                                                           null,
                                                                           true);

                    if (internalIndex != null)
                    {
                        #region Deletes messages for data store

                        if (DataTierUtil.ShouldForwardToDataTier(messageContext.RelayTTL,
                                                                 messageContext.SourceZone,
                                                                 storeContext.MyZone,
                                                                 indexTypeMapping.IndexServerMode))
                        {
                            fullDataIdList = DataTierUtil.GetFullDataIds(messageContext.ExtendedId,
                                                                         internalIndex.InternalItemList,
                                                                         indexTypeMapping.FullDataIdFieldList);
                            short relatedTypeId;
                            foreach (byte[] fullDataId in fullDataIdList)
                            {
                                if (fullDataId != null)
                                {
                                    if (storeContext.TryGetRelatedIndexTypeId(messageContext.TypeId, out relatedTypeId))
                                    {
                                        dataStorageMessageList.Add(new RelayMessage(relatedTypeId,
                                                                                    IndexCacheUtils.GeneratePrimaryId(fullDataId),
                                                                                    fullDataId,
                                                                                    MessageType.Delete));
                                    }
                                    else
                                    {
                                        LoggingUtil.Log.ErrorFormat("Invalid RelatedTypeId for TypeId - {0}", messageContext.TypeId);
                                        throw new Exception("Invalid RelatedTypeId for TypeId - " + messageContext.TypeId);
                                    }
                                }
                            }
                        }

                        #endregion

                        #region Delete messages for index store

                        BinaryStorageAdapter.Delete(
                            storeContext.IndexStorageComponent,
                            messageContext.TypeId,
                            messageContext.PrimaryId,
                            IndexServerUtils.FormExtendedId(messageContext.ExtendedId, index.ExtendedIdSuffix));

                        #endregion
                    }
                }

                if (dataStorageMessageList.Count > 0)
                {
                    storeContext.ForwarderComponent.HandleMessages(dataStorageMessageList);
                }
            }
        }
コード例 #6
0
        /// <summary>
        /// Processes the specified cache index.
        /// </summary>
        /// <param name="cacheIndex">Index of the cache.</param>
        /// <param name="messageContext">The message context.</param>
        /// <param name="storeContext">The store context.</param>
        internal static void Process(CacheIndex cacheIndex, MessageContext messageContext, IndexStoreContext storeContext)
        {
            lock (LockingUtil.Instance.GetLock(messageContext.PrimaryId))
            {
                try
                {
                    IndexTypeMapping indexTypeMapping =
                        storeContext.StorageConfiguration.CacheIndexV3StorageConfig.IndexTypeMappingCollection[messageContext.TypeId];

                    #region Extract CacheIndex and Validate from incoming message

                    ValidateSave(cacheIndex);

                    #endregion

                    #region Log CacheIndex before processing
                    StringBuilder dbgIndexInfo = null;
                    if (LoggingUtil.Log.IsDebugEnabled)
                    {
                        dbgIndexInfo = new StringBuilder();
                        dbgIndexInfo.Append("TypeId=").Append(messageContext.TypeId).Append(Environment.NewLine);
                        dbgIndexInfo.Append(IndexServerUtils.GetPrintableCacheIndex(cacheIndex,
                                                                                    storeContext.TagHashCollection,
                                                                                    messageContext.TypeId));
                    }
                    #endregion

                    #region Init vars

                    List <RelayMessage>       indexStorageMessageList = new List <RelayMessage>();
                    List <RelayMessage>       dataStorageMessageList  = new List <RelayMessage>();
                    List <CacheIndexInternal> internalIndexList       = new List <CacheIndexInternal>();
                    List <IndexItem>          cappedDeleteItemList    = new List <IndexItem>();
                    CacheIndexInternal        internalIndex;

                    #endregion

                    if (cacheIndex.IndexVirtualCountMapping == null)
                    {
                        #region Save Items

                        #region Extract CacheIndexInternal from index storage

                        if (cacheIndex.TargetIndexName == null) //Save to multiple indexes
                        {
                            #region Get CacheIndexInternal for multiple indexes

                            foreach (KeyValuePair <string /*IndexName*/, List <string> /*TagNameList*/> kvp in cacheIndex.IndexTagMapping)
                            {
                                Index indexInfo = indexTypeMapping.IndexCollection[kvp.Key];
                                internalIndex = IndexServerUtils.GetCacheIndexInternal(storeContext,
                                                                                       messageContext.TypeId,
                                                                                       cacheIndex.PrimaryId,
                                                                                       cacheIndex.IndexId,
                                                                                       indexInfo.ExtendedIdSuffix,
                                                                                       kvp.Key,
                                                                                       0,
                                                                                       null,
                                                                                       true,
                                                                                       null,
                                                                                       false,
                                                                                       false,
                                                                                       indexInfo.PrimarySortInfo,
                                                                                       indexInfo.LocalIdentityTagList,
                                                                                       indexInfo.StringHashCodeDictionary,
                                                                                       null);

                                if (internalIndex != null)
                                {
                                    // update performance counter
                                    PerformanceCounters.Instance.SetCounterValue(
                                        PerformanceCounterEnum.NumberOfItemsInIndexPerSave,
                                        messageContext.TypeId,
                                        internalIndex.OutDeserializationContext.TotalCount);
                                }

                                if (internalIndex == null || cacheIndex.ReplaceFullIndex) //CacheIndexInternal does not exists or is to be discarded
                                {
                                    internalIndex = new CacheIndexInternal
                                    {
                                        InDeserializationContext = new InDeserializationContext
                                        {
                                            TypeId                   = messageContext.TypeId,
                                            TagHashCollection        = storeContext.TagHashCollection,
                                            IndexId                  = cacheIndex.IndexId,
                                            IndexName                = kvp.Key,
                                            InclusiveFilter          = true,
                                            PrimarySortInfo          = indexInfo.PrimarySortInfo,
                                            LocalIdentityTagNames    = indexInfo.LocalIdentityTagList,
                                            StringHashCollection     = storeContext.StringHashCollection,
                                            StringHashCodeDictionary = indexInfo.StringHashCodeDictionary
                                        }
                                    };
                                }

                                internalIndexList.Add(internalIndex);
                            }

                            #endregion
                        }
                        else //Save to single index
                        {
                            #region Get CacheIndexInternal for TargetIndexName

                            Index indexInfo = indexTypeMapping.IndexCollection[cacheIndex.TargetIndexName];

                            internalIndex = IndexServerUtils.GetCacheIndexInternal(storeContext,
                                                                                   messageContext.TypeId,
                                                                                   cacheIndex.PrimaryId,
                                                                                   cacheIndex.IndexId,
                                                                                   indexInfo.ExtendedIdSuffix,
                                                                                   cacheIndex.TargetIndexName,
                                                                                   0,
                                                                                   null,
                                                                                   true,
                                                                                   null,
                                                                                   false,
                                                                                   false,
                                                                                   indexInfo.PrimarySortInfo,
                                                                                   indexInfo.LocalIdentityTagList,
                                                                                   indexInfo.StringHashCodeDictionary,
                                                                                   null);

                            if (internalIndex != null)
                            {
                                // update performance counter
                                PerformanceCounters.Instance.SetCounterValue(
                                    PerformanceCounterEnum.NumberOfItemsInIndexPerSave,
                                    messageContext.TypeId,
                                    internalIndex.OutDeserializationContext.TotalCount);
                            }

                            if (internalIndex == null || cacheIndex.ReplaceFullIndex) //CacheIndexInternal does not exists or is to be discarded
                            {
                                internalIndex = new CacheIndexInternal
                                {
                                    InDeserializationContext = new InDeserializationContext
                                    {
                                        TypeId                   = messageContext.TypeId,
                                        TagHashCollection        = storeContext.TagHashCollection,
                                        IndexId                  = cacheIndex.IndexId,
                                        IndexName                = cacheIndex.TargetIndexName,
                                        InclusiveFilter          = true,
                                        PrimarySortInfo          = indexInfo.PrimarySortInfo,
                                        LocalIdentityTagNames    = indexInfo.LocalIdentityTagList,
                                        StringHashCollection     = storeContext.StringHashCollection,
                                        StringHashCodeDictionary = indexInfo.StringHashCodeDictionary
                                    }
                                };
                            }

                            internalIndexList.Add(internalIndex);

                            #endregion
                        }
                        #endregion

                        #region Log CacheIndexInternals before save
                        if (LoggingUtil.Log.IsDebugEnabled && dbgIndexInfo != null)
                        {
                            dbgIndexInfo.Append(Environment.NewLine).Append(string.Format("BEFORE SAVE {0}",
                                                                                          IndexServerUtils.GetPrintableCacheIndexInternalList(
                                                                                              internalIndexList,
                                                                                              storeContext.TagHashCollection,
                                                                                              messageContext.TypeId)));
                        }
                        #endregion

                        #region Process Delete and Add List
                        try
                        {
                            #region Process Delete List

                            if (cacheIndex.DeleteList.Count > 0 && !cacheIndex.ReplaceFullIndex)
                            {
                                ProcessDeleteList(internalIndexList, cacheIndex.DeleteList, messageContext.TypeId);
                            }

                            #endregion

                            #region Process Add List

                            if (cacheIndex.AddList.Count > 0 || cacheIndex.UpdateMetadata)
                            {
                                ProcessAddList(internalIndexList, cappedDeleteItemList, cacheIndex, storeContext, indexTypeMapping);
                            }

                            #endregion
                        }
                        catch
                        {
                            LoggingUtil.Log.Debug(IndexServerUtils.GetPrintableCacheIndexInternalList(internalIndexList, storeContext.TagHashCollection, messageContext.TypeId));
                            throw;
                        }
                        #endregion

                        #region Log CacheIndexInternals after save
                        if (LoggingUtil.Log.IsDebugEnabled && dbgIndexInfo != null)
                        {
                            dbgIndexInfo.Append(Environment.NewLine).Append(string.Format("AFTER SAVE {0}",
                                                                                          IndexServerUtils.GetPrintableCacheIndexInternalList(internalIndexList,
                                                                                                                                              storeContext.TagHashCollection,
                                                                                                                                              messageContext.TypeId)));
                        }
                        #endregion

                        #region Data store relay messages for deletes and saves

                        if (DataTierUtil.ShouldForwardToDataTier(messageContext.RelayTTL,
                                                                 messageContext.SourceZone,
                                                                 storeContext.MyZone,
                                                                 indexTypeMapping.IndexServerMode) && !cacheIndex.PreserveData)
                        {
                            byte[] fullDataId;
                            short  relatedTypeId;
                            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);
                            }

                            #region Delete Messages

                            foreach (IndexItem indexItem in cacheIndex.DeleteList)
                            {
                                fullDataId = DataTierUtil.GetFullDataId(cacheIndex.IndexId, indexItem, indexTypeMapping.FullDataIdFieldList);
                                if (fullDataId != null)
                                {
                                    dataStorageMessageList.Add(new RelayMessage(relatedTypeId,
                                                                                IndexCacheUtils.GeneratePrimaryId(fullDataId),
                                                                                fullDataId,
                                                                                MessageType.Delete));
                                }
                            }

                            #endregion

                            #region Save Messages

                            foreach (IndexDataItem indexDataItem in cacheIndex.AddList)
                            {
                                fullDataId = DataTierUtil.GetFullDataId(cacheIndex.IndexId, indexDataItem, indexTypeMapping.FullDataIdFieldList);

                                if (fullDataId != null)
                                {
                                    dataStorageMessageList.Add(new RelayMessage(relatedTypeId,
                                                                                IndexCacheUtils.GeneratePrimaryId(fullDataId),
                                                                                fullDataId,
                                                                                DateTime.Now,
                                                                                indexDataItem.Data ?? new byte[0],
                                                                                storeContext.GetCompressOption(messageContext.TypeId),
                                                                                MessageType.Save));

                                    if (indexDataItem.Data == null || indexDataItem.Data.Length == 0)
                                    {
                                        LoggingUtil.Log.WarnFormat("Saving null data for TypeId: {0}, IndexId: {1}, ItemId: {2}, FullDataId: {3}, PrimaryId: {4}",
                                                                   relatedTypeId,
                                                                   IndexCacheUtils.GetReadableByteArray(cacheIndex.IndexId),
                                                                   IndexCacheUtils.GetReadableByteArray(indexDataItem.ItemId),
                                                                   IndexCacheUtils.GetReadableByteArray(fullDataId),
                                                                   IndexCacheUtils.GeneratePrimaryId(fullDataId));
                                    }
                                }
                            }

                            #endregion

                            #region Capped Item Delete Messages

                            foreach (IndexItem indexItem in cappedDeleteItemList)
                            {
                                fullDataId = DataTierUtil.GetFullDataId(cacheIndex.IndexId, indexItem, indexTypeMapping.FullDataIdFieldList);
                                if (fullDataId != null)
                                {
                                    dataStorageMessageList.Add(new RelayMessage(relatedTypeId,
                                                                                IndexCacheUtils.GeneratePrimaryId(fullDataId),
                                                                                fullDataId,
                                                                                MessageType.Delete));
                                }
                            }

                            #endregion

                            #region Send relay mesaages to data store

                            if (dataStorageMessageList.Count > 0)
                            {
                                storeContext.ForwarderComponent.HandleMessages(dataStorageMessageList);
                            }

                            #endregion
                        }

                        #endregion

                        #endregion

                        if (dbgIndexInfo != null)
                        {
                            LoggingUtil.Log.Debug(dbgIndexInfo.ToString());
                        }
                    }
                    else
                    {
                        #region Update Virtual Count

                        foreach (KeyValuePair <string /*IndexName*/, int /*VirtualCount*/> kvp in cacheIndex.IndexVirtualCountMapping)
                        {
                            Index indexInfo = indexTypeMapping.IndexCollection[kvp.Key];
                            internalIndex = IndexServerUtils.GetCacheIndexInternal(storeContext,
                                                                                   messageContext.TypeId,
                                                                                   cacheIndex.PrimaryId,
                                                                                   cacheIndex.IndexId,
                                                                                   indexInfo.ExtendedIdSuffix,
                                                                                   kvp.Key,
                                                                                   0,
                                                                                   null,
                                                                                   true,
                                                                                   null,
                                                                                   true,
                                                                                   false,
                                                                                   indexInfo.PrimarySortInfo,
                                                                                   indexInfo.LocalIdentityTagList,
                                                                                   indexInfo.StringHashCodeDictionary,
                                                                                   null);

                            if (internalIndex == null)
                            {
                                internalIndex = new CacheIndexInternal
                                {
                                    InDeserializationContext = new InDeserializationContext
                                    {
                                        TypeId                   = messageContext.TypeId,
                                        TagHashCollection        = storeContext.TagHashCollection,
                                        IndexId                  = cacheIndex.IndexId,
                                        IndexName                = kvp.Key,
                                        InclusiveFilter          = true,
                                        DeserializeHeaderOnly    = true,
                                        PrimarySortInfo          = indexInfo.PrimarySortInfo,
                                        LocalIdentityTagNames    = indexInfo.LocalIdentityTagList,
                                        StringHashCollection     = storeContext.StringHashCollection,
                                        StringHashCodeDictionary = indexInfo.StringHashCodeDictionary
                                    }
                                };
                            }
                            else
                            {
                                // update performance counter
                                PerformanceCounters.Instance.SetCounterValue(
                                    PerformanceCounterEnum.NumberOfItemsInIndexPerSave,
                                    messageContext.TypeId,
                                    internalIndex.OutDeserializationContext.TotalCount);
                            }

                            internalIndex.VirtualCount = kvp.Value;
                            internalIndexList.Add(internalIndex);
                        }
                        #endregion
                    }

                    #region Index storage relay messages for each CacheIndexInternal

                    #region Metadata

                    if (indexTypeMapping.MetadataStoredSeperately && cacheIndex.UpdateMetadata)
                    {
                        indexStorageMessageList.Add(new RelayMessage(messageContext.TypeId,
                                                                     cacheIndex.PrimaryId,
                                                                     cacheIndex.IndexId,
                                                                     DateTime.Now,
                                                                     cacheIndex.Metadata ?? new byte[0],
                                                                     storeContext.GetCompressOption(messageContext.TypeId),
                                                                     MessageType.Save));
                    }

                    #endregion

                    #region Index(es)

                    byte[] payload;
                    CompactBinaryWriter writer;
                    RelayMessage        indexStorageMessage;
                    byte[] extendedId;

                    foreach (CacheIndexInternal cacheIndexInternal in internalIndexList)
                    {
                        extendedId = IndexServerUtils.FormExtendedId(
                            cacheIndex.IndexId,
                            indexTypeMapping.IndexCollection[cacheIndexInternal.InDeserializationContext.IndexName].ExtendedIdSuffix);

                        // This mess is required until Moods 2.0 migrated to have IVersionSerializable version of CacheIndexInternal
                        // ** TBD - Should be removed later
                        if (LegacySerializationUtil.Instance.IsSupported(messageContext.TypeId))
                        {
                            writer = new CompactBinaryWriter(new BinaryWriter(new MemoryStream()));
                            cacheIndexInternal.Serialize(writer);
                            payload = new byte[writer.BaseStream.Length];
                            writer.BaseStream.Position = 0;
                            writer.BaseStream.Read(payload, 0, payload.Length);

                            indexStorageMessage = new RelayMessage(messageContext.TypeId,
                                                                   cacheIndex.PrimaryId,
                                                                   extendedId,
                                                                   DateTime.Now,
                                                                   payload,
                                                                   storeContext.GetCompressOption(messageContext.TypeId),
                                                                   MessageType.Save);
                        }
                        else
                        {
                            indexStorageMessage = RelayMessage.GetSaveMessageForObject(messageContext.TypeId,
                                                                                       cacheIndex.PrimaryId,
                                                                                       extendedId,
                                                                                       DateTime.Now,
                                                                                       cacheIndexInternal,
                                                                                       storeContext.GetCompressOption(messageContext.TypeId));
                        }

                        indexStorageMessageList.Add(indexStorageMessage);
                    }

                    #endregion

                    #region Send relay mesaages to index storage

                    storeContext.IndexStorageComponent.HandleMessages(indexStorageMessageList);

                    #endregion

                    #endregion
                }
                catch (Exception ex)
                {
                    LoggingUtil.Log.DebugFormat("CacheIndex: {0}", IndexServerUtils.GetPrintableCacheIndex(cacheIndex, storeContext.TagHashCollection, messageContext.TypeId));
                    throw new Exception("TypeId " + messageContext.TypeId + " -- Error processing save message.", ex);
                }
            }
        }
コード例 #7
0
        /// <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);
        }
コード例 #8
0
        /// <summary>
        /// Processes the specified filtered index delete command.
        /// </summary>
        /// <param name="filteredIndexDeleteCommand">The filtered index delete command.</param>
        /// <param name="messageContext">The message context.</param>
        /// <param name="storeContext">The store context.</param>
        internal static void Process(
            FilteredIndexDeleteCommand filteredIndexDeleteCommand,
            MessageContext messageContext,
            IndexStoreContext storeContext)
        {
            if (filteredIndexDeleteCommand != null)
            {
                IndexTypeMapping indexTypeMapping =
                    storeContext.StorageConfiguration.CacheIndexV3StorageConfig.IndexTypeMappingCollection[messageContext.TypeId];
                Index indexInfo = indexTypeMapping.IndexCollection[filteredIndexDeleteCommand.TargetIndexName];

                #region Get CacheIndexInternal for TargetIndexName

                CacheIndexInternal cacheIndexInternal = IndexServerUtils.GetCacheIndexInternal(storeContext,
                                                                                               messageContext.TypeId,
                                                                                               filteredIndexDeleteCommand.PrimaryId,
                                                                                               filteredIndexDeleteCommand.IndexId,
                                                                                               indexInfo.ExtendedIdSuffix,
                                                                                               filteredIndexDeleteCommand.TargetIndexName,
                                                                                               0,
                                                                                               filteredIndexDeleteCommand.DeleteFilter,
                                                                                               false,
                                                                                               null,
                                                                                               false,
                                                                                               true,
                                                                                               indexInfo.PrimarySortInfo,
                                                                                               indexInfo.LocalIdentityTagList,
                                                                                               indexInfo.StringHashCodeDictionary,
                                                                                               null);

                #endregion

                if (cacheIndexInternal != null)
                {
                    #region Increment perf counters' number

                    PerformanceCounters.Instance.SetCounterValue(
                        PerformanceCounterEnum.NumOfItemsInIndexPerFilterDeleteRequest,
                        messageContext.TypeId,
                        cacheIndexInternal.OutDeserializationContext.TotalCount);

                    PerformanceCounters.Instance.SetCounterValue(
                        PerformanceCounterEnum.NumOfItemsReadPerFilterDeleteRequest,
                        messageContext.TypeId,
                        cacheIndexInternal.OutDeserializationContext.ReadItemCount);

                    PerformanceCounters.Instance.SetCounterValue(
                        PerformanceCounterEnum.NumOfItemsFilteredPerFilterDeleteRequest,
                        messageContext.TypeId,
                        (cacheIndexInternal.OutDeserializationContext.TotalCount - cacheIndexInternal.OutDeserializationContext.FilteredInternalItemList.Count));

                    #endregion

                    #region Update VirtualCount

                    cacheIndexInternal.VirtualCount -= (cacheIndexInternal.OutDeserializationContext.TotalCount - cacheIndexInternal.Count);

                    #endregion

                    #region Save CacheIndexInternal to local storage since item which pass delete filter are pruned in it

                    byte[] extendedId = IndexServerUtils.FormExtendedId(filteredIndexDeleteCommand.IndexId,
                                                                        indexTypeMapping.IndexCollection[cacheIndexInternal.InDeserializationContext.IndexName].ExtendedIdSuffix);

                    RelayMessage indexStorageMessage = RelayMessage.GetSaveMessageForObject(messageContext.TypeId,
                                                                                            filteredIndexDeleteCommand.PrimaryId,
                                                                                            extendedId,
                                                                                            DateTime.Now,
                                                                                            cacheIndexInternal,
                                                                                            storeContext.GetCompressOption(messageContext.TypeId));

                    storeContext.IndexStorageComponent.HandleMessage(indexStorageMessage);

                    #endregion

                    #region Data store deletes

                    if (DataTierUtil.ShouldForwardToDataTier(messageContext.RelayTTL,
                                                             messageContext.SourceZone,
                                                             storeContext.MyZone,
                                                             indexTypeMapping.IndexServerMode) &&
                        cacheIndexInternal.OutDeserializationContext.FilteredInternalItemList != null &&
                        cacheIndexInternal.OutDeserializationContext.FilteredInternalItemList.Count > 0)
                    {
                        List <RelayMessage> dataStorageMessageList = new List <RelayMessage>();

                        short relatedTypeId;
                        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);
                        }
                        cacheIndexInternal.InternalItemList = cacheIndexInternal.OutDeserializationContext.FilteredInternalItemList;
                        List <byte[]> fullDataIdList = DataTierUtil.GetFullDataIds(cacheIndexInternal.InDeserializationContext.IndexId,
                                                                                   cacheIndexInternal.InternalItemList,
                                                                                   indexTypeMapping.FullDataIdFieldList);

                        foreach (byte[] fullDataId in fullDataIdList)
                        {
                            if (fullDataId != null)
                            {
                                dataStorageMessageList.Add(new RelayMessage(relatedTypeId,
                                                                            IndexCacheUtils.GeneratePrimaryId(fullDataId),
                                                                            fullDataId,
                                                                            MessageType.Delete));
                            }
                        }

                        if (dataStorageMessageList.Count > 0)
                        {
                            storeContext.ForwarderComponent.HandleMessages(dataStorageMessageList);
                        }
                    }

                    #endregion
                }
            }
        }