Search in sources :

Example 1 with VertexiumCypherQueryContext

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<>();
    }
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) java.util(java.util) VertexiumCypherException(org.vertexium.cypher.exceptions.VertexiumCypherException) ListMultimap(com.google.common.collect.ListMultimap) VertexiumLogger(org.vertexium.util.VertexiumLogger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VertexiumCypherTypeErrorException(org.vertexium.cypher.exceptions.VertexiumCypherTypeErrorException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) VertexiumLoggerFactory(org.vertexium.util.VertexiumLoggerFactory) org.vertexium(org.vertexium) VertexiumCypherQueryContext(org.vertexium.cypher.VertexiumCypherQueryContext) VertexiumCypherNotImplemented(org.vertexium.cypher.exceptions.VertexiumCypherNotImplemented) org.vertexium.cypher.ast.model(org.vertexium.cypher.ast.model) Lists(com.google.common.collect.Lists) Stream(java.util.stream.Stream) VertexiumCypherScope(org.vertexium.cypher.VertexiumCypherScope) org.vertexium.query(org.vertexium.query) StreamUtils.stream(org.vertexium.util.StreamUtils.stream) ObjectUtils(org.vertexium.cypher.utils.ObjectUtils) org.vertexium.cypher.executor.models.match(org.vertexium.cypher.executor.models.match) StreamUtils(org.vertexium.util.StreamUtils) StreamSupport(java.util.stream.StreamSupport) MatchConstraintBuilder(org.vertexium.cypher.executor.utils.MatchConstraintBuilder) VertexiumCypherNotImplemented(org.vertexium.cypher.exceptions.VertexiumCypherNotImplemented) Stream(java.util.stream.Stream)

Example 2 with VertexiumCypherQueryContext

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);
}
Also used : java.util(java.util) VertexiumException(org.vertexium.VertexiumException) VertexiumLogger(org.vertexium.util.VertexiumLogger) Collectors(java.util.stream.Collectors) VertexiumLoggerFactory(org.vertexium.util.VertexiumLoggerFactory) VertexiumCypherQueryContext(org.vertexium.cypher.VertexiumCypherQueryContext) org.vertexium.cypher.ast.model(org.vertexium.cypher.ast.model) Stream(java.util.stream.Stream) VertexiumCypherScope(org.vertexium.cypher.VertexiumCypherScope) StreamUtils.stream(org.vertexium.util.StreamUtils.stream) ObjectUtils(org.vertexium.cypher.utils.ObjectUtils) AggregationFunction(org.vertexium.cypher.functions.aggregate.AggregationFunction) CypherFunction(org.vertexium.cypher.functions.CypherFunction) StreamUtils(org.vertexium.util.StreamUtils) VertexiumCypherScope(org.vertexium.cypher.VertexiumCypherScope)

Example 3 with VertexiumCypherQueryContext

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;
}
Also used : VertexiumCypherResult(org.vertexium.cypher.VertexiumCypherResult) GraphFactory(org.vertexium.GraphFactory) Authorizations(org.vertexium.Authorizations) Graph(org.vertexium.Graph) CypherCompilerContext(org.vertexium.cypher.ast.CypherCompilerContext) JCommander(com.beust.jcommander.JCommander) VertexiumCypherQueryContext(org.vertexium.cypher.VertexiumCypherQueryContext) VertexiumCypherQuery(org.vertexium.cypher.VertexiumCypherQuery)

Example 4 with VertexiumCypherQueryContext

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);
}
Also used : LazyProperty(org.vertexium.cli.model.LazyProperty) java.util(java.util) InvokerHelper(org.codehaus.groovy.runtime.InvokerHelper) VertexiumCypherQuery(org.vertexium.cypher.VertexiumCypherQuery) LazyEdge(org.vertexium.cli.model.LazyEdge) LazyExtendedDataTable(org.vertexium.cli.model.LazyExtendedDataTable) VertexiumCypherResult(org.vertexium.cypher.VertexiumCypherResult) IOException(java.io.IOException) Script(groovy.lang.Script) ShellTableWriter(org.vertexium.cli.utils.ShellTableWriter) Collectors(java.util.stream.Collectors) org.vertexium(org.vertexium) VertexiumCypherQueryContext(org.vertexium.cypher.VertexiumCypherQueryContext) IOUtils(org.apache.commons.io.IOUtils) Charset(java.nio.charset.Charset) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LazyVertex(org.vertexium.cli.model.LazyVertex) CypherCompilerContext(org.vertexium.cypher.ast.CypherCompilerContext) StreamingPropertyValue(org.vertexium.property.StreamingPropertyValue) InputStream(java.io.InputStream) LazyVertex(org.vertexium.cli.model.LazyVertex) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LazyVertex(org.vertexium.cli.model.LazyVertex) VertexiumCypherQueryContext(org.vertexium.cypher.VertexiumCypherQueryContext) LazyEdge(org.vertexium.cli.model.LazyEdge) LazyEdge(org.vertexium.cli.model.LazyEdge)

Example 5 with VertexiumCypherQueryContext

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);
}
Also used : CypherCompilerContext(org.vertexium.cypher.ast.CypherCompilerContext) VertexiumCypherQueryContext(org.vertexium.cypher.VertexiumCypherQueryContext) VertexiumCypherQuery(org.vertexium.cypher.VertexiumCypherQuery)

Aggregations

VertexiumCypherQueryContext (org.vertexium.cypher.VertexiumCypherQueryContext)5 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 VertexiumCypherQuery (org.vertexium.cypher.VertexiumCypherQuery)3 CypherCompilerContext (org.vertexium.cypher.ast.CypherCompilerContext)3 Stream (java.util.stream.Stream)2 org.vertexium (org.vertexium)2 VertexiumCypherResult (org.vertexium.cypher.VertexiumCypherResult)2 VertexiumCypherScope (org.vertexium.cypher.VertexiumCypherScope)2 org.vertexium.cypher.ast.model (org.vertexium.cypher.ast.model)2 ObjectUtils (org.vertexium.cypher.utils.ObjectUtils)2 StreamUtils (org.vertexium.util.StreamUtils)2 StreamUtils.stream (org.vertexium.util.StreamUtils.stream)2 VertexiumLogger (org.vertexium.util.VertexiumLogger)2 VertexiumLoggerFactory (org.vertexium.util.VertexiumLoggerFactory)2 JCommander (com.beust.jcommander.JCommander)1 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 ListMultimap (com.google.common.collect.ListMultimap)1 Lists (com.google.common.collect.Lists)1 Script (groovy.lang.Script)1