Exemplo n.º 1
0
        private static ExprNode HandlePreviousFunctions(
            ExprNode defineItemExpression,
            IOrderedDictionary<int, IList<ExprPreviousMatchRecognizeNode>> previousNodes)
        {
            var previousVisitor = new ExprNodePreviousVisitorWParent();
            defineItemExpression.Accept(previousVisitor);

            if (previousVisitor.Previous == null) {
                return defineItemExpression;
            }

            foreach (var previousNodePair in previousVisitor.Previous) {
                var previousNode = previousNodePair.Second;
                var matchRecogPrevNode = new ExprPreviousMatchRecognizeNode();

                if (previousNodePair.Second.ChildNodes.Length == 1) {
                    matchRecogPrevNode.AddChildNode(previousNode.ChildNodes[0]);
                    matchRecogPrevNode.AddChildNode(new ExprConstantNodeImpl(1));
                }
                else if (previousNodePair.Second.ChildNodes.Length == 2) {
                    var first = previousNode.ChildNodes[0];
                    var second = previousNode.ChildNodes[1];
                    if (first is ExprConstantNode && !(second is ExprConstantNode)) {
                        matchRecogPrevNode.AddChildNode(second);
                        matchRecogPrevNode.AddChildNode(first);
                    }
                    else if (!(first is ExprConstantNode) && second is ExprConstantNode) {
                        matchRecogPrevNode.AddChildNode(first);
                        matchRecogPrevNode.AddChildNode(second);
                    }
                    else {
                        throw new ExprValidationException("PREV operator requires a constant index");
                    }
                }

                if (previousNodePair.First == null) {
                    defineItemExpression = matchRecogPrevNode;
                }
                else {
                    ExprNodeUtilityModify.ReplaceChildNode(
                        previousNodePair.First,
                        previousNodePair.Second,
                        matchRecogPrevNode);
                }

                // store in a list per index such that we can consolidate this into a single buffer
                var index = matchRecogPrevNode.ConstantIndexNumber;
                var callbackList = previousNodes.Get(index);
                if (callbackList == null) {
                    callbackList = new List<ExprPreviousMatchRecognizeNode>();
                    previousNodes.Put(index, callbackList);
                }

                callbackList.Add(matchRecogPrevNode);
            }

            return defineItemExpression;
        }
Exemplo n.º 2
0
 private void AddTrigger(long slot, ScheduleHandle handle, long triggerTime)
 {
     var handleSet = _timeHandleMap.Get(triggerTime);
     if (handleSet == null)
     {
         handleSet = new SortedDictionary<long, ScheduleHandle>();
         _timeHandleMap.Put(triggerTime, handleSet);
     }
     handleSet.Put(slot, handle);
     _handleSetMap.Put(handle, handleSet);
 }
Exemplo n.º 3
0
        public override void Add(
            EventBean theEvent,
            ExprEvaluatorContext exprEvaluatorContext)
        {
            var key = GetIndexedValue(theEvent);

            key = Coerce(key);

            if (key == null) {
                nullKeyedValues.Add(theEvent);
                return;
            }

            var events = propertyIndex.Get(key);
            if (events == null) {
                events = new LinkedHashSet<EventBean>();
                propertyIndex.Put(key, events);
            }

            events.Add(theEvent);
        }
        public override EventEvaluator Get(object expressionValue)
        {
            if (!(expressionValue is StringRange))
            {
                throw new ArgumentException("Supplied expressionValue must be of type StringRange");
            }

            var range = (StringRange)expressionValue;

            if (range.Max == null || range.Min == null)
            {
                return(RangesNullEndpoints);
            }

            return(Ranges.Get(range));
        }
        private static void MakeSendBean(
            RegressionEnvironment env,
            IOrderedDictionary <int, IList <SupportBean> > treemap,
            string theString,
            int intPrimitive)
        {
            var bean = new SupportBean(theString, intPrimitive);

            env.SendEventBean(bean);
            var existing = treemap.Get(intPrimitive);

            if (existing == null)
            {
                existing = new List <SupportBean>();
                treemap.Put(intPrimitive, existing);
            }

            existing.Add(bean);
            treemap.Put(bean.IntPrimitive, existing);
        }
Exemplo n.º 6
0
        private static AggregationServiceForgeDesc[] PlanAggregations(
            IList<ExprAggregateNode> measureAggregateExprNodes,
            StreamTypeService compositeTypeServiceMeasure,
            string[] allStreamNames,
            EventType[] allTypes,
            IOrderedDictionary<int, string> streamVariables,
            ISet<string> variablesMultiple,
            StatementBaseInfo @base,
            StatementCompileTimeServices services)
        {
            IDictionary<int, IList<ExprAggregateNode>> measureExprAggNodesPerStream =
                new Dictionary<int, IList<ExprAggregateNode>>();

            foreach (var aggregateNode in measureAggregateExprNodes) {
                // validate node and params
                var count = 0;
                var visitor = new ExprNodeIdentifierVisitor(true);
                var isIStreamOnly = new bool[allStreamNames.Length];
                var typeServiceAggregateMeasure = new StreamTypeServiceImpl(
                    allTypes,
                    allStreamNames,
                    isIStreamOnly,
                    false,
                    true);

                var validationContext =
                    new ExprValidationContextBuilder(typeServiceAggregateMeasure, @base.StatementRawInfo, services)
                        .WithAllowBindingConsumption(true)
                        .Build();
                aggregateNode.ValidatePositionals(validationContext);

                if (aggregateNode.OptionalLocalGroupBy != null) {
                    throw new ExprValidationException(
                        "Match-recognize does not allow aggregation functions to specify a group-by");
                }

                foreach (var child in aggregateNode.ChildNodes) {
                    var validated = ExprNodeUtilityValidate.GetValidatedSubtree(
                        ExprNodeOrigin.MATCHRECOGMEASURE,
                        child,
                        validationContext);
                    validated.Accept(visitor);
                    aggregateNode.SetChildNode(count++, new ExprNodeValidated(validated));
                }

                // verify properties used within the aggregation
                ISet<int> aggregatedStreams = new HashSet<int>();
                foreach (var pair in visitor.ExprProperties) {
                    aggregatedStreams.Add(pair.First);
                }

                int? multipleVarStream = null;
                foreach (var streamNumAggregated in aggregatedStreams) {
                    var variable = streamVariables.Get(streamNumAggregated);
                    if (variablesMultiple.Contains(variable)) {
                        if (multipleVarStream == null) {
                            multipleVarStream = streamNumAggregated;
                            continue;
                        }

                        throw new ExprValidationException(
                            "Aggregation functions in the measure-clause must only refer to properties of exactly one group variable returning multiple events");
                    }
                }

                if (multipleVarStream == null) {
                    throw new ExprValidationException(
                        "Aggregation functions in the measure-clause must refer to one or more properties of exactly one group variable returning multiple events");
                }

                var aggNodesForStream = measureExprAggNodesPerStream.Get(multipleVarStream.Value);
                if (aggNodesForStream == null) {
                    aggNodesForStream = new List<ExprAggregateNode>();
                    measureExprAggNodesPerStream.Put(multipleVarStream.Value, aggNodesForStream);
                }

                aggNodesForStream.Add(aggregateNode);
            }

            // validate aggregation itself
            foreach (var entry in measureExprAggNodesPerStream) {
                foreach (var aggregateNode in entry.Value) {
                    var validationContext = new ExprValidationContextBuilder(
                            compositeTypeServiceMeasure,
                            @base.StatementRawInfo,
                            services)
                        .WithAllowBindingConsumption(true)
                        .WithMemberName(new ExprValidationMemberNameQualifiedRowRecogAgg(entry.Key))
                        .Build();
                    aggregateNode.Validate(validationContext);
                }
            }

            // get aggregation service per variable
            var aggServices = new AggregationServiceForgeDesc[allStreamNames.Length];
            var declareds = Arrays.AsList(@base.StatementSpec.DeclaredExpressions);
            foreach (var entry in measureExprAggNodesPerStream) {
                EventType[] typesPerStream = {allTypes[entry.Key]};
                AggregationServiceForgeDesc desc = AggregationServiceFactoryFactory.GetService(
                    entry.Value,
                    EmptyDictionary<ExprNode, string>.Instance, 
                    declareds,
                    new ExprNode[0],
                    null,
                    EmptyList<ExprAggregateNode>.Instance,
                    EmptyList<ExprAggregateNode>.Instance,
                    EmptyList<ExprAggregateNodeGroupKey>.Instance,
                    false,
                    @base.StatementRawInfo.Annotations,
                    services.VariableCompileTimeResolver,
                    true,
                    null,
                    null,
                    typesPerStream,
                    null,
                    @base.ContextName,
                    null,
                    services.TableCompileTimeResolver,
                    false,
                    true,
                    false,
                    services.ImportServiceCompileTime,
                    @base.StatementRawInfo,
                    services.SerdeResolver);

                aggServices[entry.Key] = desc;
            }

            return aggServices;
        }
        private static void AssertOrderedDictionary(
            IOrderedDictionary <int, IList <SupportBean> > treemap,
            IOrderedDictionary <object, ICollection <EventBean> > actual)
        {
            Assert.AreEqual(treemap.Count, actual.Count);
            foreach (var key in treemap.Keys)
            {
                var expectedEvents = treemap.Get(key).ToArray();
                EPAssertionUtil.AssertEqualsExactOrder(expectedEvents, ToArrayOfUnderlying(actual.Get(key)));
            }

            CompareEntry(treemap.First(), actual.FirstEntry);
            CompareEntry(treemap.Last(), actual.LastEntry);
            CompareEntry(treemap.LessThanOrEqualTo(5), actual.LessThanOrEqualTo(5));
            CompareEntry(treemap.GreaterThanOrEqualTo(5), actual.GreaterThanOrEqualTo(5));
            CompareEntry(treemap.LessThan(5), actual.LessThan(5));
            CompareEntry(treemap.GreaterThan(5), actual.GreaterThan(5));

            Assert.AreEqual(treemap.Keys.First(), actual.FirstEntry.Key);
            Assert.AreEqual(treemap.Keys.Last(), actual.LastEntry.Key);
            Assert.AreEqual(treemap.LessThanOrEqualTo(5)?.Key, actual.LessThanOrEqualTo(5)?.Key);
            Assert.AreEqual(treemap.GreaterThanOrEqualTo(5)?.Key, actual.GreaterThanOrEqualTo(5)?.Key);
            Assert.AreEqual(treemap.LessThan(5)?.Key, actual.LessThan(5)?.Key);
            Assert.AreEqual(treemap.GreaterThan(5)?.Key, actual.GreaterThan(5)?.Key);

            Assert.AreEqual(treemap.ContainsKey(5), actual.ContainsKey(5));
            Assert.AreEqual(treemap.IsEmpty(), actual.IsEmpty());

            EPAssertionUtil.AssertEqualsExactOrder(new object[] { 1, 4, 6, 8, 9 }, actual.Keys.ToArray());

            Assert.AreEqual(1, actual.Between(9, true, 9, true).Count);
            Assert.AreEqual(1, actual.Tail(9).Count);
            Assert.AreEqual(1, actual.Tail(9, true).Count);
            Assert.AreEqual(1, actual.Head(2).Count);
            Assert.AreEqual(1, actual.Head(2, false).Count);

            Assert.AreEqual(5, actual.Count);
            Assert.AreEqual(5, actual.Keys.Count);
            Assert.AreEqual(5, actual.Values.Count);

            // values
            var values = actual.Values;

            Assert.That(values.Count, Is.EqualTo(5));
            Assert.That(values.IsEmpty(), Is.False);

            var valuesEnum = values.GetEnumerator();

            Assert.That(valuesEnum, Is.Not.Null);
            Assert.That(valuesEnum.MoveNext, Is.True);

            CollectionAssert.AreEqual(
                treemap.Get(1).ToArray(),
                ToArrayOfUnderlying(valuesEnum.Current));

            Assert.That(valuesEnum.MoveNext, Is.True);
            Assert.That(values.ToArray(), Has.Length.EqualTo(5));

            CollectionAssert.AreEqual(
                treemap.Get(1).ToArray(),
                ToArrayOfUnderlying((ICollection <EventBean>)values.ToArray()[0]));

            // ordered key set
            var oks = actual.OrderedKeys;

            //Assert.That(oks.Comparator());
            Assert.That(oks.FirstEntry, Is.EqualTo(1));
            Assert.That(oks.LastEntry, Is.EqualTo(9));
            Assert.That(oks.Count, Is.EqualTo(5));
            Assert.That(oks.IsEmpty(), Is.False);
            Assert.That(oks.Contains(6), Is.True);
            Assert.That(oks.ToArray(), Is.Not.Null);

            Assert.That(oks.LessThan(5), Is.EqualTo(4));
            Assert.That(oks.GreaterThan(5), Is.EqualTo(6));
            Assert.That(oks.LessThanOrEqualTo(5), Is.EqualTo(4));
            Assert.That(oks.GreaterThanOrEqualTo(5), Is.EqualTo(6));

            Assert.That(oks.Between(1, true, 100, true), Is.Not.Null);
            Assert.That(oks.Head(100, true), Is.Not.Null);
            Assert.That(oks.Tail(1, true), Is.Not.Null);

            // ordered key set - enumerator
            var oksit = oks.GetEnumerator();

            Assert.That(oksit, Is.Not.Null);
            Assert.That(oksit.MoveNext(), Is.True);
            Assert.That(oksit.Current, Is.EqualTo(1));
            Assert.That(oksit.MoveNext(), Is.True);


            // entry set
            ICollection <KeyValuePair <object, ICollection <EventBean> > > set = actual;

            Assert.IsFalse(set.IsEmpty());
            var setit = set.GetEnumerator();
            var entry = setit.Advance();

            Assert.AreEqual(1, entry.Key);
            Assert.IsTrue(setit.MoveNext());
            EPAssertionUtil.AssertEqualsExactOrder(treemap.Get(1).ToArray(), ToArrayOfUnderlying(entry.Value));
            var array = set.ToArray();

            Assert.AreEqual(5, array.Length);
            Assert.AreEqual(1, array[0].Key);
            EPAssertionUtil.AssertEqualsExactOrder(treemap.Get(1).ToArray(), ToArrayOfUnderlying(array[0].Value));
            Assert.IsNotNull(set.ToArray());

            // sorted map
            var events = actual.Head(100);

            Assert.AreEqual(5, events.Count);
        }
Exemplo n.º 8
0
        public override void Update(
            EventBean[] newData,
            EventBean[] oldData)
        {
            _agentInstanceContext.AuditProvider.View(newData, oldData, _agentInstanceContext, _rankWindowViewFactory);
            _agentInstanceContext.InstrumentationProvider.QViewProcessIRStream(
                _rankWindowViewFactory,
                newData,
                oldData);

            var removedEvents = new OneEventCollection();

            // Remove old data
            if (oldData != null) {
                for (var i = 0; i < oldData.Length; i++) {
                    var uniqueKey = GetUniqueKey(oldData[i]);
                    var existingSortKey = _uniqueKeySortKeys.Get(uniqueKey);

                    if (existingSortKey == null) {
                        continue;
                    }

                    var @event = RemoveFromSortedEvents(existingSortKey, uniqueKey);
                    if (@event != null) {
                        _numberOfEvents--;
                        _uniqueKeySortKeys.Remove(uniqueKey);
                        removedEvents.Add(@event);
                        InternalHandleRemovedKey(existingSortKey, oldData[i]);
                    }
                }
            }

            // Add new data
            if (newData != null) {
                for (var i = 0; i < newData.Length; i++) {
                    var uniqueKey = GetUniqueKey(newData[i]);
                    var newSortKey = GetSortValues(newData[i]);
                    var existingSortKey = _uniqueKeySortKeys.Get(uniqueKey);

                    // not currently found: its a new entry
                    if (existingSortKey == null) {
                        CompareAndAddOrPassthru(newData[i], uniqueKey, newSortKey, removedEvents);
                    }
                    else {
                        // same unique-key event found already, remove and add again
                        // key did not change, perform in-place substitute of event
                        if (existingSortKey.Equals(newSortKey)) {
                            var replaced = InplaceReplaceSortedEvents(existingSortKey, uniqueKey, newData[i]);
                            if (replaced != null) {
                                removedEvents.Add(replaced);
                            }

                            InternalHandleReplacedKey(newSortKey, newData[i], replaced);
                        }
                        else {
                            var removed = RemoveFromSortedEvents(existingSortKey, uniqueKey);
                            if (removed != null) {
                                _numberOfEvents--;
                                removedEvents.Add(removed);
                                InternalHandleRemovedKey(existingSortKey, removed);
                            }

                            CompareAndAddOrPassthru(newData[i], uniqueKey, newSortKey, removedEvents);
                        }
                    }
                }
            }

            // Remove data that sorts to the bottom of the window
            while (_numberOfEvents > _sortWindowSize) {
                var lastKey = _sortedEvents.Keys.Last();
                var existing = _sortedEvents.Get(lastKey);
                if (existing is IList<EventBean> existingList) {
                    while (_numberOfEvents > _sortWindowSize && !existingList.IsEmpty()) {
                        var newestEvent = existingList.DeleteAt(0);
                        var uniqueKey = GetUniqueKey(newestEvent);
                        _uniqueKeySortKeys.Remove(uniqueKey);
                        _numberOfEvents--;
                        removedEvents.Add(newestEvent);
                        InternalHandleRemovedKey(existing, newestEvent);
                    }

                    if (existingList.IsEmpty()) {
                        _sortedEvents.Remove(lastKey);
                    }
                }
                else {
                    var lastSortedEvent = (EventBean) existing;
                    var uniqueKey = GetUniqueKey(lastSortedEvent);
                    _uniqueKeySortKeys.Remove(uniqueKey);
                    _numberOfEvents--;
                    removedEvents.Add(lastSortedEvent);
                    _sortedEvents.Remove(lastKey);
                    InternalHandleRemovedKey(lastKey, lastSortedEvent);
                }
            }

            // If there are child views, fireStatementStopped update method
            _optionalRankedRandomAccess?.Refresh(_sortedEvents, _numberOfEvents, _sortWindowSize);

            if (Child != null) {
                EventBean[] expiredArr = null;
                if (!removedEvents.IsEmpty()) {
                    expiredArr = removedEvents.ToArray();
                }

                _agentInstanceContext.InstrumentationProvider.QViewIndicate(
                    _rankWindowViewFactory,
                    newData,
                    expiredArr);
                Child.Update(newData, expiredArr);
                _agentInstanceContext.InstrumentationProvider.AViewIndicate();
            }

            _agentInstanceContext.InstrumentationProvider.AViewProcessIRStream();
        }
 public override EventEvaluator Get(object filterConstant)
 {
     return constantsMap.Get(filterConstant);
 }
Exemplo n.º 10
0
        public override void Update(
            EventBean[] newData,
            EventBean[] oldData)
        {
            agentInstanceContext.AuditProvider.View(newData, oldData, agentInstanceContext, factory);
            agentInstanceContext.InstrumentationProvider.QViewProcessIRStream(factory, newData, oldData);

            OneEventCollection removedEvents = null;

            // Remove old data
            if (oldData != null) {
                for (var i = 0; i < oldData.Length; i++) {
                    var oldDataItem = oldData[i];
                    var sortValues = GetSortValues(oldDataItem);
                    var result = CollectionUtil.RemoveEventByKeyLazyListMap(sortValues, oldDataItem, sortedEvents);
                    if (result) {
                        eventCount--;
                        if (removedEvents == null) {
                            removedEvents = new OneEventCollection();
                        }

                        removedEvents.Add(oldDataItem);
                        InternalHandleRemoved(sortValues, oldDataItem);
                    }
                }
            }

            // Add new data
            if (newData != null) {
                for (var i = 0; i < newData.Length; i++) {
                    var newDataItem = newData[i];
                    var sortValues = GetSortValues(newDataItem);
                    CollectionUtil.AddEventByKeyLazyListMapFront(sortValues, newDataItem, sortedEvents);
                    eventCount++;
                    InternalHandleAdd(sortValues, newDataItem);
                }
            }

            // Remove data that sorts to the bottom of the window
            if (eventCount > sortWindowSize) {
                var removeCount = eventCount - sortWindowSize;
                for (var i = 0; i < removeCount; i++) {
                    // Remove the last element of the last key - sort order is key and then natural order of arrival
                    var lastKey = sortedEvents.Keys.Last();
                    var lastEntry = sortedEvents.Get(lastKey);
                    if (lastEntry is IList<EventBean>) {
                        var events = (IList<EventBean>) lastEntry;
                        var theEvent =
                            events.DeleteAt(events.Count - 1); // remove oldest event, newest events are first in list
                        eventCount--;
                        if (events.IsEmpty()) {
                            sortedEvents.Remove(lastKey);
                        }

                        if (removedEvents == null) {
                            removedEvents = new OneEventCollection();
                        }

                        removedEvents.Add(theEvent);
                        InternalHandleRemoved(lastKey, theEvent);
                    }
                    else {
                        var theEvent = (EventBean) lastEntry;
                        eventCount--;
                        sortedEvents.Remove(lastKey);
                        if (removedEvents == null) {
                            removedEvents = new OneEventCollection();
                        }

                        removedEvents.Add(theEvent);
                        InternalHandleRemoved(lastKey, theEvent);
                    }
                }
            }

            // If there are child views, fireStatementStopped update method
            optionalSortedRandomAccess?.Refresh(sortedEvents, eventCount, sortWindowSize);

            if (child != null) {
                EventBean[] expiredArr = null;
                if (removedEvents != null) {
                    expiredArr = removedEvents.ToArray();
                }

                agentInstanceContext.InstrumentationProvider.QViewIndicate(factory, newData, expiredArr);
                child.Update(newData, expiredArr);
                agentInstanceContext.InstrumentationProvider.AViewIndicate();
            }

            agentInstanceContext.InstrumentationProvider.AViewProcessIRStream();
        }