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