use of org.vertexium.cypher.VertexiumCypherQueryContext in project vertexium by visallo.
the class MatchClauseExecutor method executeFirstMatchConstraint.
private static IterableWithTotalHits<? extends Element> executeFirstMatchConstraint(VertexiumCypherQueryContext ctx, MatchConstraint<?, ?> matchConstraint, ExpressionScope scope, Long limit) {
try {
List<String> labelNames = getLabelNamesFromMatchConstraint(matchConstraint);
ListMultimap<String, CypherAstBase> propertiesMap = getPropertiesMapFromElementPatterns(ctx, matchConstraint.getPatterns());
QueryResultsIterable<? extends Element> elements;
Query query = ctx.getGraph().query(ctx.getAuthorizations()).limit(limit);
if (labelNames.size() == 0 && propertiesMap.size() == 0) {
elements = executeQuery(ctx, query, matchConstraint);
} else {
if (labelNames.size() > 0) {
Stream<String> labelNamesStream = labelNames.stream().map(ctx::normalizeLabelName);
if (matchConstraint instanceof NodeMatchConstraint) {
query = labelNamesStream.reduce(query, (q, labelName) -> q.has(ctx.getLabelPropertyName(), labelName), (q, q2) -> q);
} else if (matchConstraint instanceof RelationshipMatchConstraint) {
List<String> normalizedLabelNames = labelNamesStream.collect(Collectors.toList());
query = query.hasEdgeLabel(normalizedLabelNames);
} else {
throw new VertexiumCypherNotImplemented("unexpected constraint type: " + matchConstraint.getClass().getName());
}
}
for (Map.Entry<String, CypherAstBase> propertyMatch : propertiesMap.entries()) {
Object value = ctx.getExpressionExecutor().executeExpression(ctx, propertyMatch.getValue(), scope);
if (value instanceof CypherAstBase) {
throw new VertexiumException("unexpected value: " + value.getClass().getName() + ": " + value);
}
if (value instanceof Stream) {
value = ((Stream<?>) value).collect(Collectors.toList());
}
if (value instanceof Collection) {
query.has(propertyMatch.getKey(), Contains.IN, value);
} else {
query.has(propertyMatch.getKey(), value);
}
}
elements = executeQuery(ctx, query, matchConstraint);
}
return elements;
} catch (VertexiumPropertyNotDefinedException e) {
LOGGER.error(e.getMessage());
return new EmptyResultsQueryResultsIterable<>();
}
}
use of org.vertexium.cypher.VertexiumCypherQueryContext in project vertexium by visallo.
the class ReturnClauseExecutor method execute.
public VertexiumCypherScope execute(VertexiumCypherQueryContext ctx, boolean distinct, CypherReturnBody returnBody, VertexiumCypherScope scope) {
List<CypherReturnItem> returnItems = returnBody.getReturnItems().stream().flatMap(ri -> {
if (ri.getExpression() instanceof CypherAllLiteral) {
return getAllFieldNamesAsReturnItems(scope);
}
return Stream.of(ri);
}).collect(Collectors.toList());
LinkedHashSet<String> columnNames = getColumnNames(returnItems);
Stream<VertexiumCypherScope.Item> rows = scope.stream();
long aggregationCount = aggregationCount(ctx, returnItems);
if (returnItems.size() > 0 && aggregationCount == returnItems.size()) {
rows = Stream.of(getReturnRow(ctx, returnItems, null, scope));
} else if (aggregationCount > 0 && isGroupable(returnItems.get(0))) {
Map<Optional<?>, VertexiumCypherScope> groups = groupBy(ctx, returnItems.get(0), rows);
rows = groups.entrySet().stream().map(group -> getReturnRow(ctx, returnItems, group.getKey(), group.getValue()));
} else {
rows = rows.map(row -> getReturnRow(ctx, returnItems, null, row));
}
if (distinct) {
rows = rows.distinct();
}
VertexiumCypherScope results = VertexiumCypherScope.newItemsScope(rows, columnNames, scope);
return applyReturnBody(ctx, returnBody, results);
}
use of org.vertexium.cypher.VertexiumCypherQueryContext in project vertexium by visallo.
the class ExecuteCypherQuery method run.
private int run(String[] args) throws Exception {
Parameters params = new Parameters();
JCommander j = new JCommander(params, args);
if (params.help) {
j.usage();
return -1;
}
Map<String, String> config = ConfigurationUtils.loadConfig(params.getConfigFileNames(), params.configPropertyPrefix);
Graph graph = new GraphFactory().createGraph(config);
Authorizations authorizations = params.getAuthorizations(graph);
VertexiumCypherQueryContext ctx = new CliVertexiumCypherQueryContext(graph, authorizations);
CliVertexiumCypherQueryContext.setLabelPropertyName(params.cypherLabelProperty);
CypherCompilerContext compilerContext = new CypherCompilerContext(ctx.getFunctions());
long startTime = System.currentTimeMillis();
String queryString = params.query.stream().collect(Collectors.joining(" "));
VertexiumCypherQuery query = VertexiumCypherQuery.parse(compilerContext, queryString);
VertexiumCypherResult results = query.execute(ctx);
results.stream().forEach(row -> {
results.getColumnNames().forEach(column -> {
System.out.println(row.getByName(column));
});
});
long endTime = System.currentTimeMillis();
LOGGER.info("total time: %.2fs", ((double) (endTime - startTime) / 1000.0));
return 0;
}
use of org.vertexium.cypher.VertexiumCypherQueryContext in project vertexium by visallo.
the class VertexiumScript method vertexiumCypherResultToString.
private static String vertexiumCypherResultToString(VertexiumCypherResult cypherResult) {
VertexiumScript.getContextProperties().clear();
AtomicInteger vertexIndex = new AtomicInteger(0);
AtomicInteger edgeIndex = new AtomicInteger(0);
VertexiumCypherQueryContext ctx = new CliVertexiumCypherQueryContext(getGraph(), getAuthorizations());
LinkedHashSet<String> columnNames = cypherResult.getColumnNames();
List<List<String>> table = new ArrayList<>();
table.add(new ArrayList<>(columnNames));
table.addAll(cypherResult.stream().map(row -> columnNames.stream().map(columnName -> row.getByName(columnName)).map(o -> {
if (o instanceof Vertex) {
String vertexIndexString = "v" + vertexIndex.get();
LazyVertex lazyVertex = new LazyVertex(((Vertex) o).getId());
VertexiumScript.getContextVertices().put(vertexIndexString, lazyVertex);
vertexIndex.incrementAndGet();
return "@|bold " + vertexIndexString + ":|@ " + ((Vertex) o).getId();
}
if (o instanceof Edge) {
String edgeIndexString = "e" + edgeIndex.get();
LazyEdge lazyEdge = new LazyEdge(((Edge) o).getId());
VertexiumScript.getContextEdges().put(edgeIndexString, lazyEdge);
edgeIndex.incrementAndGet();
return "@|bold " + edgeIndexString + ":|@ " + ((Edge) o).getId();
}
return ctx.getResultWriter().columnValueToString(ctx, o);
}).collect(Collectors.toList())).collect(Collectors.toList()));
return "\n" + ShellTableWriter.tableToString(table);
}
use of org.vertexium.cypher.VertexiumCypherQueryContext in project vertexium by visallo.
the class VertexiumScript method executeCypher.
public static Object executeCypher(String code) {
VertexiumCypherQueryContext ctx = new CliVertexiumCypherQueryContext(getGraph(), getAuthorizations());
CypherCompilerContext compilerContext = new CypherCompilerContext(ctx.getFunctions());
VertexiumCypherQuery query = VertexiumCypherQuery.parse(compilerContext, code);
return query.execute(ctx);
}
Aggregations