Search in sources :

Example 1 with VertexLabel

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

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

the class SqlgGraph method createVertexLabeledIndex.

/**
 * @deprecated Please use {@link Topology#ensureVertexLabelExist(String, Map)} and {@link VertexLabel#ensureIndexExists(IndexType, List)}.
 */
@Deprecated
public void createVertexLabeledIndex(String label, Object... dummykeyValues) {
    Map<String, PropertyType> columns = SqlgUtil.transformToColumnDefinitionMap(dummykeyValues);
    SchemaTable schemaTablePair = SchemaTable.from(this, label);
    VertexLabel vertexLabel = this.getTopology().ensureVertexLabelExist(schemaTablePair.getSchema(), schemaTablePair.getTable(), columns);
    List<PropertyColumn> properties = new ArrayList<>();
    List<String> keys = SqlgUtil.transformToKeyList(dummykeyValues);
    for (String key : keys) {
        properties.add(vertexLabel.getProperty(key).get());
    }
    vertexLabel.ensureIndexExists(IndexType.NON_UNIQUE, properties);
}
Also used : VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) PropertyColumn(org.umlg.sqlg.structure.topology.PropertyColumn)

Example 3 with VertexLabel

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

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

the class TestOrStep method testOrFullText.

@Test
public void testOrFullText() {
    Assume.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");
    VertexLabel vl = this.sqlgGraph.getTopology().getVertexLabel("public", "Sentence").get();
    vl.ensureIndexExists(IndexType.getFullTextGIN("english"), Collections.singletonList(vl.getProperty("name").get()));
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("Sentence").or(__.has("name", FullText.fullTextMatch("english", "fat & rat")), __.has("name", "fatal error"));
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertEquals(2, vertices.size());
    Assert.assertTrue(vertices.containsAll(Arrays.asList(v0, v1)));
    traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal().V().hasLabel("Sentence").or(__.has("name", FullText.fullTextMatch("english", "fat & cow")), __.has("name", "fatal error"));
    vertices = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertEquals(1, vertices.size());
    Assert.assertTrue(vertices.containsAll(Arrays.asList(v1)));
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DefaultGraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal) VertexLabel(org.umlg.sqlg.structure.topology.VertexLabel) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 5 with VertexLabel

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

the class TestGremlinCompileFullTextPredicate method testDocExamplesWhere.

@Test
public void testDocExamplesWhere() 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");
    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").where(FullText.fullTextMatch("english", false, "name", "fat & rat")).toList();
    assertEquals(1, vts.size());
    assertTrue(vts.contains(v0));
    vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").where(FullText.fullTextMatch("english", false, "name", "fat & cow")).toList();
    assertEquals(0, vts.size());
    vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").where(FullText.fullTextMatch("english", false, "name", "fatal <-> error")).toList();
    assertEquals(1, vts.size());
    assertTrue(vts.contains(v1));
    vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").where(FullText.fullTextMatch("english", false, "name", "fatal & error")).toList();
    assertEquals(2, vts.size());
    assertTrue(vts.contains(v1));
    assertTrue(vts.contains(v2));
    vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").as("a").where("a", FullText.fullTextMatch("english", true, "name", "fatal error")).toList();
    assertEquals(2, vts.size());
    assertTrue(vts.contains(v1));
    assertTrue(vts.contains(v2));
    vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").as("a").where("a", FullText.fullTextMatch("english", false, "name", "!cat")).toList();
    assertEquals(2, vts.size());
    assertTrue(vts.contains(v1));
    assertTrue(vts.contains(v2));
    /*vts=this.sqlgGraph.traversal().V().hasLabel("Sentence").or(
				 __.where(FullText.fullTextMatch("english",false,"name", "fat & rat")),
				 __.where(FullText.fullTextMatch("english",false,"name", "fatal <-> error"))
				 ).toList();
		 assertEquals(2,vts.size());
		 assertTrue(vts.contains(v0));
		 assertTrue(vts.contains(v1));*/
    v2.addEdge("testEdge", v0);
    this.sqlgGraph.tx().commit();
    vts = this.sqlgGraph.traversal().V().hasLabel("Sentence").has("name", "error is not fatal").out("testEdge").where(FullText.fullTextMatch("english", false, "name", "fat & rat")).toList();
    assertEquals(1, vts.size());
    assertTrue(vts.contains(v0));
}
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)

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