use of ai.grakn.graql.Query in project grakn by graknlabs.
the class GraknClientFake method whenQueriesFail_TheBatchExecutorClientStillCompletes.
@Test
public void whenQueriesFail_TheBatchExecutorClientStillCompletes() throws GraknClientException {
Keyspace keyspace = Keyspace.of("yes");
GraknClientFake graknClient = new GraknClientFake();
graknClient.shouldThrow(new GraknClientException("UH OH"));
// Make sure there are more queries to execute than are allowed to run at once
int maxQueries = 10;
int numQueries = 100;
BatchExecutorClient.Builder clientBuilder = BatchExecutorClient.newBuilder().taskClient(graknClient).maxQueries(maxQueries);
Set<Query<?>> queriesToExecute = IntStream.range(0, numQueries).mapToObj(this::createInsertQuery).collect(toImmutableSet());
try (BatchExecutorClient client = clientBuilder.build()) {
for (Query<?> query : queriesToExecute) {
// If we don't subscribe, the query won't execute
client.add(query, keyspace).subscribe(a -> {
});
}
}
assertThat(graknClient.queriesExecuted(), containsInAnyOrder(queriesToExecute.toArray()));
}
use of ai.grakn.graql.Query in project grakn by graknlabs.
the class GraqlControllerTest method whenRunningMultiIdempotentInsertQuery_JsonResponseIsTheSameAsJavaGraql.
@Test
public void whenRunningMultiIdempotentInsertQuery_JsonResponseIsTheSameAsJavaGraql() {
String single = "insert $x label movie;";
String queryString = single + "\n" + single;
Response resp = sendQuery(queryString, APPLICATION_JSON, true, sampleKB.tx().keyspace().getValue(), true);
resp.then().statusCode(200);
Stream<Query<?>> query = sampleKB.tx().graql().parser().parseList(queryString);
String graqlResult = printer.graqlString(query.map(Query::execute).collect(Collectors.toList()));
Json expected = Json.read(graqlResult);
assertEquals(expected, Json.read(resp.body().asString()));
}
use of ai.grakn.graql.Query in project grakn by graknlabs.
the class BatchExecutorClient method add.
/**
* Will block until there is space for the query to be submitted
*/
public Observable<QueryResponse> add(Query<?> query, Keyspace keyspace, boolean keepErrors) {
QueryRequest queryRequest = new QueryRequest(query);
queryRequest.acquirePermit();
Context contextAddTimer = addTimer.time();
Observable<QueryResponse> observable = new QueriesObservableCollapser(queryRequest, keyspace).observe().doOnError(error -> failureMeter.mark()).doOnEach(a -> {
if (a.getThrowable() != null) {
LOG.error("Error while executing statement", a.getThrowable());
} else if (a.isOnNext()) {
LOG.trace("Executed {}", a.getValue());
}
}).subscribeOn(scheduler).doOnTerminate(contextAddTimer::close);
return keepErrors ? observable : ignoreErrors(observable);
}
use of ai.grakn.graql.Query in project grakn by graknlabs.
the class GraknClientImpl method graqlExecute.
@Override
public List<QueryResponse> graqlExecute(List<Query<?>> queryList, Keyspace keyspace) throws GraknClientException {
LOG.debug("Sending query list size {} to keyspace {}", queryList.size(), keyspace);
String body = queryList.stream().map(Object::toString).reduce("; ", String::concat).substring(2);
URI fullURI = UriBuilder.fromUri(uri.toURI()).path(REST.resolveTemplate(REST.WebPath.KEYSPACE_GRAQL, keyspace.getValue())).queryParam(ALLOW_MULTIPLE_QUERIES, true).queryParam(EXECUTE_WITH_INFERENCE, // Making inference true could lead to non-deterministic loading of data
false).queryParam(LOADING_DATA, // Skip serialising responses for the sake of efficiency
true).queryParam(TX_TYPE, GraknTxType.BATCH).build();
ClientResponse response = client.resource(fullURI).accept(APPLICATION_JSON).post(ClientResponse.class, body);
try {
Response.StatusType status = response.getStatusInfo();
String entity = response.getEntity(String.class);
if (!status.getFamily().equals(Family.SUCCESSFUL)) {
String queries = queryList.stream().map(Object::toString).collect(Collectors.joining("\n"));
throw new GraknClientException("Failed graqlExecute. Error status: " + status.getStatusCode() + ", error info: " + entity + "\nqueries: " + queries, response.getStatusInfo());
}
LOG.debug("Received {}", status.getStatusCode());
return queryList.stream().map(q -> QueryResponse.INSTANCE).collect(Collectors.toList());
} finally {
response.close();
}
}
use of ai.grakn.graql.Query in project grakn by graknlabs.
the class GraqlDocsTest method parse.
private void parse(GraknTx graph, String line) {
// TODO: should `commit` be considered valid Graql? It strictly isn't.
Matcher matcher = GRAQL_COMMIT.matcher(line);
matcher.find();
line = matcher.group(1);
graph.graql().parser().parseList(line).forEach(Query::execute);
}
Aggregations