/// <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);
            }
        }
Exemple #2
0
        public void TestFindIndex()
        {
            IList <FilterParamIndexBase> indexes = new List <FilterParamIndexBase>();

            // Create index list wity index that doesn't match
            var 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
            var indexTwo = IndexFactory.CreateIndex(MakeLookupable("DoubleBoxed"), lockFactory, FilterOperator.GREATER);

            indexes.Add(indexTwo);
            var 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
            var 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);
        }