All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opencypher.gremlin.client.OpProcessorCypherGremlinClient Maven / Gradle / Ivy

Go to download

Gremlin Server client wrapper that can send Cypher queries to a Cypher-enabled Gremlin Server

The newest version!
/*
 * Copyright (c) 2018-2019 "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.Map;
import java.util.concurrent.CompletableFuture;
import org.apache.tinkerpop.gremlin.driver.Client;
import org.apache.tinkerpop.gremlin.driver.ResultSet;
import org.apache.tinkerpop.gremlin.driver.Tokens;
import org.apache.tinkerpop.gremlin.driver.message.RequestMessage;

final class OpProcessorCypherGremlinClient implements CypherGremlinClient {

    private static final String CYPHER_OP_PROCESSOR_NAME = "cypher";

    private final Client client;

    OpProcessorCypherGremlinClient(Client client) {
        this.client = client;
    }

    @Override
    public void close() {
        client.close();
    }

    @Override
    public CompletableFuture submitAsync(CypherStatement statement) {
        RequestMessage requestMessage = buildRequest(statement).create();
        CompletableFuture resultSetFuture = client.submitAsync(requestMessage);

        return resultSetFuture
            .thenApply(ResultSet::iterator)
            .thenApply(CypherResultSet::new);
    }

    private static RequestMessage.Builder buildRequest(CypherStatement statement) {
        Map parameters = statement.parameters();

        RequestMessage.Builder request = RequestMessage.build(Tokens.OPS_EVAL)
            .processor(CYPHER_OP_PROCESSOR_NAME)
            .add(Tokens.ARGS_GREMLIN, statement.query());

        statement.timeout().ifPresent(timeout -> request.add(Tokens.ARGS_SCRIPT_EVAL_TIMEOUT, timeout));

        if (parameters != null && !parameters.isEmpty()) {
            request.addArg(Tokens.ARGS_BINDINGS, parameters);
        }

        return request;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy