/// <summary>
        /// Processes the specified message context.
        /// </summary>
        /// <param name="messageContext">The message context.</param>
        /// <param name="storeContext">The store context.</param>
        /// <returns>Raw CacheIndexInternal</returns>
        internal static byte[] Process(MessageContext messageContext, IndexStoreContext storeContext)
        {
            byte[]       payloadByteArray = null;
            RelayMessage getMsg           = new RelayMessage
            {
                MessageType = MessageType.Get,
                Id          = IndexCacheUtils.GeneratePrimaryId(messageContext.ExtendedId),
                ExtendedId  = IndexServerUtils.FormExtendedId(messageContext.ExtendedId, 0)                           // Pull first of the multiple indexes
            };

            if (storeContext.StorageConfiguration.CacheIndexV3StorageConfig.IndexTypeMappingCollection.Contains(messageContext.TypeId))
            {
                getMsg.TypeId = messageContext.TypeId;
            }
            else if (storeContext.RelatedTypeIds.TryGetValue(messageContext.TypeId, out getMsg.TypeId))
            {
            }
            else
            {
                LoggingUtil.Log.InfoFormat("Invalid TypeID for GetMessage {0}", messageContext.TypeId);
                return(payloadByteArray);
            }

            storeContext.IndexStorageComponent.HandleMessage(getMsg);

            if (getMsg.Payload != null)
            {
                payloadByteArray = getMsg.Payload.ByteArray;
            }
            return(payloadByteArray);
        }
        /// <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++;
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Gets the printable CacheIndexInternalList.
        /// </summary>
        /// <param name="internalIndexList">The internal index list.</param>
        /// <returns></returns>
        internal static string GetPrintableCacheIndexInternalList(List <CacheIndexInternal> internalIndexList,
                                                                  TagHashCollection tagHashCollection, short typeId)
        {
            StringBuilder logStr = new StringBuilder();

            logStr.Append("CacheIndexInternalList Info").Append(Environment.NewLine);

            int itemCount;

            foreach (CacheIndexInternal cii in internalIndexList)
            {
                if (cii.InDeserializationContext != null && cii.InDeserializationContext.IndexId != null)
                {
                    //CacheIndexInternal Name
                    logStr.Append("IndexName : ").Append(cii.InDeserializationContext.IndexName);
                    logStr.Append(", IndexId : ").Append(IndexCacheUtils.GetReadableByteArray(cii.InDeserializationContext.IndexId)).Append(Environment.NewLine);
                }

                if (cii.InternalItemList != null)
                {
                    logStr.Append("CacheIndexInternal.InternalItemList.Count : ").Append(cii.InternalItemList.Count.ToString()).Append(Environment.NewLine);
                    itemCount = 0;
                    foreach (InternalItem internalItem in cii.InternalItemList)
                    {
                        LogItem(logStr, internalItem, itemCount++, tagHashCollection, typeId);
                    }
                }
                else
                {
                    logStr.Append("CacheIndexInternal.ItemIdList = null").Append(Environment.NewLine);
                }
            }
            return(logStr.ToString());
        }
예제 #4
0
        /// <summary>
        /// Gets printable CacheIndex.
        /// </summary>
        /// <param name="clientIndex">client index.</param>
        /// <param name="tagHashCollection">tag Hash Collection.</param>
        /// <param name="typeId">type id.</param>
        /// <returns></returns>
        internal static string GetPrintableCacheIndex(CacheIndex clientIndex, TagHashCollection tagHashCollection, short typeId)
        {
            StringBuilder logStr = new StringBuilder();

            logStr.Append(Environment.NewLine).Append("Client Index Info").Append(Environment.NewLine);
            int itemCount;

            logStr.Append("IndexId : ").Append(IndexCacheUtils.GetReadableByteArray(clientIndex.IndexId)).Append(Environment.NewLine);
            logStr.Append("Metadata : ").Append(clientIndex.Metadata == null ? "Null" : IndexCacheUtils.GetReadableByteArray(clientIndex.Metadata)).Append(Environment.NewLine);
            logStr.Append("TargetIndexName : ").Append(clientIndex.TargetIndexName).Append(Environment.NewLine);
            logStr.Append("IndexTagmapping : ").Append(clientIndex.IndexTagMapping == null ? "Null" : clientIndex.IndexTagMapping.Count.ToString()).Append(Environment.NewLine);
            if (clientIndex.IndexTagMapping != null && clientIndex.IndexTagMapping.Count > 0)
            {
                logStr.Append("IndexTagmapping Items : ");
                foreach (var indexTag in clientIndex.IndexTagMapping)
                {
                    logStr.Append("IndexName : ").Append(indexTag.Key).Append(" Tags : ");
                    foreach (var tag in indexTag.Value)
                    {
                        logStr.Append(tag).Append(" , ");
                    }
                }
                logStr.Append(Environment.NewLine);
            }

            //AddList))
            if (clientIndex.AddList != null)
            {
                logStr.Append("ClientIndex.AddList.Count : ").Append(clientIndex.AddList.Count).Append(Environment.NewLine);

                itemCount = 0;
                foreach (IndexDataItem indexDataItem in clientIndex.AddList)
                {
                    LogItem(logStr, indexDataItem, itemCount, tagHashCollection, typeId);
                }
            }
            else
            {
                logStr.Append("ClientIndex.AddList = null").Append(Environment.NewLine);
            }

            //DeleteList
            if (clientIndex.DeleteList != null)
            {
                logStr.Append("ClientIndex.DeleteList.Count : ").Append(clientIndex.DeleteList.Count).Append(Environment.NewLine);

                itemCount = 0;
                foreach (IndexItem indexItem in clientIndex.DeleteList)
                {
                    LogItem(logStr, indexItem, itemCount, tagHashCollection, typeId);
                }
            }
            else
            {
                logStr.Append("ClientIndex.DeleteList = null").Append(Environment.NewLine);
            }
            return(logStr.ToString());
        }
예제 #5
0
        private static void LogItem(StringBuilder logStr, IItem iItem, int itemCount, TagHashCollection tagHashCollection, short typeId)
        {
            if (iItem.ItemId == null)
            {
                logStr.Append("ItemList[").Append(itemCount.ToString()).Append("].ItemId = null").Append(Environment.NewLine);
            }
            else if (iItem.ItemId.Length == 0)
            {
                logStr.Append("ItemList[").Append(itemCount.ToString()).Append("].ItemId.Length = 0").Append(Environment.NewLine);
            }
            else
            {
                logStr.Append("ItemList[").Append(itemCount.ToString()).Append("].ItemId = ").Append(IndexCacheUtils.GetReadableByteArray(iItem.ItemId)).Append(Environment.NewLine);
            }

            logStr.Append("Tags.Count : ");
            if (iItem is IndexItem)
            {
                var indexItem = (IndexItem)iItem;
                if (indexItem.Tags != null && indexItem.Tags.Count > 0)
                {
                    logStr.Append(indexItem.Tags.Count).Append(" - ");
                    foreach (var tag in indexItem.Tags)
                    {
                        logStr.Append(tag.Key).Append("=").Append(IndexCacheUtils.GetReadableByteArray(tag.Value)).
                        Append(", ");
                    }
                }
                else
                {
                    logStr.Append("0");
                }
            }
            else if (iItem is InternalItem)
            {
                var internalItem = (InternalItem)iItem;
                if (internalItem.TagList != null && internalItem.TagList.Count > 0)
                {
                    logStr.Append(internalItem.TagList.Count).Append(" - ");
                    foreach (var tag in internalItem.TagList)
                    {
                        logStr.Append(tagHashCollection.GetTagName(typeId, tag.Key)).
                        Append("=").Append(IndexCacheUtils.GetReadableByteArray(tag.Value)).
                        Append(", ");
                    }
                }
                else
                {
                    logStr.Append("0");
                }
            }
            logStr.Append(Environment.NewLine);
        }
예제 #6
0
        /// <summary>
        /// Processes the condition.
        /// </summary>
        /// <param name="internalItem">The internal item.</param>
        /// <param name="condition">The condition.</param>
        /// <param name="metadataPropertyCollection">The MetadataPropertyCollection.</param>
        /// <returns><c>true</c> if item passes the condition; otherwise, <c>false</c></returns>
        private static bool ProcessCondition(InternalItem internalItem,
                                             Condition condition,
                                             MetadataPropertyCollection metadataPropertyCollection)
        {
            IndexCacheUtils.ProcessMetadataPropertyCondition(condition, metadataPropertyCollection);

            if (condition.IsTag)
            {
                byte[] tagValue;
                internalItem.TryGetTagValue(condition.FieldName, out tagValue);
                return(condition.Process(tagValue));
            }
            return(condition.Process(internalItem.ItemId));
        }
예제 #7
0
        /// <summary>
        /// Formats the index ids.
        /// </summary>
        /// <param name="indexIds">The index ids.</param>
        /// <returns>formatted index ids</returns>
        internal static string FormatIndexIds(ICollection <byte[]> indexIds)
        {
            var stb = new StringBuilder();

            if (indexIds == null)
            {
                stb.Append("null");
            }
            else
            {
                stb.Append("Count: ").Append(indexIds.Count).Append(" IndexIds: ");
                foreach (var indexId in indexIds)
                {
                    stb.Append(IndexCacheUtils.GetReadableByteArray(indexId)).Append(",  ");
                }
            }

            return(stb.ToString());
        }
예제 #8
0
        /// <summary>
        /// Gets printable CacheIndex.
        /// </summary>
        /// <param name="clientIndex">client index.</param>
        /// <param name="tagHashCollection">tag Hash Collection.</param>
        /// <returns></returns>
        internal static string GetPrintableCacheIndex(CacheIndex clientIndex, TagHashCollection tagHashCollection, short typeId)
        {
            StringBuilder logStr = new StringBuilder();

            logStr.Append(Environment.NewLine).Append("Client Index Info").Append(Environment.NewLine);
            int itemCount;

            logStr.Append("IndexId : ").Append(IndexCacheUtils.GetReadableByteArray(clientIndex.IndexId)).
            Append(Environment.NewLine);

            //AddList
            if (clientIndex.AddList != null)
            {
                logStr.Append("ClientIndex.AddList.Count : ").Append(clientIndex.AddList.Count).Append(Environment.NewLine);

                itemCount = 0;
                foreach (IndexDataItem indexDataItem in clientIndex.AddList)
                {
                    LogItem(logStr, indexDataItem, itemCount, tagHashCollection, typeId);
                }
            }
            else
            {
                logStr.Append("ClientIndex.AddList = null").Append(Environment.NewLine);
            }

            //DeleteList
            if (clientIndex.DeleteList != null)
            {
                logStr.Append("ClientIndex.DeleteList.Count : ").Append(clientIndex.DeleteList.Count).Append(Environment.NewLine);

                itemCount = 0;
                foreach (IndexItem indexItem in clientIndex.DeleteList)
                {
                    LogItem(logStr, indexItem, itemCount, tagHashCollection, typeId);
                }
            }
            else
            {
                logStr.Append("ClientIndex.DeleteList = null").Append(Environment.NewLine);
            }
            return(logStr.ToString());
        }
예제 #9
0
        /// <summary>
        /// Sets the enter exit condition.
        /// </summary>
        private void SetEnterExitCondition()
        {
            if (IndexCondition != null)
            {
                IndexCondition.CreateConditions(PrimarySortInfo.FieldName,
                                                PrimarySortInfo.IsTag,
                                                PrimarySortInfo.SortOrderList[0],
                                                out enterCondition,
                                                out exitCondition);

                if (enterCondition != null)
                {
                    IndexCacheUtils.ProcessMetadataPropertyCondition(enterCondition, MetadataPropertyCollection);
                }

                if (exitCondition != null)
                {
                    IndexCacheUtils.ProcessMetadataPropertyCondition(exitCondition, MetadataPropertyCollection);
                }
            }
        }
예제 #10
0
        /// <summary>
        /// Processes the specified message context.
        /// </summary>
        /// <param name="messageContext">The message context.</param>
        /// <param name="storeContext">The store context.</param>
        /// <returns>Raw CacheIndexInternal</returns>
        internal static byte[] Process(MessageContext messageContext, IndexStoreContext storeContext)
        {
            short typeId;
            short messageTypeId = messageContext.TypeId;

            if (storeContext.StorageConfiguration.CacheIndexV3StorageConfig.IndexTypeMappingCollection.Contains(messageTypeId))
            {
                typeId = messageTypeId;
            }
            else if (!storeContext.RelatedTypeIds.TryGetValue(messageTypeId, out typeId))
            {
                LoggingUtil.Log.InfoFormat("Invalid TypeID for GetMessage {0}", messageTypeId);
                return(null);
            }

            return(storeContext.IndexStorageComponent.GetBuffer(
                       typeId,
                       new StorageKey(
                           IndexServerUtils.FormExtendedId(messageContext.ExtendedId, 0),
                           IndexCacheUtils.GeneratePrimaryId(messageContext.ExtendedId))));
        }
        /// <summary>
        /// Deserialize the class data from a stream.
        /// </summary>
        /// <param name="reader">The <see cref="IPrimitiveReader"/> that extracts used to extra data from a stream.</param>
        /// <param name="version">The value of <see cref="CurrentVersion"/> that was written to the stream when it was originally serialized to a stream;
        /// the version of the <paramref name="reader"/> data.</param>
        public void Deserialize(IPrimitiveReader reader, int version)
        {
            //Metadata
            ushort len = reader.ReadUInt16();

            if (len > 0)
            {
                Metadata = reader.ReadBytes(len);
            }

            //VirtualCount
            if (version >= 2)
            {
                virtualCount = reader.ReadInt32();
            }

            //Count
            outDeserializationContext = new OutDeserializationContext {
                TotalCount = reader.ReadInt32()
            };

            if (InDeserializationContext.DeserializeHeaderOnly)
            {
                //Note: If InDeserializationContext.DeserializeHeaderOnly property is set then InDeserializationContext.PartialByteArray shall hold all CacheIndexInternal
                //payload except metadata and header (just virtual count for now). This code path will only be used if just
                //header info like virtual count needs to be updated keeping rest of the index untouched.
                //InDeserializationContext.PartialByteArray shall be used in Serialize code
                outDeserializationContext.UnserializedCacheIndexInternal =
                    new byte[(int)reader.BaseStream.Length - (int)reader.BaseStream.Position + 1];
                reader.BaseStream.Read(outDeserializationContext.UnserializedCacheIndexInternal, 0, outDeserializationContext.UnserializedCacheIndexInternal.Length);
            }
            else
            {
                int actualItemCount = outDeserializationContext.TotalCount;

                //this.InDeserializationContext.MaxItemsPerIndex = 0 indicates need to extract all items
                //this.InDeserializationContext.MaxItemsPerIndex > 0 indicates need to extract only number of items indicated by InDeserializationContext.MaxItemsPerIndex
                if (InDeserializationContext.MaxItemsPerIndex > 0)
                {
                    if (InDeserializationContext.MaxItemsPerIndex < outDeserializationContext.TotalCount)
                    {
                        actualItemCount = InDeserializationContext.MaxItemsPerIndex;
                    }
                }

                #region Populate InternalItemList

                byte[]       itemId;
                InternalItem internalItem;
                bool         enterConditionPassed = false;

                InternalItemList = new InternalItemList();

                // Note: ---- Termination condition of the loop
                // For full index extraction loop shall terminate because of condition : internalItemList.Count < actualItemCount
                // For partial index extraction loop shall terminate because of following conditions
                //				a)  i < InDeserializationContext.TotalCount (when no sufficient items are found) OR
                //				b)  internalItemList.Count < actualItemCount (Item extraction cap is reached)
                int i = 0;
                while (InternalItemList.Count < actualItemCount && i < outDeserializationContext.TotalCount)
                {
                    i++;

                    #region Deserialize ItemId

                    len = reader.ReadUInt16();
                    if (len > 0)
                    {
                        itemId = reader.ReadBytes(len);
                    }
                    else
                    {
                        throw new Exception("Invalid ItemId - is null or length is zero for IndexId : " +
                                            IndexCacheUtils.GetReadableByteArray(InDeserializationContext.IndexId));
                    }

                    #endregion

                    #region Process IndexCondition

                    if (InDeserializationContext.EnterCondition != null || InDeserializationContext.ExitCondition != null)
                    {
                        #region Have Enter/Exit Condition

                        if (InDeserializationContext.PrimarySortInfo.IsTag == false)
                        {
                            #region Sort by ItemId

                            if (InDeserializationContext.EnterCondition != null && enterConditionPassed == false)
                            {
                                #region enter condition processing

                                if (InDeserializationContext.EnterCondition.Process(itemId))
                                {
                                    enterConditionPassed = true;
                                    internalItem         = DeserializeInternalItem(itemId, InDeserializationContext, reader);
                                    ApplyFilterAndAddItem(internalItem);
                                }
                                else
                                {
                                    SkipDeserializeInternalItem(reader);
                                    // no filter processing required
                                }

                                #endregion
                            }
                            else if (InDeserializationContext.ExitCondition != null)
                            {
                                #region exit condition processing

                                if (InDeserializationContext.ExitCondition.Process(itemId))
                                {
                                    // since item passed exit filter, we keep it.
                                    internalItem = DeserializeInternalItem(itemId, InDeserializationContext, reader);
                                    ApplyFilterAndAddItem(internalItem);
                                }
                                else
                                {
                                    // no need to search beyond this point
                                    break;
                                }

                                #endregion
                            }
                            else if (InDeserializationContext.EnterCondition != null && enterConditionPassed && InDeserializationContext.ExitCondition == null)
                            {
                                #region enter condition processing when no exit condition exists

                                internalItem = DeserializeInternalItem(itemId, InDeserializationContext, reader);
                                ApplyFilterAndAddItem(internalItem);

                                #endregion
                            }

                            #endregion
                        }
                        else
                        {
                            byte[] tagValue;

                            #region Deserialize InternalItem and fetch PrimarySortTag value

                            internalItem = DeserializeInternalItem(itemId, InDeserializationContext, reader);
                            if (!internalItem.TryGetTagValue(InDeserializationContext.PrimarySortInfo.FieldName, out tagValue))
                            {
                                throw new Exception("PrimarySortTag Not found:  " + InDeserializationContext.PrimarySortInfo.FieldName);
                            }

                            #endregion

                            #region Sort by Tag

                            if (InDeserializationContext.EnterCondition != null && enterConditionPassed == false)
                            {
                                #region enter condition processing

                                if (InDeserializationContext.EnterCondition.Process(tagValue))
                                {
                                    enterConditionPassed = true;
                                    ApplyFilterAndAddItem(internalItem);
                                }

                                #endregion
                            }
                            else if (InDeserializationContext.ExitCondition != null)
                            {
                                #region exit condition processing

                                if (InDeserializationContext.ExitCondition.Process(tagValue))
                                {
                                    // since item passed exit filter, we keep it.
                                    ApplyFilterAndAddItem(internalItem);
                                }
                                else
                                {
                                    // no need to search beyond this point
                                    break;
                                }

                                #endregion
                            }
                            else if (InDeserializationContext.EnterCondition != null && enterConditionPassed && InDeserializationContext.ExitCondition == null)
                            {
                                #region enter condition processing when no exit condition exists

                                ApplyFilterAndAddItem(internalItem);

                                #endregion
                            }

                            #endregion
                        }

                        #endregion
                    }
                    else
                    {
                        #region No Enter/Exit Condition

                        internalItem = DeserializeInternalItem(itemId, InDeserializationContext, reader);
                        ApplyFilterAndAddItem(internalItem);

                        #endregion
                    }

                    #endregion
                }

                //Set ReadItemCount on OutDeserializationContext
                outDeserializationContext.ReadItemCount = i;

                #endregion
            }
        }
        /// <summary>
        /// Serialize the class data to a stream.
        /// </summary>
        /// <param name="writer">The <see cref="IPrimitiveWriter"/> that writes to the stream.</param>
        public void Serialize(IPrimitiveWriter writer)
        {
            //Metadata
            if (Metadata == null || Metadata.Length == 0)
            {
                writer.Write((ushort)0);
            }
            else
            {
                writer.Write((ushort)Metadata.Length);
                writer.Write(Metadata);
            }

            if (!LegacySerializationUtil.Instance.IsSupported(InDeserializationContext.TypeId))
            {
                //VirtualCount
                writer.Write(virtualCount);
            }

            // Note: If InDeserializationContext.DeserializeHeaderOnly property is set then InDeserializationContext.UnserializedCacheIndexInternal shall hold all CacheIndexInternal
            // payload except metadata and virtual count. This code path will only be used if just header info like
            // virtual count needs to be updated keeping rest of the index untouched
            if (InDeserializationContext.DeserializeHeaderOnly &&
                outDeserializationContext.UnserializedCacheIndexInternal != null &&
                outDeserializationContext.UnserializedCacheIndexInternal.Length != 0)
            {
                //Count
                writer.Write(outDeserializationContext.TotalCount);

                // UnserializedCacheIndexInternal
                writer.BaseStream.Write(outDeserializationContext.UnserializedCacheIndexInternal, 0, outDeserializationContext.UnserializedCacheIndexInternal.Length);
            }
            else
            {
                //Count
                if (InternalItemList == null || InternalItemList.Count == 0)
                {
                    writer.Write(0);
                }
                else
                {
                    writer.Write(InternalItemList.Count);

                    for (int i = 0; i < InternalItemList.Count; i++)
                    {
                        //Id
                        if (InternalItemList[i].ItemId == null || InternalItemList[i].ItemId.Length == 0)
                        {
                            throw new Exception("Invalid ItemId - is null or length is zero for IndexId : " +
                                                IndexCacheUtils.GetReadableByteArray(InDeserializationContext.IndexId));
                        }
                        writer.Write((ushort)InternalItemList[i].ItemId.Length);
                        writer.Write(InternalItemList[i].ItemId);

                        //(byte)KvpListCount
                        if (InternalItemList[i].TagList == null || InternalItemList[i].TagList.Count == 0)
                        {
                            writer.Write((byte)0);
                        }
                        else
                        {
                            writer.Write((byte)InternalItemList[i].TagList.Count);

                            //KvpList
                            byte[] stringHashValue;
                            foreach (KeyValuePair <int /*TagHashCode*/, byte[] /*TagValue*/> kvp in InternalItemList[i].TagList)
                            {
                                writer.Write(kvp.Key);
                                if (kvp.Value == null || kvp.Value.Length == 0)
                                {
                                    writer.Write((ushort)0);
                                }
                                else
                                {
                                    if (InDeserializationContext.StringHashCodeDictionary != null &&
                                        InDeserializationContext.StringHashCodeDictionary.Count > 0 &&
                                        InDeserializationContext.StringHashCodeDictionary.ContainsKey(kvp.Key))
                                    {
                                        InDeserializationContext.StringHashCollection.AddStringArray(InDeserializationContext.TypeId, kvp.Value);
                                        stringHashValue = StringHashCollection.GetHashCodeByteArray(kvp.Value);
                                        writer.Write((ushort)stringHashValue.Length);
                                        writer.Write(stringHashValue);
                                    }
                                    else
                                    {
                                        writer.Write((ushort)kvp.Value.Length);
                                        writer.Write(kvp.Value);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Gets the index header.
        /// </summary>
        /// <param name="internalIndexDictionary">The internal index dictionary.</param>
        /// <param name="targetIndexCacheIndexInternal">The targetindex CacheIndexInternal.</param>
        /// <param name="indexId">The index id.</param>
        /// <param name="query">The query.</param>
        /// <param name="indexTypeMapping">The index type mapping.</param>
        /// <param name="typeId">The type id.</param>
        /// <param name="storeContext">The store context.</param>
        /// <param name="indexInIndexIdList">The indexInIndexIdList.</param>
        /// <returns>IndexHeader</returns>
        private static IndexHeader GetIndexHeader(Dictionary <KeyValuePair <byte[], string>,
                                                              CacheIndexInternal> internalIndexDictionary,
                                                  CacheIndexInternal targetIndexCacheIndexInternal,
                                                  byte[] indexId,
                                                  BaseMultiIndexIdQuery <TQueryResult> query,
                                                  IndexTypeMapping indexTypeMapping,
                                                  short typeId,
                                                  IndexStoreContext storeContext,
                                                  int indexInIndexIdList)
        {
            byte[] metadata = null;
            MetadataPropertyCollection metadataPropertyCollection = null;

            if (CheckMetaData(internalIndexDictionary, indexTypeMapping))
            {
                if (indexTypeMapping.MetadataStoredSeperately)
                {
                    #region Check if MetadataPropertyCollection is stored seperately

                    IndexServerUtils.GetMetadataStoredSeperately(indexTypeMapping,
                                                                 typeId,
                                                                 (query.PrimaryIdList != null && indexInIndexIdList < query.PrimaryIdList.Count) ?
                                                                 query.PrimaryIdList[indexInIndexIdList]:
                                                                 IndexCacheUtils.GeneratePrimaryId(indexId),
                                                                 indexId,
                                                                 storeContext,
                                                                 out metadata,
                                                                 out metadataPropertyCollection);

                    #endregion
                }
                else
                {
                    #region Check metadata on targetIndex

                    if (indexTypeMapping.IndexCollection[query.TargetIndexName].MetadataPresent)
                    {
                        if (indexTypeMapping.IndexCollection[query.TargetIndexName].IsMetadataPropertyCollection)
                        {
                            metadataPropertyCollection = targetIndexCacheIndexInternal.MetadataPropertyCollection;
                        }
                        else
                        {
                            metadata = targetIndexCacheIndexInternal.Metadata;
                        }
                    }

                    #endregion

                    #region Check metadata on other extracted indexes

                    if (query.TagsFromIndexes != null)
                    {
                        foreach (string indexName in query.TagsFromIndexes)
                        {
                            if (indexTypeMapping.IndexCollection[indexName].MetadataPresent)
                            {
                                if (indexTypeMapping.IndexCollection[indexName].IsMetadataPropertyCollection)
                                {
                                    metadataPropertyCollection =
                                        internalIndexDictionary[new KeyValuePair <byte[], string>(indexId, indexName)].
                                        MetadataPropertyCollection;
                                }
                                else
                                {
                                    metadata =
                                        internalIndexDictionary[new KeyValuePair <byte[], string>(indexId, indexName)].
                                        Metadata;
                                }
                            }
                        }
                    }

                    #endregion
                }
            }

            return(new IndexHeader
            {
                Metadata = metadata,
                MetadataPropertyCollection = metadataPropertyCollection,
                VirtualCount = targetIndexCacheIndexInternal.VirtualCount
            });
        }
        /// <summary>
        /// Processes the specified query.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <param name="messageContext">The message context.</param>
        /// <param name="storeContext">The store context.</param>
        /// <returns>Query Result</returns>
        internal TQueryResult Process(BaseMultiIndexIdQuery <TQueryResult> query,
                                      MessageContext messageContext,
                                      IndexStoreContext storeContext)
        {
            TQueryResult      result;
            List <ResultItem> resultItemList = new List <ResultItem>();
            Dictionary <byte[], IndexHeader> indexIdIndexHeaderMapping = null;
            bool             isTagPrimarySort             = false;
            string           sortFieldName                = null;
            List <SortOrder> sortOrderList                = null;
            int              totalCount                   = 0;
            int              additionalAvailableItemCount = 0;
            GroupByResult    groupByResult                = null;
            StringBuilder    exceptionInfo                = new StringBuilder();
            int              indexCap         = 0;
            IndexTypeMapping indexTypeMapping =
                storeContext.StorageConfiguration.CacheIndexV3StorageConfig.IndexTypeMappingCollection[messageContext.TypeId];

            try
            {
                if (query.IndexIdList.Count > 0)
                {
                    #region Validate Query

                    ValidateQuery(indexTypeMapping, query, messageContext);

                    #endregion

                    #region Set sort vars

                    Index targetIndexInfo = indexTypeMapping.IndexCollection[query.TargetIndexName];
                    indexCap = targetIndexInfo.MaxIndexSize;

                    if (query.TagSort != null)
                    {
                        isTagPrimarySort = query.TagSort.IsTag;
                        sortFieldName    = query.TagSort.TagName;
                        sortOrderList    = new List <SortOrder>(1)
                        {
                            query.TagSort.SortOrder
                        };
                    }
                    else
                    {
                        isTagPrimarySort = targetIndexInfo.PrimarySortInfo.IsTag;
                        sortFieldName    = targetIndexInfo.PrimarySortInfo.FieldName;
                        sortOrderList    = targetIndexInfo.PrimarySortInfo.SortOrderList;
                    }
                    BaseComparer baseComparer = new BaseComparer(isTagPrimarySort, sortFieldName, sortOrderList);
                    groupByResult = new GroupByResult(baseComparer);
                    #endregion

                    #region Prepare ResultList

                    CacheIndexInternal targetIndex;
                    IndexIdParams      indexIdParam;
                    int    maxExtractCount;
                    byte[] metadata;
                    MetadataPropertyCollection metadataPropertyCollection;
                    Dictionary <KeyValuePair <byte[], string>, CacheIndexInternal> internalIndexDictionary = new Dictionary <KeyValuePair <byte[], string>, CacheIndexInternal>();

                    int maxMergeCount = query.MaxMergeCount;

                    IndexCondition queryIndexCondition = query.IndexCondition;

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

                        indexIdParam    = query.GetParamsForIndexId(query.IndexIdList[i]);
                        maxExtractCount = ComputeMaxExtractCount(indexIdParam.MaxItems,
                                                                 query.GetAdditionalAvailableItemCount,
                                                                 indexIdParam.Filter,
                                                                 query.MaxMergeCount);

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

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

                        #endregion

                        if (targetIndex != null)
                        {
                            totalCount += targetIndex.OutDeserializationContext.TotalCount;
                            additionalAvailableItemCount += targetIndex.Count;
                            internalIndexDictionary.Add(new KeyValuePair <byte[], string>(query.IndexIdList[i], query.TargetIndexName),
                                                        targetIndex);

                            SetItemCounter(messageContext.TypeId, targetIndex.OutDeserializationContext);

                            #region Dynamic tag sort

                            if (query.TagSort != null)
                            {
                                targetIndex.Sort(query.TagSort);
                            }

                            #endregion

                            #region Get items from index and merge

                            if (query.GroupBy == null)
                            {
                                MergeAlgo.MergeItemLists(ref resultItemList,
                                                         CacheIndexInternalAdapter.GetResultItemList(targetIndex, 1, int.MaxValue),
                                                         query.MaxMergeCount,
                                                         baseComparer);
                            }
                            else
                            {
                                MergeAlgo.MergeGroupResult(ref groupByResult,
                                                           targetIndex.GroupByResult,
                                                           query.MaxMergeCount,
                                                           baseComparer);
                            }

                            if ((i != query.IndexIdList.Count - 1) && (resultItemList.Count == maxMergeCount))
                            {
                                AdjustIndexCondition(GetConditionBoundaryBytes(resultItemList, groupByResult, query.GroupBy, isTagPrimarySort, sortFieldName),
                                                     ref queryIndexCondition,
                                                     baseComparer);
                            }

                            #endregion
                        }
                    }

                    #endregion

                    #region Subset Processing

                    ProcessSubsets(query, ref resultItemList, ref groupByResult, baseComparer);

                    #endregion

                    #region Get Extra Tags for IndexIds in the list

                    //Note: Getting extra tags from GroupByResult not supported for now

                    if (query.TagsFromIndexes != null && query.TagsFromIndexes.Count != 0)
                    {
                        KeyValuePair <byte[] /*IndexId */, string /*IndexName*/> kvp;
                        CacheIndexInternal additionalCacheIndexInternal;

                        #region Form IndexId - PrimaryId Mapping

                        Dictionary <byte[] /*IndexId */, int /*PrimaryId*/> indexIdPrimaryIdMapping =
                            new Dictionary <byte[] /*IndexId */, int /*PrimaryId*/>(query.IndexIdList.Count, new ByteArrayEqualityComparer());
                        if (query.PrimaryIdList != null && query.PrimaryIdList.Count > 0)
                        {
                            //Form dictionary of IndexIdPrimaryIdMapping
                            for (int i = 0; i < query.IndexIdList.Count && i < query.PrimaryIdList.Count; i++)
                            {
                                indexIdPrimaryIdMapping.Add(query.IndexIdList[i], query.PrimaryIdList[i]);
                            }
                        }

                        #endregion

                        int indexPrimaryId;
                        foreach (ResultItem resultItem in resultItemList)
                        {
                            foreach (string indexName in query.TagsFromIndexes)
                            {
                                Index indexInfo = indexTypeMapping.IndexCollection[indexName];
                                kvp = new KeyValuePair <byte[], string>(resultItem.IndexId, indexName);
                                if (!internalIndexDictionary.ContainsKey(kvp))
                                {
                                    additionalCacheIndexInternal = IndexServerUtils.GetCacheIndexInternal(storeContext,
                                                                                                          messageContext.TypeId,
                                                                                                          indexIdPrimaryIdMapping.TryGetValue(resultItem.IndexId, out indexPrimaryId) ?
                                                                                                          indexPrimaryId :
                                                                                                          IndexCacheUtils.GeneratePrimaryId(resultItem.IndexId),
                                                                                                          resultItem.IndexId,
                                                                                                          indexInfo.ExtendedIdSuffix,
                                                                                                          indexName,
                                                                                                          0,
                                                                                                          null,
                                                                                                          true,
                                                                                                          null,
                                                                                                          false,
                                                                                                          false,
                                                                                                          indexInfo.PrimarySortInfo,
                                                                                                          indexInfo.LocalIdentityTagList,
                                                                                                          indexInfo.StringHashCodeDictionary,
                                                                                                          null,
                                                                                                          indexInfo.IsMetadataPropertyCollection,
                                                                                                          null,
                                                                                                          query.DomainSpecificProcessingType,
                                                                                                          storeContext.DomainSpecificConfig,
                                                                                                          null,
                                                                                                          null,
                                                                                                          false);

                                    if (additionalCacheIndexInternal != null)
                                    {
                                        SetItemCounter(messageContext.TypeId, additionalCacheIndexInternal.OutDeserializationContext);

                                        internalIndexDictionary.Add(kvp, additionalCacheIndexInternal);
                                        try
                                        {
                                            IndexServerUtils.GetTags(additionalCacheIndexInternal, resultItem, resultItem);
                                        }
                                        catch (Exception ex)
                                        {
                                            LoggingUtil.Log.Error(ex.ToString());
                                            exceptionInfo.Append(" | " + ex.Message);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    #endregion

                    #region Get IndexHeader

                    if (query.GetIndexHeaderType == GetIndexHeaderType.AllIndexIds)
                    {
                        //Get IndexHeader for all IndexIds
                        indexIdIndexHeaderMapping = new Dictionary <byte[], IndexHeader>(new ByteArrayEqualityComparer());

                        for (int i = 0; i < query.IndexIdList.Count; i++)
                        {
                            byte[]             indexId = query.IndexIdList[i];
                            CacheIndexInternal targetIndexCacheIndexInternal;

                            if (!indexIdIndexHeaderMapping.ContainsKey(indexId) &&
                                internalIndexDictionary.TryGetValue(new KeyValuePair <byte[], string>(indexId, query.TargetIndexName), out targetIndexCacheIndexInternal))
                            {
                                indexIdIndexHeaderMapping.Add(indexId, GetIndexHeader(internalIndexDictionary,
                                                                                      targetIndexCacheIndexInternal,
                                                                                      indexId,
                                                                                      query,
                                                                                      indexTypeMapping,
                                                                                      messageContext.TypeId,
                                                                                      storeContext,
                                                                                      i));
                            }
                        }
                    }
                    else if (query.GetIndexHeaderType == GetIndexHeaderType.ResultItemsIndexIds)
                    {
                        //Get IndexHeader just for IndexIds present in the result
                        indexIdIndexHeaderMapping = new Dictionary <byte[], IndexHeader>(new ByteArrayEqualityComparer());

                        if (query.GroupBy == null)
                        {
                            for (int i = 0; i < resultItemList.Count; i++)
                            {
                                ResultItem resultItem = resultItemList[i];
                                if (!indexIdIndexHeaderMapping.ContainsKey(resultItem.IndexId))
                                {
                                    CacheIndexInternal targetIndexCacheIndexInternal;
                                    internalIndexDictionary.TryGetValue(new KeyValuePair <byte[], string>(resultItem.IndexId, query.TargetIndexName),
                                                                        out targetIndexCacheIndexInternal);
                                    indexIdIndexHeaderMapping.Add(resultItem.IndexId,
                                                                  GetIndexHeader(internalIndexDictionary,
                                                                                 targetIndexCacheIndexInternal,
                                                                                 resultItem.IndexId,
                                                                                 query,
                                                                                 indexTypeMapping,
                                                                                 messageContext.TypeId,
                                                                                 storeContext,
                                                                                 i));
                                }
                            }
                        }
                        else
                        {
                            foreach (ResultItemBag resultItemBag in groupByResult)
                            {
                                for (int i = 0; i < resultItemBag.Count; i++)
                                {
                                    ResultItem resultItem = resultItemBag[i];
                                    if (!indexIdIndexHeaderMapping.ContainsKey(resultItem.IndexId))
                                    {
                                        CacheIndexInternal targetIndexCacheIndexInternal;
                                        internalIndexDictionary.TryGetValue(new KeyValuePair <byte[], string>(resultItem.IndexId, query.TargetIndexName),
                                                                            out targetIndexCacheIndexInternal);
                                        indexIdIndexHeaderMapping.Add(resultItem.IndexId,
                                                                      GetIndexHeader(internalIndexDictionary,
                                                                                     targetIndexCacheIndexInternal,
                                                                                     resultItem.IndexId,
                                                                                     query,
                                                                                     indexTypeMapping,
                                                                                     messageContext.TypeId,
                                                                                     storeContext,
                                                                                     i));
                                    }
                                }
                            }
                        }
                    }

                    #endregion

                    #region Get data

                    if (!query.ExcludeData)
                    {
                        DataTierUtil.GetData(resultItemList,
                                             groupByResult,
                                             storeContext, messageContext,
                                             indexTypeMapping.FullDataIdFieldList,
                                             query.FullDataIdInfo);
                    }

                    #endregion
                }

                result = new TQueryResult
                {
                    ResultItemList            = resultItemList,
                    IndexIdIndexHeaderMapping = indexIdIndexHeaderMapping,
                    TotalCount = totalCount,
                    AdditionalAvailableItemCount = additionalAvailableItemCount,
                    IsTagPrimarySort             = isTagPrimarySort,
                    SortFieldName = sortFieldName,
                    SortOrderList = sortOrderList,
                    IndexCap      = indexCap,
                    GroupByResult = groupByResult,
                    ExceptionInfo = exceptionInfo.ToString()
                };

                #region Log Potentially Bad Queries

                if (indexTypeMapping.QueryOverrideSettings != null &&
                    indexTypeMapping.QueryOverrideSettings.MaxResultItemsThresholdLog > 0 &&
                    resultItemList != null &&
                    resultItemList.Count > indexTypeMapping.QueryOverrideSettings.MaxResultItemsThresholdLog)
                {
                    LoggingUtil.Log.ErrorFormat("Encountered potentially Bad Paged Query with Large Result Set of {0}.  AddressHistory: {1}.  Query Info: {2}",
                                                resultItemList.Count,
                                                IndexServerUtils.FormatAddressHistory(messageContext.AddressHistory),
                                                FormatQueryInfo(query));
                }

                LoggingUtil.Log.DebugFormat("QueryInfo: {0}, AddressHistory: {1}", FormatQueryInfo(query), IndexServerUtils.FormatAddressHistory(messageContext.AddressHistory));

                #endregion

                SetIndexIdListCounter(messageContext.TypeId, query);
            }
            catch (Exception ex)
            {
                exceptionInfo.Append(" | " + ex.Message);
                result = new TQueryResult
                {
                    ExceptionInfo = exceptionInfo.ToString()
                };
                LoggingUtil.Log.ErrorFormat("TypeId {0} -- Error processing PagedIndexQuery : {1}", messageContext.TypeId, ex);
            }
            return(result);
        }
        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);
        }
예제 #16
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
                }
            }
        }
예제 #17
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);
                }
            }
        }
        /// <summary>
        /// Gets the index header.
        /// </summary>
        /// <param name="internalIndexDictionary">The internal index dictionary.</param>
        /// <param name="indexId">The index id.</param>
        /// <param name="query">The query.</param>
        /// <param name="indexTypeMapping">The index type mapping.</param>
        /// <param name="typeId">The type id.</param>
        /// <param name="primaryId">The primary id.</param>
        /// <param name="extendedId">The extended id.</param>
        /// <param name="storeContext">The store context.</param>
        /// <returns>IndexHeader</returns>
        private static IndexHeader GetIndexHeader(Dictionary <KeyValuePair <byte[], string>, CacheIndexInternal> internalIndexDictionary,
                                                  CacheIndexInternal targetIndexCacheIndexInternal,
                                                  byte[] indexId,
                                                  BaseMultiIndexIdQuery <TQueryResult> query,
                                                  IndexTypeMapping indexTypeMapping,
                                                  short typeId,
                                                  IndexStoreContext storeContext)
        {
            IndexHeader indexHeader = new IndexHeader();
            KeyValuePair <byte[] /*IndexId */, string /*IndexName*/> kvp;

            #region Metadata

            if (CheckMetaData(internalIndexDictionary, indexTypeMapping))
            {
                if (indexTypeMapping.MetadataStoredSeperately)
                {
                    #region Check if metadata is stored seperately

                    //Send a get message to local index storage and fetch seperately stored metadata
                    RelayMessage getMsg = new RelayMessage(typeId, IndexCacheUtils.GeneratePrimaryId(indexId), indexId, MessageType.Get);
                    storeContext.IndexStorageComponent.HandleMessage(getMsg);

                    if (getMsg.Payload != null)
                    {
                        indexHeader.Metadata = getMsg.Payload.ByteArray;
                    }

                    #endregion
                }
                else
                {
                    #region Check metadata on targetIndex

                    if (indexTypeMapping.IndexCollection[query.TargetIndexName].MetadataPresent)
                    {
                        indexHeader.Metadata = targetIndexCacheIndexInternal.Metadata;
                    }

                    #endregion

                    #region Check metadata on other extracted indexes

                    if (query.TagsFromIndexes != null)
                    {
                        foreach (string indexName in query.TagsFromIndexes)
                        {
                            if (indexTypeMapping.IndexCollection[indexName].MetadataPresent)
                            {
                                indexHeader.Metadata = internalIndexDictionary[new KeyValuePair <byte[], string>(indexId, indexName)].Metadata;
                            }
                        }
                    }

                    #endregion
                }
            }

            #endregion

            #region VirtualCount

            indexHeader.VirtualCount = targetIndexCacheIndexInternal.VirtualCount;

            #endregion

            return(indexHeader);
        }
예제 #19
0
 /// <summary>
 /// Processes the get message.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="messageContext">The message context.</param>
 private void ProcessGetMessage(RelayMessage message, MessageContext messageContext)
 {
     byte[] payloadByteArray = GetProcessor.Process(messageContext, storeContext);
     if (payloadByteArray != null)
     {
         bool compress = storeContext.GetCompressOption(message.TypeId);
         message.Payload = new RelayPayload(message.TypeId, message.Id, payloadByteArray, compress);
     }
     else
     {
         LoggingUtil.Log.InfoFormat("Miss in CacheIndexStorage for Id :  {0}, ExtendedId : ", message.Id, IndexCacheUtils.GetReadableByteArray(message.ExtendedId));
     }
 }
예제 #20
0
        /// <summary>
        /// Processes the specified intersection query.
        /// </summary>
        /// <param name="intersectionQuery">The intersection query.</param>
        /// <param name="messageContext">The message context.</param>
        /// <param name="storeContext">The store context.</param>
        /// <returns>IntersectionQueryResult</returns>
        internal static IntersectionQueryResult Process(IntersectionQuery intersectionQuery,
                                                        MessageContext messageContext,
                                                        IndexStoreContext storeContext)
        {
            //Fetch each index (assume all indexes are local) and perform intersection and return the results
            IntersectionQueryResult          intersectionQueryResult;
            List <IndexDataItem>             resultItemList            = null;
            Dictionary <byte[], IndexHeader> indexIdIndexHeaderMapping = new Dictionary <byte[], IndexHeader>(new ByteArrayEqualityComparer());
            List <string>    localIdentityTagNames = null;
            bool             isTagPrimarySort      = false;
            string           sortFieldName         = null;
            List <SortOrder> sortOrderList         = null;
            StringBuilder    exceptionInfo         = new StringBuilder();

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

                    #region Set sort vars

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

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

                    #endregion

                    #region Fetch CacheIndexInternal and Intersect

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

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

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

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

                        #endregion

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

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

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

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

                            #region Intersection

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

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

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

                        #region Get MetaData

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

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

                    #endregion

                    #region Get data

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

                    #endregion
                }

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

                // update performance counter
                PerformanceCounters.Instance.SetCounterValue(
                    PerformanceCounterEnum.IndexLookupAvgPerIntersectionQuery,
                    messageContext.TypeId,
                    intersectionQuery.IndexIdList.Count);
            }
            catch (Exception ex)
            {
                exceptionInfo.Append(" | " + ex.Message);
                intersectionQueryResult = new IntersectionQueryResult(null, null, null, false, null, null, exceptionInfo.ToString());
                LoggingUtil.Log.ErrorFormat("TypeId {0} -- Error processing IntersectionQuery : {1}", messageContext.TypeId, ex);
            }
            return(intersectionQueryResult);
        }
예제 #21
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);
                }
            }
        }
예제 #22
0
        /// <summary>
        /// Processes the specified MetadataPropertyQuery.
        /// </summary>
        /// <param name="metadataPropertyQuery">The MetadataPropertyQuery.</param>
        /// <param name="messageContext">The message context.</param>
        /// <param name="storeContext">The store context.</param>
        /// <returns></returns>
        internal static MetadataPropertyQueryResult Process(MetadataPropertyQuery metadataPropertyQuery, MessageContext messageContext, IndexStoreContext storeContext)
        {
            MetadataPropertyQueryResult metadataPropertyQueryResult;
            MetadataPropertyCollection  metadataPropertyCollection = null;

            try
            {
                IndexTypeMapping indexTypeMapping =
                    storeContext.StorageConfiguration.CacheIndexV3StorageConfig.IndexTypeMappingCollection[messageContext.TypeId];

                if (indexTypeMapping.IsMetadataPropertyCollection || indexTypeMapping.IndexCollection[metadataPropertyQuery.TargetIndexName].IsMetadataPropertyCollection)
                {
                    #region Fetch MetadataPropertyCollection

                    if (indexTypeMapping.MetadataStoredSeperately)
                    {
                        byte[] metadata;
                        IndexServerUtils.GetMetadataStoredSeperately(indexTypeMapping,
                                                                     messageContext.TypeId,
                                                                     metadataPropertyQuery.PrimaryId,
                                                                     metadataPropertyQuery.IndexId,
                                                                     storeContext,
                                                                     out metadata,
                                                                     out metadataPropertyCollection);
                    }
                    else
                    {
                        // Get CacheIndexInternal
                        Index indexInfo = indexTypeMapping.IndexCollection[metadataPropertyQuery.TargetIndexName];
                        CacheIndexInternal cacheIndexInternal = IndexServerUtils.GetCacheIndexInternal(storeContext,
                                                                                                       messageContext.
                                                                                                       TypeId,
                                                                                                       metadataPropertyQuery
                                                                                                       .PrimaryId,
                                                                                                       metadataPropertyQuery
                                                                                                       .IndexId,
                                                                                                       indexInfo.
                                                                                                       ExtendedIdSuffix,
                                                                                                       metadataPropertyQuery
                                                                                                       .
                                                                                                       TargetIndexName,
                                                                                                       0,
                                                                                                       null,
                                                                                                       false,
                                                                                                       null,
                                                                                                       true,
                                                                                                       false,
                                                                                                       null,
                                                                                                       null,
                                                                                                       null,
                                                                                                       null,
                                                                                                       true,
                                                                                                       null,
                                                                                                       DomainSpecificProcessingType
                                                                                                       .None,
                                                                                                       null,
                                                                                                       null,
                                                                                                       null,
                                                                                                       true);

                        if (cacheIndexInternal != null)
                        {
                            metadataPropertyCollection = cacheIndexInternal.MetadataPropertyCollection;
                        }
                    }

                    #endregion
                }

                metadataPropertyQueryResult = new MetadataPropertyQueryResult
                {
                    MetadataPropertyCollection = metadataPropertyCollection
                };
            }
            catch (Exception ex)
            {
                metadataPropertyQueryResult = new MetadataPropertyQueryResult
                {
                    MetadataPropertyCollection = metadataPropertyCollection,
                    ExceptionInfo = ex.Message
                };
                LoggingUtil.Log.ErrorFormat("TypeId {0} -- Error processing MetadataPropertyQuery : {1} for IndexId : {2} and TargetIndexname : {3}",
                                            messageContext.TypeId,
                                            ex,
                                            metadataPropertyQuery.IndexId != null ? IndexCacheUtils.GetReadableByteArray(metadataPropertyQuery.IndexId) : "Null",
                                            metadataPropertyQuery.TargetIndexName ?? "Null");
            }
            return(metadataPropertyQueryResult);
        }
예제 #23
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
                }
            }
        }
예제 #24
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);
        }
예제 #25
0
 public bool EqualsLocalId(T item1, T item2, List <string> localIdentityTagNames)
 {
     return(IndexCacheUtils.EqualsLocalId(item1, item2, localIdentityTagNames));
 }