Search in sources :

Example 6 with OperationView

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

the class NamedViewResolverTest method shouldBuildFullViewWhenANamedViewNeedingToBeResolvedAndMergedIsSupplied.

@Test
public void shouldBuildFullViewWhenANamedViewNeedingToBeResolvedAndMergedIsSupplied() throws CacheOperationFailedException, SerialisationException {
    // Given
    final View viewToMerge = new View.Builder().edge(TestGroups.EDGE).build();
    final NamedViewDetail namedViewDetailToMerge = new NamedViewDetail.Builder().name(NAMED_VIEW_NAME + 2).view(viewToMerge).build();
    final View finalExpectedView = new View.Builder().edge(TestGroups.EDGE).merge(FULL_VIEW).build();
    given(CACHE.getNamedView(NAMED_VIEW_NAME, CONTEXT.getUser())).willReturn(FULL_NAMED_VIEW_DETAIL);
    given(CACHE.getNamedView(NAMED_VIEW_NAME + 2, CONTEXT.getUser())).willReturn(namedViewDetailToMerge);
    final OperationChain<?> opChain = new OperationChain.Builder().first(new GetElements.Builder().view(new NamedView.Builder().name(NAMED_VIEW_NAME).merge(new NamedView.Builder().name(NAMED_VIEW_NAME + 2).build()).build()).build()).build();
    // When
    RESOLVER.preExecute(opChain, CONTEXT);
    // Then
    JsonAssert.assertEquals(finalExpectedView.toCompactJson(), ((OperationView) opChain.getOperations().get(0)).getView().toCompactJson());
}
Also used : OperationView(uk.gov.gchq.gaffer.operation.graph.OperationView) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) OperationView(uk.gov.gchq.gaffer.operation.graph.OperationView) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) NamedViewDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail) Test(org.junit.jupiter.api.Test)

Example 7 with OperationView

use of uk.gov.gchq.gaffer.operation.graph.OperationView 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 8 with OperationView

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

the class SchemaMigration method migrateOperation.

private List<Operation> migrateOperation(final Operation op) {
    final OperationView opView = OperationView.class.cast(op);
    final Map<String, ViewMigration> migratedEntities = migrateViewElements(entities, opView.getView()::getEntity);
    final Map<String, ViewMigration> migratedEdges = migrateViewElements(edges, opView.getView()::getEdge);
    final View.Builder viewBuilder = new View.Builder().merge(opView.getView());
    for (final Map.Entry<String, ViewMigration> entry : migratedEntities.entrySet()) {
        viewBuilder.entity(entry.getKey(), entry.getValue().buildViewElementDefinition());
    }
    for (final Map.Entry<String, ViewMigration> entry : migratedEdges.entrySet()) {
        viewBuilder.edge(entry.getKey(), entry.getValue().buildViewElementDefinition());
    }
    viewBuilder.config(ViewValidator.SKIP_VIEW_VALIDATION, TRUE);
    final View updatedView = viewBuilder.build();
    LOGGER.debug("Migrated view: {}", updatedView);
    opView.setView(updatedView);
    final List<Operation> migrationOps = ViewMigration.createMigrationOps(aggregateAfter, migratedEdges.values(), migratedEntities.values());
    if (LOGGER.isDebugEnabled()) {
        try {
            LOGGER.debug("Migrated operations: {}", StringUtil.toString(JSONSerialiser.serialise(new OperationChain<>(migrationOps), true)));
        } catch (final SerialisationException e) {
            LOGGER.debug("Failed to json serialise the migration operations: {}", new OperationChain<>(migrationOps));
        }
    }
    return migrationOps;
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) 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) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with OperationView

use of uk.gov.gchq.gaffer.operation.graph.OperationView 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 10 with OperationView

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

the class UpdateViewHook method updateView.

private void updateView(final OperationChain<?> opChain) {
    for (final Operation operation : opChain.flatten()) {
        if (operation instanceof OperationView) {
            final OperationView operationView = (OperationView) operation;
            final View.Builder viewBuilder = mergeView(operationView, getViewToMerge());
            if ((null != whiteListElementGroups && !whiteListElementGroups.isEmpty()) || (null != blackListElementGroups && !blackListElementGroups.isEmpty())) {
                viewBuilder.removeEntities(this::removeElementGroups);
                viewBuilder.removeEdges(this::removeElementGroups);
            }
            if (!addExtraGroups && null != operationView.getView()) {
                final Set<String> entityGroups = operationView.getView().getEntityGroups();
                viewBuilder.removeEntities(grp -> null == entityGroups || !entityGroups.contains(grp.getKey()));
                final Set<String> edgeGroups = operationView.getView().getEdgeGroups();
                viewBuilder.removeEdges(grp -> null == edgeGroups || !edgeGroups.contains(grp.getKey()));
            }
            viewBuilder.expandGlobalDefinitions();
            operationView.setView(viewBuilder.build());
        }
    }
}
Also used : OperationView(uk.gov.gchq.gaffer.operation.graph.OperationView) Operation(uk.gov.gchq.gaffer.operation.Operation) 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)

Aggregations

OperationView (uk.gov.gchq.gaffer.operation.graph.OperationView)13 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)12 Test (org.junit.jupiter.api.Test)9 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)6 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)6 Context (uk.gov.gchq.gaffer.store.Context)6 NamedView (uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView)4 Operation (uk.gov.gchq.gaffer.operation.Operation)4 Builder (uk.gov.gchq.gaffer.user.User.Builder)4 ArrayList (java.util.ArrayList)2 Operations (uk.gov.gchq.gaffer.operation.Operations)2 User (uk.gov.gchq.gaffer.user.User)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 HashMap (java.util.HashMap)1