use of ai.grakn.Keyspace in project grakn by graknlabs.
the class GraqlController method explainGraql.
@GET
@Path("/kb/{keyspace}/explain")
private String explainGraql(Request request, Response response) throws RetryException, ExecutionException {
Keyspace keyspace = Keyspace.of(mandatoryPathParameter(request, KEYSPACE_PARAM));
String queryString = mandatoryQueryParameter(request, QUERY);
response.status(SC_OK);
return executeFunctionWithRetrying(() -> {
try (GraknTx tx = factory.tx(keyspace, GraknTxType.WRITE);
Timer.Context context = executeExplanation.time()) {
Answer answer = tx.graql().infer(true).parser().<GetQuery>parseQuery(queryString).execute().stream().findFirst().orElse(new QueryAnswer());
return mapper.writeValueAsString(ExplanationBuilder.buildExplanation(answer));
}
});
}
use of ai.grakn.Keyspace 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.Keyspace 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.Keyspace in project grakn by graknlabs.
the class IndexPostProcessorTest method whenAddingCommitLogToPostProcessor_EnsureIndexStorageIsUpdated.
@Test
public void whenAddingCommitLogToPostProcessor_EnsureIndexStorageIsUpdated() {
// Create Sample Data For CommitLog
String index1 = "index1";
String index2 = "index2";
Set<ConceptId> ids1 = Arrays.asList("a", "b", "c").stream().map(ConceptId::of).collect(Collectors.toSet());
Set<ConceptId> ids2 = Arrays.asList("1", "2", "3").stream().map(ConceptId::of).collect(Collectors.toSet());
HashMap<String, Set<ConceptId>> attributes = new HashMap<>();
attributes.put(index1, ids1);
attributes.put(index2, ids2);
// Create Commit Log
Keyspace keyspace = Keyspace.of("whatakeyspace");
CommitLog commitLog = CommitLog.create(keyspace, Collections.emptyMap(), attributes);
// Call the post processor
indexPostProcessor.updateIndices(commitLog);
// Check index storage is updated
verify(indexStorage, Mockito.times(1)).addIndex(keyspace, index1, ids1);
verify(indexStorage, Mockito.times(1)).addIndex(keyspace, index2, ids2);
}
use of ai.grakn.Keyspace in project grakn by graknlabs.
the class RedisCountStorageTest method whenIncreasingCountOnRedisConcurrently_EnsureAllThreadCountsArePersisted.
@Test
public void whenIncreasingCountOnRedisConcurrently_EnsureAllThreadCountsArePersisted() throws ExecutionException, InterruptedException {
Keyspace keyspace = SampleKBLoader.randomKeyspace();
ConceptId conceptId = ConceptId.of("Roach");
int[] counts = { 5, 5, 10, 10, -8, -2, 5, 5, -7 };
ExecutorService pool = Executors.newCachedThreadPool();
Set<Future> futures = new HashSet<>();
assertEquals(0, redis.getCount(RedisCountStorage.getKeyNumInstances(keyspace, conceptId)));
for (int i = 0; i < counts.length; i++) {
int finalI = i;
futures.add(pool.submit(() -> redis.incrementCount(RedisCountStorage.getKeyNumInstances(keyspace, conceptId), counts[finalI])));
}
for (Future future : futures) {
future.get();
}
assertEquals(23, redis.getCount(RedisCountStorage.getKeyNumInstances(keyspace, conceptId)));
}
Aggregations