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());
}
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);
}
}
}
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;
}
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;
}
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());
}
}
}
Aggregations