Search in sources :

Example 81 with SchemaManager

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

the class EdgeCoreTest method testQueryByNegativeFloatProperty.

@Test
public void testQueryByNegativeFloatProperty() {
    HugeGraph graph = graph();
    SchemaManager schema = graph.schema();
    schema.indexLabel("transferByAmount").onE("transfer").range().by("amount").create();
    Vertex louise = graph.addVertex(T.label, "person", "name", "Louise", "city", "Beijing", "age", 21);
    Vertex sean = graph.addVertex(T.label, "person", "name", "Sean", "city", "Beijing", "age", 23);
    louise.addEdge("transfer", sean, "id", 1, "amount", 500.00F, "timestamp", 1L, "message", "Happy birthday!");
    louise.addEdge("transfer", sean, "id", 2, "amount", -1234.56F, "timestamp", -100L, "message", "Happy birthday!");
    graph.tx().commit();
    List<Edge> edges = graph.traversal().E().has("amount", -1234.56F).toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(IdGenerator.of(2), edges.get(0).value("id"));
    edges = graph.traversal().E().has("amount", P.between(-1235F, 0L)).toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(IdGenerator.of(2), edges.get(0).value("id"));
    edges = graph.traversal().E().has("amount", P.gt(-1235F)).toList();
    Assert.assertEquals(2, edges.size());
    edges = graph.traversal().E().has("amount", P.gte(-1234.56F)).toList();
    Assert.assertEquals(2, edges.size());
    edges = graph.traversal().E().has("amount", P.gt(-1234.56F)).toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(IdGenerator.of(1), edges.get(0).value("id"));
    edges = graph.traversal().E().has("amount", P.gt(-1234.56F)).toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(IdGenerator.of(1), edges.get(0).value("id"));
    edges = graph.traversal().E().has("amount", P.lt(-1234.56F)).toList();
    Assert.assertEquals(0, edges.size());
    edges = graph.traversal().E().has("amount", P.lte(-1234.56F)).toList();
    Assert.assertEquals(1, edges.size());
    edges = graph.traversal().E().has("amount", P.lt(0F)).toList();
    Assert.assertEquals(1, edges.size());
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) SchemaManager(com.baidu.hugegraph.schema.SchemaManager) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) FakeEdge(com.baidu.hugegraph.testutil.FakeObjects.FakeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Test(org.junit.Test)

Example 82 with SchemaManager

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

the class EdgeCoreTest method testQueryOutEdgesOfVertexBySortkeyWithRange.

@Test
public void testQueryOutEdgesOfVertexBySortkeyWithRange() {
    HugeGraph graph = graph();
    SchemaManager schema = graph.schema();
    schema.propertyKey("no").asText().create();
    schema.propertyKey("calltime").asDate().create();
    schema.vertexLabel("phone").properties("no").primaryKeys("no").enableLabelIndex(false).create();
    schema.edgeLabel("call").multiTimes().properties("calltime").sourceLabel("phone").targetLabel("phone").sortKeys("calltime").create();
    Vertex v1 = graph.addVertex(T.label, "phone", "no", "13812345678");
    Vertex v2 = graph.addVertex(T.label, "phone", "no", "13866668888");
    Vertex v10086 = graph.addVertex(T.label, "phone", "no", "10086");
    v1.addEdge("call", v2, "calltime", "2017-5-1 23:00:00");
    v1.addEdge("call", v2, "calltime", "2017-5-2 12:00:01");
    v1.addEdge("call", v2, "calltime", "2017-5-3 12:08:02");
    v1.addEdge("call", v2, "calltime", "2017-5-3 22:22:03");
    v1.addEdge("call", v2, "calltime", "2017-5-4 20:33:04");
    v1.addEdge("call", v10086, "calltime", "2017-5-2 15:30:05");
    v1.addEdge("call", v10086, "calltime", "2017-5-3 14:56:06");
    v2.addEdge("call", v10086, "calltime", "2017-5-3 17:28:07");
    graph.tx().commit();
    Assert.assertEquals(8, graph.traversal().E().toList().size());
    List<Edge> edges = graph.traversal().V(v1).outE("call").has("calltime", "2017-5-3 12:08:02").toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(Utils.date("2017-5-3 12:08:02"), edges.get(0).value("calltime"));
    edges = graph.traversal().V(v1).outE("call").has("calltime", P.lt("2017-5-2")).toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(Utils.date("2017-5-1 23:00:00"), edges.get(0).value("calltime"));
    edges = graph.traversal().V(v1).outE("call").has("calltime", P.gte("2017-5-3")).toList();
    Assert.assertEquals(4, edges.size());
    Assert.assertEquals(Utils.date("2017-5-3 12:08:02"), edges.get(0).value("calltime"));
    Assert.assertEquals(Utils.date("2017-5-3 14:56:06"), edges.get(1).value("calltime"));
    Assert.assertEquals(Utils.date("2017-5-3 22:22:03"), edges.get(2).value("calltime"));
    Assert.assertEquals(Utils.date("2017-5-4 20:33:04"), edges.get(3).value("calltime"));
    edges = graph.traversal().V(v1).outE("call").has("calltime", P.gte("2017-5-3")).where(__.otherV().hasId(v2.id())).toList();
    Assert.assertEquals(3, edges.size());
    edges = graph.traversal().V(v1).outE("call").has("calltime", P.between("2017-5-2", "2017-5-4")).toList();
    Assert.assertEquals(5, edges.size());
    edges = graph.traversal().V(v1).outE("call").has("calltime", P.between("2017-5-2", "2017-5-4")).where(__.not(__.otherV().hasId((v10086.id())))).toList();
    Assert.assertEquals(3, edges.size());
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) SchemaManager(com.baidu.hugegraph.schema.SchemaManager) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) FakeEdge(com.baidu.hugegraph.testutil.FakeObjects.FakeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Test(org.junit.Test)

Example 83 with SchemaManager

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

the class IndexLabelCoreTest method testAddIndexLabelOfVertex.

@Test
public void testAddIndexLabelOfVertex() {
    super.initPropertyKeys();
    SchemaManager schema = graph().schema();
    schema.propertyKey("born").asDate().ifNotExist().create();
    schema.propertyKey("fans").asLong().ifNotExist().create();
    schema.propertyKey("height").asFloat().ifNotExist().create();
    schema.propertyKey("idNo").asText().ifNotExist().create();
    schema.propertyKey("category").asText().valueSet().ifNotExist().create();
    schema.propertyKey("score").asInt().valueSet().ifNotExist().create();
    schema.vertexLabel("person").properties("id", "name", "age", "city", "born", "tags", "category", "score", "fans", "height", "weight", "idNo").primaryKeys("id").create();
    schema.indexLabel("personByName").onV("person").secondary().by("name").create();
    schema.indexLabel("personByCity").onV("person").search().by("city").create();
    schema.indexLabel("personByAge").onV("person").range().by("age").create();
    schema.indexLabel("personByBorn").onV("person").range().by("born").create();
    schema.indexLabel("personByFans").onV("person").range().by("fans").create();
    schema.indexLabel("personByHeight").onV("person").range().by("height").create();
    schema.indexLabel("personByWeight").onV("person").range().by("weight").create();
    schema.indexLabel("personByIdNo").onV("person").unique().by("idNo").create();
    schema.indexLabel("personByTags").onV("person").secondary().by("tags").create();
    schema.indexLabel("personByCategory").onV("person").search().by("category").create();
    schema.indexLabel("personByScore").onV("person").secondary().by("score").create();
    VertexLabel person = schema.getVertexLabel("person");
    IndexLabel personByName = schema.getIndexLabel("personByName");
    IndexLabel personByCity = schema.getIndexLabel("personByCity");
    IndexLabel personByAge = schema.getIndexLabel("personByAge");
    IndexLabel personByBorn = schema.getIndexLabel("personByBorn");
    IndexLabel personByFans = schema.getIndexLabel("personByFans");
    IndexLabel personByHeight = schema.getIndexLabel("personByHeight");
    IndexLabel personByWeight = schema.getIndexLabel("personByWeight");
    IndexLabel personByIdNo = schema.getIndexLabel("personByIdNo");
    IndexLabel personByTags = schema.getIndexLabel("personByTags");
    IndexLabel personByCategory = schema.getIndexLabel("personByCategory");
    IndexLabel personByScore = schema.getIndexLabel("personByScore");
    Assert.assertNotNull(personByName);
    Assert.assertNotNull(personByCity);
    Assert.assertNotNull(personByAge);
    Assert.assertNotNull(personByBorn);
    Assert.assertNotNull(personByFans);
    Assert.assertNotNull(personByHeight);
    Assert.assertNotNull(personByWeight);
    Assert.assertNotNull(personByIdNo);
    Assert.assertNotNull(personByTags);
    Assert.assertNotNull(personByCategory);
    Assert.assertEquals(11, person.indexLabels().size());
    assertContainsIl(person.indexLabels(), "personByName", "personByCity", "personByAge", "personByBorn", "personByFans", "personByHeight", "personByWeight", "personByIdNo", "personByTags", "personByCategory", "personByScore");
    Assert.assertEquals(HugeType.VERTEX_LABEL, personByName.baseType());
    Assert.assertEquals(HugeType.VERTEX_LABEL, personByCity.baseType());
    Assert.assertEquals(HugeType.VERTEX_LABEL, personByAge.baseType());
    Assert.assertEquals(HugeType.VERTEX_LABEL, personByBorn.baseType());
    Assert.assertEquals(HugeType.VERTEX_LABEL, personByFans.baseType());
    Assert.assertEquals(HugeType.VERTEX_LABEL, personByHeight.baseType());
    Assert.assertEquals(HugeType.VERTEX_LABEL, personByWeight.baseType());
    Assert.assertEquals(HugeType.VERTEX_LABEL, personByIdNo.baseType());
    Assert.assertEquals(HugeType.VERTEX_LABEL, personByTags.baseType());
    Assert.assertEquals(HugeType.VERTEX_LABEL, personByCategory.baseType());
    Assert.assertEquals(HugeType.VERTEX_LABEL, personByScore.baseType());
    assertVLEqual("person", personByName.baseValue());
    assertVLEqual("person", personByCity.baseValue());
    assertVLEqual("person", personByAge.baseValue());
    assertVLEqual("person", personByBorn.baseValue());
    assertVLEqual("person", personByFans.baseValue());
    assertVLEqual("person", personByHeight.baseValue());
    assertVLEqual("person", personByWeight.baseValue());
    assertVLEqual("person", personByIdNo.baseValue());
    assertVLEqual("person", personByTags.baseValue());
    assertVLEqual("person", personByCategory.baseValue());
    assertVLEqual("person", personByScore.baseValue());
    Assert.assertEquals(IndexType.SECONDARY, personByName.indexType());
    Assert.assertEquals(IndexType.SEARCH, personByCity.indexType());
    Assert.assertEquals(IndexType.RANGE_INT, personByAge.indexType());
    Assert.assertEquals(IndexType.RANGE_LONG, personByBorn.indexType());
    Assert.assertEquals(IndexType.RANGE_LONG, personByFans.indexType());
    Assert.assertEquals(IndexType.RANGE_FLOAT, personByHeight.indexType());
    Assert.assertEquals(IndexType.RANGE_DOUBLE, personByWeight.indexType());
    Assert.assertEquals(IndexType.UNIQUE, personByIdNo.indexType());
    Assert.assertEquals(IndexType.SECONDARY, personByTags.indexType());
    Assert.assertEquals(IndexType.SEARCH, personByCategory.indexType());
    Assert.assertEquals(IndexType.SECONDARY, personByScore.indexType());
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) SchemaManager(com.baidu.hugegraph.schema.SchemaManager) Test(org.junit.Test)

Example 84 with SchemaManager

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

the class IndexLabelCoreTest method testRebuildIndexLabelOfVertex.

@Test
public void testRebuildIndexLabelOfVertex() {
    Assume.assumeTrue("Not support range condition query", storeFeatures().supportsQueryWithRangeCondition());
    super.initPropertyKeys();
    SchemaManager schema = graph().schema();
    schema.vertexLabel("person").properties("name", "age", "city").primaryKeys("name").create();
    schema.indexLabel("personByCity").onV("person").secondary().by("city").create();
    schema.indexLabel("personByAge").onV("person").range().by("age").create();
    VertexLabel person = schema.getVertexLabel("person");
    Assert.assertEquals(2, person.indexLabels().size());
    assertContainsIl(person.indexLabels(), "personByCity", "personByAge");
    graph().addVertex(T.label, "person", "name", "Baby", "city", "Hongkong", "age", 3);
    graph().tx().commit();
    Vertex vertex = graph().traversal().V().hasLabel("person").has("city", "Hongkong").next();
    Assert.assertNotNull(vertex);
    vertex = graph().traversal().V().hasLabel("person").has("age", P.inside(2, 4)).next();
    Assert.assertNotNull(vertex);
    schema.indexLabel("personByCity").rebuild();
    vertex = graph().traversal().V().hasLabel("person").has("city", "Hongkong").next();
    Assert.assertNotNull(vertex);
    schema.indexLabel("personByAge").rebuild();
    vertex = graph().traversal().V().hasLabel("person").has("age", P.inside(2, 4)).next();
    Assert.assertNotNull(vertex);
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) SchemaManager(com.baidu.hugegraph.schema.SchemaManager) Test(org.junit.Test)

Example 85 with SchemaManager

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

the class IndexLabelCoreTest method testAddIndexLabelWithRepeatIndex.

@Test
public void testAddIndexLabelWithRepeatIndex() {
    super.initPropertyKeys();
    SchemaManager schema = graph().schema();
    schema.vertexLabel("person").properties("name", "age", "city", "weight").create();
    // Repeat index tests for existed range index
    schema.indexLabel("personByAge").onV("person").range().by("age").create();
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("personByAge1").onV("person").range().by("age").create();
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("personByAge2").onV("person").secondary().by("age").create();
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("personByAge3").onV("person").shard().by("age").create();
    });
    schema.indexLabel("personByAge4").onV("person").unique().by("age").create();
    schema.getIndexLabel("personByAge");
    schema.getIndexLabel("personByAge4");
    // Repeat index tests for existed secondary index(number)
    schema.vertexLabel("person1").properties("name", "age", "city", "weight").create();
    schema.indexLabel("person1ByAge").onV("person1").secondary().by("age").create();
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("person1ByAge1").onV("person1").secondary().by("age").create();
    });
    schema.indexLabel("person1ByAge2").onV("person1").shard().by("age").create();
    schema.indexLabel("person1ByAge3").onV("person1").range().by("age").create();
    schema.indexLabel("person1ByAge4").onV("person1").unique().by("age").create();
    Assert.assertThrows(NotFoundException.class, () -> {
        schema.getIndexLabel("person1ByAge");
    });
    Assert.assertThrows(NotFoundException.class, () -> {
        schema.getIndexLabel("person1ByAge2");
    });
    schema.getIndexLabel("person1ByAge3");
    schema.getIndexLabel("person1ByAge4");
    // Repeat index tests for existed secondary index(string)
    schema.vertexLabel("person2").properties("name", "age", "city", "weight").create();
    schema.indexLabel("person2ByCity").onV("person2").secondary().by("city").create();
    schema.indexLabel("person2ByCity1").onV("person2").search().by("city").create();
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("person2ByCity2").onV("person2").secondary().by("city").create();
    });
    schema.indexLabel("person2ByCity3").onV("person2").unique().by("city").create();
    schema.getIndexLabel("person2ByCity");
    schema.getIndexLabel("person2ByCity1");
    schema.getIndexLabel("person2ByCity3");
    // Repeat index tests for existed shard index
    schema.vertexLabel("person3").properties("name", "age", "city", "weight").create();
    schema.indexLabel("person3ByAge").onV("person3").shard().by("age").create();
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("person3ByAge1").onV("person3").secondary().by("age").create();
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("person3ByAge2").onV("person3").shard().by("age").create();
    });
    schema.indexLabel("person3ByAge3").onV("person3").range().by("age").create();
    schema.indexLabel("person3ByAge4").onV("person3").unique().by("age").create();
    Assert.assertThrows(NotFoundException.class, () -> {
        schema.getIndexLabel("person3ByAge");
    });
    schema.getIndexLabel("person3ByAge3");
    schema.getIndexLabel("person3ByAge4");
    // Repeat index tests for existed search index
    schema.vertexLabel("person4").properties("name", "age", "city", "weight").create();
    schema.indexLabel("person4ByCity").onV("person4").search().by("city").create();
    schema.indexLabel("person4ByCity1").onV("person4").secondary().by("city").create();
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("person4ByCity2").onV("person4").search().by("city").create();
    });
    schema.indexLabel("person4ByCity3").onV("person4").unique().by("city").create();
    schema.getIndexLabel("person4ByCity");
    schema.getIndexLabel("person4ByCity1");
    schema.getIndexLabel("person4ByCity3");
    // Repeat index tests for existed composite secondary index
    schema.vertexLabel("person5").properties("name", "age", "city", "weight").create();
    schema.indexLabel("person5ByCityAndName").onV("person5").secondary().by("city", "name").create();
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("person5ByCity1").onV("person5").secondary().by("city").create();
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("person5ByCity2").onV("person5").shard().by("city").create();
    });
    schema.indexLabel("person5ByCity3").onV("person5").search().by("city").create();
    schema.indexLabel("person5ByCity4").onV("person5").unique().by("city").create();
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("person5ByCityAndName1").onV("person5").secondary().by("city", "name").create();
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("person5ByCityAndName2").onV("person5").shard().by("city", "name").create();
    });
    schema.getIndexLabel("person5ByCity3");
    schema.getIndexLabel("person5ByCity4");
    schema.indexLabel("person5ByCity4").remove();
    schema.indexLabel("person5ByCityAndName3").onV("person5").unique().by("city", "name").create();
    schema.getIndexLabel("person5ByCityAndName3");
    // Repeat index tests for existed composite shard index
    schema.vertexLabel("person6").properties("name", "age", "city", "weight").create();
    schema.indexLabel("person6ByCityAndName").onV("person6").shard().by("city", "name").create();
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("person6ByCity1").onV("person6").secondary().by("city").create();
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("person6ByCity2").onV("person6").shard().by("city").create();
    });
    schema.indexLabel("person6ByCity3").onV("person6").search().by("city").create();
    schema.indexLabel("person6ByCity4").onV("person6").unique().by("city").create();
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("person6ByCityAndName1").onV("person6").secondary().by("city", "name").create();
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("person6ByCityAndName2").onV("person6").shard().by("city").create();
    });
    schema.getIndexLabel("person6ByCity3");
    schema.getIndexLabel("person6ByCity4");
    schema.indexLabel("person6ByCity4").remove();
    schema.indexLabel("person6ByCityAndName3").onV("person6").unique().by("city", "name").create();
    schema.getIndexLabel("person6ByCityAndName3");
    // Repeat index tests for existed unique index
    schema.vertexLabel("person7").properties("name", "age", "city", "weight").create();
    schema.indexLabel("person7ByCity").onV("person7").unique().by("city").create();
    schema.indexLabel("person7ByCity1").onV("person7").secondary().by("city").create();
    schema.indexLabel("person7ByCity1").remove();
    schema.indexLabel("person7ByCity2").onV("person7").shard().by("city").create();
    schema.indexLabel("person7ByCity3").onV("person7").search().by("city").create();
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("person7ByCity4").onV("person7").unique().by("city").create();
    });
    schema.indexLabel("person7ByCityAndName1").onV("person7").secondary().by("city", "name").create();
    schema.indexLabel("person7ByCityAndName1").remove();
    schema.indexLabel("person7ByCityAndName2").onV("person7").shard().by("city", "name").create();
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        schema.indexLabel("person7ByCityAndName3").onV("person7").unique().by("city", "name").create();
    });
    schema.getIndexLabel("person5ByCity3");
    schema.getIndexLabel("person7ByCityAndName2");
}
Also used : 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