コード例 #1
0
        private void CompareAndAddOrPassthru(EventBean eventBean, Object uniqueKey, Object newSortKey, OneEventCollection removedEvents)
        {
            // determine full or not
            if (_numberOfEvents >= _sortWindowSize)
            {
                var compared = _comparator.Compare(_sortedEvents.Keys.Last(), newSortKey);

                // this new event will fall outside of the ranks or coincides with the last entry, so its an old event already
                if (compared < 0)
                {
                    removedEvents.Add(eventBean);
                }
                // this new event is higher in sort key then the last entry so we are interested
                else
                {
                    _uniqueKeySortKeys.Put(uniqueKey, newSortKey);
                    _numberOfEvents++;
                    CollectionUtil.AddEventByKeyLazyListMapBack(newSortKey, eventBean, _sortedEvents);
                    InternalHandleAddedKey(newSortKey, eventBean);
                }
            }
            // not yet filled, need to add
            else
            {
                _uniqueKeySortKeys.Put(uniqueKey, newSortKey);
                _numberOfEvents++;
                CollectionUtil.AddEventByKeyLazyListMapBack(newSortKey, eventBean, _sortedEvents);
                InternalHandleAddedKey(newSortKey, eventBean);
            }
        }
コード例 #2
0
ファイル: TimeOrderView.cs プロジェクト: qinfengzhu/nesper
        public override void Update(EventBean[] newData, EventBean[] oldData)
        {
            using (Instrument.With(
                       i => i.QViewProcessIRStream(this, _viewFactory.ViewName, newData, oldData),
                       i => i.AViewProcessIRStream()))
            {
                EventBean[] postOldEventsArray = null;

                // Remove old data
                if (oldData != null)
                {
                    for (int i = 0; i < oldData.Length; i++)
                    {
                        var oldDataItem = oldData[i];
                        var sortValues  = GetTimestamp(oldDataItem);
                        var result      = CollectionUtil.RemoveEventByKeyLazyListMap(sortValues, oldDataItem, _sortedEvents);
                        if (result)
                        {
                            _eventCount--;
                            if (postOldEventsArray == null)
                            {
                                postOldEventsArray = oldData;
                            }
                            else
                            {
                                postOldEventsArray = CollectionUtil.AddArrayWithSetSemantics(
                                    postOldEventsArray, oldData);
                            }
                            InternalHandleRemoved(sortValues, oldDataItem);
                        }
                    }
                }

                if ((newData != null) && (newData.Length > 0))
                {
                    // figure out the current tail time
                    long engineTime     = _agentInstanceContext.StatementContext.SchedulingService.Time;
                    long windowTailTime = engineTime - _timeDeltaComputation.DeltaAdd(engineTime) + 1;
                    long oldestEvent    = long.MaxValue;
                    if (_sortedEvents.IsNotEmpty())
                    {
                        oldestEvent = (long)_sortedEvents.Keys.First();
                    }
                    bool addedOlderEvent = false;

                    // add events or post events as remove stream if already older then tail time
                    List <EventBean> postOldEvents = null;
                    for (int i = 0; i < newData.Length; i++)
                    {
                        // get timestamp of event
                        var newEvent  = newData[i];
                        var timestamp = GetTimestamp(newEvent);

                        // if the event timestamp indicates its older then the tail of the window, release it
                        if (timestamp < windowTailTime)
                        {
                            if (postOldEvents == null)
                            {
                                postOldEvents = new List <EventBean>(2);
                            }
                            postOldEvents.Add(newEvent);
                        }
                        else
                        {
                            if (timestamp < oldestEvent)
                            {
                                addedOlderEvent = true;
                                oldestEvent     = timestamp.Value;
                            }

                            // add to list
                            CollectionUtil.AddEventByKeyLazyListMapBack(timestamp, newEvent, _sortedEvents);
                            _eventCount++;
                            InternalHandleAdd(timestamp, newEvent);
                        }
                    }

                    // If we do have data, check the callback
                    if (_sortedEvents.IsNotEmpty())
                    {
                        // If we haven't scheduled a callback yet, schedule it now
                        if (!_isCallbackScheduled)
                        {
                            long callbackWait = oldestEvent - windowTailTime + 1;
                            _agentInstanceContext.StatementContext.SchedulingService.Add(
                                callbackWait, _handle, _scheduleSlot);
                            _isCallbackScheduled = true;
                        }
                        else
                        {
                            // We may need to reschedule, and older event may have been added
                            if (addedOlderEvent)
                            {
                                oldestEvent = (long)_sortedEvents.Keys.First();
                                long callbackWait = oldestEvent - windowTailTime + 1;
                                _agentInstanceContext.StatementContext.SchedulingService.Remove(_handle, _scheduleSlot);
                                _agentInstanceContext.StatementContext.SchedulingService.Add(
                                    callbackWait, _handle, _scheduleSlot);
                                _isCallbackScheduled = true;
                            }
                        }
                    }

                    if (postOldEvents != null)
                    {
                        postOldEventsArray = postOldEvents.ToArray();
                    }

                    if (_optionalSortedRandomAccess != null)
                    {
                        _optionalSortedRandomAccess.Refresh(_sortedEvents, _eventCount, _eventCount);
                    }
                }

                // Update child views
                if (HasViews)
                {
                    Instrument.With(
                        i => i.QViewIndicate(this, _viewFactory.ViewName, newData, postOldEventsArray),
                        i => i.AViewIndicate(),
                        () => UpdateChildren(newData, postOldEventsArray));
                }
            }
        }
コード例 #3
0
        public override void Update(
            EventBean[] newData,
            EventBean[] oldData)
        {
            agentInstanceContext.AuditProvider.View(newData, oldData, agentInstanceContext, factory);
            agentInstanceContext.InstrumentationProvider.QViewProcessIRStream(factory, newData, oldData);

            EventBean[] postOldEventsArray = null;

            // Remove old data
            if (oldData != null) {
                for (var i = 0; i < oldData.Length; i++) {
                    var oldDataItem = oldData[i];
                    object sortValues = GetTimestamp(oldDataItem);
                    var result = CollectionUtil.RemoveEventByKeyLazyListMap(sortValues, oldDataItem, sortedEvents);
                    if (result) {
                        eventCount--;
                        if (postOldEventsArray == null) {
                            postOldEventsArray = oldData;
                        }
                        else {
                            postOldEventsArray = CollectionUtil.AddArrayWithSetSemantics(postOldEventsArray, oldData);
                        }
                    }
                }
            }

            if (newData != null && newData.Length > 0) {
                // figure out the current tail time
                var runtimeTime = agentInstanceContext.StatementContext.SchedulingService.Time;
                var windowTailTime = runtimeTime -
                                     timePeriodProvide.DeltaAdd(runtimeTime, null, true, agentInstanceContext) +
                                     1;
                var oldestEvent = long.MaxValue;
                if (!sortedEvents.IsEmpty()) {
                    oldestEvent = sortedEvents.First().Key.AsInt64();
                }

                var addedOlderEvent = false;

                // add events or post events as remove stream if already older then tail time
                List<EventBean> postOldEvents = null;
                for (var i = 0; i < newData.Length; i++) {
                    // get timestamp of event
                    var newEvent = newData[i];
                    var timestamp = GetTimestamp(newEvent);

                    // if the event timestamp indicates its older then the tail of the window, release it
                    if (timestamp < windowTailTime) {
                        if (postOldEvents == null) {
                            postOldEvents = new List<EventBean>(2);
                        }

                        postOldEvents.Add(newEvent);
                    }
                    else {
                        if (timestamp < oldestEvent) {
                            addedOlderEvent = true;
                            oldestEvent = timestamp.Value;
                        }

                        // add to list
                        CollectionUtil.AddEventByKeyLazyListMapBack(timestamp, newEvent, sortedEvents);
                        eventCount++;
                    }
                }

                // If we do have data, check the callback
                if (!sortedEvents.IsEmpty()) {
                    // If we haven't scheduled a callback yet, schedule it now
                    if (!isCallbackScheduled) {
                        var callbackWait = oldestEvent - windowTailTime + 1;
                        agentInstanceContext.AuditProvider.ScheduleAdd(
                            callbackWait,
                            agentInstanceContext,
                            handle,
                            ScheduleObjectType.view,
                            factory.ViewName);
                        agentInstanceContext.StatementContext.SchedulingService.Add(callbackWait, handle, scheduleSlot);
                        isCallbackScheduled = true;
                    }
                    else {
                        // We may need to reschedule, and older event may have been added
                        if (addedOlderEvent) {
                            oldestEvent = sortedEvents.First().Key.AsInt64();
                            var callbackWait = oldestEvent - windowTailTime + 1;
                            agentInstanceContext.AuditProvider.ScheduleRemove(
                                agentInstanceContext,
                                handle,
                                ScheduleObjectType.view,
                                factory.ViewName);
                            agentInstanceContext.StatementContext.SchedulingService.Remove(handle, scheduleSlot);
                            agentInstanceContext.AuditProvider.ScheduleAdd(
                                callbackWait,
                                agentInstanceContext,
                                handle,
                                ScheduleObjectType.view,
                                factory.ViewName);
                            agentInstanceContext.StatementContext.SchedulingService.Add(
                                callbackWait,
                                handle,
                                scheduleSlot);
                            isCallbackScheduled = true;
                        }
                    }
                }

                if (postOldEvents != null) {
                    postOldEventsArray = postOldEvents.ToArray();
                }

                optionalSortedRandomAccess?.Refresh(sortedEvents, eventCount, eventCount);
            }

            // update child views
            if (Child != null) {
                agentInstanceContext.InstrumentationProvider.QViewIndicate(factory, newData, postOldEventsArray);
                Child.Update(newData, postOldEventsArray);
                agentInstanceContext.InstrumentationProvider.AViewIndicate();
            }

            agentInstanceContext.InstrumentationProvider.AViewProcessIRStream();
        }