All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.espertech.esper.runtime.internal.kernel.service.DeployerHelperDependencies Maven / Gradle / Ivy

There is a newer version: 9.0.0
Show newest version
/*
 ***************************************************************************************
 *  Copyright (C) 2006 EsperTech, Inc. All rights reserved.                            *
 *  http://www.espertech.com/esper                                                     *
 *  http://www.espertech.com                                                           *
 *  ---------------------------------------------------------------------------------- *
 *  The software in this package is published under the terms of the GPL license       *
 *  a copy of which has been included with this distribution in the license.txt file.  *
 ***************************************************************************************
 */
package com.espertech.esper.runtime.internal.kernel.service;

import com.espertech.esper.common.internal.collection.PathRegistry;
import com.espertech.esper.common.internal.context.module.ModuleIndexMeta;
import com.espertech.esper.common.internal.epl.lookupplansubord.EventTableIndexMetadata;
import com.espertech.esper.common.internal.epl.lookupplansubord.EventTableIndexMetadataEntry;
import com.espertech.esper.common.internal.epl.namedwindow.path.NamedWindowMetaData;
import com.espertech.esper.common.internal.epl.script.core.NameAndParamNum;
import com.espertech.esper.common.internal.epl.table.compiletime.TableMetaData;
import com.espertech.esper.runtime.client.EPDeploymentDependencyConsumed;
import com.espertech.esper.runtime.client.EPDeploymentDependencyProvided;
import com.espertech.esper.runtime.client.util.EPObjectType;
import com.espertech.esper.runtime.internal.deploymentlifesvc.DeploymentLifecycleService;

import java.util.*;
import java.util.function.Function;

public class DeployerHelperDependencies {

    private final static Function SCRIPT_OBJECTNAME = nameParam -> nameParam.getName() + "#" + Integer.toString(nameParam.getParamNum());
    private final static Function INDEX_OBJECTNAME = idx -> idx.getIndexName() + (idx.isNamedWindow() ? " on named-window " : " on table ") + idx.getInfraName();

    // what are the dependencies that the given deployment consumes from other modules?
    public static EPDeploymentDependencyConsumed getDependenciesConsumed(String selfDeploymentId, EPServicesPath paths, DeploymentLifecycleService deploymentLifecycleService) {
        DeploymentInternal selfDeployment = deploymentLifecycleService.getDeploymentById(selfDeploymentId);
        if (selfDeployment == null) {
            return null;
        }
        String[] consumedDeploymentIds = selfDeployment.getDeploymentIdDependencies();
        List consumed = new ArrayList<>(4);
        for (String providerDeploymentId : consumedDeploymentIds) {
            DeploymentInternal providingDeployment = deploymentLifecycleService.getDeploymentById(providerDeploymentId);
            if (providingDeployment == null) {
                continue;
            }
            String moduleName = providingDeployment.getModuleProvider().getModuleName();
            handleConsumed(providerDeploymentId, providingDeployment.getPathNamedWindows(), EPObjectType.NAMEDWINDOW, paths.getNamedWindowPathRegistry(), moduleName, selfDeploymentId, consumed, name -> name);
            handleConsumed(providerDeploymentId, providingDeployment.getPathTables(), EPObjectType.TABLE, paths.getTablePathRegistry(), moduleName, selfDeploymentId, consumed, name -> name);
            handleConsumed(providerDeploymentId, providingDeployment.getPathVariables(), EPObjectType.VARIABLE, paths.getVariablePathRegistry(), moduleName, selfDeploymentId, consumed, name -> name);
            handleConsumed(providerDeploymentId, providingDeployment.getPathContexts(), EPObjectType.CONTEXT, paths.getContextPathRegistry(), moduleName, selfDeploymentId, consumed, name -> name);
            handleConsumed(providerDeploymentId, providingDeployment.getPathEventTypes(), EPObjectType.EVENTTYPE, paths.getEventTypePathRegistry(), moduleName, selfDeploymentId, consumed, name -> name);
            handleConsumed(providerDeploymentId, providingDeployment.getPathExprDecls(), EPObjectType.EXPRESSION, paths.getExprDeclaredPathRegistry(), moduleName, selfDeploymentId, consumed, name -> name);
            handleConsumed(providerDeploymentId, providingDeployment.getPathScripts(), EPObjectType.SCRIPT, paths.getScriptPathRegistry(), moduleName, selfDeploymentId, consumed, SCRIPT_OBJECTNAME);
            handleConsumed(providerDeploymentId, providingDeployment.getPathClassProvideds(), EPObjectType.CLASSPROVIDED, paths.getClassProvidedPathRegistry(), moduleName, selfDeploymentId, consumed, name -> name);

            for (ModuleIndexMeta objectName : providingDeployment.getPathIndexes()) {
                EventTableIndexMetadata indexMetadata = getIndexMetadata(objectName, moduleName, paths);
                if (indexMetadata == null) {
                    continue;
                }
                EventTableIndexMetadataEntry meta = indexMetadata.getIndexEntryByName(objectName.getIndexName());
                if (meta != null && meta.getReferringDeployments() != null && meta.getReferringDeployments().length > 0) {
                    boolean found = false;
                    for (String dep : meta.getReferringDeployments()) {
                        if (dep.equals(selfDeploymentId)) {
                            found = true;
                            break;
                        }
                    }
                    if (found) {
                        consumed.add(new EPDeploymentDependencyConsumed.Item(providerDeploymentId, EPObjectType.INDEX, INDEX_OBJECTNAME.apply(objectName)));
                    }
                }
            }
        }
        return new EPDeploymentDependencyConsumed(consumed);
    }

    // what are the dependencies that the given deployment provides to other modules?
    public static EPDeploymentDependencyProvided getDependenciesProvided(String selfDeploymentId, EPServicesPath paths, DeploymentLifecycleService deploymentLifecycleService) {
        DeploymentInternal selfDeployment = deploymentLifecycleService.getDeploymentById(selfDeploymentId);
        if (selfDeployment == null) {
            return null;
        }
        List dependencies = new ArrayList<>(4);
        String moduleName = selfDeployment.getModuleProvider().getModuleName();
        handleProvided(selfDeployment.getPathNamedWindows(), EPObjectType.NAMEDWINDOW, paths.getNamedWindowPathRegistry(), moduleName, dependencies, name -> name);
        handleProvided(selfDeployment.getPathTables(), EPObjectType.TABLE, paths.getTablePathRegistry(), moduleName, dependencies, name -> name);
        handleProvided(selfDeployment.getPathVariables(), EPObjectType.VARIABLE, paths.getVariablePathRegistry(), moduleName, dependencies, name -> name);
        handleProvided(selfDeployment.getPathContexts(), EPObjectType.CONTEXT, paths.getContextPathRegistry(), moduleName, dependencies, name -> name);
        handleProvided(selfDeployment.getPathEventTypes(), EPObjectType.EVENTTYPE, paths.getEventTypePathRegistry(), moduleName, dependencies, name -> name);
        handleProvided(selfDeployment.getPathExprDecls(), EPObjectType.EXPRESSION, paths.getExprDeclaredPathRegistry(), moduleName, dependencies, name -> name);
        handleProvided(selfDeployment.getPathScripts(), EPObjectType.SCRIPT, paths.getScriptPathRegistry(), moduleName, dependencies, SCRIPT_OBJECTNAME);
        handleProvided(selfDeployment.getPathClassProvideds(), EPObjectType.CLASSPROVIDED, paths.getClassProvidedPathRegistry(), moduleName, dependencies, name -> name);

        for (ModuleIndexMeta objectName : selfDeployment.getPathIndexes()) {
            EventTableIndexMetadata indexMetadata = getIndexMetadata(objectName, moduleName, paths);
            if (indexMetadata == null) {
                continue;
            }
            EventTableIndexMetadataEntry meta = indexMetadata.getIndexEntryByName(objectName.getIndexName());
            if (meta != null && meta.getReferringDeployments() != null && meta.getReferringDeployments().length > 0) {
                Set referred = new HashSet<>(Arrays.asList(meta.getReferringDeployments()));
                referred.remove(selfDeploymentId);
                if (!referred.isEmpty()) {
                    dependencies.add(new EPDeploymentDependencyProvided.Item(EPObjectType.INDEX, INDEX_OBJECTNAME.apply(objectName), referred));
                }
            }
        }

        return new EPDeploymentDependencyProvided(dependencies);
    }

    private static EventTableIndexMetadata getIndexMetadata(ModuleIndexMeta objectName, String moduleName, EPServicesPath paths) {
        if (objectName.isNamedWindow()) {
            NamedWindowMetaData metaData = paths.getNamedWindowPathRegistry().getWithModule(objectName.getInfraName(), moduleName);
            return metaData == null ? null : metaData.getIndexMetadata();
        }
        TableMetaData metaData = paths.getTablePathRegistry().getWithModule(objectName.getInfraName(), moduleName);
        return metaData == null ? null : metaData.getIndexMetadata();
    }

    private static  void handleConsumed(String providerDeploymentId, K[] objectNames, EPObjectType objectType, PathRegistry registry, String moduleName, String selfDeploymentId, List consumed, Function objectNameFunction) {
        for (K objectName : objectNames) {
            try {
                Set ids = registry.getDependencies(objectName, moduleName);
                if (ids != null && ids.contains(selfDeploymentId)) {
                    consumed.add(new EPDeploymentDependencyConsumed.Item(providerDeploymentId, objectType, objectNameFunction.apply(objectName)));
                }
            } catch (IllegalArgumentException ex) {
                // not handled
            }
        }
    }

    private static  void handleProvided(K[] objectNames, EPObjectType objectType, PathRegistry registry, String moduleName, List dependencies, Function objectNameFunction) {
        for (K objectName : objectNames) {
            try {
                Set ids = registry.getDependencies(objectName, moduleName);
                if (ids != null) {
                    dependencies.add(new EPDeploymentDependencyProvided.Item(objectType, objectNameFunction.apply(objectName), new HashSet<>(ids)));
                }
            } catch (IllegalArgumentException ex) {
                // no need to handle
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy