Skip to content

Commit

Permalink
Add explain to Cypher Clients Fixes #5.
Browse files Browse the repository at this point in the history
  • Loading branch information
dwitry committed Mar 1, 2018
1 parent 306b1f1 commit 4da3be6
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import java.util.Map;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.opencypher.gremlin.groups.SkipWithGremlinGroovy;
import org.opencypher.gremlin.rules.GremlinServerExternalResource;

public class ExplainTest {
Expand All @@ -34,11 +32,7 @@ private List<Map<String, Object>> submitAndGet(String cypher) {
return gremlinServer.cypherGremlinClient().submit(cypher).all();
}

/**
* Explain only works with `cypher-gremlin-server-plugin`
*/
@Test
@Category(SkipWithGremlinGroovy.class)
public void explainIntegration() throws Exception {
String cypher = "EXPLAIN\n" +
"MATCH (s)-[:MEMBER_OF]->(ss)\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package org.opencypher.gremlin.client;

import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.opencypher.gremlin.client.ExplainTranslation.getExplanation;
import static org.opencypher.gremlin.translation.StatementOption.EXPLAIN;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import org.apache.tinkerpop.gremlin.driver.Client;
Expand Down Expand Up @@ -44,6 +48,11 @@ public void close() {
@Override
public CompletableFuture<CypherResultSet> submitAsync(String cypher, Map<String, ?> parameters) {
CypherAstWrapper ast = CypherAstWrapper.parse(cypher, parameters);

if (ast.getOptions().contains(EXPLAIN)) {
return completedFuture(getExplanation(ast));
}

Translator<GraphTraversal, P> translator = Translator.builder().traversal().build(flavor);
Bytecode bytecode = ast.buildTranslation(translator).asAdmin().getBytecode();
CompletableFuture<ResultSet> resultSetFuture = client.submitAsync(bytecode);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2018 "Neo4j, Inc." [https://neo4j.com]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opencypher.gremlin.client;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.tinkerpop.gremlin.driver.Result;
import org.opencypher.gremlin.translation.CypherAstWrapper;
import org.opencypher.gremlin.translation.groovy.GroovyPredicate;
import org.opencypher.gremlin.translation.translator.Translator;

class ExplainTranslation {
static CypherResultSet getExplanation(CypherAstWrapper ast) {
Map<String, Object> explanation = new LinkedHashMap<>();
Translator<String, GroovyPredicate> translator = Translator.builder()
.gremlinGroovy()
.inlineParameters()
.build();
explanation.put("translation", ast.buildTranslation(translator));
explanation.put("options", ast.getOptions().toString());
List<Result> results = Collections.singletonList(new Result(explanation));
return new CypherResultSet(results.iterator());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package org.opencypher.gremlin.client;

import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.opencypher.gremlin.client.ExplainTranslation.getExplanation;
import static org.opencypher.gremlin.translation.StatementOption.EXPLAIN;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import org.apache.tinkerpop.gremlin.driver.Client;
Expand Down Expand Up @@ -42,6 +46,11 @@ public void close() {
@Override
public CompletableFuture<CypherResultSet> submitAsync(String cypher, Map<String, ?> parameters) {
CypherAstWrapper ast = CypherAstWrapper.parse(cypher, parameters);

if (ast.getOptions().contains(EXPLAIN)) {
return completedFuture(getExplanation(ast));
}

Translator<String, GroovyPredicate> translator = Translator.builder().gremlinGroovy().build(flavor);
String gremlin = ast.buildTranslation(translator);
Map<String, Object> extractedParameters = ast.getExtractedParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@

import static java.util.concurrent.CompletableFuture.completedFuture;
import static java.util.stream.Collectors.toList;
import static org.opencypher.gremlin.client.ExplainTranslation.getExplanation;
import static org.opencypher.gremlin.translation.StatementOption.EXPLAIN;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
Expand All @@ -29,7 +28,6 @@
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.opencypher.gremlin.translation.CypherAstWrapper;
import org.opencypher.gremlin.translation.groovy.GroovyPredicate;
import org.opencypher.gremlin.translation.translator.Translator;
import org.opencypher.gremlin.traversal.ReturnNormalizer;

Expand All @@ -51,15 +49,7 @@ public CompletableFuture<CypherResultSet> submitAsync(String cypher, Map<String,
CypherAstWrapper ast = CypherAstWrapper.parse(cypher, parameters);

if (ast.getOptions().contains(EXPLAIN)) {
Map<String, Object> explanation = new LinkedHashMap<>();
Translator<String, GroovyPredicate> translator = Translator.builder()
.gremlinGroovy()
.inlineParameters()
.build();
explanation.put("translation", ast.buildTranslation(translator));
explanation.put("options", ast.getOptions().toString());
List<Result> results = Collections.singletonList(new Result(explanation));
return completedFuture(new CypherResultSet(results.iterator()));
return completedFuture(getExplanation(ast));
}

DefaultGraphTraversal g = new DefaultGraphTraversal(gts.clone());
Expand Down

0 comments on commit 4da3be6

Please sign in to comment.