use of com.baidu.hugegraph.schema.SchemaManager in project incubator-hugegraph by apache.
the class VertexCoreTest method initPageTestData.
private void initPageTestData() {
SchemaManager schema = graph().schema();
schema.propertyKey("lang").asText().ifNotExist().create();
schema.vertexLabel("programmer").properties("name", "age", "city").useCustomizeStringId().nullableKeys("age").ifNotExist().create();
schema.vertexLabel("software").properties("name", "lang", "price").useCustomizeStringId().nullableKeys("price").ifNotExist().create();
schema.indexLabel("programmerByNameAndAge").onV("programmer").by("name", "age").secondary().ifNotExist().create();
schema.indexLabel("programmerByAge").onV("programmer").range().by("age").ifNotExist().create();
schema.indexLabel("programmerByCity").onV("programmer").search().by("city").ifNotExist().create();
schema.indexLabel("softwareByName").onV("software").secondary().by("name").ifNotExist().create();
schema.indexLabel("softwareByLang").onV("software").secondary().by("lang").ifNotExist().create();
schema.indexLabel("softwareByPrice").onV("software").by("price").range().ifNotExist().create();
String[] cities = { "Beijing Haidian", "Beijing Chaoyang", "Shanghai" };
for (int i = 1; i <= 18; i++) {
String id = "p_marko" + i;
/*
* The city of each programmer is:
* [1, 6]: Beijing Haidian, [7, 12]: Beijing Chaoyang,
* [13, 18]: Shanghai
*/
String city = cities[(i - 1) / 6];
graph().addVertex(T.label, "programmer", T.id, id, "name", "marko", "age", 30, "city", city);
}
for (int i = 1; i <= 16; i++) {
String id = "s_marko" + i;
/*
* The price of each software is:
* [1, 4]: 100, [5, 8]: 200, [9, 12]: 300, [13, 16]: 400
*/
int price = ((i - 1) / 4 + 1) * 100;
graph().addVertex(T.label, "software", T.id, id, "name", "marko", "lang", "java", "price", price);
}
this.commitTx();
}
use of com.baidu.hugegraph.schema.SchemaManager in project incubator-hugegraph by apache.
the class VertexCoreTest method initSchema.
@Before
public void initSchema() {
SchemaManager schema = graph().schema();
LOG.debug("=============== propertyKey ================");
schema.propertyKey("id").asInt().create();
schema.propertyKey("name").asText().create();
schema.propertyKey("dynamic").asBoolean().create();
schema.propertyKey("time").asText().create();
schema.propertyKey("age").asInt().valueSingle().create();
schema.propertyKey("comment").asText().valueList().create();
schema.propertyKey("contribution").asText().valueSet().create();
schema.propertyKey("lived").asText().create();
schema.propertyKey("description").asText().create();
schema.propertyKey("city").asText().create();
schema.propertyKey("cpu").asText().create();
schema.propertyKey("ram").asText().create();
schema.propertyKey("band").asText().create();
schema.propertyKey("price").asInt().create();
schema.propertyKey("weight").asDouble().create();
schema.propertyKey("birth").asDate().create();
LOG.debug("=============== vertexLabel ================");
schema.vertexLabel("person").properties("name", "age", "city", "birth").primaryKeys("name").nullableKeys("age", "birth").create();
schema.vertexLabel("computer").properties("name", "band", "cpu", "ram", "price").primaryKeys("name", "band").nullableKeys("ram", "cpu", "price").ifNotExist().create();
schema.vertexLabel("author").properties("id", "name", "age", "lived").primaryKeys("id").nullableKeys("age", "lived").create();
schema.vertexLabel("language").properties("name", "dynamic").primaryKeys("name").nullableKeys("dynamic").create();
schema.vertexLabel("book").properties("name", "price").primaryKeys("name").nullableKeys("price").create();
schema.vertexLabel("review").properties("id", "comment", "contribution").primaryKeys("id").nullableKeys("comment", "contribution").create();
schema.vertexLabel("fan").properties("name", "age", "city").primaryKeys("name").ttl(3000L).ifNotExist().create();
schema.vertexLabel("follower").properties("name", "age", "city", "birth").primaryKeys("name").ttl(3000L).ttlStartTime("birth").ifNotExist().create();
}
use of com.baidu.hugegraph.schema.SchemaManager in project incubator-hugegraph by apache.
the class VertexCoreTest method testAddVertexWithAutomaticIdStrategyAndForceStringId.
@Test
public void testAddVertexWithAutomaticIdStrategyAndForceStringId() {
HugeGraph graph = graph();
SchemaManager schema = graph.schema();
// May be set forceString=false by config
Whitebox.setInternalState(SnowflakeIdGenerator.instance(graph), "forceString", true);
schema.vertexLabel("programmer").useAutomaticId().properties("name", "age", "city").create();
Vertex v1 = graph.addVertex(T.label, "programmer", "name", "marko", "age", 18, "city", "Beijing");
this.mayCommitTx();
List<Vertex> vertices = graph.traversal().V().toList();
Assert.assertEquals(1, vertices.size());
assertContains(vertices, T.label, "programmer", "name", "marko", "age", 18, "city", "Beijing");
Vertex v2 = graph.addVertex(T.label, "programmer", "name", "marko", "age", 18, "city", "Beijing");
this.mayCommitTx();
Assert.assertNotEquals(v1.id(), v2.id());
vertices = graph.traversal().V().toList();
Assert.assertEquals(2, vertices.size());
vertices = graph.traversal().V(v2.id()).toList();
Assert.assertEquals(1, vertices.size());
assertContains(vertices, T.label, "programmer", "name", "marko", "age", 18, "city", "Beijing");
}
use of com.baidu.hugegraph.schema.SchemaManager in project incubator-hugegraph by apache.
the class VertexCoreTest method testQueryByUUIDProperty.
@Test
public void testQueryByUUIDProperty() {
HugeGraph graph = graph();
SchemaManager schema = graph.schema();
schema.propertyKey("uuid").asUUID().create();
schema.vertexLabel("user").primaryKeys("id").properties("id", "uuid").create();
schema.indexLabel("userByUuid").secondary().onV("user").by("uuid").create();
UUID uuid1 = UUID.randomUUID();
UUID uuid2 = UUID.randomUUID();
graph().addVertex(T.label, "user", "id", 1, "uuid", uuid1);
graph().addVertex(T.label, "user", "id", 2, "uuid", uuid2);
this.mayCommitTx();
List<Vertex> vertices = graph.traversal().V().hasLabel("user").has("uuid", uuid1).toList();
Assert.assertEquals(1, vertices.size());
assertContains(vertices, T.label, "user", "id", 1, "uuid", uuid1);
vertices = graph.traversal().V().hasLabel("user").has("uuid", uuid2).toList();
Assert.assertEquals(1, vertices.size());
assertContains(vertices, T.label, "user", "id", 2, "uuid", uuid2);
}
use of com.baidu.hugegraph.schema.SchemaManager in project incubator-hugegraph by apache.
the class VertexCoreTest method testAddOlapRangeAndOlapSecondaryProperties.
@Test
public void testAddOlapRangeAndOlapSecondaryProperties() {
Assume.assumeTrue("Not support olap properties", storeFeatures().supportsOlapProperties());
HugeGraph graph = graph();
SchemaManager schema = graph.schema();
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));
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(), "pagerank", 0.1D);
graph.addVertex(T.id, id2.asObject(), "pagerank", 0.2D);
graph.addVertex(T.id, id3.asObject(), "pagerank", 0.3D);
graph.addVertex(T.id, id4.asObject(), "pagerank", 0.4D);
graph.addVertex(T.id, id5.asObject(), "pagerank", 0.5D);
graph.addVertex(T.id, id6.asObject(), "pagerank", 0.6D);
graph.addVertex(T.id, id7.asObject(), "pagerank", 0.7D);
graph.addVertex(T.id, id8.asObject(), "pagerank", 0.8D);
graph.addVertex(T.id, id9.asObject(), "pagerank", 0.9D);
graph.addVertex(T.id, id10.asObject(), "pagerank", 1.0D);
this.commitTx();
graph.addVertex(T.id, id1.asObject(), "wcc", "a");
graph.addVertex(T.id, id2.asObject(), "wcc", "a");
graph.addVertex(T.id, id3.asObject(), "wcc", "c");
graph.addVertex(T.id, id4.asObject(), "wcc", "c");
graph.addVertex(T.id, id5.asObject(), "wcc", "c");
graph.addVertex(T.id, id6.asObject(), "wcc", "f");
graph.addVertex(T.id, id7.asObject(), "wcc", "f");
graph.addVertex(T.id, id8.asObject(), "wcc", "f");
graph.addVertex(T.id, id9.asObject(), "wcc", "f");
graph.addVertex(T.id, id10.asObject(), "wcc", "f");
this.commitTx();
Assert.assertEquals(GraphReadMode.OLTP_ONLY, graph.readMode());
Assert.assertThrows(NotAllowException.class, () -> {
graph.traversal().V().has("pagerank", 0.1D).has("wcc", "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("pagerank", 0.3D).has("wcc", "b").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.6D).has("wcc", "f").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("wcc", "a").toList();
Assert.assertEquals(1, vertices.size());
Assert.assertEquals(graph.traversal().V(id1).next(), vertices.get(0));
vertices = graph.traversal().V().has("pagerank", 0.3D).has("wcc", "c").toList();
Assert.assertEquals(1, vertices.size());
Assert.assertEquals(graph.traversal().V(id3).next(), vertices.get(0));
vertices = graph.traversal().V().has("pagerank", 0.6D).has("wcc", "f").toList();
Assert.assertEquals(1, vertices.size());
Assert.assertEquals(graph.traversal().V(id6).next(), vertices.get(0));
vertices = graph.traversal().V().has("pagerank", P.gt(0.9D)).has("wcc", "f").toList();
Assert.assertEquals(1, vertices.size());
Assert.assertEquals(graph.traversal().V(id10).next(), vertices.get(0));
vertices = graph.traversal().V().has("pagerank", P.lt(0.2D)).has("wcc", "a").toList();
Assert.assertEquals(1, vertices.size());
Assert.assertEquals(graph.traversal().V(id1).next(), vertices.get(0));
vertices = graph.traversal().V().has("pagerank", P.gte(0.9D)).has("wcc", "f").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("pagerank", P.lte(0.2D)).has("wcc", "a").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("pagerank", P.inside(0.2D, 0.9D)).has("wcc", "c").toList();
Assert.assertEquals(3, 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()));
vertices = graph.traversal().V().has("pagerank", P.inside(0.2D, 0.9D)).has("wcc", "f").toList();
Assert.assertEquals(3, vertices.size());
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);
}
Aggregations