Search in sources :

Example 31 with SchemaManager

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

the class VertexCoreTest method testAddVerticesWithUniqueIndexForNullableProperties.

@Test
public void testAddVerticesWithUniqueIndexForNullableProperties() {
    SchemaManager schema = graph().schema();
    schema.vertexLabel("user").properties("name", "city", "age").nullableKeys("name", "city", "age").create();
    schema.indexLabel("userByNameCityAge").onV("user").by("name", "city", "age").unique().create();
    graph().addVertex(T.label, "user", "name", "Tom", "city", "Beijing", "age", 18);
    this.commitTx();
    // Nullable properties
    graph().addVertex(T.label, "user", "name", "Tom", "city", "Beijing");
    this.commitTx();
    graph().addVertex(T.label, "user", "name", "Tom", "age", 18);
    this.commitTx();
    graph().addVertex(T.label, "user", "city", "Beijing", "age", 18);
    this.commitTx();
    graph().addVertex(T.label, "user", "name", "Tom");
    this.commitTx();
    graph().addVertex(T.label, "user", "age", 18);
    this.commitTx();
    graph().addVertex(T.label, "user", "city", "Beijing");
    this.commitTx();
    graph().addVertex(T.label, "user");
    this.commitTx();
    // Empty String properties
    graph().addVertex(T.label, "user", "name", "", "city", "", "age", 18);
    this.commitTx();
    graph().addVertex(T.label, "user", "name", "", "city", "");
    this.commitTx();
    graph().addVertex(T.label, "user", "name", "", "age", 18);
    this.commitTx();
    graph().addVertex(T.label, "user", "city", "", "age", 18);
    this.commitTx();
    graph().addVertex(T.label, "user", "name", "");
    this.commitTx();
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        graph().addVertex(T.label, "user", "age", 18);
        this.commitTx();
    }, e -> {
        String message = e.getMessage();
        Assert.assertTrue(message.contains("Unique constraint " + "userByNameCityAge"));
        Assert.assertTrue(message.contains("conflict is found"));
    });
    graph().addVertex(T.label, "user", "city", "");
    this.commitTx();
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        graph().addVertex(T.label, "user");
        this.commitTx();
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        graph().addVertex(T.label, "user", "city", "\u0001");
        this.commitTx();
    });
}
Also used : SchemaManager(com.baidu.hugegraph.schema.SchemaManager) Test(org.junit.Test)

Example 32 with SchemaManager

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

the class VertexCoreTest method testAddOlapRangeProperties.

@Test
public void testAddOlapRangeProperties() {
    Assume.assumeTrue("Not support olap properties", storeFeatures().supportsOlapProperties());
    HugeGraph graph = graph();
    SchemaManager schema = graph.schema();
    String olapPropName = "pagerank";
    schema.propertyKey(olapPropName).asDouble().valueSingle().writeType(WriteType.OLAP_RANGE).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, 0.1D);
    graph.addVertex(T.id, id2.asObject(), olapPropName, 0.2D);
    graph.addVertex(T.id, id3.asObject(), olapPropName, 0.3D);
    graph.addVertex(T.id, id4.asObject(), olapPropName, 0.4D);
    graph.addVertex(T.id, id5.asObject(), olapPropName, 0.5D);
    graph.addVertex(T.id, id6.asObject(), olapPropName, 0.6D);
    graph.addVertex(T.id, id7.asObject(), olapPropName, 0.7D);
    graph.addVertex(T.id, id8.asObject(), olapPropName, 0.8D);
    graph.addVertex(T.id, id9.asObject(), olapPropName, 0.9D);
    graph.addVertex(T.id, id10.asObject(), olapPropName, 1.0D);
    this.commitTx();
    Assert.assertEquals(GraphReadMode.OLTP_ONLY, graph.readMode());
    Assert.assertThrows(NotAllowException.class, () -> {
        graph.traversal().V().has(olapPropName, 0.1D).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, 0.3D).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, 0.6D).hasNext();
    }, e -> {
        Assert.assertContains("Not allowed to query by olap property key", e.getMessage());
    });
    graph.traversal().V(id1).next();
    graph.readMode(GraphReadMode.ALL);
    List<Vertex> vertices = graph.traversal().V().has(olapPropName, 0.1D).toList();
    Assert.assertEquals(1, vertices.size());
    Assert.assertEquals(graph.traversal().V(id1).next(), vertices.get(0));
    vertices = graph.traversal().V().has(olapPropName, 0.3D).toList();
    Assert.assertEquals(1, vertices.size());
    Assert.assertEquals(graph.traversal().V(id3).next(), vertices.get(0));
    vertices = graph.traversal().V().has(olapPropName, 0.6D).toList();
    Assert.assertEquals(1, vertices.size());
    Assert.assertEquals(graph.traversal().V(id6).next(), vertices.get(0));
    vertices = graph.traversal().V().has(olapPropName, P.gt(0.9D)).toList();
    Assert.assertEquals(1, vertices.size());
    Assert.assertEquals(graph.traversal().V(id10).next(), vertices.get(0));
    vertices = graph.traversal().V().has(olapPropName, P.lt(0.2D)).toList();
    Assert.assertEquals(1, vertices.size());
    Assert.assertEquals(graph.traversal().V(id1).next(), vertices.get(0));
    vertices = graph.traversal().V().has(olapPropName, P.gte(0.9D)).toList();
    Assert.assertEquals(2, vertices.size());
    Assert.assertTrue(vertices.contains(graph.traversal().V(id9).next()));
    Assert.assertTrue(vertices.contains(graph.traversal().V(id10).next()));
    vertices = graph.traversal().V().has(olapPropName, P.lte(0.2D)).toList();
    Assert.assertEquals(2, vertices.size());
    Assert.assertTrue(vertices.contains(graph.traversal().V(id1).next()));
    Assert.assertTrue(vertices.contains(graph.traversal().V(id2).next()));
    vertices = graph.traversal().V().has(olapPropName, P.inside(0.2D, 0.9D)).toList();
    Assert.assertEquals(6, vertices.size());
    Assert.assertTrue(vertices.contains(graph.traversal().V(id3).next()));
    Assert.assertTrue(vertices.contains(graph.traversal().V(id4).next()));
    Assert.assertTrue(vertices.contains(graph.traversal().V(id5).next()));
    Assert.assertTrue(vertices.contains(graph.traversal().V(id6).next()));
    Assert.assertTrue(vertices.contains(graph.traversal().V(id7).next()));
    Assert.assertTrue(vertices.contains(graph.traversal().V(id8).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 33 with SchemaManager

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

the class VertexCoreTest method testQueryByJointIndexesWithCompositeIndexIncludeOtherField.

@Test
public void testQueryByJointIndexesWithCompositeIndexIncludeOtherField() {
    SchemaManager schema = graph().schema();
    schema.vertexLabel("dog").properties("name", "age", "city").nullableKeys("age").create();
    schema.indexLabel("dogByAge").onV("dog").range().by("age").create();
    schema.indexLabel("dogByCityAndName").onV("dog").secondary().by("city", "name").create();
    graph().addVertex(T.label, "dog", "name", "Tom", "city", "Hongkong", "age", 3);
    this.mayCommitTx();
    List<Vertex> vertices = graph().traversal().V().has("age", P.gt(2)).has("city", "Hongkong").toList();
    Assert.assertEquals(1, vertices.size());
}
Also used : FakeVertex(com.baidu.hugegraph.testutil.FakeObjects.FakeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SchemaManager(com.baidu.hugegraph.schema.SchemaManager) Test(org.junit.Test)

Example 34 with SchemaManager

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

the class VertexCoreTest method testAddVertexWithUniqueIndex.

@Test
public void testAddVertexWithUniqueIndex() {
    SchemaManager schema = graph().schema();
    schema.vertexLabel("user").properties("name").create();
    schema.indexLabel("userByName").onV("user").by("name").unique().create();
    Vertex v = graph().addVertex(T.label, "user", "name", "Tom");
    this.commitTx();
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        graph().addVertex(T.label, "user", "name", "Tom");
        this.commitTx();
    });
    v.remove();
    graph().addVertex(T.label, "user", "name", "Tom");
    this.commitTx();
}
Also used : FakeVertex(com.baidu.hugegraph.testutil.FakeObjects.FakeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SchemaManager(com.baidu.hugegraph.schema.SchemaManager) Test(org.junit.Test)

Example 35 with SchemaManager

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

the class VertexCoreTest method testAddVertexWithCustomizeStringIdStrategy.

@Test
public void testAddVertexWithCustomizeStringIdStrategy() {
    HugeGraph graph = graph();
    SchemaManager schema = graph.schema();
    schema.vertexLabel("programmer").useCustomizeStringId().properties("name", "age", "city").create();
    graph.addVertex(T.label, "programmer", T.id, "123456", "name", "marko", "age", 18, "city", "Beijing");
    this.mayCommitTx();
    List<Vertex> vertices = graph.traversal().V("123456").toList();
    Assert.assertEquals(1, vertices.size());
    Id id = (Id) vertices.get(0).id();
    Assert.assertEquals(IdType.STRING, id.type());
    Assert.assertEquals("123456", id.asString());
    assertContains(vertices, T.label, "programmer", "name", "marko", "age", 18, "city", "Beijing");
}
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)

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