use of ai.grakn.GraknTx in project grakn by graknlabs.
the class GraknTxTinkerFactoryTest method whenGettingGraphFromFactoryClosingItAndGettingItAgain_ReturnGraph.
@Test
public void whenGettingGraphFromFactoryClosingItAndGettingItAgain_ReturnGraph() {
TxFactoryTinker factory = new TxFactoryTinker(session);
GraknTx graph1 = factory.open(GraknTxType.WRITE);
graph1.close();
GraknTxTinker graph2 = factory.open(GraknTxType.WRITE);
assertEquals(graph1, graph2);
}
use of ai.grakn.GraknTx in project grakn by graknlabs.
the class GraknTxTinkerTest method addRandomEntity.
private synchronized void addRandomEntity() {
try (GraknTx graph = Grakn.session(Grakn.IN_MEMORY, tx.keyspace()).open(GraknTxType.WRITE)) {
graph.getEntityType("Thing").addEntity();
graph.commit();
}
}
use of ai.grakn.GraknTx 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.GraknTx 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.GraknTx 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);
}
}
Aggregations