public override void HandleMatching(EventBean[] triggerEvents, EventBean[] matchingEvents) { if (InstrumentationHelper.ENABLED) { InstrumentationHelper.Get().QInfraOnAction(OnTriggerType.ON_MERGE, triggerEvents, matchingEvents); } var newData = new OneEventCollection(); OneEventCollection oldData = null; var eventsPerStream = new EventBean[3]; // first:named window, second: trigger, third:before-update (optional) if ((matchingEvents == null) || (matchingEvents.Length == 0)) { var unmatched = _parent.NamedWindowOnMergeHelper.Unmatched; foreach (var triggerEvent in triggerEvents) { eventsPerStream[1] = triggerEvent; if (InstrumentationHelper.ENABLED) { InstrumentationHelper.Get().QInfraMergeWhenThens(false, triggerEvent, unmatched.Count); } var count = -1; foreach (var action in unmatched) { count++; if (InstrumentationHelper.ENABLED) { InstrumentationHelper.Get().QInfraMergeWhenThenItem(false, count); } if (!action.IsApplies(eventsPerStream, base.ExprEvaluatorContext)) { if (InstrumentationHelper.ENABLED) { InstrumentationHelper.Get().AInfraMergeWhenThenItem(false, false); } continue; } action.Apply(null, eventsPerStream, newData, oldData, base.ExprEvaluatorContext); if (InstrumentationHelper.ENABLED) { InstrumentationHelper.Get().AInfraMergeWhenThenItem(false, true); } break; // apply no other actions } if (InstrumentationHelper.ENABLED) { InstrumentationHelper.Get().AInfraMergeWhenThens(false); } } } else { // handle update/ oldData = new OneEventCollection(); var matched = _parent.NamedWindowOnMergeHelper.Matched; foreach (var triggerEvent in triggerEvents) { eventsPerStream[1] = triggerEvent; if (InstrumentationHelper.ENABLED) { InstrumentationHelper.Get().QInfraMergeWhenThens(true, triggerEvent, matched.Count); } foreach (var matchingEvent in matchingEvents) { eventsPerStream[0] = matchingEvent; var count = -1; foreach (var action in matched) { count++; if (InstrumentationHelper.ENABLED) { InstrumentationHelper.Get().QInfraMergeWhenThenItem(true, count); } if (!action.IsApplies(eventsPerStream, base.ExprEvaluatorContext)) { if (InstrumentationHelper.ENABLED) { InstrumentationHelper.Get().AInfraMergeWhenThenItem(true, false); } continue; } action.Apply(matchingEvent, eventsPerStream, newData, oldData, base.ExprEvaluatorContext); if (InstrumentationHelper.ENABLED) { InstrumentationHelper.Get().AInfraMergeWhenThenItem(true, true); } break; // apply no other actions } } if (InstrumentationHelper.ENABLED) { InstrumentationHelper.Get().AInfraMergeWhenThens(true); } } } if (!newData.IsEmpty() || (oldData != null && !oldData.IsEmpty())) { if ((MetricReportingPath.IsMetricsEnabled) && (_parent.CreateNamedWindowMetricHandle.IsEnabled) && !newData.IsEmpty()) { _parent.MetricReportingService.AccountTime(_parent.CreateNamedWindowMetricHandle, 0, 0, newData.ToArray().Length); } // Events to delete are indicated via old data // The on-merge listeners receive the events deleted, but only if there is interest if (_parent.StatementResultService.IsMakeNatural) { var eventsPerStreamNaturalNew = newData.IsEmpty() ? null : newData.ToArray(); var eventsPerStreamNaturalOld = (oldData == null || oldData.IsEmpty()) ? null : oldData.ToArray(); RootView.Update(EventBeanUtility.Denaturalize(eventsPerStreamNaturalNew), EventBeanUtility.Denaturalize(eventsPerStreamNaturalOld)); UpdateChildren(eventsPerStreamNaturalNew, eventsPerStreamNaturalOld); } else { var eventsPerStreamNew = newData.IsEmpty() ? null : newData.ToArray(); var eventsPerStreamOld = (oldData == null || oldData.IsEmpty()) ? null : oldData.ToArray(); RootView.Update(eventsPerStreamNew, eventsPerStreamOld); if (_parent.StatementResultService.IsMakeSynthetic) { UpdateChildren(eventsPerStreamNew, eventsPerStreamOld); } } } // Keep the last delete records _lastResult = matchingEvents; if (InstrumentationHelper.ENABLED) { InstrumentationHelper.Get().AInfraOnAction(); } }
public override void Update(EventBean[] newData, EventBean[] oldData) { if (InstrumentationHelper.ENABLED) { InstrumentationHelper.Get().QViewProcessIRStream(this, _rankWindowViewFactory.ViewName, newData, oldData); } var removedEvents = new OneEventCollection(); // Remove old data if (oldData != null) { for (var i = 0; i < oldData.Length; i++) { var uniqueKey = GetUniqueValues(oldData[i]); var existingSortKey = _uniqueKeySortKeys.Get(uniqueKey); if (existingSortKey == null) { continue; } var theEvent = RemoveFromSortedEvents(existingSortKey, uniqueKey); if (theEvent != null) { _numberOfEvents--; _uniqueKeySortKeys.Remove(uniqueKey); removedEvents.Add(theEvent); InternalHandleRemovedKey(existingSortKey, oldData[i]); } } } // Add new data if (newData != null) { for (var i = 0; i < newData.Length; i++) { var uniqueKey = GetUniqueValues(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); } // same unique-key event found already, remove and add again else { // 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 if (_numberOfEvents > _sortWindowSize) { while (_numberOfEvents > _sortWindowSize) { var lastKey = _sortedEvents.Keys.Last(); var existing = _sortedEvents.Get(lastKey); if (existing is IList <EventBean> ) { var existingList = (IList <EventBean>)existing; while (_numberOfEvents > _sortWindowSize && !existingList.IsEmpty()) { var newestEvent = existingList.DeleteAt(0); var uniqueKey = GetUniqueValues(newestEvent); _uniqueKeySortKeys.Remove(uniqueKey); _numberOfEvents--; removedEvents.Add(newestEvent); InternalHandleRemovedKey(existing, newestEvent); } if (existingList.IsEmpty()) { _sortedEvents.Remove(lastKey); } } else { var lastSortedEvent = (EventBean)existing; var uniqueKey = GetUniqueValues(lastSortedEvent); _uniqueKeySortKeys.Remove(uniqueKey); _numberOfEvents--; removedEvents.Add(lastSortedEvent); _sortedEvents.Remove(lastKey); InternalHandleRemovedKey(lastKey, lastSortedEvent); } } } // If there are child views, fireStatementStopped Update method if (_optionalRankedRandomAccess != null) { _optionalRankedRandomAccess.Refresh(_sortedEvents, _numberOfEvents, _sortWindowSize); } if (HasViews) { EventBean[] expiredArr = null; if (!removedEvents.IsEmpty()) { expiredArr = removedEvents.ToArray(); } if (InstrumentationHelper.ENABLED) { InstrumentationHelper.Get().QViewIndicate(this, _rankWindowViewFactory.ViewName, newData, expiredArr); } UpdateChildren(newData, expiredArr); if (InstrumentationHelper.ENABLED) { InstrumentationHelper.Get().AViewIndicate(); } } if (InstrumentationHelper.ENABLED) { InstrumentationHelper.Get().AViewProcessIRStream(); } }