Search in sources :

Example 1 with Operations

use of uk.gov.gchq.gaffer.operation.Operations in project Gaffer by gchq.

the class Store method runJob.

private JobDetail runJob(final Operation operation, final JobDetail jobDetail, final Context context) {
    final OperationChain<?> clonedOp = (operation instanceof Operations) ? (OperationChain) operation.shallowClone() : OperationChain.wrap(operation).shallowClone();
    if (isSupported(ExportToGafferResultCache.class)) {
        boolean hasExport = false;
        for (final Operation op : clonedOp.getOperations()) {
            if (op instanceof ExportToGafferResultCache) {
                hasExport = true;
                break;
            }
        }
        if (!hasExport) {
            clonedOp.getOperations().add(new ExportToGafferResultCache());
        }
    }
    runAsync(() -> {
        try {
            handleOperation(clonedOp, context);
            addOrUpdateJobDetail(clonedOp, context, null, JobStatus.FINISHED);
        } catch (final Error e) {
            addOrUpdateJobDetail(clonedOp, context, e.getMessage(), JobStatus.FAILED);
            throw e;
        } catch (final Exception e) {
            LOGGER.warn("Operation chain job failed to execute", e);
            addOrUpdateJobDetail(clonedOp, context, e.getMessage(), JobStatus.FAILED);
        }
    });
    return jobDetail;
}
Also used : ExportToGafferResultCache(uk.gov.gchq.gaffer.operation.impl.export.resultcache.ExportToGafferResultCache) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) DeleteNamedOperation(uk.gov.gchq.gaffer.named.operation.DeleteNamedOperation) Operation(uk.gov.gchq.gaffer.operation.Operation) AddNamedOperation(uk.gov.gchq.gaffer.named.operation.AddNamedOperation) GetAllNamedOperations(uk.gov.gchq.gaffer.named.operation.GetAllNamedOperations) Operations(uk.gov.gchq.gaffer.operation.Operations) SchemaException(uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException) SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) OperationException(uk.gov.gchq.gaffer.operation.OperationException)

Example 2 with Operations

use of uk.gov.gchq.gaffer.operation.Operations in project Gaffer by gchq.

the class Graph method updateOperationChainView.

private void updateOperationChainView(final Operations<?> operations) {
    for (final Operation operation : operations.getOperations()) {
        if (operation instanceof Operations) {
            updateOperationChainView((Operations) operation);
        } else if (operation instanceof OperationView) {
            View opView = ((OperationView) operation).getView();
            if (null == opView) {
                opView = config.getView();
            } else if (!(opView instanceof NamedView) && !opView.hasGroups() && !opView.isAllEdges() && !opView.isAllEntities()) {
                // merge with both Entities and Edges
                if (!isEmpty(opView.getGlobalElements()) || (isEmpty(opView.getGlobalEdges()) && isEmpty(opView.getGlobalEntities()))) {
                    opView = new View.Builder().merge(config.getView()).merge(opView).build();
                } else {
                    // We have either global edges or entities in
                    // opView, but not both
                    final View originalView = opView;
                    final View partialConfigView = new View.Builder().merge(config.getView()).removeEdges((x -> isEmpty(originalView.getGlobalEdges()))).removeEntities((x -> isEmpty(originalView.getGlobalEntities()))).build();
                    opView = new View.Builder().merge(partialConfigView).merge(opView).build();
                }
            } else if (opView.isAllEdges() || opView.isAllEntities()) {
                View.Builder opViewBuilder = new View.Builder().merge(opView);
                if (opView.isAllEdges()) {
                    opViewBuilder.edges(getSchema().getEdgeGroups());
                }
                if (opView.isAllEntities()) {
                    opViewBuilder.entities(getSchema().getEntityGroups());
                }
                opView = opViewBuilder.build();
            }
            opView.expandGlobalDefinitions();
            ((OperationView) operation).setView(opView);
        }
    }
}
Also used : Job(uk.gov.gchq.gaffer.jobtracker.Job) LoggerFactory(org.slf4j.LoggerFactory) SchemaException(uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) URI(java.net.URI) Path(java.nio.file.Path) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) Pair(uk.gov.gchq.gaffer.commonutil.pair.Pair) Set(java.util.Set) Collectors(java.util.stream.Collectors) IOUtils(org.apache.commons.io.IOUtils) Store(uk.gov.gchq.gaffer.store.Store) ReflectionUtil(uk.gov.gchq.koryphe.util.ReflectionUtil) List(java.util.List) Context(uk.gov.gchq.gaffer.store.Context) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) CloseableUtil(uk.gov.gchq.gaffer.commonutil.CloseableUtil) NamedOperationResolver(uk.gov.gchq.gaffer.graph.hook.NamedOperationResolver) OperationException(uk.gov.gchq.gaffer.operation.OperationException) StoreTrait(uk.gov.gchq.gaffer.store.StoreTrait) Operations(uk.gov.gchq.gaffer.operation.Operations) GraphHook(uk.gov.gchq.gaffer.graph.hook.GraphHook) JSONSerialiser(uk.gov.gchq.gaffer.jsonserialisation.JSONSerialiser) StoreException(uk.gov.gchq.gaffer.store.StoreException) User(uk.gov.gchq.gaffer.user.User) OperationView(uk.gov.gchq.gaffer.operation.graph.OperationView) UpdateViewHook(uk.gov.gchq.gaffer.graph.hook.UpdateViewHook) ArrayList(java.util.ArrayList) GraphLibrary(uk.gov.gchq.gaffer.store.library.GraphLibrary) Lists(com.google.common.collect.Lists) Output(uk.gov.gchq.gaffer.operation.io.Output) NoGraphLibrary(uk.gov.gchq.gaffer.store.library.NoGraphLibrary) StreamUtil(uk.gov.gchq.gaffer.commonutil.StreamUtil) Logger(org.slf4j.Logger) Properties(java.util.Properties) NamedViewResolver(uk.gov.gchq.gaffer.graph.hook.NamedViewResolver) Files(java.nio.file.Files) FileUtils(org.apache.commons.io.FileUtils) JobDetail(uk.gov.gchq.gaffer.jobtracker.JobDetail) IOException(java.io.IOException) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) Operation(uk.gov.gchq.gaffer.operation.Operation) Schema(uk.gov.gchq.gaffer.store.schema.Schema) CollectionUtils.isEmpty(org.apache.commons.collections.CollectionUtils.isEmpty) FunctionAuthoriser(uk.gov.gchq.gaffer.graph.hook.FunctionAuthoriser) FunctionAuthoriserUtil(uk.gov.gchq.gaffer.graph.hook.FunctionAuthoriserUtil) Collections(java.util.Collections) InputStream(java.io.InputStream) OperationView(uk.gov.gchq.gaffer.operation.graph.OperationView) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operation(uk.gov.gchq.gaffer.operation.Operation) Operations(uk.gov.gchq.gaffer.operation.Operations) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) OperationView(uk.gov.gchq.gaffer.operation.graph.OperationView)

Example 3 with Operations

use of uk.gov.gchq.gaffer.operation.Operations in project Gaffer by gchq.

the class FederatedStoreUtil method updateOperationForGraph.

/**
 * <p>
 * Within FederatedStore an {@link Operation} is executed against a
 * collection of many graphs.
 * </p>
 * <p>
 * Problem: When an Operation contains View information about an Element
 * which is not known by the Graph; It will fail validation when executed.
 * </p>
 * <p>
 * Solution: For each operation, remove all elements from the View that is
 * unknown to the graph. This method will also update AddElements operations
 * to allow elements to be added to various federated graphs with different
 * schemas at the same time without causing validation errors.
 * </p>
 *
 * @param operation current operation
 * @param graph     current graph
 * @param <OP>      Operation type
 * @return cloned operation with modified View for the given graph.
 */
public static <OP extends Operation> OP updateOperationForGraph(final OP operation, final Graph graph) {
    OP resultOp = (OP) operation.shallowClone();
    if (nonNull(resultOp.getOptions())) {
        resultOp.setOptions(new HashMap<>(resultOp.getOptions()));
    }
    if (resultOp instanceof Operations) {
        final Operations<Operation> operations = (Operations) resultOp;
        final List<Operation> resultOperations = new ArrayList<>();
        for (final Operation nestedOp : operations.getOperations()) {
            final Operation updatedNestedOp = updateOperationForGraph(nestedOp, graph);
            if (null == updatedNestedOp) {
                resultOp = null;
                break;
            }
            resultOperations.add(updatedNestedOp);
        }
        operations.updateOperations(resultOperations);
    } else if (resultOp instanceof OperationView) {
        final View view = ((OperationView) resultOp).getView();
        if (null != view && view.hasGroups()) {
            final View validView = createValidView(view, graph.getSchema());
            if (view != validView) {
                // then clone the operation and add the new view.
                if (validView.hasGroups()) {
                    ((OperationView) resultOp).setView(validView);
                } else if (!graph.hasTrait(StoreTrait.DYNAMIC_SCHEMA)) {
                    // The view has no groups so the operation would return
                    // nothing, so we shouldn't execute the operation.
                    resultOp = null;
                }
            }
        }
    } else if (resultOp instanceof AddElements) {
        final AddElements addElements = ((AddElements) resultOp);
        if (null == addElements.getInput()) {
            if (!addElements.isValidate() || !addElements.isSkipInvalidElements()) {
                LOGGER.debug("Invalid elements will be skipped when added to {}", graph.getGraphId());
                resultOp = (OP) addElements.shallowClone();
                ((AddElements) resultOp).setValidate(true);
                ((AddElements) resultOp).setSkipInvalidElements(true);
            }
        } else {
            resultOp = (OP) addElements.shallowClone();
            final Set<String> graphGroups = graph.getSchema().getGroups();
            final Iterable<? extends Element> filteredInput = Iterables.filter(addElements.getInput(), element -> graphGroups.contains(null != element ? element.getGroup() : null));
            ((AddElements) resultOp).setInput(filteredInput);
        }
    }
    return resultOp;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) ArrayList(java.util.ArrayList) Operation(uk.gov.gchq.gaffer.operation.Operation) OperationView(uk.gov.gchq.gaffer.operation.graph.OperationView) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) OperationView(uk.gov.gchq.gaffer.operation.graph.OperationView) Operations(uk.gov.gchq.gaffer.operation.Operations)

Example 4 with Operations

use of uk.gov.gchq.gaffer.operation.Operations in project Gaffer by gchq.

the class AddOperationsToChain method addOperationsToChain.

private List<Operation> addOperationsToChain(final Operations<?> operations, final AdditionalOperations additionalOperations) {
    final List<Operation> opList = new ArrayList<>();
    if (null != operations && !operations.getOperations().isEmpty()) {
        final Class<? extends Operation> operationsClass = operations.getOperationsClass();
        for (final Operation originalOp : operations.getOperations()) {
            final List<Operation> beforeOps = additionalOperations.getBefore().get(originalOp.getClass().getName());
            addOps(beforeOps, operationsClass, opList);
            if (originalOp instanceof Operations) {
                final List<Operation> nestedOpList = addOperationsToChain((Operations) originalOp, additionalOperations);
                try {
                    ((Operations) originalOp).updateOperations(nestedOpList);
                } catch (final Exception e) {
                // ignore exception - this would be caused by the operation list not allowing modifications
                }
            }
            opList.add(originalOp);
            final List<Operation> afterOps = additionalOperations.getAfter().get(originalOp.getClass().getName());
            addOps(afterOps, operationsClass, opList);
        }
    }
    return opList;
}
Also used : ArrayList(java.util.ArrayList) Operation(uk.gov.gchq.gaffer.operation.Operation) Operations(uk.gov.gchq.gaffer.operation.Operations)

Example 5 with Operations

use of uk.gov.gchq.gaffer.operation.Operations in project Gaffer by gchq.

the class NamedOperationResolver method resolveNamedOperations.

private void resolveNamedOperations(final Operations<?> operations, final User user) {
    final List<Operation> updatedOperations = new ArrayList<>(operations.getOperations().size());
    for (final Operation operation : operations.getOperations()) {
        if (operation instanceof NamedOperation) {
            updatedOperations.addAll(resolveNamedOperation((NamedOperation) operation, user));
        } else {
            if (operation instanceof Operations) {
                resolveNamedOperations(((Operations<?>) operation), user);
            }
            updatedOperations.add(operation);
        }
    }
    operations.updateOperations((List) updatedOperations);
}
Also used : ArrayList(java.util.ArrayList) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operation(uk.gov.gchq.gaffer.operation.Operation) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operations(uk.gov.gchq.gaffer.operation.Operations)

Aggregations

Operation (uk.gov.gchq.gaffer.operation.Operation)6 Operations (uk.gov.gchq.gaffer.operation.Operations)6 ArrayList (java.util.ArrayList)5 NamedOperation (uk.gov.gchq.gaffer.named.operation.NamedOperation)4 SchemaException (uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException)2 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)2 OperationException (uk.gov.gchq.gaffer.operation.OperationException)2 OperationView (uk.gov.gchq.gaffer.operation.graph.OperationView)2 Lists (com.google.common.collect.Lists)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Collections (java.util.Collections)1 List (java.util.List)1 Properties (java.util.Properties)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 CollectionUtils.isEmpty (org.apache.commons.collections.CollectionUtils.isEmpty)1