Search in sources :

Example 1 with Schema

use of org.umlg.sqlg.structure.topology.Schema in project sqlg by pietermartin.

the class SchemaTableTree method hasOnlyOneInOutEdgeLabel.

private boolean hasOnlyOneInOutEdgeLabel(SchemaTable schemaTable) {
    Optional<Schema> schemaOptional = sqlgGraph.getTopology().getSchema(schemaTable.getSchema());
    Preconditions.checkState(schemaOptional.isPresent(), "BUG: %s not found in the topology.", schemaTable.getSchema());
    Schema schema = schemaOptional.get();
    boolean result = true;
    if (schemaTable.isVertexTable()) {
        // Need to delete any in/out edges.
        Optional<VertexLabel> vertexLabelOptional = schema.getVertexLabel(schemaTable.withOutPrefix().getTable());
        Preconditions.checkState(vertexLabelOptional.isPresent(), "BUG: %s not found in the topology.", schemaTable.withOutPrefix().getTable());
        VertexLabel vertexLabel = vertexLabelOptional.get();
        Collection<EdgeLabel> outEdgeLabels = vertexLabel.getOutEdgeLabels().values();
        for (EdgeLabel edgeLabel : outEdgeLabels) {
            result = edgeLabel.getOutVertexLabels().size() == 1;
            if (!result) {
                break;
            }
        }
        if (result) {
            Collection<EdgeLabel> inEdgeLabels = vertexLabel.getInEdgeLabels().values();
            for (EdgeLabel edgeLabel : inEdgeLabels) {
                result = edgeLabel.getInVertexLabels().size() == 1;
                if (!result) {
                    break;
                }
            }
        }
    }
    return result;
}
Also used : VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) Schema(org.umlg.sqlg.structure.topology.Schema) EdgeLabel(org.umlg.sqlg.structure.topology.EdgeLabel)

Example 2 with Schema

use of org.umlg.sqlg.structure.topology.Schema 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 3 with Schema

use of org.umlg.sqlg.structure.topology.Schema in project sqlg by pietermartin.

the class TestBatchGlobalUniqueIndexes method testGlobalUniqueIndexOnVertexNormalBatchMode.

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
public void testGlobalUniqueIndexOnVertexNormalBatchMode() throws InterruptedException {
    Assume.assumeTrue(this.sqlgGraph.getSqlDialect().supportsBatchMode());
    Map<String, PropertyType> properties = new HashMap<>();
    properties.put("namec", PropertyType.STRING);
    properties.put("namea", PropertyType.STRING);
    properties.put("nameb", PropertyType.STRING);
    this.sqlgGraph.getTopology().ensureVertexLabelExist("A", properties);
    @SuppressWarnings("OptionalGetWithoutIsPresent") Collection<PropertyColumn> propertyColumns = this.sqlgGraph.getTopology().getPublicSchema().getVertexLabel("A").get().getProperties().values();
    this.sqlgGraph.getTopology().ensureGlobalUniqueIndexExist(new HashSet<>(propertyColumns));
    this.sqlgGraph.tx().commit();
    Schema globalUniqueIndexSchema = this.sqlgGraph.getTopology().getGlobalUniqueIndexSchema();
    Optional<GlobalUniqueIndex> globalUniqueIndexOptional = globalUniqueIndexSchema.getGlobalUniqueIndex("A_namea_A_nameb_A_namec");
    Assert.assertTrue(globalUniqueIndexOptional.isPresent());
    Optional<PropertyColumn> nameaPropertyColumnOptional = this.sqlgGraph.getTopology().getPublicSchema().getVertexLabel("A").get().getProperty("namea");
    Assert.assertTrue(nameaPropertyColumnOptional.isPresent());
    @SuppressWarnings("OptionalGetWithoutIsPresent") Set<GlobalUniqueIndex> globalUniqueIndices = nameaPropertyColumnOptional.get().getGlobalUniqueIndices();
    Assert.assertEquals(1, globalUniqueIndices.size());
    GlobalUniqueIndex globalUniqueIndex = globalUniqueIndices.iterator().next();
    Assert.assertEquals("A_namea_A_nameb_A_namec", globalUniqueIndex.getName());
    this.sqlgGraph.tx().normalBatchModeOn();
    Vertex a = this.sqlgGraph.addVertex(T.label, "A", "namea", "a");
    this.sqlgGraph.tx().commit();
    try {
        this.sqlgGraph.tx().normalBatchModeOn();
        this.sqlgGraph.addVertex(T.label, "A", "namea", "a");
        this.sqlgGraph.tx().commit();
        Assert.fail("GlobalUniqueIndex should prevent this from executing");
    } catch (Exception e) {
    // swallow
    }
    this.sqlgGraph.tx().rollback();
    this.sqlgGraph.tx().normalBatchModeOn();
    this.sqlgGraph.addVertex(T.label, "A", "namea", "aa");
    this.sqlgGraph.tx().commit();
    testGlobalUniqueIndexOnVertexNormalBatchMode_assert(this.sqlgGraph, globalUniqueIndex, a);
    if (this.sqlgGraph1 != null) {
        Thread.sleep(1000);
        testGlobalUniqueIndexOnVertexNormalBatchMode_assert(this.sqlgGraph1, globalUniqueIndex, a);
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) Schema(org.umlg.sqlg.structure.topology.Schema) IOException(java.io.IOException) PropertyVetoException(java.beans.PropertyVetoException) GlobalUniqueIndex(org.umlg.sqlg.structure.topology.GlobalUniqueIndex) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 4 with Schema

use of org.umlg.sqlg.structure.topology.Schema in project sqlg by pietermartin.

the class TestTopologyDeleteSpecific method testSchemaDelete.

/**
 * this failed with a NPE because we lost the table definition we're working on
 *
 * @throws Exception
 */
@Test
public void testSchemaDelete() throws Exception {
    Assume.assumeTrue(this.sqlgGraph.getSqlDialect().supportsDistribution());
    String schema = "willDelete";
    Vertex v1 = sqlgGraph.addVertex(T.label, schema + ".t1", "name", "n1", "hello", "world");
    sqlgGraph.tx().commit();
    Configuration c = getConfigurationClone();
    c.setProperty(SqlgGraph.DISTRIBUTED, true);
    sqlgGraph = SqlgGraph.open(c);
    sqlgGraph.getTopology().getSchema(schema).ifPresent((Schema s) -> s.remove(false));
    sqlgGraph.tx().commit();
    v1 = sqlgGraph.addVertex(T.label, schema + ".t1", "name", "n1");
    Vertex v2 = sqlgGraph.addVertex(T.label, schema + ".t2", "name", "n2");
    Edge e1 = v1.addEdge("e1", v2);
    sqlgGraph.tx().commit();
    sqlgGraph.tx().normalBatchModeOn();
    v1.property("hello", "world");
    // this line was failing
    e1.property("hello", "world");
    sqlgGraph.tx().commit();
    assertEquals("world", e1.value("hello"));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Configuration(org.apache.commons.configuration.Configuration) Schema(org.umlg.sqlg.structure.topology.Schema) Edge(org.apache.tinkerpop.gremlin.structure.Edge) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 5 with Schema

use of org.umlg.sqlg.structure.topology.Schema in project sqlg by pietermartin.

the class TestSchemaEagerCreation method testAddEdgeLabelViaOutVertexLabel.

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
public void testAddEdgeLabelViaOutVertexLabel() {
    VertexLabel a = this.sqlgGraph.getTopology().ensureSchemaExist("A").ensureVertexLabelExist("A");
    Optional<Schema> schemaOptional = this.sqlgGraph.getTopology().getSchema("A");
    assertTrue(schemaOptional.isPresent());
    VertexLabel b = schemaOptional.get().ensureVertexLabelExist("B");
    a.ensureEdgeLabelExist("ab", b);
    this.sqlgGraph.tx().commit();
    Optional<EdgeLabel> edgeLabel = this.sqlgGraph.getTopology().getSchema("A").get().getEdgeLabel("ab");
    assertTrue(edgeLabel.isPresent());
    assertEquals("ab", edgeLabel.get().getLabel());
}
Also used : VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) Schema(org.umlg.sqlg.structure.topology.Schema) EdgeLabel(org.umlg.sqlg.structure.topology.EdgeLabel) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Aggregations

Schema (org.umlg.sqlg.structure.topology.Schema)10 Test (org.junit.Test)7 BaseTest (org.umlg.sqlg.test.BaseTest)7 VertexLabel (org.umlg.sqlg.structure.topology.VertexLabel)6 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)4 EdgeLabel (org.umlg.sqlg.structure.topology.EdgeLabel)4 PropertyVetoException (java.beans.PropertyVetoException)3 IOException (java.io.IOException)3 PropertyColumn (org.umlg.sqlg.structure.topology.PropertyColumn)3 Configuration (org.apache.commons.configuration.Configuration)2 Edge (org.apache.tinkerpop.gremlin.structure.Edge)2 GlobalUniqueIndex (org.umlg.sqlg.structure.topology.GlobalUniqueIndex)2 HashMap (java.util.HashMap)1 ConfigurationException (org.apache.commons.configuration.ConfigurationException)1 Triple (org.apache.commons.lang3.tuple.Triple)1 SchemaTableTree (org.umlg.sqlg.sql.parse.SchemaTableTree)1 SqlgSqlExecutor (org.umlg.sqlg.strategy.SqlgSqlExecutor)1 SqlgGraph (org.umlg.sqlg.structure.SqlgGraph)1