Search in sources :

Example 26 with VertexLabel

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

the class TestTopology method testAddColumns.

@Test
public void testAddColumns() {
    // Create Schema
    Topology topology = sqlgGraph.getTopology();
    topology.ensureSchemaExist("TEST");
    Map<String, Object> columns = new HashMap<>();
    columns.put("Test1", "");
    columns.put("Test2", "");
    // Add a vertex and remove to create columns
    Vertex v = sqlgGraph.addVertex("TEST" + "." + "TEST_Table", columns);
    v.remove();
    sqlgGraph.tx().commit();
    columns = new HashMap<>();
    columns.put("Test1", "T1");
    columns.put("Test2", "T2");
    // Add the data
    Vertex vv = sqlgGraph.addVertex("TEST" + "." + "TEST_Table", columns);
    sqlgGraph.tx().commit();
    // Simulating second load
    // Remove the whole table label
    Optional<VertexLabel> tableVertexLabel = sqlgGraph.getTopology().getVertexLabel("TEST", "TEST_Table");
    if (tableVertexLabel.isPresent()) {
        tableVertexLabel.get().remove(false);
    }
    columns = new HashMap<>();
    columns.put("Test1", "");
    columns.put("Test2", "");
    columns.put("Test3", "");
    // Add a vertex with more columns than previously had
    v = sqlgGraph.addVertex("TEST" + "." + "TEST_Table", columns);
    v.remove();
    sqlgGraph.tx().commit();
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HashMap(java.util.HashMap) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) Topology(org.umlg.sqlg.structure.topology.Topology) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 27 with VertexLabel

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

the class TestBatchStreamTemporaryVertex method testStreamTemporaryVertexMultipleThreads.

// Testing issue #226
@Test
public void testStreamTemporaryVertexMultipleThreads() throws InterruptedException {
    VertexLabel haloVertexLabel = this.sqlgGraph.getTopology().ensureVertexLabelExist("halo");
    haloVertexLabel.ensurePropertiesExist(new HashMap<String, PropertyType>() {

        {
            put("this", PropertyType.STRING);
        }
    });
    this.sqlgGraph.getTopology().ensureVertexLabelExist("A");
    this.sqlgGraph.tx().commit();
    final CountDownLatch countDownLatch1 = new CountDownLatch(1);
    final CountDownLatch countDownLatch2 = new CountDownLatch(1);
    final Thread thread1 = new Thread("thread1") {

        @Override
        public void run() {
            TestBatchStreamTemporaryVertex.this.sqlgGraph.tx().streamingBatchModeOn();
            TestBatchStreamTemporaryVertex.this.sqlgGraph.streamTemporaryVertex("halo", new LinkedHashMap<String, Object>() {

                {
                    put("this", "that");
                }
            });
            countDownLatch1.countDown();
            System.out.println("countDownLatch1 countDown");
            TestBatchStreamTemporaryVertex.this.sqlgGraph.streamTemporaryVertex("halo", new LinkedHashMap<String, Object>() {

                {
                    put("this", "that");
                }
            });
            try {
                countDownLatch2.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            // If Topology.temporaryTable has been cleared then the next line will block.
            // It will block because it will try to create the temp table but the copy command is already in progress.
            // The copy command needs to finish before the driver will allow any other command to execute.
            TestBatchStreamTemporaryVertex.this.sqlgGraph.streamTemporaryVertex("halo", new LinkedHashMap<String, Object>() {

                {
                    put("this", "that");
                }
            });
            TestBatchStreamTemporaryVertex.this.sqlgGraph.tx().commit();
        }
    };
    thread1.start();
    final Thread thread2 = new Thread("thread2") {

        @Override
        public void run() {
            try {
                countDownLatch1.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("thread2 countDownLatch ");
            TestBatchStreamTemporaryVertex.this.sqlgGraph.addVertex(T.label, "A");
            TestBatchStreamTemporaryVertex.this.sqlgGraph.tx().commit();
            countDownLatch2.countDown();
        }
    };
    thread2.start();
    thread1.join();
    thread2.join();
}
Also used : VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) PropertyType(org.umlg.sqlg.structure.PropertyType) CountDownLatch(java.util.concurrent.CountDownLatch) BaseTest(org.umlg.sqlg.test.BaseTest)

Example 28 with VertexLabel

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

the class TestBatchGlobalUniqueIndexes method testGlobalUniqueIndexOnEdgeNormalBatchMode.

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
public void testGlobalUniqueIndexOnEdgeNormalBatchMode() throws InterruptedException {
    Assume.assumeTrue(this.sqlgGraph.getSqlDialect().supportsBatchMode());
    Map<String, PropertyType> properties = new HashMap<>();
    properties.put("name", PropertyType.STRING);
    VertexLabel vertexLabelA = this.sqlgGraph.getTopology().ensureVertexLabelExist("A", properties);
    VertexLabel vertexLabelB = this.sqlgGraph.getTopology().ensureVertexLabelExist("B", properties);
    properties.clear();
    properties.put("namea", PropertyType.STRING);
    properties.put("nameb", PropertyType.STRING);
    properties.put("namec", PropertyType.STRING);
    vertexLabelA.ensureEdgeLabelExist("ab", vertexLabelB, properties);
    @SuppressWarnings("OptionalGetWithoutIsPresent") Collection<PropertyColumn> propertyColumns = this.sqlgGraph.getTopology().getPublicSchema().getEdgeLabel("ab").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("ab_namea_ab_nameb_ab_namec");
    Assert.assertTrue(globalUniqueIndexOptional.isPresent());
    Optional<PropertyColumn> nameaPropertyColumnOptional = this.sqlgGraph.getTopology().getPublicSchema().getEdgeLabel("ab").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("ab_namea_ab_nameb_ab_namec", globalUniqueIndex.getName());
    this.sqlgGraph.tx().normalBatchModeOn();
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b");
    Edge edge = a1.addEdge("ab", b1, "namea", "a", "nameb", "b", "namec", "c");
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.tx().normalBatchModeOn();
    try {
        a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a");
        b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b");
        a1.addEdge("ab", b1, "namea", "a", "nameb", "b", "namec", "c");
        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();
    testGlobalUniqueIndexOnEdgeNormalBatchMode_assert(this.sqlgGraph, globalUniqueIndex, edge);
    if (this.sqlgGraph1 != null) {
        Thread.sleep(1000);
        testGlobalUniqueIndexOnEdgeNormalBatchMode_assert(this.sqlgGraph1, globalUniqueIndex, edge);
    }
}
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) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) GlobalUniqueIndex(org.umlg.sqlg.structure.topology.GlobalUniqueIndex) Edge(org.apache.tinkerpop.gremlin.structure.Edge) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 29 with VertexLabel

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

the class TestBatchGlobalUniqueIndexes method testVertexUniqueConstraintDeleteBatchMode.

@Test
public void testVertexUniqueConstraintDeleteBatchMode() throws InterruptedException {
    Assume.assumeTrue(this.sqlgGraph.getSqlDialect().supportsBatchMode());
    VertexLabel vertexLabel = this.sqlgGraph.getTopology().getPublicSchema().ensureVertexLabelExist("A", new HashMap<String, PropertyType>() {

        {
            put("namea", PropertyType.STRING);
            put("nameb", PropertyType.STRING);
            put("namec", PropertyType.STRING);
        }
    });
    Set<PropertyColumn> properties = new HashSet<>(vertexLabel.getProperties().values());
    this.sqlgGraph.getTopology().ensureGlobalUniqueIndexExist(properties);
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.tx().normalBatchModeOn();
    for (int i = 1; i < 1001; i++) {
        Vertex a = this.sqlgGraph.addVertex(T.label, "A", "namea", "a" + i);
    }
    this.sqlgGraph.tx().commit();
    Assert.assertEquals(3000, this.sqlgGraph.globalUniqueIndexes().V().count().next().intValue());
    try {
        this.sqlgGraph.addVertex(T.label, "A", "namea", "a1");
        Assert.fail("GlobalUniqueIndex should prevent this form happening");
    } catch (Exception e) {
        // swallow
        this.sqlgGraph.tx().rollback();
    }
    this.sqlgGraph.tx().normalBatchModeOn();
    Vertex a1 = this.sqlgGraph.traversal().V().hasLabel("A").has("namea", "a1").next();
    a1.remove();
    this.sqlgGraph.tx().commit();
    Assert.assertEquals(2997, this.sqlgGraph.globalUniqueIndexes().V().count().next().intValue());
    this.sqlgGraph.addVertex(T.label, "A", "namea", "a1");
    // this time it passes.
    this.sqlgGraph.tx().commit();
    testVertexUniqueConstraintDeleteBatchMode_assert(this.sqlgGraph);
    if (this.sqlgGraph1 != null) {
        Thread.sleep(1000);
        testVertexUniqueConstraintDeleteBatchMode_assert(this.sqlgGraph1);
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) IOException(java.io.IOException) PropertyVetoException(java.beans.PropertyVetoException) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 30 with VertexLabel

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

the class TestBatchGlobalUniqueIndexes method testBatchModeGlobalUniqueIndexOnPropertyThatDoesNotYetExists2.

@Test
public void testBatchModeGlobalUniqueIndexOnPropertyThatDoesNotYetExists2() throws InterruptedException {
    Assume.assumeTrue(this.sqlgGraph.getSqlDialect().supportsBatchMode());
    Map<String, PropertyType> properties = new HashMap<String, PropertyType>() {

        {
            put("name", PropertyType.STRING);
        }
    };
    VertexLabel vertexLabel = this.sqlgGraph.getTopology().getPublicSchema().ensureVertexLabelExist("A", properties);
    this.sqlgGraph.getTopology().ensureGlobalUniqueIndexExist(new HashSet<>(vertexLabel.getProperties().values()));
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.tx().normalBatchModeOn();
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.tx().normalBatchModeOn();
    a1.property("name", "123");
    this.sqlgGraph.tx().commit();
    Assert.assertEquals(1, this.sqlgGraph.globalUniqueIndexes().V().count().next().intValue());
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.tx().normalBatchModeOn();
    try {
        Vertex a2 = this.sqlgGraph.addVertex(T.label, "A");
        a2.property("name", "123");
        this.sqlgGraph.tx().commit();
        Assert.fail("GlobalUniqueIndex should prevent this from happening");
    } catch (Exception e) {
    // swallow
    }
    if (this.sqlgGraph1 != null) {
        Thread.sleep(1000);
        try {
            Vertex a2 = this.sqlgGraph1.addVertex(T.label, "A");
            a2.property("name", "123");
            this.sqlgGraph1.tx().commit();
            Assert.fail("GlobalUniqueIndex should prevent this from happening");
        } catch (Exception e) {
        // swallow
        }
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) IOException(java.io.IOException) PropertyVetoException(java.beans.PropertyVetoException) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Aggregations

VertexLabel (org.umlg.sqlg.structure.topology.VertexLabel)31 BaseTest (org.umlg.sqlg.test.BaseTest)23 Test (org.junit.Test)22 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)17 EdgeLabel (org.umlg.sqlg.structure.topology.EdgeLabel)11 PropertyColumn (org.umlg.sqlg.structure.topology.PropertyColumn)9 Schema (org.umlg.sqlg.structure.topology.Schema)7 PropertyVetoException (java.beans.PropertyVetoException)6 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 PropertyType (org.umlg.sqlg.structure.PropertyType)3 Direction (org.apache.tinkerpop.gremlin.structure.Direction)2 Edge (org.apache.tinkerpop.gremlin.structure.Edge)2 SqlgSqlExecutor (org.umlg.sqlg.strategy.SqlgSqlExecutor)2 org.umlg.sqlg.structure (org.umlg.sqlg.structure)2 GlobalUniqueIndex (org.umlg.sqlg.structure.topology.GlobalUniqueIndex)2 Preconditions (com.google.common.base.Preconditions)1 java.util (java.util)1 Collections (java.util.Collections)1 Map (java.util.Map)1