Search in sources :

Example 11 with SchemaManager

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

the class VertexCoreTest method testAddVertexWithCollectionIndex.

@Test
public void testAddVertexWithCollectionIndex() {
    SchemaManager schema = graph().schema();
    schema.propertyKey("tags").asText().valueSet().create();
    schema.propertyKey("category").asText().valueSet().create();
    schema.propertyKey("country").asText().create();
    schema.propertyKey("score").asInt().valueSet().create();
    schema.vertexLabel("soft").properties("name", "tags", "score", "country", "category").primaryKeys("name").create();
    schema.indexLabel("softByTag").onV("soft").by("tags").secondary().create();
    schema.indexLabel("softByScore").onV("soft").by("score").secondary().create();
    schema.indexLabel("softByCategory").onV("soft").by("category").search().create();
    HugeGraph graph = graph();
    graph.addVertex(T.label, "soft", "name", "hugegraph", "country", "china", "score", ImmutableList.of(5, 4, 3), "category", ImmutableList.of("graph database", "db"), "tags", ImmutableList.of("graphdb", "gremlin"));
    graph.addVertex(T.label, "soft", "name", "neo4j", "country", "usa", "score", ImmutableList.of(5, 4), "category", ImmutableList.of("graphdb", "database"), "tags", ImmutableList.of("graphdb", "cypher"));
    graph.addVertex(T.label, "soft", "name", "janusgraph", "country", "usa", "score", ImmutableList.of(5), "category", ImmutableList.of("hello graph", "graph database"), "tags", ImmutableList.of("graphdb", "gremlin"));
    // TODO: test mayCommitTx() after support textContains(collection, str)
    this.commitTx();
    List<Vertex> vertices;
    vertices = graph.traversal().V().has("soft", "category", ConditionP.textContains("hello database")).toList();
    Assert.assertEquals(3, vertices.size());
    vertices = graph.traversal().V().has("soft", "category", ConditionP.textContains("graph")).toList();
    Assert.assertEquals(2, vertices.size());
    Assert.assertThrows(IllegalStateException.class, () -> {
        graph.traversal().V().has("soft", "tags", "gremlin").toList();
    });
    // query by contains
    vertices = graph.traversal().V().has("soft", "tags", ConditionP.contains("gremlin")).toList();
    Assert.assertEquals(2, vertices.size());
    // secondary-index with list/set of number properties
    vertices = graph.traversal().V().has("soft", "score", ConditionP.contains(5)).toList();
    Assert.assertEquals(3, vertices.size());
    vertices = graph.traversal().V().has("soft", "name", "hugegraph").toList();
    Assert.assertEquals(1, vertices.size());
    // add a new tag
    Vertex vertex = vertices.get(0);
    vertex.property("tags", ImmutableSet.of("new_tag"));
    this.commitTx();
    vertices = graph.traversal().V().has("soft", "tags", ConditionP.contains("new_tag")).toList();
    Assert.assertEquals(1, vertices.size());
    // delete tag gremlin
    vertex = graph.addVertex(T.label, "soft", "name", "hugegraph", "country", "china", "score", ImmutableList.of(5, 4, 3), "category", ImmutableList.of("hello graph", "graph database"), "tags", ImmutableList.of("graphdb", "new_tag"));
    this.mayCommitTx();
    vertices = graph.traversal().V().has("soft", "tags", ConditionP.contains("gremlin")).toList();
    Assert.assertEquals(1, vertices.size());
    vertices = graph.traversal().V().has("soft", "tags", ConditionP.contains("new_tag")).toList();
    Assert.assertEquals(1, vertices.size());
    vertices = graph.traversal().V().has("soft", "tags", ConditionP.contains("graphdb")).toList();
    Assert.assertEquals(3, vertices.size());
    // remove
    vertex.remove();
    this.mayCommitTx();
    vertices = graph.traversal().V().has("soft", "tags", ConditionP.contains("new_tag")).toList();
    Assert.assertEquals(0, vertices.size());
    vertices = graph.traversal().V().has("soft", "tags", ConditionP.contains("graphdb")).toList();
    Assert.assertEquals(2, vertices.size());
}
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) Test(org.junit.Test)

Example 12 with SchemaManager

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

the class VertexCoreTest method testQueryVertexByAggregateProperty.

@Test
public void testQueryVertexByAggregateProperty() {
    Assume.assumeTrue("Not support aggregate property", storeFeatures().supportsAggregateProperty());
    HugeGraph graph = graph();
    SchemaManager schema = graph.schema();
    schema.propertyKey("worstScore").asInt().valueSingle().calcMin().ifNotExist().create();
    schema.propertyKey("bestScore").asInt().valueSingle().calcMax().ifNotExist().create();
    schema.propertyKey("testNum").asInt().valueSingle().calcSum().ifNotExist().create();
    schema.propertyKey("no").asText().valueSingle().calcOld().ifNotExist().create();
    schema.propertyKey("rank").asInt().valueSet().calcSet().ifNotExist().create();
    schema.propertyKey("reword").asInt().valueList().calcList().ifNotExist().create();
    schema.vertexLabel("student").properties("name", "worstScore", "bestScore", "testNum", "no", "rank", "reword").primaryKeys("name").nullableKeys("worstScore", "bestScore", "testNum", "no", "rank", "reword").ifNotExist().create();
    schema.indexLabel("studentByWorstScore").onV("student").by("worstScore").range().ifNotExist().create();
    schema.indexLabel("studentByBestScore").onV("student").by("bestScore").range().ifNotExist().create();
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("studentByTestNum").onV("student").by("testNum").range().ifNotExist().create();
    }, e -> {
        Assert.assertContains("The aggregate type SUM is not indexable", e.getMessage());
    });
    schema.indexLabel("studentByNo").onV("student").by("no").secondary().ifNotExist().create();
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("studentByRank").onV("student").by("rank").secondary().ifNotExist().create();
    }, e -> {
        Assert.assertTrue(e.getMessage(), e.getMessage().contains("The aggregate type SET is not indexable"));
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("studentByReword").onV("student").by("reword").secondary().ifNotExist().create();
    }, e -> {
        Assert.assertTrue(e.getMessage(), e.getMessage().contains("The aggregate type LIST is not indexable"));
    });
    graph.addVertex(T.label, "student", "name", "Tom", "worstScore", 55, "bestScore", 96, "testNum", 1, "no", "001");
    this.commitTx();
    List<Vertex> vertices = graph.traversal().V().hasLabel("student").has("name", "Tom").toList();
    Assert.assertEquals(1, vertices.size());
    Vertex tom = vertices.get(0);
    Assert.assertEquals(55, tom.value("worstScore"));
    Assert.assertEquals(96, tom.value("bestScore"));
    Assert.assertEquals(1, tom.value("testNum"));
    Assert.assertEquals("001", tom.value("no"));
    List<Vertex> results = graph.traversal().V().has("worstScore", P.gt(50)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("worstScore", P.lt(60)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("worstScore", 55).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("bestScore", P.gt(50)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("bestScore", P.lt(100)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("bestScore", 96).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("no", "001").toList();
    Assert.assertEquals(vertices, results);
    tom.property("worstScore", 45);
    tom.property("bestScore", 98);
    tom.property("testNum", 1);
    tom.property("no", "002");
    this.commitTx();
    vertices = graph.traversal().V().hasLabel("student").has("name", "Tom").toList();
    Assert.assertEquals(1, vertices.size());
    tom = vertices.get(0);
    Assert.assertEquals(45, tom.value("worstScore"));
    Assert.assertEquals(98, tom.value("bestScore"));
    Assert.assertEquals(2, tom.value("testNum"));
    Assert.assertEquals("001", tom.value("no"));
    results = graph.traversal().V().has("worstScore", P.gt(30)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("worstScore", P.lt(60)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("worstScore", 45).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("bestScore", P.gt(50)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("bestScore", P.lt(100)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("bestScore", 98).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("no", "001").toList();
    Assert.assertEquals(vertices, results);
    tom = graph.traversal().V().hasLabel("student").has("name", "Tom").next();
    tom.property("worstScore", 65);
    tom.property("bestScore", 99);
    tom.property("testNum", 1);
    tom.property("no", "003");
    this.commitTx();
    vertices = graph.traversal().V().hasLabel("student").has("name", "Tom").toList();
    Assert.assertEquals(1, vertices.size());
    tom = vertices.get(0);
    Assert.assertEquals(45, tom.value("worstScore"));
    Assert.assertEquals(99, tom.value("bestScore"));
    Assert.assertEquals(3, tom.value("testNum"));
    Assert.assertEquals("001", tom.value("no"));
    results = graph.traversal().V().has("worstScore", P.gt(30)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("worstScore", P.lt(60)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("worstScore", 45).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("bestScore", P.gt(50)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("bestScore", P.lt(100)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("bestScore", 99).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("no", "001").toList();
    Assert.assertEquals(vertices, results);
    tom = graph.traversal().V().hasLabel("student").has("name", "Tom").next();
    tom.property("worstScore", 75);
    tom.property("bestScore", 100);
    tom.property("testNum", 1);
    tom.property("no", "004");
    this.commitTx();
    vertices = graph.traversal().V().hasLabel("student").has("name", "Tom").toList();
    Assert.assertEquals(1, vertices.size());
    tom = vertices.get(0);
    Assert.assertEquals(45, tom.value("worstScore"));
    Assert.assertEquals(100, tom.value("bestScore"));
    Assert.assertEquals(4, tom.value("testNum"));
    Assert.assertEquals("001", tom.value("no"));
    results = graph.traversal().V().has("worstScore", P.gt(30)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("worstScore", P.lt(60)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("worstScore", 45).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("bestScore", P.gt(50)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("bestScore", P.lt(101)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("bestScore", 100).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("no", "001").toList();
    Assert.assertEquals(vertices, results);
    tom = graph.traversal().V().hasLabel("student").has("name", "Tom").next();
    tom.property("worstScore", 35);
    tom.property("bestScore", 99);
    tom.property("testNum", 1);
    tom.property("no", "005");
    this.commitTx();
    tom.property("worstScore", 65);
    tom.property("bestScore", 93);
    tom.property("testNum", 1);
    tom.property("no", "006");
    this.commitTx();
    tom.property("worstScore", 58);
    tom.property("bestScore", 63);
    tom.property("testNum", 1);
    tom.property("no", "007");
    this.commitTx();
    vertices = graph.traversal().V().hasLabel("student").has("name", "Tom").toList();
    Assert.assertEquals(1, vertices.size());
    tom = vertices.get(0);
    Assert.assertEquals(35, tom.value("worstScore"));
    Assert.assertEquals(100, tom.value("bestScore"));
    Assert.assertEquals(7, tom.value("testNum"));
    Assert.assertEquals("001", tom.value("no"));
    results = graph.traversal().V().has("worstScore", P.gt(30)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("worstScore", P.lt(60)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("worstScore", 35).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("bestScore", P.gt(50)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("bestScore", P.lt(101)).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("bestScore", 100).toList();
    Assert.assertEquals(vertices, results);
    results = graph.traversal().V().has("no", "001").toList();
    Assert.assertEquals(vertices, results);
}
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) Test(org.junit.Test)

Example 13 with SchemaManager

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

the class VertexCoreTest method testAddVertexWithCustomizeUuidIdStrategy.

@Test
public void testAddVertexWithCustomizeUuidIdStrategy() {
    HugeGraph graph = graph();
    SchemaManager schema = graph.schema();
    schema.vertexLabel("programmer").useCustomizeUuidId().properties("name", "age", "city").create();
    graph.addVertex(T.label, "programmer", T.id, "835e1153-9281-4957-8691-cf79258e90eb", "name", "marko", "age", 18, "city", "Beijing");
    graph.addVertex(T.label, "programmer", T.id, UUID.fromString("835e1153-9281-4957-8691-cf79258e90eb"), "name", "marko", "age", 18, "city", "Beijing");
    graph.addVertex(T.label, "programmer", T.id, "835e1153928149578691cf79258e90ee", "name", "marko", "age", 19, "city", "Beijing");
    this.commitTx();
    Assert.assertEquals(2L, graph.traversal().V().count().next());
    Object uuid = Text.uuid("835e1153928149578691cf79258e90eb");
    List<Vertex> vertices = graph.traversal().V(uuid).toList();
    Assert.assertEquals(1, vertices.size());
    Id id = (Id) vertices.get(0).id();
    Assert.assertEquals(IdType.UUID, id.type());
    Assert.assertEquals("835e1153-9281-4957-8691-cf79258e90eb", id.asString());
    assertContains(vertices, T.label, "programmer", "name", "marko", "age", 18, "city", "Beijing");
    uuid = Text.uuid("835e1153-9281-4957-8691-cf79258e90ee");
    vertices = graph.traversal().V(uuid).toList();
    Assert.assertEquals(1, vertices.size());
    id = (Id) vertices.get(0).id();
    Assert.assertEquals(IdType.UUID, id.type());
    Assert.assertEquals("835e1153-9281-4957-8691-cf79258e90ee", id.asString());
    assertContains(vertices, T.label, "programmer", "name", "marko", "age", 19, "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)

Example 14 with SchemaManager

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

the class VertexCoreTest method testQueryByDoublePropertyWithMaxMinValue.

@Test
public void testQueryByDoublePropertyWithMaxMinValue() {
    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 secondBiggest = 0x1.ffffffffffffeP+1023;
    final double secondSmallest = 0x0.0000000000002P-1022;
    graph().addVertex(T.label, "number", "id", 0, "double", 0.123456789012345678901d);
    graph().addVertex(T.label, "number", "id", 1, "double", Double.MAX_VALUE);
    graph().addVertex(T.label, "number", "id", 2, "double", -Double.MAX_VALUE);
    graph().addVertex(T.label, "number", "id", 3, "double", Double.MIN_VALUE);
    graph().addVertex(T.label, "number", "id", 4, "double", -Double.MIN_VALUE);
    graph().addVertex(T.label, "number", "id", 5, "double", secondBiggest);
    graph().addVertex(T.label, "number", "id", 6, "double", -secondBiggest);
    graph().addVertex(T.label, "number", "id", 7, "double", secondSmallest);
    graph().addVertex(T.label, "number", "id", 8, "double", -secondSmallest);
    this.mayCommitTx();
    List<Vertex> vertices = graph.traversal().V().hasLabel("number").has("double", 0.123456789012345678901d).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "number", "id", 0, "double", 0.123456789012345678901d);
    vertices = graph.traversal().V().hasLabel("number").has("double", Double.MAX_VALUE).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "number", "id", 1, "double", Double.MAX_VALUE);
    vertices = graph.traversal().V().hasLabel("number").has("double", -Double.MAX_VALUE).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "number", "id", 2, "double", -Double.MAX_VALUE);
    vertices = graph.traversal().V().hasLabel("number").has("double", Double.MIN_VALUE).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "number", "id", 3, "double", Double.MIN_VALUE);
    vertices = graph.traversal().V().hasLabel("number").has("double", -Double.MIN_VALUE).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "number", "id", 4, "double", -Double.MIN_VALUE);
    vertices = graph.traversal().V().hasLabel("number").has("double", secondBiggest).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "number", "id", 5, "double", secondBiggest);
    vertices = graph.traversal().V().hasLabel("number").has("double", -secondBiggest).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "number", "id", 6, "double", -secondBiggest);
    vertices = graph.traversal().V().hasLabel("number").has("double", secondSmallest).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "number", "id", 7, "double", secondSmallest);
    vertices = graph.traversal().V().hasLabel("number").has("double", -secondSmallest).toList();
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "number", "id", 8, "double", -secondSmallest);
}
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) Test(org.junit.Test)

Example 15 with SchemaManager

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

the class VertexCoreTest method testAddVertexWithCustomizeUuidIdStrategyWithoutValidId.

@Test
public void testAddVertexWithCustomizeUuidIdStrategyWithoutValidId() {
    HugeGraph graph = graph();
    SchemaManager schema = graph.schema();
    schema.vertexLabel("programmer").useCustomizeUuidId().properties("name", "age", "city").create();
    // Expect id, but no id
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        graph.addVertex(T.label, "programmer", "name", "marko", "age", 18, "city", "Beijing");
    });
    // Expect uuid id, but got number id
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        graph.addVertex(T.label, "programmer", T.id, 123456, "name", "marko", "age", 18, "city", "Beijing");
    });
    // Expect uuid id, but got invalid string id
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        graph.addVertex(T.label, "programmer", T.id, "123456", "name", "marko", "age", 18, "city", "Beijing");
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) SchemaManager(com.baidu.hugegraph.schema.SchemaManager) 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