public override EPStatementStartResult StartInternal(
            EPServicesContext services,
            StatementContext statementContext,
            bool isNewStatement,
            bool isRecoveringStatement,
            bool isRecoveringResilient)
        {
            var expressionName =
                services.ExprDeclaredService.AddExpressionOrScript(StatementSpec.CreateExpressionDesc);

            // define output event type
            var typeName   = "EventType_Expression_" + expressionName;
            var resultType = services.EventAdapterService.CreateAnonymousMapType(
                typeName, Collections.GetEmptyMap <String, Object>(), true);

            var stopMethod = new ProxyEPStatementStopMethod(
                () =>
            {
                // no action
            });

            var destroyMethod = new ProxyEPStatementDestroyMethod(
                () => services.ExprDeclaredService.DestroyedExpression(StatementSpec.CreateExpressionDesc));

            Viewable resultView = new ZeroDepthStreamNoIterate(resultType);

            statementContext.StatementAgentInstanceFactory = new StatementAgentInstanceFactoryNoAgentInstance(resultView);
            return(new EPStatementStartResult(resultView, stopMethod, destroyMethod));
        }
Esempio n. 2
0
        public override EPStatementStartResult StartInternal(EPServicesContext services, StatementContext statementContext, bool isNewStatement, bool isRecoveringStatement, bool isRecoveringResilient)
        {
            var createGraphDesc      = StatementSpec.CreateGraphDesc;
            var agentInstanceContext = GetDefaultAgentInstanceContext(statementContext);

            // define output event type
            var typeName   = "EventType_Graph_" + createGraphDesc.GraphName;
            var resultType = services.EventAdapterService.CreateAnonymousMapType(typeName, Collections.GetEmptyMap <String, Object>(), true);

            services.DataFlowService.AddStartGraph(createGraphDesc, statementContext, services, agentInstanceContext, isNewStatement);

            var stopMethod = new ProxyEPStatementStopMethod(() =>
                                                            services.DataFlowService.StopGraph(createGraphDesc.GraphName));

            var destroyMethod = new ProxyEPStatementDestroyMethod(() =>
                                                                  services.DataFlowService.RemoveGraph(createGraphDesc.GraphName));

            var resultView = new ZeroDepthStreamNoIterate(resultType);

            statementContext.StatementAgentInstanceFactory = new StatementAgentInstanceFactoryNoAgentInstance(resultView);
            return(new EPStatementStartResult(resultView, stopMethod, destroyMethod));
        }
        public override EPStatementStartResult StartInternal(
            EPServicesContext services,
            StatementContext statementContext,
            bool isNewStatement,
            bool isRecoveringStatement,
            bool isRecoveringResilient)
        {
            CreateSchemaDesc spec = StatementSpec.CreateSchemaDesc;

            EPLValidationUtil.ValidateTableExists(services.TableService, spec.SchemaName);
            EventType eventType = HandleCreateSchema(services, statementContext, spec);

            // enter a reference
            services.StatementEventTypeRefService.AddReferences(
                statementContext.StatementName, new String[]
            {
                spec.SchemaName
            });

            EventType             allocatedEventType = eventType;
            EPStatementStopMethod stopMethod         = new ProxyEPStatementStopMethod(() =>
            {
                services.StatementEventTypeRefService.RemoveReferencesStatement(statementContext.StatementName);
                if (services.StatementEventTypeRefService.GetStatementNamesForType(spec.SchemaName).IsEmpty())
                {
                    services.EventAdapterService.RemoveType(allocatedEventType.Name);
                    services.FilterService.RemoveType(allocatedEventType);
                }
            });

            Viewable viewable = new ViewableDefaultImpl(eventType);

            // assign agent instance factory (an empty op)
            statementContext.StatementAgentInstanceFactory = new StatementAgentInstanceFactoryNoAgentInstance(viewable);

            return(new EPStatementStartResult(viewable, stopMethod, null));
        }
Esempio n. 4
0
        public override EPStatementStartResult StartInternal(
            EPServicesContext services,
            StatementContext statementContext,
            bool isNewStatement,
            bool isRecoveringStatement,
            bool isRecoveringResilient)
        {
            if (_statementSpec.OptionalContextName != null)
            {
                throw new ExprValidationException("A create-context statement cannot itself be associated to a context, please declare a nested context instead");
            }
            var context = _statementSpec.ContextDesc;
            var agentInstanceContext = GetDefaultAgentInstanceContext(statementContext);

            // compile filter specs, if any
            ISet <string> eventTypesReferenced = new HashSet <string>();

            ValidateContextDetail(services, statementContext, eventTypesReferenced, context.ContextDetail, agentInstanceContext);
            services.StatementEventTypeRefService.AddReferences(statementContext.StatementName, CollectionUtil.ToArray(eventTypesReferenced));

            // define output event type
            var typeName = "EventType_Context_" + context.ContextName;
            var statementResultEventType = services.EventAdapterService.CreateAnonymousMapType(typeName, Collections.GetEmptyMap <string, object>(), true);

            // add context - does not activate that context
            services.ContextManagementService.AddContextSpec(services, agentInstanceContext, context, isRecoveringResilient, statementResultEventType);

            EPStatementStopMethod stopMethod = new ProxyEPStatementStopMethod(() =>
            {
                // no action
            });

            EPStatementDestroyMethod destroyMethod = new ProxyEPStatementDestroyMethod(() => services.ContextManagementService.DestroyedContext(context.ContextName));

            return(new EPStatementStartResult(new ZeroDepthStreamNoIterate(statementResultEventType), stopMethod, destroyMethod));
        }
        public override EPStatementStartResult StartInternal(EPServicesContext services, StatementContext statementContext, bool isNewStatement, bool isRecoveringStatement, bool isRecoveringResilient)
        {
            // validate use of table: may not both read and write
            ValidateTableAccessUse(StatementSpec.IntoTableSpec, StatementSpec.TableNodes);

            var contextName = StatementSpec.OptionalContextName;
            var defaultAgentInstanceContext = GetDefaultAgentInstanceContext(statementContext);
            var selectDesc = EPStatementStartMethodSelectUtil.Prepare(StatementSpec, services, statementContext, isRecoveringResilient, defaultAgentInstanceContext, IsQueryPlanLogging(services), null, null, null);

            statementContext.StatementAgentInstanceFactory = selectDesc.StatementAgentInstanceFactorySelect;

            // allow extension to walk
            statementContext.StatementExtensionServicesContext.PreStartWalk(selectDesc);

            // Determine context
            EPStatementStopMethod stopStatementMethod;
            Viewable           finalViewable;
            AggregationService aggregationService;
            IDictionary <ExprSubselectNode, SubSelectStrategyHolder>       subselectStrategyInstances;
            IDictionary <ExprPriorNode, ExprPriorEvalStrategy>             priorStrategyInstances;
            IDictionary <ExprPreviousNode, ExprPreviousEvalStrategy>       previousStrategyInstances;
            IDictionary <ExprTableAccessNode, ExprTableAccessEvalStrategy> tableAccessStrategyInstances;
            var preloadList = Collections.GetEmptyList <StatementAgentInstancePreload>();
            RegexExprPreviousEvalStrategy matchRecognizePrevEvalStrategy;

            // With context - delegate instantiation to context
            if (StatementSpec.OptionalContextName != null)
            {
                // use statement-wide agent-instance-specific aggregation service
                aggregationService = statementContext.StatementAgentInstanceRegistry.AgentInstanceAggregationService;

                // use statement-wide agent-instance-specific subselects
                var aiRegistryExpr = statementContext.StatementAgentInstanceRegistry.AgentInstanceExprService;

                subselectStrategyInstances = new Dictionary <ExprSubselectNode, SubSelectStrategyHolder>();
                foreach (var entry in selectDesc.SubSelectStrategyCollection.Subqueries)
                {
                    var specificService = aiRegistryExpr.AllocateSubselect(entry.Key);
                    entry.Key.Strategy = specificService;

                    IDictionary <ExprPriorNode, ExprPriorEvalStrategy> subselectPriorStrategies = new Dictionary <ExprPriorNode, ExprPriorEvalStrategy>();
                    foreach (var subselectPrior in entry.Value.PriorNodesList)
                    {
                        var specificSubselectPriorService = aiRegistryExpr.AllocatePrior(subselectPrior);
                        subselectPriorStrategies.Put(subselectPrior, specificSubselectPriorService);
                    }

                    IDictionary <ExprPreviousNode, ExprPreviousEvalStrategy> subselectPreviousStrategies = new Dictionary <ExprPreviousNode, ExprPreviousEvalStrategy>();
                    foreach (var subselectPrevious in entry.Value.PrevNodesList)
                    {
                        var specificSubselectPreviousService = aiRegistryExpr.AllocatePrevious(subselectPrevious);
                        subselectPreviousStrategies.Put(subselectPrevious, specificSubselectPreviousService);
                    }

                    var subselectAggregation = aiRegistryExpr.AllocateSubselectAggregation(entry.Key);
                    var strategyHolder       = new SubSelectStrategyHolder(specificService, subselectAggregation, subselectPriorStrategies, subselectPreviousStrategies, null, null, null);
                    subselectStrategyInstances.Put(entry.Key, strategyHolder);
                }

                // use statement-wide agent-instance-specific "prior"
                priorStrategyInstances = new Dictionary <ExprPriorNode, ExprPriorEvalStrategy>();
                foreach (var priorNode in selectDesc.ViewResourceDelegateUnverified.PriorRequests)
                {
                    var specificService = aiRegistryExpr.AllocatePrior(priorNode);
                    priorStrategyInstances.Put(priorNode, specificService);
                }

                // use statement-wide agent-instance-specific "previous"
                previousStrategyInstances = new Dictionary <ExprPreviousNode, ExprPreviousEvalStrategy>();
                foreach (var previousNode in selectDesc.ViewResourceDelegateUnverified.PreviousRequests)
                {
                    var specificService = aiRegistryExpr.AllocatePrevious(previousNode);
                    previousStrategyInstances.Put(previousNode, specificService);
                }

                // use statement-wide agent-instance-specific match-recognize "previous"
                matchRecognizePrevEvalStrategy = aiRegistryExpr.AllocateMatchRecognizePrevious();

                // use statement-wide agent-instance-specific tables
                tableAccessStrategyInstances = new Dictionary <ExprTableAccessNode, ExprTableAccessEvalStrategy>();
                if (StatementSpec.TableNodes != null)
                {
                    foreach (ExprTableAccessNode tableNode in StatementSpec.TableNodes)
                    {
                        var specificService = aiRegistryExpr.AllocateTableAccess(tableNode);
                        tableAccessStrategyInstances.Put(tableNode, specificService);
                    }
                }

                var mergeView = new ContextMergeView(selectDesc.ResultSetProcessorPrototypeDesc.ResultSetProcessorFactory.ResultEventType);
                finalViewable = mergeView;

                var statement = new ContextManagedStatementSelectDesc(StatementSpec, statementContext, mergeView, selectDesc.StatementAgentInstanceFactorySelect,
                                                                      selectDesc.ResultSetProcessorPrototypeDesc.AggregationServiceFactoryDesc.Expressions,
                                                                      selectDesc.SubSelectStrategyCollection);
                services.ContextManagementService.AddStatement(contextName, statement, isRecoveringResilient);
                var selectStop = selectDesc.StopMethod;
                stopStatementMethod = new ProxyEPStatementStopMethod(() =>
                {
                    services.ContextManagementService.StoppedStatement(
                        contextName,
                        statementContext.StatementName,
                        statementContext.StatementId,
                        statementContext.Expression,
                        statementContext.ExceptionHandlingService);
                    selectStop.Stop();
                });

                selectDesc.DestroyCallbacks.AddCallback(new EPStatementDestroyCallbackContext(services.ContextManagementService, contextName, statementContext.StatementName, statementContext.StatementId));
            }
            // Without context - start here
            else
            {
                var resultOfStart = (StatementAgentInstanceFactorySelectResult)selectDesc.StatementAgentInstanceFactorySelect.NewContext(defaultAgentInstanceContext, isRecoveringResilient);
                finalViewable = resultOfStart.FinalView;
                var startResultStop = services.EpStatementFactory.MakeStopMethod(resultOfStart);
                var selectStop      = selectDesc.StopMethod;
                stopStatementMethod = new ProxyEPStatementStopMethod(() =>
                {
                    StatementAgentInstanceUtil.StopSafe(startResultStop, statementContext);
                    selectStop.Stop();
                });
                aggregationService           = resultOfStart.OptionalAggegationService;
                subselectStrategyInstances   = resultOfStart.SubselectStrategies;
                priorStrategyInstances       = resultOfStart.PriorNodeStrategies;
                previousStrategyInstances    = resultOfStart.PreviousNodeStrategies;
                tableAccessStrategyInstances = resultOfStart.TableAccessEvalStrategies;
                preloadList = resultOfStart.PreloadList;

                matchRecognizePrevEvalStrategy = null;
                if (resultOfStart.TopViews.Length > 0)
                {
                    EventRowRegexNFAViewService matchRecognize = EventRowRegexHelper.RecursiveFindRegexService(resultOfStart.TopViews[0]);
                    if (matchRecognize != null)
                    {
                        matchRecognizePrevEvalStrategy = matchRecognize.PreviousEvaluationStrategy;
                    }
                }

                if (statementContext.StatementExtensionServicesContext != null && statementContext.StatementExtensionServicesContext.StmtResources != null)
                {
                    StatementResourceHolder holder = statementContext.StatementExtensionServicesContext.ExtractStatementResourceHolder(resultOfStart);
                    statementContext.StatementExtensionServicesContext.StmtResources.Unpartitioned = holder;
                    statementContext.StatementExtensionServicesContext.PostProcessStart(resultOfStart, isRecoveringResilient);
                }
            }

            var matchRecognizeNodes = selectDesc.StatementAgentInstanceFactorySelect.ViewResourceDelegate.PerStream[0].MatchRecognizePreviousRequests;

            // assign strategies to expression nodes
            EPStatementStartMethodHelperAssignExpr.AssignExpressionStrategies(
                selectDesc, aggregationService, subselectStrategyInstances, priorStrategyInstances,
                previousStrategyInstances, matchRecognizeNodes, matchRecognizePrevEvalStrategy,
                tableAccessStrategyInstances);

            // execute preload if any
            foreach (var preload in preloadList)
            {
                preload.ExecutePreload();
            }

            // handle association to table
            if (StatementSpec.IntoTableSpec != null)
            {
                services.StatementVariableRefService.AddReferences(statementContext.StatementName, StatementSpec.IntoTableSpec.Name);
            }

            return(new EPStatementStartResult(finalViewable, stopStatementMethod, selectDesc.DestroyCallbacks));
        }
        public override EPStatementStartResult StartInternal(
            EPServicesContext services,
            StatementContext statementContext,
            bool isNewStatement,
            bool isRecoveringStatement,
            bool isRecoveringResilient)
        {
            var createDesc = _statementSpec.CreateVariableDesc;

            VariableServiceUtil.CheckAlreadyDeclaredTable(createDesc.VariableName, services.TableService);

            // Get assignment value
            object value = null;

            if (createDesc.Assignment != null)
            {
                // Evaluate assignment expression
                StreamTypeService typeService = new StreamTypeServiceImpl(
                    new EventType[0], new string[0], new bool[0], services.EngineURI, false);
                var evaluatorContextStmt = new ExprEvaluatorContextStatement(statementContext, false);
                var validationContext    = new ExprValidationContext(
                    typeService,
                    statementContext.MethodResolutionService, null,
                    statementContext.SchedulingService,
                    statementContext.VariableService,
                    statementContext.TableService, evaluatorContextStmt,
                    statementContext.EventAdapterService,
                    statementContext.StatementName, statementContext.StatementId,
                    statementContext.Annotations,
                    statementContext.ContextDescriptor,
                    statementContext.ScriptingService,
                    false, false, false, false, null, false);
                var validated = ExprNodeUtility.GetValidatedSubtree(
                    ExprNodeOrigin.VARIABLEASSIGN, createDesc.Assignment, validationContext);
                value = validated.ExprEvaluator.Evaluate(new EvaluateParams(null, true, evaluatorContextStmt));
            }

            // Create variable
            try
            {
                services.VariableService.CreateNewVariable(
                    _statementSpec.OptionalContextName, createDesc.VariableName, createDesc.VariableType, createDesc.IsConstant,
                    createDesc.IsArray, createDesc.IsArrayOfPrimitive, value, services.EngineImportService);
            }
            catch (VariableExistsException ex)
            {
                // for new statement we don't allow creating the same variable
                if (isNewStatement)
                {
                    throw new ExprValidationException("Cannot create variable: " + ex.Message, ex);
                }
            }
            catch (VariableDeclarationException ex)
            {
                throw new ExprValidationException("Cannot create variable: " + ex.Message, ex);
            }

            var destroyMethod = new EPStatementDestroyCallbackList();

            destroyMethod.AddCallback(new ProxyDestroyCallback(() =>
            {
                try
                {
                    services.StatementVariableRefService.RemoveReferencesStatement(statementContext.StatementName);
                }
                catch (Exception ex)
                {
                    Log.Error("Error removing variable '" + createDesc.VariableName + "': " + ex.Message);
                }
            }));

            var stopMethod = new ProxyEPStatementStopMethod(() => { });

            var      variableMetaData = services.VariableService.GetVariableMetaData(createDesc.VariableName);
            Viewable outputView;
            var      eventType = CreateVariableView.GetEventType(
                statementContext.StatementId, services.EventAdapterService, variableMetaData);
            var contextFactory =
                new StatementAgentInstanceFactoryCreateVariable(
                    createDesc, _statementSpec, statementContext, services, variableMetaData, eventType);

            statementContext.StatementAgentInstanceFactory = contextFactory;

            if (_statementSpec.OptionalContextName != null)
            {
                var mergeView = new ContextMergeView(eventType);
                outputView = mergeView;
                var statement =
                    new ContextManagedStatementCreateVariableDesc(_statementSpec, statementContext, mergeView, contextFactory);
                services.ContextManagementService.AddStatement(
                    _statementSpec.OptionalContextName, statement, isRecoveringResilient);

                var contextManagementService = services.ContextManagementService;
                destroyMethod.AddCallback(new ProxyDestroyCallback(() => contextManagementService.DestroyedStatement(
                                                                       _statementSpec.OptionalContextName, statementContext.StatementName,
                                                                       statementContext.StatementId)));
            }
            else
            {
                var resultOfStart =
                    (StatementAgentInstanceFactoryCreateVariableResult)
                    contextFactory.NewContext(GetDefaultAgentInstanceContext(statementContext), isRecoveringResilient);
                outputView = resultOfStart.FinalView;

                if (statementContext.StatementExtensionServicesContext != null &&
                    statementContext.StatementExtensionServicesContext.StmtResources != null)
                {
                    var holder =
                        statementContext.StatementExtensionServicesContext.ExtractStatementResourceHolder(resultOfStart);
                    statementContext.StatementExtensionServicesContext.StmtResources.Unpartitioned = holder;
                    statementContext.StatementExtensionServicesContext.PostProcessStart(resultOfStart, isRecoveringResilient);
                }
            }

            services.StatementVariableRefService.AddReferences(
                statementContext.StatementName, Collections.SingletonList(createDesc.VariableName), null);
            return(new EPStatementStartResult(outputView, stopMethod, destroyMethod));
        }
        public override EPStatementStartResult StartInternal(
            EPServicesContext services,
            StatementContext statementContext,
            bool isNewStatement,
            bool isRecoveringStatement,
            bool isRecoveringResilient)
        {
            // define stop
            var stopCallbacks = new List <StopCallback>();

            // determine context
            var contextName = _statementSpec.OptionalContextName;

            // Create view factories and parent view based on a filter specification
            // Since only for non-joins we get the existing stream's lock and try to reuse it's views
            var filterStreamSpec = (FilterStreamSpecCompiled)_statementSpec.StreamSpecs[0];
            InstrumentationAgent instrumentationAgentCreateWindowInsert = null;

            if (InstrumentationHelper.ENABLED)
            {
                var eventTypeName = filterStreamSpec.FilterSpec.FilterForEventType.Name;
                instrumentationAgentCreateWindowInsert = new ProxyInstrumentationAgent()
                {
                    ProcIndicateQ = () => InstrumentationHelper.Get().QFilterActivationNamedWindowInsert(eventTypeName),
                    ProcIndicateA = () => InstrumentationHelper.Get().AFilterActivationNamedWindowInsert(),
                };
            }
            var activator = services.ViewableActivatorFactory.CreateFilterProxy(services, filterStreamSpec.FilterSpec, statementContext.Annotations, false, instrumentationAgentCreateWindowInsert, false, 0);

            // create data window view factories
            var unmaterializedViewChain = services.ViewService.CreateFactories(0, filterStreamSpec.FilterSpec.ResultEventType, filterStreamSpec.ViewSpecs, filterStreamSpec.Options, statementContext, false, -1);

            // verify data window
            VerifyDataWindowViewFactoryChain(unmaterializedViewChain.FactoryChain);

            // get processor for variant-streams and versioned typed
            var windowName = _statementSpec.CreateWindowDesc.WindowName;
            var optionalRevisionProcessor = statementContext.ValueAddEventService.GetValueAddProcessor(windowName);

            // add named window processor (one per named window for all agent instances)
            var isPrioritized = services.EngineSettingsService.EngineSettings.ExecutionConfig.IsPrioritized;
            var isEnableSubqueryIndexShare = HintEnum.ENABLE_WINDOW_SUBQUERY_INDEXSHARE.GetHint(_statementSpec.Annotations) != null;

            if (!isEnableSubqueryIndexShare && unmaterializedViewChain.FactoryChain[0] is VirtualDWViewFactory)
            {
                isEnableSubqueryIndexShare = true;      // index share is always enabled for virtual data window (otherwise it wouldn't make sense)
            }
            var isBatchingDataWindow     = DetermineBatchingDataWindow(unmaterializedViewChain.FactoryChain);
            var virtualDataWindowFactory = DetermineVirtualDataWindow(unmaterializedViewChain.FactoryChain);
            var optionalUniqueKeyProps   = ViewServiceHelper.GetUniqueCandidateProperties(unmaterializedViewChain.FactoryChain, _statementSpec.Annotations);
            var processor = services.NamedWindowMgmtService.AddProcessor(
                windowName, contextName, filterStreamSpec.FilterSpec.ResultEventType,
                statementContext.StatementResultService, optionalRevisionProcessor, statementContext.Expression,
                statementContext.StatementName, isPrioritized, isEnableSubqueryIndexShare, isBatchingDataWindow,
                virtualDataWindowFactory != null, optionalUniqueKeyProps,
                _statementSpec.CreateWindowDesc.AsEventTypeName,
                statementContext, services.NamedWindowDispatchService);

            Viewable finalViewable;
            EPStatementStopMethod    stopStatementMethod;
            EPStatementDestroyMethod destroyStatementMethod;

            try {
                // add stop callback
                stopCallbacks.Add(new ProxyStopCallback(() =>
                {
                    services.NamedWindowMgmtService.RemoveProcessor(windowName);
                    if (virtualDataWindowFactory != null)
                    {
                        virtualDataWindowFactory.DestroyNamedWindow();
                    }
                }));

                // Add a wildcard to the select clause as subscribers received the window contents
                _statementSpec.SelectClauseSpec.SetSelectExprList(new SelectClauseElementWildcard());
                _statementSpec.SelectStreamDirEnum = SelectClauseStreamSelectorEnum.RSTREAM_ISTREAM_BOTH;

                // obtain result set processor factory
                StreamTypeService typeService   = new StreamTypeServiceImpl(new EventType[] { processor.NamedWindowType }, new string[] { windowName }, new bool[] { true }, services.EngineURI, false);
                var resultSetProcessorPrototype = ResultSetProcessorFactoryFactory.GetProcessorPrototype(
                    _statementSpec, statementContext, typeService, null, new bool[0], true, null, null, services.ConfigSnapshot, services.ResultSetProcessorHelperFactory, false, false);

                // obtain factory for output limiting
                var outputViewFactory = OutputProcessViewFactoryFactory.Make(
                    _statementSpec,
                    services.InternalEventRouter,
                    statementContext,
                    resultSetProcessorPrototype.ResultSetProcessorFactory.ResultEventType, null,
                    services.TableService,
                    resultSetProcessorPrototype.ResultSetProcessorFactory.ResultSetProcessorType,
                    services.ResultSetProcessorHelperFactory,
                    services.StatementVariableRefService);

                // create context factory
                // Factory for statement-context instances
                var contextFactory = new StatementAgentInstanceFactoryCreateWindow(statementContext, _statementSpec, services, activator, unmaterializedViewChain, resultSetProcessorPrototype, outputViewFactory, isRecoveringStatement);
                statementContext.StatementAgentInstanceFactory = contextFactory;

                // With context - delegate instantiation to context
                EPStatementStopMethod stopMethod = new EPStatementStopMethodImpl(statementContext, stopCallbacks);
                if (_statementSpec.OptionalContextName != null)
                {
                    var mergeView = new ContextMergeView(processor.NamedWindowType);
                    finalViewable = mergeView;

                    var statement = new ContextManagedStatementCreateWindowDesc(_statementSpec, statementContext, mergeView, contextFactory);
                    services.ContextManagementService.AddStatement(contextName, statement, isRecoveringResilient);
                    stopStatementMethod = new ProxyEPStatementStopMethod(() =>
                    {
                        services.ContextManagementService.StoppedStatement(contextName, statementContext.StatementName, statementContext.StatementId, statementContext.Expression, statementContext.ExceptionHandlingService);
                        stopMethod.Stop();
                    });

                    destroyStatementMethod = new ProxyEPStatementDestroyMethod(() =>
                                                                               services.ContextManagementService.DestroyedStatement(contextName, statementContext.StatementName, statementContext.StatementId));
                }
                // Without context - start here
                else
                {
                    var agentInstanceContext = GetDefaultAgentInstanceContext(statementContext);
                    StatementAgentInstanceFactoryCreateWindowResult resultOfStart;
                    try {
                        resultOfStart = (StatementAgentInstanceFactoryCreateWindowResult)contextFactory.NewContext(agentInstanceContext, isRecoveringResilient);
                    }
                    catch (Exception) {
                        services.NamedWindowMgmtService.RemoveProcessor(windowName);
                        throw;
                    }
                    finalViewable = resultOfStart.FinalView;
                    var stopCallback = services.EpStatementFactory.MakeStopMethod(resultOfStart);
                    stopStatementMethod = new ProxyEPStatementStopMethod(() =>
                    {
                        stopCallback.Stop();
                        stopMethod.Stop();
                    });
                    destroyStatementMethod = null;

                    if (statementContext.StatementExtensionServicesContext != null && statementContext.StatementExtensionServicesContext.StmtResources != null)
                    {
                        var holder = statementContext.StatementExtensionServicesContext.ExtractStatementResourceHolder(resultOfStart);
                        statementContext.StatementExtensionServicesContext.StmtResources.Unpartitioned = holder;
                        statementContext.StatementExtensionServicesContext.PostProcessStart(resultOfStart, isRecoveringResilient);
                    }
                }
            }
            catch (ExprValidationException) {
                services.NamedWindowMgmtService.RemoveProcessor(windowName);
                throw;
            }
            catch (Exception) {
                services.NamedWindowMgmtService.RemoveProcessor(windowName);
                throw;
            }

            services.StatementVariableRefService.AddReferences(statementContext.StatementName, windowName);

            return(new EPStatementStartResult(finalViewable, stopStatementMethod, destroyStatementMethod));
        }
Esempio n. 8
0
        public override EPStatementStartResult StartInternal(EPServicesContext services, StatementContext statementContext, bool isNewStatement, bool isRecoveringStatement, bool isRecoveringResilient)
        {
            var spec = _statementSpec.CreateIndexDesc;
            var namedWindowProcessor = services.NamedWindowMgmtService.GetProcessor(spec.WindowName);
            var tableMetadata        = services.TableService.GetTableMetadata(spec.WindowName);

            if (namedWindowProcessor == null && tableMetadata == null)
            {
                throw new ExprValidationException("A named window or table by name '" + spec.WindowName + "' does not exist");
            }
            var indexedEventType = namedWindowProcessor != null ? namedWindowProcessor.NamedWindowType : tableMetadata.InternalEventType;
            var infraContextName = namedWindowProcessor != null ? namedWindowProcessor.ContextName : tableMetadata.ContextName;

            EPLValidationUtil.ValidateContextName(namedWindowProcessor == null, spec.WindowName, infraContextName, _statementSpec.OptionalContextName, true);

            // validate index
            var explicitIndexDesc = EventTableIndexUtil.ValidateCompileExplicitIndex(spec.IndexName, spec.IsUnique, spec.Columns, indexedEventType, statementContext);
            var advancedIndexDesc = explicitIndexDesc.AdvancedIndexProvisionDesc == null ? null : explicitIndexDesc.AdvancedIndexProvisionDesc.IndexDesc;
            var imk = new IndexMultiKey(spec.IsUnique, explicitIndexDesc.HashPropsAsList, explicitIndexDesc.BtreePropsAsList, advancedIndexDesc);

            // for tables we add the index to metadata
            if (tableMetadata != null)
            {
                services.TableService.ValidateAddIndex(statementContext.StatementName, tableMetadata, spec.IndexName, explicitIndexDesc, imk);
            }
            else
            {
                namedWindowProcessor.ValidateAddIndex(statementContext.StatementName, spec.IndexName, explicitIndexDesc, imk);
            }

            // allocate context factory
            Viewable viewable       = new ViewableDefaultImpl(indexedEventType);
            var      contextFactory = new StatementAgentInstanceFactoryCreateIndex(
                services, spec, viewable, namedWindowProcessor,
                tableMetadata?.TableName,
                _statementSpec.OptionalContextName,
                explicitIndexDesc);

            statementContext.StatementAgentInstanceFactory = contextFactory;

            // provide destroy method which de-registers interest in this index
            var finalTableService  = services.TableService;
            var finalStatementName = statementContext.StatementName;
            var destroyMethod      = new EPStatementDestroyCallbackList();

            if (tableMetadata != null)
            {
                destroyMethod.AddCallback(() => finalTableService.RemoveIndexReferencesStmtMayRemoveIndex(finalStatementName, tableMetadata));
            }
            else
            {
                destroyMethod.AddCallback(() => namedWindowProcessor.RemoveIndexReferencesStmtMayRemoveIndex(imk, finalStatementName));
            }

            EPStatementStopMethod stopMethod;

            if (_statementSpec.OptionalContextName != null)
            {
                var mergeView = new ContextMergeView(indexedEventType);
                var statement = new ContextManagedStatementCreateIndexDesc(_statementSpec, statementContext, mergeView, contextFactory);
                services.ContextManagementService.AddStatement(_statementSpec.OptionalContextName, statement, isRecoveringResilient);
                stopMethod = new ProxyEPStatementStopMethod(() => {});

                var contextManagementService = services.ContextManagementService;
                destroyMethod.AddCallback(() => contextManagementService.DestroyedStatement(_statementSpec.OptionalContextName, statementContext.StatementName, statementContext.StatementId));
            }
            else
            {
                var defaultAgentInstanceContext = GetDefaultAgentInstanceContext(statementContext);
                StatementAgentInstanceFactoryResult result;
                try {
                    result = contextFactory.NewContext(defaultAgentInstanceContext, isRecoveringResilient);
                }
                catch (EPException ex) {
                    if (ex.InnerException is ExprValidationException)
                    {
                        throw (ExprValidationException)ex.InnerException;
                    }
                    destroyMethod.Destroy();
                    throw;
                }
                catch (Exception)
                {
                    destroyMethod.Destroy();
                    throw;
                }
                var stopCallback = services.EpStatementFactory.MakeStopMethod(result);
                stopMethod = new ProxyEPStatementStopMethod(stopCallback.Stop);

                if (statementContext.StatementExtensionServicesContext != null && statementContext.StatementExtensionServicesContext.StmtResources != null)
                {
                    var holder = statementContext.StatementExtensionServicesContext.ExtractStatementResourceHolder(result);
                    statementContext.StatementExtensionServicesContext.StmtResources.Unpartitioned = holder;
                    statementContext.StatementExtensionServicesContext.PostProcessStart(result, isRecoveringResilient);
                }
            }

            if (tableMetadata != null)
            {
                services.StatementVariableRefService.AddReferences(statementContext.StatementName, tableMetadata.TableName);
            }
            else
            {
                services.StatementVariableRefService.AddReferences(statementContext.StatementName, namedWindowProcessor.NamedWindowType.Name);
            }

            return(new EPStatementStartResult(viewable, stopMethod, destroyMethod));
        }
        public override EPStatementStartResult StartInternal(
            EPServicesContext services,
            StatementContext statementContext,
            bool isNewStatement,
            bool isRecoveringStatement,
            bool isRecoveringResilient)
        {
            var statementSpec = base.StatementSpec;

            // define stop and destroy
            var stopCallbacks    = new List <StopCallback>();
            var destroyCallbacks = new EPStatementDestroyCallbackList();

            // determine context
            var contextName = statementSpec.OptionalContextName;

            if (contextName != null)
            {
                throw new ExprValidationException("Update IStream is not supported in conjunction with a context");
            }

            // First we create streams for subselects, if there are any
            var subSelectStreamDesc =
                EPStatementStartMethodHelperSubselect.CreateSubSelectActivation(
                    services, statementSpec, statementContext, destroyCallbacks);

            var    streamSpec = statementSpec.StreamSpecs[0];
            var    updateSpec = statementSpec.UpdateSpec;
            string triggereventTypeName;

            if (streamSpec is FilterStreamSpecCompiled)
            {
                var filterStreamSpec = (FilterStreamSpecCompiled)streamSpec;
                triggereventTypeName = filterStreamSpec.FilterSpec.FilterForEventTypeName;
            }
            else if (streamSpec is NamedWindowConsumerStreamSpec)
            {
                var namedSpec = (NamedWindowConsumerStreamSpec)streamSpec;
                triggereventTypeName = namedSpec.WindowName;
            }
            else if (streamSpec is TableQueryStreamSpec)
            {
                throw new ExprValidationException("Tables cannot be used in an update-istream statement");
            }
            else
            {
                throw new ExprValidationException("Unknown stream specification streamEventType: " + streamSpec);
            }

            // determine a stream name
            var streamName = triggereventTypeName;

            if (updateSpec.OptionalStreamName != null)
            {
                streamName = updateSpec.OptionalStreamName;
            }

            var streamEventType = services.EventAdapterService.GetEventTypeByName(triggereventTypeName);
            var typeService     = new StreamTypeServiceImpl(
                new EventType[] { streamEventType },
                new string[] { streamName },
                new bool[] { true },
                services.EngineURI, false);

            // determine subscriber result types
            var evaluatorContextStmt = new ExprEvaluatorContextStatement(statementContext, false);

            statementContext.StatementResultService.SetSelectClause(
                new Type[] { streamEventType.UnderlyingType },
                new string[] { "*" },
                false, null, evaluatorContextStmt);

            // Materialize sub-select views
            var subSelectStrategyCollection =
                EPStatementStartMethodHelperSubselect.PlanSubSelect(
                    services, statementContext, IsQueryPlanLogging(services), subSelectStreamDesc, new string[]
            {
                streamName
            }, new EventType[]
            {
                streamEventType
            }, new string[]
            {
                triggereventTypeName
            }, statementSpec.DeclaredExpressions, null);

            var validationContext = new ExprValidationContext(
                statementContext.Container,
                typeService,
                statementContext.EngineImportService,
                statementContext.StatementExtensionServicesContext,
                null,
                statementContext.SchedulingService,
                statementContext.VariableService,
                statementContext.TableService, evaluatorContextStmt,
                statementContext.EventAdapterService,
                statementContext.StatementName,
                statementContext.StatementId,
                statementContext.Annotations,
                statementContext.ContextDescriptor,
                statementContext.ScriptingService,
                false, false, false, false, null,
                false);

            foreach (var assignment in updateSpec.Assignments)
            {
                var validated = ExprNodeUtility.GetValidatedAssignment(assignment, validationContext);
                assignment.Expression = validated;
                EPStatementStartMethodHelperValidate.ValidateNoAggregations(
                    validated, "Aggregation functions may not be used within an update-clause");
            }
            if (updateSpec.OptionalWhereClause != null)
            {
                var validated = ExprNodeUtility.GetValidatedSubtree(
                    ExprNodeOrigin.WHERE, updateSpec.OptionalWhereClause, validationContext);
                updateSpec.OptionalWhereClause = validated;
                EPStatementStartMethodHelperValidate.ValidateNoAggregations(
                    validated, "Aggregation functions may not be used within an update-clause");
            }

            // preprocessing view
            var onExprView = new InternalRoutePreprocessView(streamEventType, statementContext.StatementResultService);

            // validation
            var routerDesc =
                services.InternalEventRouter.GetValidatePreprocessing(
                    onExprView.EventType, updateSpec, statementContext.Annotations);

            // create context factory
            var contextFactory = new StatementAgentInstanceFactoryUpdate(
                statementContext, services, streamEventType, updateSpec, onExprView, routerDesc,
                subSelectStrategyCollection);

            statementContext.StatementAgentInstanceFactory = contextFactory;

            // perform start of hook-up to start
            Viewable finalViewable;
            EPStatementStopMethod stopStatementMethod;
            IDictionary <ExprSubselectNode, SubSelectStrategyHolder> subselectStrategyInstances;

            // With context - delegate instantiation to context
            var stopMethod = new EPStatementStopMethodImpl(statementContext, stopCallbacks);

            if (statementSpec.OptionalContextName != null)
            {
                // use statement-wide agent-instance-specific subselects
                var aiRegistryExpr = statementContext.StatementAgentInstanceRegistry.AgentInstanceExprService;
                subselectStrategyInstances = new Dictionary <ExprSubselectNode, SubSelectStrategyHolder>();
                foreach (var node in subSelectStrategyCollection.Subqueries.Keys)
                {
                    var specificService = aiRegistryExpr.AllocateSubselect(node);
                    node.Strategy = specificService;
                    subselectStrategyInstances.Put(
                        node, new SubSelectStrategyHolder(null, null, null, null, null, null, null));
                }

                var mergeView = new ContextMergeView(onExprView.EventType);
                finalViewable = mergeView;

                var statement = new ContextManagedStatementOnTriggerDesc(
                    statementSpec, statementContext, mergeView, contextFactory);
                services.ContextManagementService.AddStatement(
                    statementSpec.OptionalContextName, statement, isRecoveringResilient);
                stopStatementMethod = new ProxyEPStatementStopMethod
                {
                    ProcStop = () =>
                    {
                        services.ContextManagementService.StoppedStatement(
                            contextName, statementContext.StatementName, statementContext.StatementId,
                            statementContext.Expression, statementContext.ExceptionHandlingService);
                        stopMethod.Stop();
                    }
                };

                destroyCallbacks.AddCallback(
                    new EPStatementDestroyCallbackContext(
                        services.ContextManagementService, statementSpec.OptionalContextName,
                        statementContext.StatementName, statementContext.StatementId));
            }
            else
            {
                // Without context - start here
                var agentInstanceContext = GetDefaultAgentInstanceContext(statementContext);
                var resultOfStart        =
                    (StatementAgentInstanceFactoryUpdateResult)
                    contextFactory.NewContext(agentInstanceContext, isRecoveringResilient);
                finalViewable = resultOfStart.FinalView;
                var stopCallback = services.EpStatementFactory.MakeStopMethod(resultOfStart);
                stopStatementMethod = new ProxyEPStatementStopMethod
                {
                    ProcStop = () =>
                    {
                        stopCallback.Stop();
                        stopMethod.Stop();
                    }
                };
                subselectStrategyInstances = resultOfStart.SubselectStrategies;

                if (statementContext.StatementExtensionServicesContext != null &&
                    statementContext.StatementExtensionServicesContext.StmtResources != null)
                {
                    var holder =
                        statementContext.StatementExtensionServicesContext.ExtractStatementResourceHolder(resultOfStart);
                    statementContext.StatementExtensionServicesContext.StmtResources.Unpartitioned = holder;
                    statementContext.StatementExtensionServicesContext.PostProcessStart(
                        resultOfStart, isRecoveringResilient);
                }
            }

            // assign subquery nodes
            EPStatementStartMethodHelperAssignExpr.AssignSubqueryStrategies(
                subSelectStrategyCollection, subselectStrategyInstances);

            return(new EPStatementStartResult(finalViewable, stopStatementMethod, destroyCallbacks));
        }
Esempio n. 10
0
        public override EPStatementStartResult StartInternal(EPServicesContext services, StatementContext statementContext, bool isNewStatement, bool isRecoveringStatement, bool isRecoveringResilient)
        {
            var createDesc = _statementSpec.CreateTableDesc;

            // determine whether already declared
            VariableServiceUtil.CheckAlreadyDeclaredVariable(createDesc.TableName, services.VariableService);
            if (isNewStatement)
            {
                VariableServiceUtil.CheckAlreadyDeclaredTable(createDesc.TableName, services.TableService);
            }
            if (services.EventAdapterService.GetEventTypeByName(createDesc.TableName) != null)
            {
                throw new ExprValidationException("An event type or schema by name '" + createDesc.TableName + "' already exists");
            }

            // Determine event type names
            var internalTypeName = "table_" + createDesc.TableName + "__internal";
            var publicTypeName   = "table_" + createDesc.TableName + "__public";

            TableMetadata metadata;

            try
            {
                // determine key types
                var keyTypes = GetKeyTypes(createDesc.Columns, services.EngineImportService);

                // check column naming, interpret annotations
                var columnDescs = ValidateExpressions(createDesc.Columns, services, statementContext);

                // analyze and plan the state holders
                var plan = AnalyzePlanAggregations(createDesc.TableName, statementContext, columnDescs, services, internalTypeName, publicTypeName);
                var tableStateRowFactory = plan.StateRowFactory;

                // register new table
                var queryPlanLogging = services.ConfigSnapshot.EngineDefaults.Logging.IsEnableQueryPlan;
                metadata = services.TableService.AddTable(createDesc.TableName, statementContext.Expression, statementContext.StatementName, keyTypes, plan.TableColumns, tableStateRowFactory, plan.NumberMethodAggregations, statementContext, plan.InternalEventType,
                                                          plan.PublicEventType, plan.EventToPublic, queryPlanLogging);
            }
            catch (ExprValidationException ex)
            {
                services.EventAdapterService.RemoveType(internalTypeName);
                services.EventAdapterService.RemoveType(publicTypeName);
                throw ex;
            }

            // allocate context factory
            var contextFactory = new StatementAgentInstanceFactoryCreateTable(metadata);

            statementContext.StatementAgentInstanceFactory = contextFactory;
            Viewable outputView;
            EPStatementStopMethod    stopStatementMethod;
            EPStatementDestroyMethod destroyStatementMethod;

            if (_statementSpec.OptionalContextName != null)
            {
                var contextName = _statementSpec.OptionalContextName;
                var mergeView   = new ContextMergeView(metadata.PublicEventType);
                outputView = mergeView;
                var statement = new ContextManagedStatementCreateAggregationVariableDesc(_statementSpec, statementContext, mergeView, contextFactory);
                services.ContextManagementService.AddStatement(_statementSpec.OptionalContextName, statement, isRecoveringResilient);

                stopStatementMethod = new ProxyEPStatementStopMethod(() =>
                                                                     services.ContextManagementService.StoppedStatement(
                                                                         contextName, statementContext.StatementName, statementContext.StatementId,
                                                                         statementContext.Expression, statementContext.ExceptionHandlingService));

                destroyStatementMethod = new ProxyEPStatementDestroyMethod(() =>
                {
                    services.ContextManagementService.DestroyedStatement(contextName, statementContext.StatementName, statementContext.StatementId);
                    services.StatementVariableRefService.RemoveReferencesStatement(statementContext.StatementName);
                });
            }
            else
            {
                var defaultAgentInstanceContext = GetDefaultAgentInstanceContext(statementContext);
                var result = contextFactory.NewContext(defaultAgentInstanceContext, false);
                if (statementContext.StatementExtensionServicesContext != null && statementContext.StatementExtensionServicesContext.StmtResources != null)
                {
                    var holder = statementContext.StatementExtensionServicesContext.ExtractStatementResourceHolder(result);
                    statementContext.StatementExtensionServicesContext.StmtResources.Unpartitioned = holder;
                }
                outputView = result.FinalView;

                stopStatementMethod    = new ProxyEPStatementStopMethod(() => { });
                destroyStatementMethod = new ProxyEPStatementDestroyMethod(() =>
                                                                           services.StatementVariableRefService.RemoveReferencesStatement(statementContext.StatementName));
            }

            services.StatementVariableRefService.AddReferences(statementContext.StatementName, createDesc.TableName);

            return(new EPStatementStartResult(outputView, stopStatementMethod, destroyStatementMethod));
        }