Search in sources :

Example 6 with Keyspace

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);
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Type(ai.grakn.concept.Type) Timer(com.codahale.metrics.Timer) Keyspace(ai.grakn.Keyspace) Things(ai.grakn.engine.controller.response.Things) Label(ai.grakn.concept.Label)

Example 7 with Keyspace

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);
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Timer(com.codahale.metrics.Timer) Keyspace(ai.grakn.Keyspace) Jacksonisable(ai.grakn.engine.Jacksonisable) Link(ai.grakn.engine.controller.response.Link)

Example 8 with Keyspace

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);
    }
}
Also used : Concept(ai.grakn.engine.controller.response.Concept) GraknTx(ai.grakn.GraknTx) Timer(com.codahale.metrics.Timer) Keyspace(ai.grakn.Keyspace) ListResource(ai.grakn.engine.controller.response.ListResource)

Example 9 with Keyspace

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());
}
Also used : Keyspace(ai.grakn.Keyspace) CommitLog(ai.grakn.kb.log.CommitLog) Test(org.junit.Test)

Example 10 with Keyspace

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;
        }
    }
}
Also used : Path(java.nio.file.Path) ConsoleReader(jline.console.ConsoleReader) SimpleURI(ai.grakn.util.SimpleURI) GraknSession(ai.grakn.GraknSession) IOException(java.io.IOException) StatusRuntimeException(io.grpc.StatusRuntimeException) Keyspace(ai.grakn.Keyspace) StatusRuntimeException(io.grpc.StatusRuntimeException)

Aggregations

Keyspace (ai.grakn.Keyspace)30 Test (org.junit.Test)14 GraknTx (ai.grakn.GraknTx)9 GraknTxType (ai.grakn.GraknTxType)7 ConceptId (ai.grakn.concept.ConceptId)7 List (java.util.List)6 BatchExecutorClient (ai.grakn.client.BatchExecutorClient)5 Timer (com.codahale.metrics.Timer)5 LoggerFactory (org.slf4j.LoggerFactory)5 GraknSession (ai.grakn.GraknSession)4 Role (ai.grakn.concept.Role)4 InsertQuery (ai.grakn.graql.InsertQuery)4 SimpleURI (ai.grakn.util.SimpleURI)4 Before (org.junit.Before)4 Logger (org.slf4j.Logger)4 GraknClient (ai.grakn.client.GraknClient)3 AttributeType (ai.grakn.concept.AttributeType)3 EntityType (ai.grakn.concept.EntityType)3 RelationshipType (ai.grakn.concept.RelationshipType)3 Graql (ai.grakn.graql.Graql)3