Search in sources :

Example 6 with SqlgGraph

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

the class TestMultipleThreadMultipleJvm method testMultiThreadedVertexLabelCreation.

@Test
public void testMultiThreadedVertexLabelCreation() throws Exception {
    // number graphs, pretending its a separate jvm
    int NUMBER_OF_GRAPHS = 5;
    int NUMBER_OF_SCHEMAS = 100;
    // Pre-create all the graphs
    List<SqlgGraph> graphs = new ArrayList<>();
    for (int i = 0; i < NUMBER_OF_GRAPHS; i++) {
        graphs.add(SqlgGraph.open(configuration));
    }
    logger.info(String.format("Done firing up %d graphs", NUMBER_OF_GRAPHS));
    ExecutorService poolPerGraph = Executors.newFixedThreadPool(NUMBER_OF_GRAPHS);
    CompletionService<SqlgGraph> poolPerGraphsExecutorCompletionService = new ExecutorCompletionService<>(poolPerGraph);
    try {
        Map<String, PropertyType> properties = new HashMap<>();
        properties.put("name", PropertyType.STRING);
        properties.put("age", PropertyType.INTEGER);
        List<Future<SqlgGraph>> results = new ArrayList<>();
        for (final SqlgGraph sqlgGraphAsync : graphs) {
            for (int i = 0; i < NUMBER_OF_SCHEMAS; i++) {
                final int count = i;
                results.add(poolPerGraphsExecutorCompletionService.submit(() -> {
                    // noinspection Duplicates
                    try {
                        VertexLabel outVertexLabel = sqlgGraphAsync.getTopology().ensureVertexLabelExist("schema_" + count, "tableOut_" + count, properties);
                        VertexLabel inVertexLabel = sqlgGraphAsync.getTopology().ensureVertexLabelExist("schema_" + count, "tableIn_" + count, properties);
                        sqlgGraphAsync.getTopology().ensureEdgeLabelExist("edge_" + count, outVertexLabel, inVertexLabel, properties);
                        final Random random = new Random();
                        if (random.nextBoolean()) {
                            sqlgGraphAsync.tx().commit();
                        } else {
                            sqlgGraphAsync.tx().rollback();
                        }
                    } catch (Exception e) {
                        sqlgGraphAsync.tx().rollback();
                        throw new RuntimeException(e);
                    }
                    return sqlgGraphAsync;
                }));
            }
        }
        for (Future<SqlgGraph> result : results) {
            result.get(5, TimeUnit.MINUTES);
        }
        Thread.sleep(1000);
        for (SqlgGraph graph : graphs) {
            assertEquals(this.sqlgGraph.getTopology(), graph.getTopology());
            assertEquals(this.sqlgGraph.getTopology().toJson(), graph.getTopology().toJson());
        }
        logger.info("starting inserting data");
        for (final SqlgGraph sqlgGraphAsync : graphs) {
            for (int i = 0; i < NUMBER_OF_SCHEMAS; i++) {
                final int count = i;
                results.add(poolPerGraphsExecutorCompletionService.submit(() -> {
                    // noinspection Duplicates
                    try {
                        Vertex v1 = sqlgGraphAsync.addVertex(T.label, "schema_" + count + "." + "tableOut_" + count, "name", "asdasd", "age", 1);
                        Vertex v2 = sqlgGraphAsync.addVertex(T.label, "schema_" + count + "." + "tableIn_" + count, "name", "asdasd", "age", 1);
                        v1.addEdge("edge_" + count, v2, "name", "asdasd", "age", 1);
                        final Random random = new Random();
                        if (random.nextBoolean()) {
                            sqlgGraphAsync.tx().rollback();
                        } else {
                            sqlgGraphAsync.tx().commit();
                        }
                    } catch (Exception e) {
                        sqlgGraphAsync.tx().rollback();
                        throw new RuntimeException(e);
                    }
                    return sqlgGraphAsync;
                }));
            }
        }
        poolPerGraph.shutdown();
        for (Future<SqlgGraph> result : results) {
            result.get(30, TimeUnit.SECONDS);
        }
        // Because of the rollBack logic the insert code may also create topology elements, so sleep a bit for notify to do its thing.
        Thread.sleep(1000);
        logger.info("starting querying data");
        Set<Vertex> vertices = this.sqlgGraph.traversal().V().out().toSet();
        this.sqlgGraph.tx().rollback();
        for (SqlgGraph graph : graphs) {
            logger.info("assert querying data");
            Set<Vertex> actual = graph.traversal().V().out().toSet();
            logger.info("vertices.size = " + vertices.size() + " actual.size = " + actual.size());
            assertEquals(vertices, actual);
            graph.tx().rollback();
        }
    } finally {
        for (SqlgGraph graph : graphs) {
            graph.close();
        }
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) PropertyType(org.umlg.sqlg.structure.PropertyType) IOException(java.io.IOException) PropertyVetoException(java.beans.PropertyVetoException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 7 with SqlgGraph

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

the class TestMultipleThreadMultipleJvm method testMultiThreadedSchemaCreation2.

@Test
public void testMultiThreadedSchemaCreation2() throws Exception {
    // number graphs, pretending its a separate jvm
    int NUMBER_OF_GRAPHS = 5;
    int NUMBER_OF_SCHEMAS = 100;
    // Pre-create all the graphs
    List<SqlgGraph> graphs = new ArrayList<>();
    for (int i = 0; i < NUMBER_OF_GRAPHS; i++) {
        graphs.add(SqlgGraph.open(configuration));
    }
    logger.info(String.format("Done firing up %d graphs", NUMBER_OF_GRAPHS));
    ExecutorService poolPerGraph = Executors.newFixedThreadPool(NUMBER_OF_GRAPHS);
    CompletionService<SqlgGraph> poolPerGraphsExecutorCompletionService = new ExecutorCompletionService<>(poolPerGraph);
    try {
        List<Future<SqlgGraph>> results = new ArrayList<>();
        for (final SqlgGraph sqlgGraphAsync : graphs) {
            for (int i = 0; i < NUMBER_OF_SCHEMAS; i++) {
                final int count = i;
                results.add(poolPerGraphsExecutorCompletionService.submit(() -> {
                    // noinspection Duplicates
                    try {
                        sqlgGraphAsync.getTopology().ensureSchemaExist("schema_" + count);
                        final Random random = new Random();
                        if (random.nextBoolean()) {
                            sqlgGraphAsync.tx().commit();
                        } else {
                            sqlgGraphAsync.tx().rollback();
                        }
                    } catch (Exception e) {
                        sqlgGraphAsync.tx().rollback();
                        throw new RuntimeException(e);
                    }
                    return sqlgGraphAsync;
                }));
            }
        }
        poolPerGraph.shutdown();
        for (Future<SqlgGraph> result : results) {
            result.get(100, TimeUnit.SECONDS);
        }
        Thread.sleep(1000);
        for (SqlgGraph graph : graphs) {
            assertEquals(this.sqlgGraph.getTopology(), graph.getTopology());
        }
    } finally {
        for (SqlgGraph graph : graphs) {
            graph.close();
        }
    }
}
Also used : SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) IOException(java.io.IOException) PropertyVetoException(java.beans.PropertyVetoException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 8 with SqlgGraph

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

the class TestMultipleThreadMultipleJvm method testMultiThreadedLocking.

@Test
public void testMultiThreadedLocking() throws Exception {
    // number graphs, pretending its a separate jvm
    int NUMBER_OF_GRAPHS = 10;
    ExecutorService sqlgGraphsExecutorService = Executors.newFixedThreadPool(100);
    CompletionService<Boolean> sqlgGraphsExecutorCompletionService = new ExecutorCompletionService<>(sqlgGraphsExecutorService);
    List<SqlgGraph> graphs = new ArrayList<>();
    try {
        // Pre-create all the graphs
        for (int i = 0; i < NUMBER_OF_GRAPHS; i++) {
            graphs.add(SqlgGraph.open(configuration));
        }
        List<Future<Boolean>> results = new ArrayList<>();
        for (SqlgGraph sqlgGraphAsync : graphs) {
            results.add(sqlgGraphsExecutorCompletionService.submit(() -> {
                ((SqlSchemaChangeDialect) sqlgGraphAsync.getSqlDialect()).lock(sqlgGraphAsync);
                sqlgGraphAsync.tx().rollback();
                return true;
            }));
        }
        sqlgGraphsExecutorService.shutdown();
        for (Future<Boolean> result : results) {
            result.get(10, TimeUnit.SECONDS);
        }
    } finally {
        for (SqlgGraph graph : graphs) {
            graph.close();
        }
    }
}
Also used : SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 9 with SqlgGraph

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

the class TestMultipleThreadMultipleJvm method testMultiThreadedSchemaCreation.

@Test
public void testMultiThreadedSchemaCreation() throws Exception {
    // number graphs, pretending its a separate jvm
    int NUMBER_OF_GRAPHS = 5;
    int NUMBER_OF_SCHEMAS = 100;
    // Pre-create all the graphs
    List<SqlgGraph> graphs = new ArrayList<>();
    for (int i = 0; i < NUMBER_OF_GRAPHS; i++) {
        graphs.add(SqlgGraph.open(configuration));
    }
    logger.info(String.format("Done firing up %d graphs", NUMBER_OF_GRAPHS));
    ExecutorService poolPerGraph = Executors.newFixedThreadPool(NUMBER_OF_GRAPHS);
    CompletionService<SqlgGraph> poolPerGraphsExecutorCompletionService = new ExecutorCompletionService<>(poolPerGraph);
    try {
        List<Future<SqlgGraph>> results = new ArrayList<>();
        for (final SqlgGraph sqlgGraphAsync : graphs) {
            results.add(poolPerGraphsExecutorCompletionService.submit(() -> {
                for (int i = 0; i < NUMBER_OF_SCHEMAS; i++) {
                    // noinspection Duplicates
                    try {
                        sqlgGraphAsync.getTopology().ensureSchemaExist("schema_" + i);
                        final Random random = new Random();
                        if (random.nextBoolean()) {
                            sqlgGraphAsync.tx().commit();
                        } else {
                            sqlgGraphAsync.tx().rollback();
                        }
                    } catch (Exception e) {
                        sqlgGraphAsync.tx().rollback();
                        throw new RuntimeException(e);
                    }
                }
                return sqlgGraphAsync;
            }));
        }
        poolPerGraph.shutdown();
        for (Future<SqlgGraph> result : results) {
            result.get(100, TimeUnit.SECONDS);
        }
        Thread.sleep(1000);
        for (SqlgGraph graph : graphs) {
            assertEquals(this.sqlgGraph.getTopology(), graph.getTopology());
            for (Schema schema : graph.getTopology().getSchemas()) {
                assertTrue(schema.isCommitted());
            }
        }
    } finally {
        for (SqlgGraph graph : graphs) {
            graph.close();
        }
    }
}
Also used : SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) Schema(org.umlg.sqlg.structure.topology.Schema) IOException(java.io.IOException) PropertyVetoException(java.beans.PropertyVetoException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 10 with SqlgGraph

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

the class TestLocalDate method testZonedDateTime.

@Test
public void testZonedDateTime() throws Exception {
    ZoneId zoneIdShanghai = ZoneId.of("Asia/Shanghai");
    ZonedDateTime zonedDateTimeAGT = ZonedDateTime.of(LocalDateTime.now(), zoneIdShanghai);
    this.sqlgGraph.addVertex(T.label, "A", "zonedDateTime", zonedDateTimeAGT);
    this.sqlgGraph.tx().commit();
    // Create a new sqlgGraph
    try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
        List<? extends Property<ZonedDateTime>> properties = sqlgGraph1.traversal().V().hasLabel("A").<ZonedDateTime>properties("zonedDateTime").toList();
        Assert.assertEquals(1, properties.size());
        Assert.assertTrue(properties.get(0).isPresent());
        ZonedDateTime value = properties.get(0).<ZonedDateTime>value();
        Assert.assertEquals(zonedDateTimeAGT, value);
    }
}
Also used : SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) 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