Search in sources :

Example 46 with SqlgGraph

use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.

the class TestForeignKeysAreOptional method testForeignKeysOnHsqldb.

@Test
public void testForeignKeysOnHsqldb() throws Exception {
    Assume.assumeTrue(this.sqlgGraph.getSqlDialect().getClass().getSimpleName().contains("Hsqldb"));
    Configuration conf = getConfigurationClone();
    conf.setProperty("implement.foreign.keys", "true");
    try (SqlgGraph g = SqlgGraph.open(conf)) {
        Vertex v1 = g.addVertex(T.label, "Person");
        Vertex v2 = g.addVertex(T.label, "Person");
        v1.addEdge("Edge1", v2);
        g.tx().commit();
        Connection conn = g.tx().getConnection();
        try (PreparedStatement preparedStatement = conn.prepareStatement("select * from information_schema.constraint_table_usage where CONSTRAINT_NAME like '%FK%'")) {
            ResultSet rs = preparedStatement.executeQuery();
            Assert.assertTrue(rs.next());
        }
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) Configuration(org.apache.commons.configuration.Configuration) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 47 with SqlgGraph

use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.

the class TestForeignKeysAreOptional method testForeignKeysOffHsqldb.

@Test
public void testForeignKeysOffHsqldb() throws Exception {
    Assume.assumeTrue(this.sqlgGraph.getSqlDialect().getClass().getSimpleName().contains("Hsqldb"));
    Configuration conf = getConfigurationClone();
    conf.setProperty("implement.foreign.keys", "false");
    try (SqlgGraph g = SqlgGraph.open(conf)) {
        Vertex v1 = g.addVertex(T.label, "Person");
        Vertex v2 = g.addVertex(T.label, "Person");
        v1.addEdge("Edge1", v2);
        g.tx().commit();
        Connection conn = g.tx().getConnection();
        try (PreparedStatement preparedStatement = conn.prepareStatement("select * from information_schema.constraint_table_usage " + "where TABLE_NAME = 'E_Edge1' and CONSTRAINT_NAME like '%FK%'")) {
            ResultSet rs = preparedStatement.executeQuery();
            Assert.assertFalse(rs.next());
        }
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) Configuration(org.apache.commons.configuration.Configuration) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 48 with SqlgGraph

use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.

the class TestForeignKeysAreOptional method testForeignKeysOnPostgres.

@Test
public void testForeignKeysOnPostgres() throws Exception {
    Assume.assumeTrue(this.sqlgGraph.getSqlDialect().getClass().getSimpleName().contains("Postgres"));
    Configuration conf = getConfigurationClone();
    conf.setProperty("implement.foreign.keys", "true");
    try (SqlgGraph g = SqlgGraph.open(conf)) {
        Vertex v1 = g.addVertex(T.label, "Person");
        Vertex v2 = g.addVertex(T.label, "Person");
        v1.addEdge("Edge1", v2);
        g.tx().commit();
        Connection conn = g.tx().getConnection();
        DatabaseMetaData dm = conn.getMetaData();
        ResultSet rs = dm.getImportedKeys("sqlggraphdb", "public", "E_Edge1");
        Assert.assertTrue(rs.next());
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) Configuration(org.apache.commons.configuration.Configuration) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 49 with SqlgGraph

use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.

the class TestCaptureSchemaTableEdges method testLoadTableLabels.

@Test
public void testLoadTableLabels() throws Exception {
    Vertex person1 = this.sqlgGraph.addVertex(T.label, "Person", "name", "pieter");
    Vertex car1 = this.sqlgGraph.addVertex(T.label, "Car", "name", "bmw");
    person1.addEdge("drives", car1);
    Vertex car2 = this.sqlgGraph.addVertex(T.label, "Car", "name", "toyota");
    person1.addEdge("drives", car2);
    Vertex bmw = this.sqlgGraph.addVertex(T.label, "Model", "name", "bmw");
    car1.addEdge("model", bmw);
    Vertex toyota = this.sqlgGraph.addVertex(T.label, "Model", "name", "toyota");
    car2.addEdge("model", toyota);
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.close();
    try (SqlgGraph sqlgGraph = SqlgGraph.open(configuration)) {
        Map<SchemaTable, Pair<Set<SchemaTable>, Set<SchemaTable>>> localTabels = sqlgGraph.getTopology().getTableLabels();
        assertTrue(localTabels.containsKey(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "V_Person")));
        assertTrue(localTabels.containsKey(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "V_Car")));
        assertTrue(localTabels.containsKey(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "V_Model")));
        Pair<Set<SchemaTable>, Set<SchemaTable>> person = localTabels.get(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "V_Person"));
        assertEquals(0, person.getLeft().size());
        assertEquals(1, person.getRight().size());
        assertEquals(sqlgGraph.getSqlDialect().getPublicSchema() + ".E_drives", person.getRight().iterator().next().toString());
        Pair<Set<SchemaTable>, Set<SchemaTable>> car = localTabels.get(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "V_Car"));
        assertEquals(1, car.getLeft().size());
        assertEquals(1, car.getRight().size());
        assertEquals(sqlgGraph.getSqlDialect().getPublicSchema() + ".E_drives", car.getLeft().iterator().next().toString());
        Pair<Set<SchemaTable>, Set<SchemaTable>> model = localTabels.get(SchemaTable.of(sqlgGraph.getSqlDialect().getPublicSchema(), "V_Model"));
        assertEquals(1, model.getLeft().size());
        assertEquals(0, model.getRight().size());
        assertEquals(sqlgGraph.getSqlDialect().getPublicSchema() + ".E_model", model.getLeft().iterator().next().toString());
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) Set(java.util.Set) SchemaTable(org.umlg.sqlg.structure.SchemaTable) Pair(org.apache.commons.lang3.tuple.Pair) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 50 with SqlgGraph

use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.

the class TestMultiThread method testMultipleGraphsMultipleLabels.

/**
 * test when each graph is created in its own thread, in distributed mode
 * each thread created a different label
 *
 * @throws Exception
 */
@Test
public void testMultipleGraphsMultipleLabels() throws Exception {
    URL sqlProperties = Thread.currentThread().getContextClassLoader().getResource("sqlg.properties");
    try {
        configuration = new PropertiesConfiguration(sqlProperties);
        Assume.assumeTrue(isPostgres());
        configuration.addProperty("distributed", true);
        if (!configuration.containsKey("jdbc.url"))
            throw new IllegalArgumentException(String.format("SqlGraph configuration requires that the %s be set", "jdbc.url"));
    } catch (ConfigurationException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    ExecutorService executorService = newFixedThreadPool(200);
    int loop = 20;
    for (int i = 0; i < loop; i++) {
        String n = "person" + i;
        executorService.submit(() -> {
            try {
                try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
                    sqlgGraph1.addVertex(T.label, "Person" + n, "name", n);
                    sqlgGraph1.tx().commit();
                }
            } catch (Exception e) {
                e.printStackTrace();
                fail(e.getMessage());
            }
        });
    }
    executorService.shutdown();
    executorService.awaitTermination(10, TimeUnit.SECONDS);
    try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
        for (int i = 0; i < loop; i++) {
            String n = "person" + i;
            Assert.assertEquals(1, sqlgGraph1.traversal().V().hasLabel("Person" + n).count().next().longValue());
        }
    }
}
Also used : SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) ConfigurationException(org.apache.commons.configuration.ConfigurationException) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) URL(java.net.URL) ConfigurationException(org.apache.commons.configuration.ConfigurationException) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Aggregations

SqlgGraph (org.umlg.sqlg.structure.SqlgGraph)75 Test (org.junit.Test)68 BaseTest (org.umlg.sqlg.test.BaseTest)64 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)44 ConfigurationException (org.apache.commons.configuration.ConfigurationException)10 PropertyVetoException (java.beans.PropertyVetoException)9 IOException (java.io.IOException)9 Connection (java.sql.Connection)7 Edge (org.apache.tinkerpop.gremlin.structure.Edge)6 ResultSet (java.sql.ResultSet)4 Configuration (org.apache.commons.configuration.Configuration)4 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)4 ReducingBarrierStep (org.apache.tinkerpop.gremlin.process.traversal.step.util.ReducingBarrierStep)4 Statement (java.sql.Statement)3 PropertyType (org.umlg.sqlg.structure.PropertyType)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 URL (java.net.URL)2 DatabaseMetaData (java.sql.DatabaseMetaData)2 PreparedStatement (java.sql.PreparedStatement)2