public void UpdateTable(
            ICollection<EventBean> eventsUnsafeIter,
            TableInstance instance,
            EventBean[] eventsPerStream,
            ExprEvaluatorContext exprEvaluatorContext)
        {
            // copy references to array - as it is allowed to pass an index-originating collection
            // and those same indexes are being changed now
            var events = new EventBean[eventsUnsafeIter.Count];
            var count = 0;
            foreach (var @event in eventsUnsafeIter) {
                events[count++] = @event;
            }

            // remove from affected indexes
            foreach (var affectedIndexName in affectedIndexNames) {
                var index = instance.GetIndex(affectedIndexName);
                index.Remove(events, instance.AgentInstanceContext);
            }

            // update (no-copy unless original values required)
            foreach (var @event in events) {
                eventsPerStream[0] = @event;
                var updatedEvent = (ObjectArrayBackedEventBean) @event;

                // if "initial.property" is part of the assignment expressions, provide initial value event
                if (updateHelper.IsRequiresStream2InitialValueEvent) {
                    var prev = new object[updatedEvent.Properties.Length];
                    Array.Copy(updatedEvent.Properties, 0, prev, 0, prev.Length);
                    eventsPerStream[2] = new ObjectArrayEventBean(prev, updatedEvent.EventType);
                }

                // apply in-place updates
                updateHelper.UpdateNoCopy(updatedEvent, eventsPerStream, exprEvaluatorContext);
                instance.HandleRowUpdated(updatedEvent);
            }

            // add to affected indexes
            foreach (var affectedIndexName in affectedIndexNames) {
                var index = instance.GetIndex(affectedIndexName);
                index.Add(events, instance.AgentInstanceContext);
            }
        }
Beispiel #2
0
        public void UpdateTable(
            ICollection<EventBean> eventsUnsafeIter,
            TableInstance instance,
            EventBean[] eventsPerStream,
            ExprEvaluatorContext exprEvaluatorContext)
        {
            // copy references to array - as it is allowed to pass an index-originating collection
            // and those same indexes are being changed now
            var events = new EventBean[eventsUnsafeIter.Count];
            var count = 0;
            foreach (var @event in eventsUnsafeIter) {
                events[count++] = @event;
            }

            // remove from affected indexes
            foreach (var affectedIndexName in _affectedIndexNames) {
                var index = instance.GetIndex(affectedIndexName);
                index.Remove(events, exprEvaluatorContext);
            }

            // copy event data, since we are updating unique keys and must guarantee rollback (no half update)
            var previousData = new object[events.Length][];

            // copy and then update
            for (var i = 0; i < events.Length; i++) {
                eventsPerStream[0] = events[i];

                // copy non-aggregated value references
                var updatedEvent = (ObjectArrayBackedEventBean) events[i];
                var prev = new object[updatedEvent.Properties.Length];
                Array.Copy(updatedEvent.Properties, 0, prev, 0, prev.Length);
                previousData[i] = prev;

                // if "initial.property" is part of the assignment expressions, provide initial value event
                if (_updateHelper.IsRequiresStream2InitialValueEvent) {
                    eventsPerStream[2] = new ObjectArrayEventBean(prev, updatedEvent.EventType);
                }

                // apply in-place updates
                instance.HandleRowUpdateKeyBeforeUpdate(updatedEvent);
                _updateHelper.UpdateNoCopy(updatedEvent, eventsPerStream, exprEvaluatorContext);
                instance.HandleRowUpdateKeyAfterUpdate(updatedEvent);
            }

            // add to affected indexes
            try {
                foreach (var affectedIndexName in _affectedIndexNames) {
                    var index = instance.GetIndex(affectedIndexName);
                    index.Add(events, exprEvaluatorContext);
                }
            }
            catch (EPException) {
                // rollback
                // remove updated events
                foreach (var affectedIndexName in _affectedIndexNames) {
                    var index = instance.GetIndex(affectedIndexName);
                    index.Remove(events, exprEvaluatorContext);
                }

                // rollback change to events
                for (var i = 0; i < events.Length; i++) {
                    var oa = (ObjectArrayBackedEventBean) events[i];
                    oa.PropertyValues = previousData[i];
                }

                // add old events
                foreach (var affectedIndexName in _affectedIndexNames) {
                    var index = instance.GetIndex(affectedIndexName);
                    index.Add(events, exprEvaluatorContext);
                }

                throw;
            }
        }