Beispiel #1
0
        private static byte[] GetRecency(InternalItem internalItem, StreamRecencyConfig streamRecencyConfig)
        {
            //     Nt = N0 * e^(-decay constant * t)
            //i.e. Recency = InitialRecency * e^(-decay constant * timeElapsedSinceActivityCreation)

            byte[] activityTypeBytes;
            double halfLife;

            if (String.Compare("ItemId", streamRecencyConfig.TypeTagName, StringComparison.OrdinalIgnoreCase) == 0)
            {
                activityTypeBytes = internalItem.ItemId;
            }
            else
            {
                internalItem.TryGetTagValue(streamRecencyConfig.TypeTagName, out activityTypeBytes);
            }

            if (activityTypeBytes != null)
            {
                int activityType = activityTypeBytes.Length == sizeof(Int32) ?
                                   BitConverter.ToInt32(activityTypeBytes, 0) :
                                   activityTypeBytes[0];

                halfLife = streamRecencyConfig.TypeDecayMappingCollection.Contains(activityType) ?
                           streamRecencyConfig.TypeDecayMappingCollection[activityType].HalfLife :
                           streamRecencyConfig.DefaultHalfLife;
            }
            else
            {
                halfLife = streamRecencyConfig.DefaultHalfLife;
            }

            // decay constant limited to 3 decimal places
            double decayConstant = Math.Round(Math.Log(2) / halfLife, 3);

            byte[] timestampBytes;
            if (String.Compare("ItemId", streamRecencyConfig.TimeStampTagName, StringComparison.OrdinalIgnoreCase) == 0)
            {
                timestampBytes = internalItem.ItemId;
            }
            else
            {
                internalItem.TryGetTagValue(streamRecencyConfig.TimeStampTagName, out timestampBytes);
            }
            double t = DateTime.Now.Subtract(new SmallDateTime(BitConverter.ToInt32(timestampBytes, 0)).FullDateTime).TotalDays;

            return(BitConverter.GetBytes(streamRecencyConfig.InitialRecencyValue * Math.Pow(Math.E, -decayConstant * t)));
        }
Beispiel #2
0
 /// <summary>
 /// Processes the condition.
 /// </summary>
 /// <param name="internalItem">The internal item.</param>
 /// <param name="condition">The condition.</param>
 /// <returns><c>true</c> if item passes the condition; otherwise, <c>false</c></returns>
 private static bool ProcessCondition(InternalItem internalItem, Condition condition)
 {
     if (condition.IsTag)
     {
         byte[] tagValue;
         internalItem.TryGetTagValue(condition.FieldName, out tagValue);
         return(condition.Process(tagValue));
     }
     return(condition.Process(internalItem.ItemId));
 }
Beispiel #3
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));
        }
Beispiel #4
0
        /// <summary>
        /// Determines whether repositioning of index item is required or not.
        /// </summary>
        /// <param name="indexInfo">The index info.</param>
        /// <param name="addItem">The add item.</param>
        /// <param name="internalItem">The internal item.</param>
        /// <returns>
        ///     <c>true</c> if repositioning of index item is required; otherwise, <c>false</c>.
        /// </returns>
        private static bool IsRepositioningOfIndexItemRequired(Index indexInfo, IndexDataItem addItem, InternalItem internalItem)
        {
            byte[] sortTagValueInAddItem;
            if (indexInfo.PrimarySortInfo.IsTag && addItem.Tags.TryGetValue(indexInfo.PrimarySortInfo.FieldName, out sortTagValueInAddItem))
            {
                // Index is sorted by tag and tag might have been updated
                byte[] sortTagValueInIndex;
                internalItem.TryGetTagValue(indexInfo.PrimarySortInfo.FieldName, out sortTagValueInIndex);

                if (!ByteArrayComparerUtil.CompareByteArrays(sortTagValueInAddItem, sortTagValueInIndex))
                {
                    return(true);
                }
            }
            return(false);
        }
        /// <summary>
        /// Determines whether repositioning of index item is required or not.
        /// </summary>
        /// <param name="indexInfo">The index info.</param>
        /// <param name="addItem">The add item.</param>
        /// <param name="internalItem">The internal item.</param>
        /// <returns>
        /// 	<c>true</c> if repositioning of index item is required; otherwise, <c>false</c>.
        /// </returns>
        private static bool IsRepositioningOfIndexItemRequired(Index indexInfo, IndexDataItem addItem, InternalItem internalItem)
        {
            byte[] sortTagValueInAddItem;
            if (indexInfo.PrimarySortInfo.IsTag && addItem.Tags.TryGetValue(indexInfo.PrimarySortInfo.FieldName, out sortTagValueInAddItem))
            {
                // Index is sorted by tag and tag might have been updated
                byte[] sortTagValueInIndex;
                internalItem.TryGetTagValue(indexInfo.PrimarySortInfo.FieldName, out sortTagValueInIndex);

                if (!ByteArrayComparerUtil.CompareByteArrays(sortTagValueInAddItem, sortTagValueInIndex))
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// Applies the filter and adds the item.
        /// </summary>
        /// <param name="internalItem">The internal item.</param>
        private void ApplyFilterAndAddItem(InternalItem internalItem)
        {
            if (InDeserializationContext.CapCondition != null &&
                InDeserializationContext.CapCondition.FilterCaps != null &&
                InDeserializationContext.CapCondition.FilterCaps.Count > 0)
            {
                #region CapCondition Exists

                byte[] tagValue;
                if (internalItem.TryGetTagValue(InDeserializationContext.CapCondition.FieldName, out tagValue))
                {
                    #region  Filter Cap found for tagValue

                    FilterCap filterCap;
                    if (InDeserializationContext.CapCondition.FilterCaps.TryGetValue(tagValue, out filterCap))
                    {
                        if (filterCap.Cap > 0 && FilterPassed(internalItem, GetCappedOrParentFilter(filterCap)))
                        {
                            filterCap.Cap--;
                            AddItem(internalItem, false);
                        }
                    }

                    #endregion

                    #region Filter Cap not found for tagValue

                    else if (FilterPassed(internalItem, InDeserializationContext.Filter))
                    {
                        AddItem(internalItem, false);
                    }

                    #endregion
                }

                #region Apply parent filter

                else if (FilterPassed(internalItem, InDeserializationContext.Filter))
                {
                    AddItem(internalItem, false);
                }

                #endregion

                #endregion
            }

            #region CapCondition Doesn't  Exist

            else if (FilterPassed(internalItem, InDeserializationContext.Filter))
            {
                AddItem(internalItem, false);
            }

            #endregion
        }