use of com.baidu.hugegraph.structure.schema.PropertyKey in project incubator-hugegraph-toolchain by apache.
the class PropertyKeyApiTest method testAddPropertyKeyWithUserData.
@Test
public void testAddPropertyKeyWithUserData() {
PropertyKey age = schema().propertyKey("age").userdata("min", 0).userdata("max", 100).build();
PropertyKey.PropertyKeyWithTask propertyKeyWithTask;
propertyKeyWithTask = propertyKeyAPI.create(age);
Assert.assertEquals(0L, propertyKeyWithTask.taskId());
age = propertyKeyWithTask.propertyKey();
Assert.assertEquals(3, age.userdata().size());
Assert.assertEquals(0, age.userdata().get("min"));
Assert.assertEquals(100, age.userdata().get("max"));
String time = (String) age.userdata().get("~create_time");
Date createTime = DateUtil.parse(time);
Assert.assertTrue(createTime.before(DateUtil.now()));
PropertyKey id = schema().propertyKey("id").userdata("length", 15).userdata("length", 18).build();
propertyKeyWithTask = propertyKeyAPI.create(id);
Assert.assertEquals(0L, propertyKeyWithTask.taskId());
id = propertyKeyWithTask.propertyKey();
// The same key user data will be overwritten
Assert.assertEquals(2, id.userdata().size());
Assert.assertEquals(18, id.userdata().get("length"));
time = (String) id.userdata().get("~create_time");
createTime = DateUtil.parse(time);
Assert.assertTrue(createTime.before(DateUtil.now()));
PropertyKey sex = schema().propertyKey("sex").userdata("range", ImmutableList.of("male", "female")).build();
propertyKeyWithTask = propertyKeyAPI.create(sex);
Assert.assertEquals(0L, propertyKeyWithTask.taskId());
sex = propertyKeyWithTask.propertyKey();
Assert.assertEquals(2, sex.userdata().size());
Assert.assertEquals(ImmutableList.of("male", "female"), sex.userdata().get("range"));
time = (String) sex.userdata().get("~create_time");
createTime = DateUtil.parse(time);
Assert.assertTrue(createTime.before(DateUtil.now()));
}
use of com.baidu.hugegraph.structure.schema.PropertyKey in project incubator-hugegraph-toolchain by apache.
the class GraphService method fillProperties.
private void fillProperties(int connId, SchemaLabelEntity schema, GraphElement element, Map<String, Object> properties) {
HugeClient client = this.client(connId);
for (Map.Entry<String, Object> entry : properties.entrySet()) {
String key = entry.getKey();
Object rawValue = entry.getValue();
// Skip nullable property
if (schema.getNullableProps().contains(key)) {
if (rawValue instanceof String && StringUtils.isEmpty((String) rawValue)) {
continue;
}
}
PropertyKeyEntity pkEntity = this.pkService.get(key, connId);
PropertyKey propertyKey = PropertyKeyService.convert(pkEntity, client);
assert propertyKey != null;
Object value;
try {
// DataTypeUtil.convert in loader need param InputSource
FileSource source = new FileSource();
ListFormat listFormat = new ListFormat("", "", ",");
source.listFormat(listFormat);
value = DataTypeUtil.convert(rawValue, propertyKey, source);
} catch (IllegalArgumentException e) {
throw new ExternalException("graph.property.convert.failed", e, key, rawValue);
}
element.property(key, value);
}
}
use of com.baidu.hugegraph.structure.schema.PropertyKey in project incubator-hugegraph-toolchain by apache.
the class EdgeLabelService method reuse.
public void reuse(ConflictDetail detail, int connId) {
Ex.check(!detail.hasConflict(), "schema.cannot-reuse-conflict");
HugeClient client = this.client(connId);
List<PropertyKey> propertyKeys = this.pkService.filter(detail, client);
if (!propertyKeys.isEmpty()) {
try {
this.pkService.addBatch(propertyKeys, client);
} catch (Exception e) {
throw new ExternalException("schema.propertykey.reuse.failed", e);
}
}
List<VertexLabel> vertexLabels = this.vlService.filter(detail, client);
if (!vertexLabels.isEmpty()) {
try {
this.vlService.addBatch(vertexLabels, client);
} catch (Exception e) {
this.pkService.removeBatch(propertyKeys, client);
throw new ExternalException("schema.vertexlabel.reuse.failed", e);
}
}
List<EdgeLabel> edgeLabels = this.filter(detail, client);
if (!edgeLabels.isEmpty()) {
try {
this.addBatch(edgeLabels, client);
} catch (Exception e) {
this.vlService.removeBatch(vertexLabels, client);
this.pkService.removeBatch(propertyKeys, client);
throw new ExternalException("schema.edgelabel.reuse.failed", e);
}
}
List<IndexLabel> indexLabels = this.piService.filter(detail, client);
if (!indexLabels.isEmpty()) {
try {
this.piService.addBatch(indexLabels, client);
} catch (Exception e) {
this.removeBatch(edgeLabels, client);
this.vlService.removeBatch(vertexLabels, client);
this.pkService.removeBatch(propertyKeys, client);
throw new ExternalException("schema.propertyindex.reuse.failed", e);
}
}
}
use of com.baidu.hugegraph.structure.schema.PropertyKey in project incubator-hugegraph-toolchain by apache.
the class PropertyKeyService method reuse.
public void reuse(ConflictDetail detail, int connId) {
// Assume that the conflict detail is valid
Ex.check(!detail.hasConflict(), "schema.cannot-reuse-conflict");
HugeClient client = this.client(connId);
List<PropertyKey> propertyKeys = this.filter(detail, client);
if (propertyKeys.isEmpty()) {
return;
}
try {
this.addBatch(propertyKeys, client);
} catch (Exception e) {
throw new ExternalException("schema.propertykey.reuse.failed", e);
}
}
use of com.baidu.hugegraph.structure.schema.PropertyKey in project incubator-hugegraph-toolchain by apache.
the class PropertyKeyAPI method create.
public PropertyKey.PropertyKeyWithTask create(PropertyKey propertyKey) {
Object pkey = this.checkCreateOrUpdate(propertyKey);
RestResult result = this.client.post(this.path(), pkey);
if (this.client.apiVersionLt("0.65")) {
return new PropertyKey.PropertyKeyWithTask(result.readObject(PropertyKey.class), 0L);
}
return result.readObject(PropertyKey.PropertyKeyWithTask.class);
}
Aggregations