/// <summary> /// Determine among the passed in filter parameters any parameter that matches the given index on property /// name and filter operator type. Returns null if none of the parameters matches the index. /// </summary> /// <param name="parameters">is the filter parameter list</param> /// <param name="index">is a filter parameter constant value index</param> /// <returns> /// filter parameter, or null if no matching parameter found. /// </returns> public static FilterValueSetParam FindParameter( IEnumerable <FilterValueSetParam> parameters, FilterParamIndexBase index) { if (index is FilterParamIndexLookupableBase) { var propBasedIndex = (FilterParamIndexLookupableBase)index; var indexLookupable = propBasedIndex.Lookupable; var indexOperator = propBasedIndex.FilterOperator; foreach (FilterValueSetParam parameter in parameters) { var lookupable = parameter.Lookupable; var paramOperator = parameter.FilterOperator; if ((lookupable.Equals(indexLookupable)) && (paramOperator.Equals(indexOperator))) { return(parameter); } } } else { foreach (FilterValueSetParam parameter in parameters) { var paramOperator = parameter.FilterOperator; if (paramOperator.Equals(index.FilterOperator)) { return(parameter); } } } return(null); }
private void Verify(FilterParamIndexBase index, long?testValue, bool[] expected) { _testBean.LongBoxed = testValue; index.MatchEvent(_testEventBean, _matchesList); for (int i = 0; i < expected.Length; i++) { Assert.AreEqual(expected[i], _testEvaluators[i].GetAndResetCountInvoked() == 1); } }
public void TestFindParameter() { FilterParamIndexBase indexOne = IndexFactory.CreateIndex(MakeLookupable("BoolPrimitive"), _lockFactory, FilterOperator.EQUAL); Assert.IsNull(IndexHelper.FindParameter(_parameters, indexOne)); FilterParamIndexBase indexTwo = IndexFactory.CreateIndex(MakeLookupable("TheString"), _lockFactory, FilterOperator.EQUAL); Assert.AreEqual(_parameterThree, IndexHelper.FindParameter(_parameters, indexTwo)); FilterParamIndexBase indexThree = IndexFactory.CreateIndex(MakeLookupable("IntPrimitive"), _lockFactory, FilterOperator.GREATER); Assert.AreEqual(_parameterOne, IndexHelper.FindParameter(_parameters, indexThree)); }
public void TestCreateIndex() { // Create a "greater" index FilterParamIndexBase index = IndexFactory.CreateIndex(MakeLookupable("IntPrimitive"), _lockFactory, FilterOperator.GREATER); Assert.IsTrue(index != null); Assert.IsTrue(index is FilterParamIndexCompare); Assert.IsTrue(GetPropName(index).Equals("IntPrimitive")); Assert.IsTrue(index.FilterOperator == FilterOperator.GREATER); // Create an "equals" index index = IndexFactory.CreateIndex(MakeLookupable("TheString"), _lockFactory, FilterOperator.EQUAL); Assert.IsTrue(index != null); Assert.IsTrue(index is FilterParamIndexEquals); Assert.AreEqual(GetPropName(index), "TheString"); Assert.IsTrue(index.FilterOperator == FilterOperator.EQUAL); // Create an "not equals" index index = IndexFactory.CreateIndex(MakeLookupable("TheString"), _lockFactory, FilterOperator.NOT_EQUAL); Assert.IsTrue(index != null); Assert.IsTrue(index is FilterParamIndexNotEquals); Assert.AreEqual(GetPropName(index), "TheString"); Assert.IsTrue(index.FilterOperator == FilterOperator.NOT_EQUAL); // Create a range index index = IndexFactory.CreateIndex(MakeLookupable("DoubleBoxed"), _lockFactory, FilterOperator.RANGE_CLOSED); Assert.IsTrue(index is FilterParamIndexDoubleRange); index = IndexFactory.CreateIndex(MakeLookupable("DoubleBoxed"), _lockFactory, FilterOperator.NOT_RANGE_CLOSED); Assert.IsTrue(index is FilterParamIndexDoubleRangeInverted); // Create a in-index index = IndexFactory.CreateIndex(MakeLookupable("DoubleBoxed"), _lockFactory, FilterOperator.IN_LIST_OF_VALUES); Assert.IsTrue(index is FilterParamIndexIn); index = IndexFactory.CreateIndex(MakeLookupable("DoubleBoxed"), _lockFactory, FilterOperator.NOT_IN_LIST_OF_VALUES); Assert.IsTrue(index is FilterParamIndexNotIn); // Create a boolean-expression-index index = IndexFactory.CreateIndex(MakeLookupable("boolean"), _lockFactory, FilterOperator.BOOLEAN_EXPRESSION); Assert.IsTrue(index is FilterParamIndexBooleanExpr); index = IndexFactory.CreateIndex(MakeLookupable("boolean"), _lockFactory, FilterOperator.BOOLEAN_EXPRESSION); Assert.IsTrue(index is FilterParamIndexBooleanExpr); }
public void TestFindIndex() { List <FilterParamIndexBase> indexes = new List <FilterParamIndexBase>(); // Create index list wity index that doesn't match FilterParamIndexBase indexOne = IndexFactory.CreateIndex(MakeLookupable("BoolPrimitive"), _lockFactory, FilterOperator.EQUAL); indexes.Add(indexOne); Assert.IsTrue(IndexHelper.FindIndex(_parameters, indexes) == null); // Create index list wity index that doesn't match indexOne = IndexFactory.CreateIndex(MakeLookupable("DoubleBoxed"), _lockFactory, FilterOperator.GREATER_OR_EQUAL); indexes.Clear(); indexes.Add(indexOne); Assert.IsTrue(IndexHelper.FindIndex(_parameters, indexes) == null); // Add an index that does match a parameter FilterParamIndexBase indexTwo = IndexFactory.CreateIndex(MakeLookupable("DoubleBoxed"), _lockFactory, FilterOperator.GREATER); indexes.Add(indexTwo); Pair <FilterValueSetParam, FilterParamIndexBase> pair = IndexHelper.FindIndex(_parameters, indexes); Assert.IsTrue(pair != null); Assert.AreEqual(_parameterTwo, pair.First); Assert.AreEqual(indexTwo, pair.Second); // Add another index that does match a parameter, should return first match however which is doubleBoxed FilterParamIndexBase indexThree = IndexFactory.CreateIndex(MakeLookupable("IntPrimitive"), _lockFactory, FilterOperator.GREATER); indexes.Add(indexThree); pair = IndexHelper.FindIndex(_parameters, indexes); Assert.AreEqual(_parameterOne, pair.First); Assert.AreEqual(indexThree, pair.Second); // Try again removing one index indexes.Remove(indexTwo); pair = IndexHelper.FindIndex(_parameters, indexes); Assert.AreEqual(_parameterOne, pair.First); Assert.AreEqual(indexThree, pair.Second); }
/// <summary> /// Determine among the passed in filter parameters any parameter that matches the given index on property name and /// filter operator type. Returns null if none of the parameters matches the index. /// </summary> /// <param name="parameters">is the filter parameter list</param> /// <param name="index">is a filter parameter constant value index</param> /// <returns>filter parameter, or null if no matching parameter found.</returns> public static FilterValueSetParam FindParameter( ICollection<FilterValueSetParam> parameters, FilterParamIndexBase index) { if (index is FilterParamIndexLookupableBase) { var propBasedIndex = (FilterParamIndexLookupableBase) index; FilterSpecLookupable indexLookupable = propBasedIndex.Lookupable; FilterOperator indexOperator = propBasedIndex.FilterOperator; foreach (FilterValueSetParam parameter in parameters) { FilterSpecLookupable lookupable = parameter.Lookupable; FilterOperator paramOperator = parameter.FilterOperator; if ((lookupable.Equals(indexLookupable)) && (paramOperator.Equals(indexOperator))) { return parameter; } } } else { foreach (FilterValueSetParam parameter in parameters) { FilterOperator paramOperator = parameter.FilterOperator; if (paramOperator.Equals(index.FilterOperator)) { return parameter; } } } return null; }
private void Verify(FilterParamIndexBase index, long?testValue, int numExpected) { _testBean.LongBoxed = testValue; index.MatchEvent(_testEventBean, _matchesList); Assert.AreEqual(numExpected, _testEvaluator.GetAndResetCountInvoked()); }
private void VerifyDoublePrimitive(FilterParamIndexBase index, double testValue, int numExpected) { _testBean.DoublePrimitive = testValue; index.MatchEvent(_testEventBean, _matchesList); Assert.AreEqual(numExpected, _testEvaluator.GetAndResetCountInvoked()); }
/// <summary> /// Remove an index, returning true if it was found and removed or false if not in collection. /// NOTE: the client to this method must use the read-write lock of this object to lock, if /// required by the client code. /// </summary> /// <param name="index">is the index to remove</param> /// <returns>true if found, false if not existing</returns> public bool Remove(FilterParamIndexBase index) { return(_indizes.Remove(index)); }
/// <summary> /// Add an index. The same index can be added twice - there is no checking done. /// NOTE: the client to this method must use the read-write lock of this object /// to lock, if required by the client code. /// </summary> /// <param name="index">index to add</param> public void Add(FilterParamIndexBase index) { _indizes.Add(index); }
private String GetPropName(FilterParamIndexBase index) { FilterParamIndexLookupableBase propIndex = (FilterParamIndexLookupableBase)index; return(propIndex.Lookupable.Expression); }
public EventTypeIndexBuilderIndexLookupablePair(FilterParamIndexBase index, Object lookupable) { Index = index; Lookupable = lookupable; }
/// <summary> /// Add to an index the value to filter for. /// </summary> /// <param name="remainingParameters">The remaining parameters.</param> /// <param name="filterCallback">The filter callback.</param> /// <param name="index">is the index to add to</param> /// <param name="filterForValue">is the filter parameter value to add</param> /// <param name="treePathInfo">is the specification to fill on where is was added</param> /// <param name="lockFactory">The lock factory.</param> private static void AddToIndex( ArrayDeque <FilterValueSetParam> remainingParameters, FilterHandle filterCallback, FilterParamIndexBase index, Object filterForValue, ArrayDeque <EventTypeIndexBuilderIndexLookupablePair> treePathInfo, FilterServiceGranularLockFactory lockFactory) { if ((ExecutionPathDebugLog.IsEnabled) && (Log.IsDebugEnabled)) { Log.Debug(".addToIndex ({0}) Adding to index {1} expressionValue={2}", Thread.CurrentThread.ManagedThreadId, index, filterForValue); } EventEvaluator eventEvaluator; using (index.ReadWriteLock.AcquireReadLock()) { eventEvaluator = index[filterForValue]; // The filter parameter value already existed in bean, add and release locks if (eventEvaluator != null) { var added = AddToEvaluator(remainingParameters, filterCallback, eventEvaluator, treePathInfo, lockFactory); if (added) { return; } } } // new filter parameter value, need a write lock using (index.ReadWriteLock.AcquireWriteLock()) { eventEvaluator = index[filterForValue]; // It may exist now since another thread could have added the entry if (eventEvaluator != null) { var added = AddToEvaluator(remainingParameters, filterCallback, eventEvaluator, treePathInfo, lockFactory); if (added) { return; } // The found eventEvaluator must be converted to a new FilterHandleSetNode var nextIndexX = (FilterParamIndexBase)eventEvaluator; var newNode = new FilterHandleSetNode(lockFactory.ObtainNew()); newNode.Add(nextIndexX); index.Remove(filterForValue); index[filterForValue] = newNode; AddToNode(remainingParameters, filterCallback, newNode, treePathInfo, lockFactory); return; } // The index does not currently have this filterCallback value, // if there are no remaining parameters, create a node if (remainingParameters.IsEmpty()) { var node = new FilterHandleSetNode(lockFactory.ObtainNew()); AddToNode(remainingParameters, filterCallback, node, treePathInfo, lockFactory); index[filterForValue] = node; return; } // If there are remaining parameters, create a new index for the next parameter FilterValueSetParam parameterPickedForIndex = remainingParameters.RemoveFirst(); var nextIndex = IndexFactory.CreateIndex(parameterPickedForIndex.Lookupable, lockFactory, parameterPickedForIndex.FilterOperator); index[filterForValue] = nextIndex; treePathInfo.Add(new EventTypeIndexBuilderIndexLookupablePair(nextIndex, parameterPickedForIndex.FilterForValue)); AddToIndex(remainingParameters, filterCallback, nextIndex, parameterPickedForIndex.FilterForValue, treePathInfo, lockFactory); } }
// Remove filterCallback from index, returning true if index empty after removal private static bool RemoveFromIndex( FilterHandle filterCallback, FilterParamIndexBase index, EventTypeIndexBuilderIndexLookupablePair[] treePathInfo, int treePathPosition, Object filterForValue) { using (index.ReadWriteLock.AcquireWriteLock()) { EventEvaluator eventEvaluator = index[filterForValue]; if (eventEvaluator == null) { Log.Warn(".removeFromIndex ({0}) Could not find the filterCallback value in index, index={1} value={2} filterCallback={3}", Thread.CurrentThread.ManagedThreadId, index, filterForValue, filterCallback); return(false); } if (eventEvaluator is FilterHandleSetNode) { var node = (FilterHandleSetNode)eventEvaluator; var isEmptyX = RemoveFromNode(filterCallback, node, treePathInfo, treePathPosition); if (isEmptyX) { // Since we are holding a write lock to this index, there should not be a chance that // another thread had been adding anything to this FilterHandleSetNode index.Remove(filterForValue); } return(index.Count == 0); } var nextIndex = (FilterParamIndexBase)eventEvaluator; var nextPair = treePathPosition < treePathInfo.Length ? treePathInfo[treePathPosition++] : null; if (nextPair == null) { Log.Fatal(".removeFromIndex Expected an inner index to this index, this=" + filterCallback); Debug.Assert(false); return(false); } if (nextPair.Index != nextIndex) { Log.Fatal(".removeFromIndex Expected an index for filterCallback that differs from the found index, this=" + filterCallback + " expected=" + nextPair.Index); Debug.Assert(false); return(false); } var nextExpressionValue = nextPair.Lookupable; var isEmpty = RemoveFromIndex(filterCallback, nextPair.Index, treePathInfo, treePathPosition, nextExpressionValue); if (isEmpty) { // Since we are holding a write lock to this index, there should not be a chance that // another thread had been adding anything to this FilterHandleSetNode index.Remove(filterForValue); } var size = index.Count; return(size == 0); } }