Search in sources :

Example 1 with GraknTxType

use of ai.grakn.GraknTxType in project grakn by graknlabs.

the class GrpcOpenRequestExecutorImpl method execute.

@Override
public EmbeddedGraknTx<?> execute(Open request) {
    Keyspace keyspace = GrpcUtil.getKeyspace(request);
    GraknTxType txType = GrpcUtil.getTxType(request);
    return txFactory.tx(keyspace, txType);
}
Also used : GraknTxType(ai.grakn.GraknTxType) Keyspace(ai.grakn.Keyspace)

Example 2 with GraknTxType

use of ai.grakn.GraknTxType in project grakn by graknlabs.

the class GraqlController method executeGraql.

@POST
@Path("/kb/{keyspace}/graql")
private String executeGraql(Request request, Response response) throws RetryException, ExecutionException {
    Keyspace keyspace = Keyspace.of(mandatoryPathParameter(request, KEYSPACE_PARAM));
    String queryString = mandatoryBody(request);
    // Run the query with reasoning on or off
    Optional<Boolean> infer = queryParameter(request, EXECUTE_WITH_INFERENCE).map(Boolean::parseBoolean);
    // Allow multiple queries to be executed
    boolean multiQuery = parseBoolean(queryParameter(request, ALLOW_MULTIPLE_QUERIES).orElse("false"));
    // Define all anonymous variables in the query
    Optional<Boolean> defineAllVars = queryParameter(request, DEFINE_ALL_VARS).map(Boolean::parseBoolean);
    // Used to check if serialisation of results is needed. When loading we skip this for the sake of speed
    boolean skipSerialisation = parseBoolean(queryParameter(request, LOADING_DATA).orElse("false"));
    // Check the transaction type to use
    GraknTxType txType = queryParameter(request, TX_TYPE).map(String::toUpperCase).map(GraknTxType::valueOf).orElse(GraknTxType.WRITE);
    // This is used to determine the response format
    // TODO: Maybe we should really try to stick with one representation? This would require dashboard console interpreting the json representation
    final String acceptType;
    if (APPLICATION_TEXT.equals(Requests.getAcceptType(request))) {
        acceptType = APPLICATION_TEXT;
    } else {
        acceptType = APPLICATION_JSON;
    }
    response.type(APPLICATION_JSON);
    // Execute the query and get the results
    LOG.debug("Executing graql query: {}", StringUtils.abbreviate(queryString, 100));
    LOG.trace("Full query: {}", queryString);
    return executeFunctionWithRetrying(() -> {
        try (EmbeddedGraknTx<?> tx = factory.tx(keyspace, txType);
            Timer.Context context = executeGraql.time()) {
            QueryBuilder builder = tx.graql();
            infer.ifPresent(builder::infer);
            QueryParser parser = builder.parser();
            defineAllVars.ifPresent(parser::defineAllVars);
            response.status(SC_OK);
            return executeQuery(tx, queryString, acceptType, multiQuery, skipSerialisation, parser);
        } finally {
            LOG.debug("Executed graql query");
        }
    });
}
Also used : GraknTxType(ai.grakn.GraknTxType) QueryBuilder(ai.grakn.graql.QueryBuilder) QueryParser(ai.grakn.graql.QueryParser) Timer(com.codahale.metrics.Timer) Keyspace(ai.grakn.Keyspace) Boolean.parseBoolean(java.lang.Boolean.parseBoolean) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

GraknTxType (ai.grakn.GraknTxType)2 Keyspace (ai.grakn.Keyspace)2 QueryBuilder (ai.grakn.graql.QueryBuilder)1 QueryParser (ai.grakn.graql.QueryParser)1 Timer (com.codahale.metrics.Timer)1 Boolean.parseBoolean (java.lang.Boolean.parseBoolean)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1