Search in sources :

Example 6 with VertexLabel

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

the class TestGremlinCompileFullTextPredicate method testPerfTwoColumns.

@Test
@Ignore("check manually index is used")
public void testPerfTwoColumns() throws SQLException {
    assumeTrue(isPostgres());
    Vertex v0 = this.sqlgGraph.addVertex(T.label, "Sentence", "name1", "a fat cat sat on a mat", "name2", "and ate a fat rat");
    int LOOPS = 10000;
    for (int a = 0; a < LOOPS; a++) {
        this.sqlgGraph.addVertex(T.label, "Sentence", "name1", "loop1" + a, "name2", "loop2" + a);
    }
    VertexLabel vl = this.sqlgGraph.getTopology().getVertexLabel("public", "Sentence").get();
    vl.ensureIndexExists(IndexType.getFullTextGIN("english"), Arrays.asList(vl.getProperty("name1").get(), vl.getProperty("name2").get()));
    this.sqlgGraph.tx().commit();
    long t0 = System.currentTimeMillis();
    List<Vertex> vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").where(FullText.fullTextMatch("english", false, Arrays.asList("name1", "name2"), "fat & rat")).toList();
    assertEquals(1, vts.size());
    assertTrue(vts.contains(v0));
    long t1 = System.currentTimeMillis();
    long delta = t1 - t0;
    System.out.println("query time:" + delta + "ms");
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) Ignore(org.junit.Ignore) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 7 with VertexLabel

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

the class TestGremlinCompileFullTextPredicate method testDocExamples.

@Test
public void testDocExamples() throws SQLException {
    assumeTrue(isPostgres());
    Vertex v0 = this.sqlgGraph.addVertex(T.label, "Sentence", "name", "a fat cat sat on a mat and ate a fat rat");
    Vertex v1 = this.sqlgGraph.addVertex(T.label, "Sentence", "name", "fatal error");
    Vertex v2 = this.sqlgGraph.addVertex(T.label, "Sentence", "name", "error is not fatal");
    /*this.sqlgGraph.tx().commit();
		 try (Statement s=this.sqlgGraph.tx().getConnection().createStatement();){
			 String create="CREATE INDEX sentence_idx ON \""+SchemaManager.VERTEX_PREFIX+"Sentence\" USING GIN (to_tsvector('english', \"public\".\"V_Sentence\".\"name\"))";
			 s.execute(create);
		 }*/
    VertexLabel vl = this.sqlgGraph.getTopology().getVertexLabel("public", "Sentence").get();
    vl.ensureIndexExists(IndexType.getFullTextGIN("english"), Collections.singletonList(vl.getProperty("name").get()));
    this.sqlgGraph.tx().commit();
    List<Vertex> vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").has("name", FullText.fullTextMatch("english", "fat & rat")).toList();
    assertEquals(1, vts.size());
    assertTrue(vts.contains(v0));
    vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").has("name", FullText.fullTextMatch("english", "fat & cow")).toList();
    assertEquals(0, vts.size());
    vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").has("name", FullText.fullTextMatch("english", "fatal <-> error")).toList();
    assertEquals(1, vts.size());
    assertTrue(vts.contains(v1));
    vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").has("name", FullText.fullTextMatch("english", "fatal & error")).toList();
    assertEquals(2, vts.size());
    assertTrue(vts.contains(v1));
    assertTrue(vts.contains(v2));
    vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").has("name", FullText.fullTextMatch("english", true, "fatal error")).toList();
    assertEquals(2, vts.size());
    assertTrue(vts.contains(v1));
    assertTrue(vts.contains(v2));
    vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").has("name", FullText.fullTextMatch("english", "!cat")).toList();
    assertEquals(2, vts.size());
    assertTrue(vts.contains(v1));
    assertTrue(vts.contains(v2));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 8 with VertexLabel

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

the class TestBatchGlobalUniqueIndexes method testVertexUniqueConstraintUpdateNormalBatchMode.

@Test
public void testVertexUniqueConstraintUpdateNormalBatchMode() 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();
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "namea", "a");
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.tx().normalBatchModeOn();
    a1.property("namea", "aa");
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.tx().normalBatchModeOn();
    try {
        // this should pass
        this.sqlgGraph.addVertex(T.label, "A", "namea", "a");
        this.sqlgGraph.tx().commit();
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("GlobalUniqueIndex should not fire");
    }
    testVertexUniqueConstraintUpdateNormalBatchMode_assert(this.sqlgGraph);
    if (this.sqlgGraph1 != null) {
        Thread.sleep(1000);
        testVertexUniqueConstraintUpdateNormalBatchMode_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 9 with VertexLabel

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

the class TestBatchGlobalUniqueIndexes method testBatchModeGlobalUniqueIndexOnPropertyThatDoesNotYetExists.

@Test
public void testBatchModeGlobalUniqueIndexOnPropertyThatDoesNotYetExists() 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();
    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());
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A");
    this.sqlgGraph.tx().commit();
    try {
        this.sqlgGraph.tx().normalBatchModeOn();
        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 {
            this.sqlgGraph1.tx().normalBatchModeOn();
            a2 = this.sqlgGraph1.traversal().V(a2).next();
            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)

Example 10 with VertexLabel

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

the class SqlgVertex method load.

@Override
protected void load() {
    // new vertexes have no id, impossible to load, but then all its properties are already cached.
    if ((this.properties.isEmpty() && !this.sqlgGraph.tx().isInBatchMode()) || (this.properties.isEmpty() && this.sqlgGraph.getSqlDialect().supportsBatchMode() && this.sqlgGraph.tx().isInBatchMode() && !this.sqlgGraph.tx().getBatchManager().vertexIsCached(this))) {
        if (this.sqlgGraph.getSqlDialect().supportsBatchMode() && this.sqlgGraph.tx().isOpen() && this.sqlgGraph.tx().getBatchManager().isStreaming()) {
            throw new IllegalStateException("streaming is in progress, first flush or commit before querying.");
        }
        // Generate the columns to prevent 'ERROR: cached plan must not change result type" error'
        // This happens when the schema changes after the statement is prepared.
        @SuppressWarnings("OptionalGetWithoutIsPresent") VertexLabel vertexLabel = this.sqlgGraph.getTopology().getSchema(this.schema).get().getVertexLabel(this.table).get();
        StringBuilder sql = new StringBuilder("SELECT\n\t");
        sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes("ID"));
        for (PropertyColumn propertyColumn : vertexLabel.getProperties().values()) {
            sql.append(", ");
            sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(propertyColumn.getName()));
            // additional columns for time zone, etc.
            String[] ps = propertyColumn.getPropertyType().getPostFixes();
            if (ps != null) {
                for (String p : propertyColumn.getPropertyType().getPostFixes()) {
                    sql.append(", ");
                    sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(propertyColumn.getName() + p));
                }
            }
        }
        sql.append("\nFROM\n\t");
        sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(this.schema));
        sql.append(".");
        sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes(VERTEX_PREFIX + this.table));
        sql.append("\nWHERE\n\t");
        sql.append(this.sqlgGraph.getSqlDialect().maybeWrapInQoutes("ID"));
        sql.append(" = ?");
        if (this.sqlgGraph.getSqlDialect().needsSemicolon()) {
            sql.append(";");
        }
        Connection conn = this.sqlgGraph.tx().getConnection();
        if (logger.isDebugEnabled()) {
            logger.debug(sql.toString());
        }
        try (PreparedStatement preparedStatement = conn.prepareStatement(sql.toString())) {
            preparedStatement.setLong(1, this.recordId.getId());
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                loadResultSet(resultSet);
            } else {
                throw new IllegalStateException(String.format("Vertex with label %s and id %d does not exist.", new Object[] { this.schema + "." + this.table, this.recordId.getId() }));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel)

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