use of org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor in project incubator-hugegraph by apache.
the class ContextGremlinServer method removeGraph.
private void removeGraph(String name) {
GraphManager manager = this.getServerGremlinExecutor().getGraphManager();
GremlinExecutor executor = this.getServerGremlinExecutor().getGremlinExecutor();
try {
manager.removeGraph(name);
manager.removeTraversalSource(G_PREFIX + name);
Whitebox.invoke(executor, "globalBindings", new Class<?>[] { Object.class }, "remove", name);
} catch (Exception e) {
throw new HugeException("Failed to remove graph '%s' from " + "gremlin server context", e, name);
}
}
use of org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor in project incubator-hugegraph by apache.
the class ContextGremlinServer method injectGraph.
private void injectGraph(HugeGraph graph) {
String name = graph.name();
GraphManager manager = this.getServerGremlinExecutor().getGraphManager();
GremlinExecutor executor = this.getServerGremlinExecutor().getGremlinExecutor();
manager.putGraph(name, graph);
GraphTraversalSource g = manager.getGraph(name).traversal();
manager.putTraversalSource(G_PREFIX + name, g);
Whitebox.invoke(executor, "globalBindings", new Class<?>[] { String.class, Object.class }, "put", name, graph);
}
use of org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor in project act-platform by mnemonic-no.
the class TraverseGraphDelegate method executeTraversal.
private void executeTraversal(Collection<UUID> startingObjects, String query) throws InvalidArgumentException, OperationTimeoutException {
try (Graph graph = createGraph();
GremlinExecutor executor = createExecutor()) {
// Create the first step of the graph traversal, i.e. starting the traversal at the Object(s) specified in the request.
// This is injected into the script execution as variable 'g'. Every query has to start from 'g'.
GraphTraversal<Vertex, Vertex> startingPoint = graph.traversal().V(startingObjects.toArray());
Map<String, java.lang.Object> bindings = MapUtils.map(T("g", startingPoint));
// Start script execution and wait until result arrived or execution is aborted.
// Use 'withResult' callback here because the graph will then be iterated inside the 'eval' thread, thus, every
// exception caused by the traversal will be handled inside that thread as well which will result in an ExecutionException.
executor.eval(query, SCRIPT_ENGINE, bindings, this::produceTraversalResult).get();
} catch (ExecutionException ex) {
// Exceptions causing the script execution to fail are wrapped inside an ExecutionException. Need to unwrap them.
Throwable cause = ObjectUtils.ifNull(ex.getCause(), ex);
// In both cases throw an own OperationTimeoutException in order to signal the timeout to the user.
if (cause instanceof TimeoutException) {
throw new OperationTimeoutException("The performed graph traversal query timed out.", "graph.traversal.timeout");
}
// e.g. invalid syntax, an unsupported operation such as 'addE()', or an operation not allowed by the sandbox.
throw new InvalidArgumentException().addValidationError(cause.getMessage(), "graph.traversal.failure", "query", query);
} catch (Exception ex) {
// Something bad happened, abort method.
throw new IllegalStateException("Could not perform graph traversal.", ex);
}
}
use of org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor in project act-platform by mnemonic-no.
the class TraverseGraphHandler method executeTraversal.
private Collection<Object> executeTraversal(Collection<UUID> startingObjects, String query, TraverseParams traverseParams) throws InvalidArgumentException, OperationTimeoutException {
// The result will be written into this collection.
Collection<Object> traversalResult = new ArrayList<>();
try (Graph graph = createGraph(traverseParams);
GremlinExecutor executor = createExecutor()) {
// Create the first step of the graph traversal, i.e. starting the traversal at the Object(s) specified in the request.
// This is injected into the script execution as variable 'g'. Every query has to start from 'g'.
GraphTraversal<Vertex, Vertex> startingPoint = graph.traversal().V(startingObjects.toArray());
Map<String, Object> bindings = MapUtils.map(T("g", startingPoint));
// Start script execution and wait until result arrived or execution is aborted.
// Use 'withResult' callback here because the graph will then be iterated inside the 'eval' thread, thus, every
// exception caused by the traversal will be handled inside that thread as well which will result in an ExecutionException.
executor.eval(query, SCRIPT_ENGINE, bindings, createResultConsumer(traversalResult, traverseParams)).get();
} catch (ExecutionException ex) {
// Exceptions causing the script execution to fail are wrapped inside an ExecutionException. Need to unwrap them.
Throwable cause = ObjectUtils.ifNull(ex.getCause(), ex);
// In both cases throw an own OperationTimeoutException in order to signal the timeout to the user.
if (cause instanceof TimeoutException) {
throw new OperationTimeoutException("The performed graph traversal query timed out.", "graph.traversal.timeout");
}
// e.g. invalid syntax, an unsupported operation such as 'addE()', or an operation not allowed by the sandbox.
throw new InvalidArgumentException().addValidationError(cause.getMessage(), "graph.traversal.failure", "query", query);
} catch (Exception ex) {
// Something bad happened, abort method.
throw new IllegalStateException("Could not perform graph traversal.", ex);
}
return traversalResult;
}
use of org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor in project GraphScope by alibaba.
the class GaiaGraphOpProcessor method createLifeCycle.
@Override
protected GremlinExecutor.LifeCycle createLifeCycle(Context ctx, Supplier<GremlinExecutor> gremlinExecutorSupplier, BindingSupplier bindingsSupplier) {
final RequestMessage msg = ctx.getRequestMessage();
final Settings settings = ctx.getSettings();
final Map<String, Object> args = msg.getArgs();
final long seto = args.containsKey(Tokens.ARGS_SCRIPT_EVAL_TIMEOUT) ? ((Number) args.get(Tokens.ARGS_SCRIPT_EVAL_TIMEOUT)).longValue() : settings.scriptEvaluationTimeout;
if (config.getGraphType() == GraphType.MAXGRAPH) {
graphStore.updateSnapShotId();
}
return GremlinExecutor.LifeCycle.build().scriptEvaluationTimeoutOverride(seto).beforeEval(b -> {
try {
b.putAll(bindingsSupplier.get());
} catch (OpProcessorException ope) {
throw new RuntimeException(ope);
}
}).transformResult(o -> {
if (o != null && o instanceof Traversal) {
applyStrategy((Traversal) o, config, graphStore);
}
return o;
}).withResult(o -> {
if (o != null && o instanceof Traversal) {
long queryId = (long) queryIdMaker.getId(o);
TraversalBuilder traversalBuilder = new TraversalBuilder((Traversal.Admin) o).addConfig(PlanConfig.QUERY_ID, queryId).addConfig(PlanConfig.TAG_ID_MAKER, new TagIdMaker((Traversal.Admin) o)).addConfig(PlanConfig.QUERY_CONFIG, PlanUtils.getDefaultConfig(queryId, config));
if (config.getGraphType() == GraphType.MAXGRAPH) {
traversalBuilder.addConfig(PlanConfig.SNAPSHOT_ID, Long.valueOf(graphStore.getSnapShotId()));
}
AbstractBuilder jobReqBuilder = new TraversalTranslator(traversalBuilder).translate();
PlanUtils.print(jobReqBuilder);
broadcastProcessor.broadcast(jobReqBuilder.build(), new GremlinResultProcessor(ctx, new DefaultResultParser(traversalBuilder, graphStore, config)));
} else {
List<Object> results = new ArrayList<>();
if (o != null) {
results.add(o);
}
writeResultList(ctx, results, ResponseStatusCode.SUCCESS);
}
}).create();
}
Aggregations