示例#1
0
        public static EventStream GetAuditProxy(String engineURI, String statementName, Attribute[] annotations, FilterSpecCompiled filterSpec, EventStream designated)
        {
            var audit = AuditEnum.STREAM.GetAudit(annotations);

            if (audit == null)
            {
                return(designated);
            }

            var writer = new StringWriter();

            writer.Write(filterSpec.FilterForEventType.Name);
            if (filterSpec.Parameters != null && filterSpec.Parameters.Length > 0)
            {
                writer.Write('(');
                String delimiter = "";
                foreach (FilterSpecParam[] paramLine in filterSpec.Parameters)
                {
                    writer.Write(delimiter);
                    WriteFilter(writer, paramLine);
                    delimiter = " or ";
                }
                writer.Write(')');
            }

            return((EventStream)EventStreamProxy.NewInstance(engineURI, statementName, writer.ToString(), designated));
        }
示例#2
0
        /// <summary>
        /// See the method of the same name in <seealso cref="com.espertech.esper.view.stream.StreamFactoryService" />.
        /// Always attempts to reuse an existing event stream. May thus return a new event stream or an existing event
        /// stream depending on whether filter criteria match.
        /// </summary>
        /// <param name="statementId">the statement id</param>
        /// <param name="filterSpec">is the filter definition</param>
        /// <param name="filterService">filter service to activate filter if not already active</param>
        /// <param name="epStatementAgentInstanceHandle">is the statement resource lock</param>
        /// <param name="isJoin">is indicatng whether the stream will participate in a join statement, information necessary for stream reuse and multithreading concerns</param>
        /// <param name="agentInstanceContext"></param>
        /// <param name="hasOrderBy">if the consumer has order-by</param>
        /// <param name="filterWithSameTypeSubselect">if set to <c>true</c> [filter with same type subselect].</param>
        /// <param name="annotations">The annotations.</param>
        /// <param name="stateless">if set to <c>true</c> [stateless].</param>
        /// <param name="streamNum">The stream num.</param>
        /// <param name="isCanIterateUnbound">if set to <c>true</c> [is can iterate unbound].</param>
        /// <returns>
        /// newly createdStatement event stream, not reusing existing instances
        /// </returns>
        /// <exception cref="IllegalStateException">Filter spec object already found in collection</exception>
        public Pair <EventStream, IReaderWriterLock> CreateStream(
            int statementId,
            FilterSpecCompiled filterSpec,
            FilterService filterService,
            EPStatementAgentInstanceHandle epStatementAgentInstanceHandle,
            bool isJoin,
            AgentInstanceContext agentInstanceContext,
            bool hasOrderBy,
            bool filterWithSameTypeSubselect,
            Attribute[] annotations,
            bool stateless,
            int streamNum,
            bool isCanIterateUnbound)
        {
            EventStream eventStream;

            if (Log.IsDebugEnabled)
            {
                Log.Debug(".createStream hashCode=" + filterSpec.GetHashCode() + " filter=" + filterSpec);
            }

            // Check if a stream for this filter already exists
            StreamEntry entry;
            var         forceNewStream = isJoin || (!_isReuseViews) || hasOrderBy || filterWithSameTypeSubselect || stateless;

            if (forceNewStream)
            {
                entry = _eventStreamsIdentity.Get(filterSpec);
            }
            else
            {
                entry = _eventStreamsRefCounted[filterSpec];
            }

            // If pair exists, either reference count or illegal state
            if (entry != null)
            {
                if (forceNewStream)
                {
                    throw new IllegalStateException("Filter spec object already found in collection");
                }
                else
                {
                    Log.Debug(".createStream filter already found");
                    _eventStreamsRefCounted.Reference(filterSpec);

                    // audit proxy
                    eventStream = EventStreamProxy.GetAuditProxy(
                        _engineURI, epStatementAgentInstanceHandle.StatementHandle.StatementName, annotations,
                        filterSpec, entry.EventStream);

                    // We return the lock of the statement first establishing the stream to use that as the new statement's lock
                    return(new Pair <EventStream, IReaderWriterLock>(
                               eventStream, entry.Callback.AgentInstanceHandle.StatementAgentInstanceLock));
                }
            }

            // New event stream
            var resultEventType = filterSpec.ResultEventType;
            var zeroDepthStream = isCanIterateUnbound
                ? (EventStream) new ZeroDepthStreamIterable(resultEventType)
                : (EventStream) new ZeroDepthStreamNoIterate(resultEventType);

            // audit proxy
            var inputStream = EventStreamProxy.GetAuditProxy(
                _engineURI, epStatementAgentInstanceHandle.StatementHandle.StatementName, annotations, filterSpec,
                zeroDepthStream);

            eventStream = inputStream;
            FilterHandleCallback filterCallback;

            if (filterSpec.OptionalPropertyEvaluator != null)
            {
                filterCallback = new ProxyFilterHandleCallback()
                {
                    ProcStatementId = () => statementId,
                    ProcMatchFound  = (theEvent, allStmtMatches) =>
                    {
                        var result = filterSpec.OptionalPropertyEvaluator.GetProperty(theEvent, agentInstanceContext);
                        if (result != null)
                        {
                            eventStream.Insert(result);
                        }
                    },
                    ProcIsSubselect = () => false
                };
            }
            else
            {
                filterCallback = new ProxyFilterHandleCallback()
                {
                    ProcStatementId = () => statementId,
                    ProcMatchFound  = (theEvent, allStmtMatches) =>
                    {
                        if (InstrumentationHelper.ENABLED)
                        {
                            InstrumentationHelper.Get().QFilterActivationStream(theEvent.EventType.Name, streamNum);
                        }
                        eventStream.Insert(theEvent);
                        if (InstrumentationHelper.ENABLED)
                        {
                            InstrumentationHelper.Get().AFilterActivationStream();
                        }
                    },
                    ProcIsSubselect = () => false
                };
            }

            var handle = new EPStatementHandleCallback(epStatementAgentInstanceHandle, filterCallback);

            // Store stream for reuse
            entry = new StreamEntry(eventStream, handle);
            if (forceNewStream)
            {
                _eventStreamsIdentity.Put(filterSpec, entry);
            }
            else
            {
                _eventStreamsRefCounted[filterSpec] = entry;
            }

            // Activate filter
            var filterValues       = filterSpec.GetValueSet(null, agentInstanceContext, null);
            var filterServiceEntry = filterService.Add(filterValues, handle);

            entry.FilterServiceEntry = filterServiceEntry;

            return(new Pair <EventStream, IReaderWriterLock>(inputStream, null));
        }