Skip to content

Separate procedure definitions from implementations #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,50 +23,47 @@
import static org.opencypher.gremlin.extension.CypherBindingType.FLOAT;
import static org.opencypher.gremlin.extension.CypherBindingType.INTEGER;
import static org.opencypher.gremlin.extension.CypherBindingType.STRING;
import static org.opencypher.gremlin.extension.CypherProcedure.cypherProcedure;

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

public final class TestProcedures implements Supplier<Set<CypherProcedure>> {
public final class TestProcedures implements Supplier<CypherProcedureDefinition> {

private final Set<CypherProcedure> procedures = new HashSet<>();
private final CypherProcedureDefinition procedures = new CypherProcedureDefinition();

public TestProcedures() {
procedures.add(cypherProcedure(
procedures.define(
"test.getName",
emptyList(),
singletonList(binding("name", STRING)),
arguments -> asList(
singletonMap("name", "marko"),
singletonMap("name", "vadas")
)
));
);

procedures.add(cypherProcedure(
procedures.define(
"test.inc",
singletonList(binding("a", INTEGER)),
singletonList(binding("r", INTEGER)),
arguments -> {
long a = (long) arguments.get("a");
return singletonList(singletonMap("r", a + 1));
}
));
);

procedures.add(cypherProcedure(
procedures.define(
"test.incF",
singletonList(binding("a", FLOAT)),
singletonList(binding("r", FLOAT)),
arguments -> {
double a = (double) arguments.get("a");
return singletonList(singletonMap("r", a + 1));
}
));
);

procedures.add(cypherProcedure(
procedures.define(
"test.multi",
emptyList(),
asList(binding("foo", STRING), binding("bar", STRING)),
Expand All @@ -76,18 +73,18 @@ public TestProcedures() {
row.put("foo", "foo");
return singletonList(row);
}
));
);

procedures.add(cypherProcedure(
procedures.define(
"test.void",
emptyList(),
emptyList(),
arguments -> emptyList()
));
);
}

@Override
public Set<CypherProcedure> get() {
return new HashSet<>(procedures);
public CypherProcedureDefinition get() {
return procedures;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ private List<Map<String, Object>> submitAndGet(String cypher, Map<String, Object
.traversal(g)
.enableCypherExtensions()
.build();
CypherAst ast = CypherAst.parse(cypher, parameters, procedureContext);
Seq<GremlinStep> ir = ast.translate(flavor);
CypherAst ast = CypherAst.parse(cypher, parameters, procedureContext.getSignatures());
Seq<GremlinStep> ir = ast.translate(flavor, procedureContext);
GraphTraversal<?, ?> traversal = TranslationWriter.write(ir, translator, parameters);
ReturnNormalizer returnNormalizer = ReturnNormalizer.create(ast.getReturnTypes());
return traversal.toStream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import static java.util.stream.Collectors.toList;
import static org.opencypher.gremlin.extension.CypherBinding.binding;
import static org.opencypher.gremlin.extension.CypherProcedure.cypherProcedure;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -46,7 +45,7 @@ public static void register(String signature, List<String> header, List<Map<Stri
List<CypherBinding> arguments = matchArguments(signatureMatcher.group("arguments"));
List<CypherBinding> results = matchArguments(signatureMatcher.group("results"));

ProcedureContext.global().unsafeRegister(cypherProcedure(
ProcedureContext.global().unsafeRegister(
name,
arguments,
results,
Expand All @@ -58,7 +57,7 @@ public static void register(String signature, List<String> header, List<Map<Stri
.map(row -> extractKeys(out, row))
.collect(toList());
}
));
);
}

public static void clear() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ object TckGremlinCypherValueConverter {

private def emptyHeaderWorkaround(query: String, header: List[String], rows: List[Map[String, CypherValue]]) = {
if (rows.isEmpty) {
val procedureContext = ProcedureContext.global()
val ast = CypherAst.parse(query, new util.HashMap[String, Any], procedureContext)
val procedures = ProcedureContext.global().getSignatures
val ast = CypherAst.parse(query, new util.HashMap[String, Any], procedures)
val columns = ast.statement.returnColumns
CypherValueRecords.emptyWithHeader(columns)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ private void evalCypher(Context context) throws OpProcessorException {
DefaultGraphTraversal g = new DefaultGraphTraversal(gts.clone());
Map<String, Object> parameters = ParameterNormalizer.normalize(getParameters(args));
ProcedureContext procedureContext = ProcedureContext.global();
CypherAst ast = CypherAst.parse(cypher, parameters, procedureContext);
CypherAst ast = CypherAst.parse(cypher, parameters, procedureContext.getSignatures());

TranslatorFlavor flavor = TranslatorFlavor.gremlinServer();
Seq<GremlinStep> ir = ast.translate(flavor);
Seq<GremlinStep> ir = ast.translate(flavor, procedureContext);

Translator<String, GroovyPredicate> stringTranslator = Translator.builder()
.gremlinGroovy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,8 @@

import java.util.List;
import java.util.Map;
import java.util.function.Function;

@FunctionalInterface
public interface CypherProcedure {

String name();

List<CypherBinding> arguments();

List<CypherBinding> results();

List<Map<String, Object>> call(Map<String, Object> arguments);

static CypherProcedure cypherProcedure(
String name,
List<CypherBinding> arguments,
List<CypherBinding> results,
Function<Map<String, Object>, List<Map<String, Object>>> implementation) {
return new CypherProcedure() {
@Override
public String name() {
return name;
}

@Override
public List<CypherBinding> arguments() {
return arguments;
}

@Override
public List<CypherBinding> results() {
return results;
}

@Override
public List<Map<String, Object>> call(Map<String, Object> arguments) {
return implementation.apply(arguments);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.extension;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class CypherProcedureDefinition {
private final Map<String, CypherProcedureSignature> signatures;
private final Map<String, CypherProcedure> implementations;

public CypherProcedureDefinition() {
signatures = new HashMap<>();
implementations = new HashMap<>();
}

public void define(String name, List<CypherBinding> arguments, List<CypherBinding> results, CypherProcedure implementation
) {
signatures.put(name, new CypherProcedureSignature(arguments, results));
implementations.put(name, implementation);
}

public Map<String, CypherProcedureSignature> getSignatures() {
return signatures;
}

public Map<String, CypherProcedure> getImplementations() {
return implementations;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.extension;

import static java.util.Collections.unmodifiableList;

import java.util.List;

public class CypherProcedureSignature {
private final List<CypherBinding> arguments;
private final List<CypherBinding> results;

public CypherProcedureSignature(List<CypherBinding> arguments, List<CypherBinding> results) {
this.arguments = arguments;
this.results = results;
}

public List<CypherBinding> getArguments() {
return unmodifiableList(arguments);
}

public List<CypherBinding> getResults() {
return unmodifiableList(results);
}
}
Loading