Search in sources :

Example 1 with SchemaManager

use of com.baidu.hugegraph.schema.SchemaManager in project incubator-hugegraph by apache.

the class SchemaDefine method createPropertyKey.

protected String createPropertyKey(String name, DataType dataType, Cardinality cardinality) {
    SchemaManager schema = this.schema();
    PropertyKey propertyKey = schema.propertyKey(name).dataType(dataType).cardinality(cardinality).build();
    this.graph.schemaTransaction().addPropertyKey(propertyKey);
    return name;
}
Also used : SchemaManager(com.baidu.hugegraph.schema.SchemaManager) PropertyKey(com.baidu.hugegraph.schema.PropertyKey)

Example 2 with SchemaManager

use of com.baidu.hugegraph.schema.SchemaManager in project incubator-hugegraph by apache.

the class VertexCoreTest method testQueryByDoubleProperty.

@Test
public void testQueryByDoubleProperty() {
    HugeGraph graph = graph();
    SchemaManager schema = graph.schema();
    schema.propertyKey("double").asDouble().create();
    schema.vertexLabel("number").primaryKeys("id").properties("id", "double").create();
    schema.indexLabel("numberByDouble").range().onV("number").by("double").create();
    final double max7 = Double.valueOf(String.valueOf(Float.MAX_VALUE));
    /*
         * The double precision type typically has a range of around 1E-307 to
         * 1E+308 with a precision of at least 15 digits. (postgresql)
         * https://www.postgresql.org/docs/9.5/datatype-numeric.html#DATATYPE-NUMERIC-TABLE
         */
    final double max15 = new BigDecimal(Double.MAX_VALUE).movePointLeft(308).setScale(15, BigDecimal.ROUND_DOWN).movePointRight(308).doubleValue();
    final double min15 = new BigDecimal(1.234567890987654321d).setScale(15, BigDecimal.ROUND_DOWN).movePointLeft(307).doubleValue();
    graph().addVertex(T.label, "number", "id", 1, "double", 7);
    graph().addVertex(T.label, "number", "id", 2, "double", 3.14f);
    graph().addVertex(T.label, "number", "id", 3, "double", Math.PI);
    graph().addVertex(T.label, "number", "id", 4, "double", // 12345678901234.566
    12345678901234.567d);
    graph().addVertex(T.label, "number", "id", 5, "double", max7);
    graph().addVertex(T.label, "number", "id", 6, "double", -max7);
    graph().addVertex(T.label, "number", "id", 7, "double", max15);
    graph().addVertex(T.label, "number", "id", 8, "double", -max15);
    graph().addVertex(T.label, "number", "id", 9, "double", min15);
    graph().addVertex(T.label, "number", "id", 10, "double", -min15);
    this.mayCommitTx();
    List<Vertex> vertices = graph.traversal().V().hasLabel("number").has("double", 7).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "number", "id", 1, "double", 7d);
    vertices = graph.traversal().V().hasLabel("number").has("double", 3.14f).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "number", "id", 2, "double", 3.14d);
    vertices = graph.traversal().V().hasLabel("number").has("double", Math.PI).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "number", "id", 3, "double", Math.PI);
    vertices = graph.traversal().V().hasLabel("number").has("double", 12345678901234.567d).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "number", "id", 4, "double", 12345678901234.567d);
    vertices = graph.traversal().V().hasLabel("number").has("double", Float.MAX_VALUE).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "number", "id", 5, "double", max7);
    vertices = graph.traversal().V().hasLabel("number").has("double", -Float.MAX_VALUE).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "number", "id", 6, "double", -max7);
    vertices = graph.traversal().V().hasLabel("number").has("double", max15).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "number", "id", 7, "double", max15);
    vertices = graph.traversal().V().hasLabel("number").has("double", -max15).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "number", "id", 8, -max15);
    vertices = graph.traversal().V().hasLabel("number").has("double", min15).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "number", "id", 9, "double", min15);
    vertices = graph.traversal().V().hasLabel("number").has("double", -min15).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "number", "id", 10, -min15);
}
Also used : FakeVertex(com.baidu.hugegraph.testutil.FakeObjects.FakeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) SchemaManager(com.baidu.hugegraph.schema.SchemaManager) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 3 with SchemaManager

use of com.baidu.hugegraph.schema.SchemaManager in project incubator-hugegraph by apache.

the class VertexCoreTest method testAddVertexWithInvalidPropertyValueOfDouble.

@Test
public void testAddVertexWithInvalidPropertyValueOfDouble() {
    HugeGraph graph = graph();
    SchemaManager schema = graph.schema();
    schema.propertyKey("double").asDouble().create();
    schema.vertexLabel("number").properties("double").create();
    BigDecimal two = new BigDecimal(2);
    BigDecimal value = new BigDecimal(Double.MIN_VALUE).divide(two);
    double dvalue = graph.addVertex(T.label, "number", "double", value).value("double");
    Assert.assertEquals(0.0d, dvalue, 0.0d);
    value = new BigDecimal(-Double.MIN_VALUE).divide(two);
    dvalue = graph.addVertex(T.label, "number", "double", value).value("double");
    Assert.assertEquals(0.0d, dvalue, 0.0d);
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) SchemaManager(com.baidu.hugegraph.schema.SchemaManager) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 4 with SchemaManager

use of com.baidu.hugegraph.schema.SchemaManager in project incubator-hugegraph by apache.

the class VertexCoreTest method testQueryOlapRangeAndRegularSecondaryProperties.

@Test
public void testQueryOlapRangeAndRegularSecondaryProperties() {
    Assume.assumeTrue("Not support olap properties", storeFeatures().supportsOlapProperties());
    HugeGraph graph = graph();
    SchemaManager schema = graph.schema();
    schema.indexLabel("authorByAge").onV("author").range().by("age").create();
    schema.indexLabel("authorByLived").onV("author").secondary().by("lived").create();
    schema.propertyKey("pagerank").asDouble().valueSingle().writeType(WriteType.OLAP_RANGE).ifNotExist().create();
    schema.propertyKey("wcc").asText().valueSingle().writeType(WriteType.OLAP_SECONDARY).ifNotExist().create();
    this.init10VerticesAndCommit();
    String author = graph.vertexLabel("author").id().asString();
    Id id1 = SplicingIdGenerator.splicing(author, LongEncoding.encodeNumber(1));
    Id id2 = SplicingIdGenerator.splicing(author, LongEncoding.encodeNumber(2));
    graph.addVertex(T.id, id1.asObject(), "pagerank", 0.1D);
    graph.addVertex(T.id, id2.asObject(), "pagerank", 0.2D);
    this.commitTx();
    graph.addVertex(T.id, id1.asObject(), "wcc", "a");
    graph.addVertex(T.id, id2.asObject(), "wcc", "b");
    this.commitTx();
    Assert.assertEquals(GraphReadMode.OLTP_ONLY, graph.readMode());
    Assert.assertThrows(NotAllowException.class, () -> {
        graph.traversal().V().has("pagerank", 0.1D).has("lived", "Canadian").hasNext();
    }, e -> {
        Assert.assertContains("Not allowed to query by olap property key", e.getMessage());
    });
    Assert.assertEquals(GraphReadMode.OLTP_ONLY, graph.readMode());
    Assert.assertThrows(NotAllowException.class, () -> {
        graph.traversal().V().has("wcc", "a").has("age", 62).hasNext();
    }, e -> {
        Assert.assertContains("Not allowed to query by olap property key", e.getMessage());
    });
    Assert.assertEquals(GraphReadMode.OLTP_ONLY, graph.readMode());
    Assert.assertThrows(NotAllowException.class, () -> {
        graph.traversal().V().has("pagerank", 0.1D).has("age", 62).hasNext();
    }, e -> {
        Assert.assertContains("Not allowed to query by olap property key", e.getMessage());
    });
    graph.readMode(GraphReadMode.ALL);
    List<Vertex> vertices = graph.traversal().V().has("pagerank", 0.1D).has("lived", "Canadian").toList();
    Assert.assertEquals(1, vertices.size());
    Assert.assertEquals(graph.traversal().V(id1).next(), vertices.get(0));
    vertices = graph.traversal().V().has("pagerank", P.lte(0.1D)).has("age", P.gte(62)).toList();
    Assert.assertEquals(1, vertices.size());
    Assert.assertEquals(graph.traversal().V(id1).next(), vertices.get(0));
    vertices = graph.traversal().V().has("pagerank", P.lte(0.1D)).has("age", P.gte(62)).has("lived", "Canadian").toList();
    Assert.assertEquals(1, vertices.size());
    Assert.assertEquals(graph.traversal().V(id1).next(), vertices.get(0));
    vertices = graph.traversal().V().has("age", P.gt(5)).has("wcc", "b").toList();
    Assert.assertEquals(1, vertices.size());
    Assert.assertEquals(graph.traversal().V(id2).next(), vertices.get(0));
    vertices = graph.traversal().V().has("pagerank", P.gte(0.1D)).has("age", P.gte(10)).toList();
    Assert.assertEquals(2, vertices.size());
    Assert.assertTrue(vertices.contains(graph.traversal().V(id1).next()));
    Assert.assertTrue(vertices.contains(graph.traversal().V(id2).next()));
    Set<Vertex> vertexSet = graph.traversal().V().has("pagerank", P.lte(0.9D)).has("wcc", P.within("a", "b")).has("age", P.gt(20)).has("lived", P.within("Canadian", "California")).toSet();
    Assert.assertEquals(2, vertices.size());
    Assert.assertTrue(vertexSet.contains(graph.traversal().V(id1).next()));
    Assert.assertTrue(vertexSet.contains(graph.traversal().V(id2).next()));
    graph.readMode(GraphReadMode.OLTP_ONLY);
}
Also used : FakeVertex(com.baidu.hugegraph.testutil.FakeObjects.FakeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) SchemaManager(com.baidu.hugegraph.schema.SchemaManager) Id(com.baidu.hugegraph.backend.id.Id) Test(org.junit.Test)

Example 5 with SchemaManager

use of com.baidu.hugegraph.schema.SchemaManager in project incubator-hugegraph by apache.

the class VertexCoreTest method testAddOlapNoneProperties.

@Test
public void testAddOlapNoneProperties() {
    Assume.assumeTrue("Not support olap properties", storeFeatures().supportsOlapProperties());
    HugeGraph graph = graph();
    SchemaManager schema = graph.schema();
    String olapPropName = "olap";
    schema.propertyKey(olapPropName).asText().valueSingle().writeType(WriteType.OLAP_COMMON).ifNotExist().create();
    this.init10VerticesAndCommit();
    String author = graph.vertexLabel("author").id().asString();
    Id id1 = SplicingIdGenerator.splicing(author, LongEncoding.encodeNumber(1));
    Id id2 = SplicingIdGenerator.splicing(author, LongEncoding.encodeNumber(2));
    String language = graph.vertexLabel("language").id().asString();
    Id id3 = SplicingIdGenerator.splicing(language, "java");
    Id id4 = SplicingIdGenerator.splicing(language, "c++");
    Id id5 = SplicingIdGenerator.splicing(language, "python");
    String book = graph.vertexLabel("book").id().asString();
    Id id6 = SplicingIdGenerator.splicing(book, "java-1");
    Id id7 = SplicingIdGenerator.splicing(book, "java-2");
    Id id8 = SplicingIdGenerator.splicing(book, "java-3");
    Id id9 = SplicingIdGenerator.splicing(book, "java-4");
    Id id10 = SplicingIdGenerator.splicing(book, "java-5");
    graph.addVertex(T.id, id1.asObject(), olapPropName, "a");
    graph.addVertex(T.id, id2.asObject(), olapPropName, "b");
    graph.addVertex(T.id, id3.asObject(), olapPropName, "c");
    graph.addVertex(T.id, id4.asObject(), olapPropName, "d");
    graph.addVertex(T.id, id5.asObject(), olapPropName, "e");
    graph.addVertex(T.id, id6.asObject(), olapPropName, "f");
    graph.addVertex(T.id, id7.asObject(), olapPropName, "g");
    graph.addVertex(T.id, id8.asObject(), olapPropName, "h");
    graph.addVertex(T.id, id9.asObject(), olapPropName, "i");
    graph.addVertex(T.id, id10.asObject(), olapPropName, "j");
    this.commitTx();
    Assert.assertEquals(GraphReadMode.OLTP_ONLY, graph.readMode());
    Assert.assertThrows(NotAllowException.class, () -> {
        graph.traversal().V().has(olapPropName, "a").hasNext();
    }, e -> {
        Assert.assertContains("Not allowed to query by olap property key", e.getMessage());
    });
    Assert.assertEquals(GraphReadMode.OLTP_ONLY, graph.readMode());
    Assert.assertThrows(NotAllowException.class, () -> {
        graph.traversal().V().has(olapPropName, "c").hasNext();
    }, e -> {
        Assert.assertContains("Not allowed to query by olap property key", e.getMessage());
    });
    Assert.assertEquals(GraphReadMode.OLTP_ONLY, graph.readMode());
    Assert.assertThrows(NotAllowException.class, () -> {
        graph.traversal().V().has(olapPropName, "f").hasNext();
    }, e -> {
        Assert.assertContains("Not allowed to query by olap property key", e.getMessage());
    });
    graph.readMode(GraphReadMode.ALL);
    Assert.assertThrows(NoIndexException.class, () -> {
        graph.traversal().V().has(olapPropName, "a").hasNext();
    });
    Assert.assertThrows(NoIndexException.class, () -> {
        graph.traversal().V().has(olapPropName, "c").hasNext();
    });
    Assert.assertThrows(NoIndexException.class, () -> {
        graph.traversal().V().has(olapPropName, "f").hasNext();
    });
    graph.readMode(GraphReadMode.OLTP_ONLY);
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) SchemaManager(com.baidu.hugegraph.schema.SchemaManager) Id(com.baidu.hugegraph.backend.id.Id) Test(org.junit.Test)

Aggregations

SchemaManager (com.baidu.hugegraph.schema.SchemaManager)261 Test (org.junit.Test)227 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)90 HugeGraph (com.baidu.hugegraph.HugeGraph)76 FakeVertex (com.baidu.hugegraph.testutil.FakeObjects.FakeVertex)40 Edge (org.apache.tinkerpop.gremlin.structure.Edge)30 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)27 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)22 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)20 EdgeLabel (com.baidu.hugegraph.schema.EdgeLabel)19 FakeEdge (com.baidu.hugegraph.testutil.FakeObjects.FakeEdge)19 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)15 Id (com.baidu.hugegraph.backend.id.Id)12 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)8 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)7 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)7 Date (java.util.Date)6 Before (org.junit.Before)6 HashSet (java.util.HashSet)3 HugeException (com.baidu.hugegraph.HugeException)2