/// <summary>Add to the current node building up the tree path information.</summary> /// <param name="remainingParameters">any remaining parameters</param> /// <param name="filterCallback">the filter callback</param> /// <param name="currentNode">is the node to add to</param> /// <param name="lockFactory">the lock factory</param> private static void AddToNode( ArrayDeque<FilterValueSetParam> remainingParameters, FilterHandle filterCallback, FilterHandleSetNode currentNode, FilterServiceGranularLockFactory lockFactory) { // If no parameters are specified, add to current node, and done if (remainingParameters.IsEmpty()) { using (currentNode.NodeRWLock.WriteLock.Acquire()) { currentNode.Add(filterCallback); } return; } // Need to find an existing index that matches one of the filter parameters Pair<FilterValueSetParam, FilterParamIndexBase> pair; using (currentNode.NodeRWLock.ReadLock.Acquire()) { pair = IndexHelper.FindIndex(remainingParameters, currentNode.Indizes); // Found an index matching a filter parameter if (pair != null) { remainingParameters.Remove(pair.First); var filterForValue = pair.First.FilterForValue; var index = pair.Second; AddToIndex(remainingParameters, filterCallback, index, filterForValue, lockFactory); return; } } // An index for any of the filter parameters was not found, create one using (currentNode.NodeRWLock.WriteLock.Acquire()) { pair = IndexHelper.FindIndex(remainingParameters, currentNode.Indizes); // Attempt to find an index again this time under a write lock if (pair != null) { remainingParameters.Remove(pair.First); var filterForValue = pair.First.FilterForValue; var indexInner = pair.Second; AddToIndex(remainingParameters, filterCallback, indexInner, filterForValue, lockFactory); return; } // No index found that matches any parameters, create a new one // Pick the next parameter for an index var parameterPickedForIndex = remainingParameters.RemoveFirst(); var index = IndexFactory.CreateIndex(parameterPickedForIndex.Lookupable, lockFactory, parameterPickedForIndex.FilterOperator); currentNode.Add(index); AddToIndex(remainingParameters, filterCallback, index, parameterPickedForIndex.FilterForValue, lockFactory); } }
public void TestNodeGetSet() { FilterHandle exprOne = new SupportFilterHandle(); // Check pre-conditions Assert.IsTrue(testNode.NodeRWLock != null); Assert.IsFalse(testNode.Contains(exprOne)); Assert.AreEqual(0, testNode.FilterCallbackCount); Assert.AreEqual(0, testNode.Indizes.Count); Assert.IsTrue(testNode.IsEmpty()); testNode.Add(exprOne); // Check after add Assert.IsTrue(testNode.Contains(exprOne)); Assert.AreEqual(1, testNode.FilterCallbackCount); Assert.IsFalse(testNode.IsEmpty()); // Add an indexOne EventType eventType = SupportEventTypeFactory .GetInstance(container) .CreateBeanType(typeof(SupportBean)); SupportExprEventEvaluator eval = new SupportExprEventEvaluator(eventType.GetGetter("IntPrimitive")); ExprFilterSpecLookupable lookupable = new ExprFilterSpecLookupable( "IntPrimitive", eval, null, eventType.GetPropertyType("IntPrimitive"), false, null); FilterParamIndexBase indexOne = new SupportFilterParamIndex(lookupable); testNode.Add(indexOne); // Check after add Assert.AreEqual(1, testNode.Indizes.Count); Assert.AreEqual(indexOne, testNode.Indizes[0]); // Check removes Assert.IsTrue(testNode.Remove(exprOne)); Assert.IsFalse(testNode.IsEmpty()); Assert.IsFalse(testNode.Remove(exprOne)); Assert.IsTrue(testNode.Remove(indexOne)); Assert.IsFalse(testNode.Remove(indexOne)); Assert.IsTrue(testNode.IsEmpty()); }
public void SetUp() { SupportBean testBean = new SupportBean(); testEventBean = SupportEventBeanFactory .GetInstance(container) .CreateObject(testBean); testEventType = testEventBean.EventType; handleSetNode = new FilterHandleSetNode(new SlimReaderWriterLock()); filterCallback = new SupportFilterHandle(); handleSetNode.Add(filterCallback); testIndex = new EventTypeIndex(new FilterServiceGranularLockFactoryReentrant( container.RWLockManager())); testIndex.Add(testEventType, handleSetNode); }
/// <summary> /// Add to an index the value to filter for. /// </summary> /// <param name="index">is the index to add to</param> /// <param name="filterForValue">is the filter parameter value to add</param> /// <param name="remainingParameters">any remaining parameters</param> /// <param name="filterCallback">the filter callback</param> /// <param name="lockFactory">the lock factory</param> private static void AddToIndex( ArrayDeque<FilterValueSetParam> remainingParameters, FilterHandle filterCallback, FilterParamIndexBase index, object filterForValue, FilterServiceGranularLockFactory lockFactory) { EventEvaluator eventEvaluator; using (index.ReadWriteLock.ReadLock.Acquire()) { eventEvaluator = index.Get(filterForValue); // The filter parameter value already existed in bean, add and release locks if (eventEvaluator != null) { var added = AddToEvaluator(remainingParameters, filterCallback, eventEvaluator, lockFactory); if (added) { return; } } } // new filter parameter value, need a write lock using (index.ReadWriteLock.WriteLock.Acquire()) { eventEvaluator = index.Get(filterForValue); // It may exist now since another thread could have added the entry if (eventEvaluator != null) { var added = AddToEvaluator(remainingParameters, filterCallback, eventEvaluator, lockFactory); if (added) { return; } // The found eventEvaluator must be converted to a new FilterHandleSetNode var nextIndexInner = (FilterParamIndexBase) eventEvaluator; var newNode = new FilterHandleSetNode(lockFactory.ObtainNew()); newNode.Add(nextIndexInner); index.Remove(filterForValue); index.Put(filterForValue, newNode); AddToNode(remainingParameters, filterCallback, newNode, 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, lockFactory); index.Put(filterForValue, node); return; } // If there are remaining parameters, create a new index for the next parameter var parameterPickedForIndex = remainingParameters.RemoveFirst(); var nextIndex = IndexFactory.CreateIndex(parameterPickedForIndex.Lookupable, lockFactory, parameterPickedForIndex.FilterOperator); index.Put(filterForValue, nextIndex); AddToIndex(remainingParameters, filterCallback, nextIndex, parameterPickedForIndex.FilterForValue, lockFactory); } }