Search in sources :

Example 61 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class EngineContext method clearGraphs.

private static void clearGraphs(EngineGraknTxFactory factory) {
    // Drop all keyspaces
    final Set<String> keyspaceNames = new HashSet<String>();
    try (GraknTx systemGraph = factory.tx(GraknKeyspaceStore.SYSTEM_KB_KEYSPACE, GraknTxType.WRITE)) {
        systemGraph.graql().match(var("x").isa("keyspace-name")).forEach(x -> x.concepts().forEach(y -> {
            keyspaceNames.add(y.asAttribute().getValue().toString());
        }));
    }
    keyspaceNames.forEach(name -> {
        GraknTx graph = factory.tx(Keyspace.of(name), GraknTxType.WRITE);
        graph.admin().delete();
    });
    factory.refreshConnections();
}
Also used : Keyspace(ai.grakn.Keyspace) LoggerFactory(org.slf4j.LoggerFactory) RedisSanityCheck(ai.grakn.engine.data.RedisSanityCheck) EngineGraknTxFactory(ai.grakn.engine.factory.EngineGraknTxFactory) GraknConfig(ai.grakn.engine.GraknConfig) CountStorage(ai.grakn.engine.task.postprocessing.CountStorage) GraknTx(ai.grakn.GraknTx) RedisWrapper(ai.grakn.engine.data.RedisWrapper) JedisLockProvider(ai.grakn.engine.lock.JedisLockProvider) JedisPool(redis.clients.jedis.JedisPool) RedisCountStorage(ai.grakn.engine.task.postprocessing.redisstorage.RedisCountStorage) GraknTxType(ai.grakn.GraknTxType) Graql.var(ai.grakn.graql.Graql.var) GraknKeyspaceStoreImpl(ai.grakn.engine.GraknKeyspaceStoreImpl) GraknKeyspaceStore(ai.grakn.engine.GraknKeyspaceStore) Set(java.util.Set) GrpcOpenRequestExecutorImpl(ai.grakn.engine.rpc.GrpcOpenRequestExecutorImpl) GraknConfigKey(ai.grakn.GraknConfigKey) IndexPostProcessor(ai.grakn.engine.task.postprocessing.IndexPostProcessor) IndexStorage(ai.grakn.engine.task.postprocessing.IndexStorage) List(java.util.List) ExceptionUtils.getFullStackTrace(org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace) GraknEngineServer(ai.grakn.engine.GraknEngineServer) RedisIndexStorage(ai.grakn.engine.task.postprocessing.redisstorage.RedisIndexStorage) RestAssured(com.jayway.restassured.RestAssured) GraknEngineStatus(ai.grakn.engine.GraknEngineStatus) SimpleURI(ai.grakn.util.SimpleURI) Iterables(com.google.common.collect.Iterables) GraknTestUtil(ai.grakn.util.GraknTestUtil) TestRule(org.junit.rules.TestRule) CountPostProcessor(ai.grakn.engine.task.postprocessing.CountPostProcessor) GrpcOpenRequestExecutor(ai.grakn.grpc.GrpcOpenRequestExecutor) LockProvider(ai.grakn.engine.lock.LockProvider) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) ServerBuilder(io.grpc.ServerBuilder) Server(io.grpc.Server) GrpcServer(ai.grakn.engine.rpc.GrpcServer) MetricRegistry(com.codahale.metrics.MetricRegistry) GraknEngineServerFactory(ai.grakn.engine.GraknEngineServerFactory) IOException(java.io.IOException) PostProcessor(ai.grakn.engine.task.postprocessing.PostProcessor) GraknSystemKeyspaceSession(ai.grakn.engine.GraknSystemKeyspaceSession) QueueSanityCheck(ai.grakn.engine.data.QueueSanityCheck) EmbeddedGraknSession(ai.grakn.factory.EmbeddedGraknSession) SampleKBLoader.randomKeyspace(ai.grakn.util.SampleKBLoader.randomKeyspace) GrpcGraknService(ai.grakn.engine.rpc.GrpcGraknService) Service(spark.Service) EngineID(ai.grakn.engine.util.EngineID) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig) Collections(java.util.Collections) GraknTx(ai.grakn.GraknTx) HashSet(java.util.HashSet)

Example 62 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class MigratorTestUtils method load.

public static void load(GraknSession factory, File schema) {
    try (GraknTx graph = factory.open(GraknTxType.WRITE)) {
        graph.graql().parse(Files.readLines(schema, StandardCharsets.UTF_8).stream().collect(joining("\n"))).execute();
        graph.commit();
    } catch (IOException | InvalidKBException e) {
        throw new RuntimeException(e);
    }
}
Also used : GraknTx(ai.grakn.GraknTx) InvalidKBException(ai.grakn.exception.InvalidKBException) IOException(java.io.IOException)

Example 63 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class CSVMigratorTest method whenLinesIsSetToN_OnlyFirstNLinesAreProcessed.

@Test
public void whenLinesIsSetToN_OnlyFirstNLinesAreProcessed() {
    load(factory, getFile("csv", "pets/schema.gql"));
    String template = getFileAsString("csv", "pets/template.gql");
    declareAndLoad(template, "pets/data/pets.quotes", new MigratorBuilder().setUri(engine.uri()).setKeyspace(keyspace).setRetries(0).setFailFast(false).setLines(3).build());
    try (GraknTx graph = factory.open(GraknTxType.WRITE)) {
        // Re Open Transaction
        Collection<Entity> pets = graph.getEntityType("pet").instances().collect(Collectors.toSet());
        TestCase.assertEquals(3, pets.size());
        Collection<Entity> cats = graph.getEntityType("cat").instances().collect(Collectors.toSet());
        TestCase.assertEquals(2, cats.size());
        Collection<Entity> dogs = graph.getEntityType("dog").instances().collect(Collectors.toSet());
        TestCase.assertEquals(0, dogs.size());
    }
}
Also used : GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) MigratorBuilder(ai.grakn.migration.base.MigratorBuilder) MigratorTestUtils.getFileAsString(ai.grakn.test.migration.MigratorTestUtils.getFileAsString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 64 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class CSVMigratorTest method whenParsedLineIsEmpty_ErrorIsNotThrownAndThoseLinesAreSkipped.

@Test
public void whenParsedLineIsEmpty_ErrorIsNotThrownAndThoseLinesAreSkipped() {
    load(factory, getFile("csv", "pets/schema.gql"));
    // Only dont insert Puffball
    String template = "if (<name> != \"Puffball\") do { insert $x isa cat; }";
    declareAndLoad(template, "pets/data/pets.quotes", defaultMigrator);
    // Re Open Transaction
    GraknTx graph = factory.open(GraknTxType.WRITE);
    assertEquals(8, graph.getEntityType("pet").instances().count());
}
Also used : GraknTx(ai.grakn.GraknTx) MigratorTestUtils.getFileAsString(ai.grakn.test.migration.MigratorTestUtils.getFileAsString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 65 with GraknTx

use of ai.grakn.GraknTx in project grakn by graknlabs.

the class JsonMigratorMainTest method runAndAssertDataCorrect.

private void runAndAssertDataCorrect(String... args) {
    run(args);
    try (GraknTx graph = session.open(GraknTxType.READ)) {
        EntityType personType = graph.getEntityType("person");
        assertEquals(1, personType.instances().count());
        Entity person = personType.instances().iterator().next();
        Entity address = getProperty(graph, person, "has-address").asEntity();
        Entity streetAddress = getProperty(graph, address, "address-has-street").asEntity();
        Attribute number = getResource(graph, streetAddress, Label.of("number"));
        assertEquals(21L, number.getValue());
        Collection<Thing> phoneNumbers = getProperties(graph, person, "has-phone");
        assertEquals(2, phoneNumbers.size());
    }
}
Also used : EntityType(ai.grakn.concept.EntityType) GraknTx(ai.grakn.GraknTx) Entity(ai.grakn.concept.Entity) Attribute(ai.grakn.concept.Attribute) Thing(ai.grakn.concept.Thing)

Aggregations

GraknTx (ai.grakn.GraknTx)243 Test (org.junit.Test)189 EntityType (ai.grakn.concept.EntityType)54 GetQuery (ai.grakn.graql.GetQuery)52 Entity (ai.grakn.concept.Entity)51 QueryBuilder (ai.grakn.graql.QueryBuilder)49 Role (ai.grakn.concept.Role)48 RelationshipType (ai.grakn.concept.RelationshipType)46 Answer (ai.grakn.graql.admin.Answer)44 Set (java.util.Set)44 EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)33 Label (ai.grakn.concept.Label)28 Attribute (ai.grakn.concept.Attribute)27 Concept (ai.grakn.concept.Concept)27 HashSet (java.util.HashSet)26 GraknSession (ai.grakn.GraknSession)22 AttributeType (ai.grakn.concept.AttributeType)22 ConceptId (ai.grakn.concept.ConceptId)22 List (java.util.List)19 GraknTxType (ai.grakn.GraknTxType)17