use of ai.grakn.Keyspace in project grakn by graknlabs.
the class ConceptController method getTypeInstances.
private String getTypeInstances(Request request, Response response) throws JsonProcessingException {
response.type(APPLICATION_JSON);
Keyspace keyspace = Keyspace.of(mandatoryPathParameter(request, KEYSPACE_PARAM));
Label label = Label.of(mandatoryPathParameter(request, LABEL_PARAMETER));
int offset = getOffset(request);
int limit = getLimit(request);
try (GraknTx tx = factory.tx(keyspace, READ);
Timer.Context context = instancesGetTimer.time()) {
Type type = tx.getType(label);
if (type == null) {
response.status(SC_NOT_FOUND);
return "";
}
// Get the wrapper
Things things = ConceptBuilder.buildThings(type, offset, limit);
response.status(SC_OK);
return objectMapper.writeValueAsString(things);
}
}
use of ai.grakn.Keyspace in project grakn by graknlabs.
the class ConceptController method getConceptCollection.
private <X extends ai.grakn.concept.Concept> String getConceptCollection(Request request, Response response, String key, Function<GraknTx, X> getter, Function<X, Stream<Jacksonisable>> collector) throws JsonProcessingException {
response.type(APPLICATION_JSON);
Keyspace keyspace = Keyspace.of(mandatoryPathParameter(request, KEYSPACE_PARAM));
try (GraknTx tx = factory.tx(keyspace, READ);
Timer.Context context = labelGetTimer.time()) {
X concept = getter.apply(tx);
// If the concept was not found return;
if (concept == null) {
response.status(SC_NOT_FOUND);
return "[]";
}
List<Jacksonisable> list = collector.apply(concept).collect(Collectors.toList());
Link link = Link.create(request.pathInfo());
ListResource<Jacksonisable> listResource = ListResource.create(link, key, list);
return objectMapper.writeValueAsString(listResource);
}
}
use of ai.grakn.Keyspace in project grakn by graknlabs.
the class ConceptController method getConcepts.
private String getConcepts(Request request, Response response, String key, Function<GraknTx, Stream<? extends ai.grakn.concept.Concept>> getter) throws JsonProcessingException {
response.type(APPLICATION_JSON);
Keyspace keyspace = Keyspace.of(mandatoryPathParameter(request, KEYSPACE_PARAM));
try (GraknTx tx = factory.tx(keyspace, READ);
Timer.Context context = labelGetTimer.time()) {
List<Concept> concepts = getter.apply(tx).map(ConceptBuilder::<Concept>build).collect(Collectors.toList());
ListResource list = ListResource.create(Requests.selfLink(request), key, concepts);
response.status(SC_OK);
return objectMapper.writeValueAsString(list);
}
}
use of ai.grakn.Keyspace in project grakn by graknlabs.
the class CountPostProcessorTest method whenUpdatingInstanceCounts_EnsureRedisIsUpdated.
@Test
public void whenUpdatingInstanceCounts_EnsureRedisIsUpdated() {
// Create fake commit log
CommitLog commitLog = CommitLog.create(keyspace, newInstanceCounts, Collections.emptyMap());
// Update The Counts
countPostProcessor.updateCounts(commitLog);
// Check the calls
newInstanceCounts.forEach((id, value) -> {
// Redis is updated
verify(countStorage, Mockito.times(1)).getShardCount(keyspace, id);
verify(countStorage, Mockito.times(1)).incrementInstanceCount(keyspace, id, value);
});
// No Sharding takes place
verify(factoryMock, Mockito.times(0)).tx(any(Keyspace.class), any());
}
use of ai.grakn.Keyspace in project grakn by graknlabs.
the class GraqlConsole method start.
public static boolean start(GraqlShellOptions options, SessionProvider sessionProvider, String historyFile, PrintStream sout, PrintStream serr) throws InterruptedException, IOException {
List<String> queries = null;
// ------- Check option ------------------------
String query = options.getQuery();
// This is a best-effort guess as to whether the user has made a mistake, without parsing the query
if (query != null) {
queries = ImmutableList.of(query);
if (!query.contains("$") && query.trim().startsWith("match")) {
serr.println(ErrorMessage.NO_VARIABLE_IN_QUERY.getMessage());
}
}
// Print usage message if requested or if invalid arguments provided
if (options.displayHelp()) {
options.printUsage(sout);
return true;
}
if (options.displayVersion()) {
sout.println(GraknVersion.VERSION);
return true;
}
// ------- Check option ------------------------
Path path = options.getBatchLoadPath();
if (path != null) {
Keyspace keyspace = options.getKeyspace();
SimpleURI location = options.getUri();
SimpleURI httpUri = location != null ? location : Grakn.DEFAULT_URI;
try {
BatchLoader.sendBatchRequest(httpUri, keyspace, path, sout, serr);
} catch (Exception e) {
sout.println("Batch failed \n" + CommonUtil.simplifyExceptionMessage(e));
return false;
}
return true;
}
// -------- If no option set we start GraqlShell ----------
OutputFormat outputFormat = options.getOutputFormat();
boolean infer = options.shouldInfer();
ConsoleReader console = new ConsoleReader(System.in, sout);
GraknSession session = sessionProvider.getSession(options, console);
try (GraqlShell shell = new GraqlShell(historyFile, session, console, serr, outputFormat, infer)) {
List<Path> filePaths = options.getFiles();
if (filePaths != null) {
queries = loadQueries(filePaths);
}
// Start shell
shell.start(queries);
return !shell.errorOccurred();
} catch (StatusRuntimeException e) {
if (e.getStatus().getCode().equals(Status.Code.UNAVAILABLE)) {
serr.println(ErrorMessage.COULD_NOT_CONNECT.getMessage());
return false;
} else {
throw e;
}
}
}
Aggregations