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;
}
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);
}
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);
}
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);
}
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);
}
Aggregations