use of com.alibaba.maxgraph.compiler.api.schema.DataType in project GraphScope by alibaba.
the class PropertyDef method parseProto.
public static PropertyDef parseProto(PropertyDefPb proto) {
int id = proto.getId();
int innerId = proto.getInnerId();
String name = proto.getName();
DataType dataType = DataType.parseProto(proto.getDataType());
PropertyValue defaultValue = PropertyValue.parseProto(proto.getDefaultValue());
boolean pk = proto.getPk();
String comment = proto.getComment();
return new PropertyDef(id, innerId, name, dataType, defaultValue, pk, comment);
}
use of com.alibaba.maxgraph.compiler.api.schema.DataType in project GraphScope by alibaba.
the class SerdeUtils method objectToBytes.
public static byte[] objectToBytes(DataType dataType, Object valObject) {
try {
switch(dataType) {
case BOOL:
return new byte[] { (byte) ((Boolean) valObject ? 1 : 0) };
case CHAR:
return ByteBuffer.allocate(Character.BYTES).putChar((Character) valObject).array();
case SHORT:
return ByteBuffer.allocate(Short.BYTES).putShort(Short.valueOf(valObject.toString())).array();
case INT:
return ByteBuffer.allocate(Integer.BYTES).putInt(Integer.valueOf(valObject.toString())).array();
case LONG:
return ByteBuffer.allocate(Long.BYTES).putLong(Long.valueOf(valObject.toString())).array();
case FLOAT:
return ByteBuffer.allocate(Float.BYTES).putFloat(Float.valueOf(valObject.toString())).array();
case DOUBLE:
return ByteBuffer.allocate(Double.BYTES).putDouble(Double.valueOf(valObject.toString())).array();
case STRING:
return ((String) valObject).getBytes(StandardCharsets.UTF_8);
case BYTES:
return (byte[]) valObject;
case INT_LIST:
List<Integer> intList = ((List<Object>) valObject).stream().map(o -> Integer.valueOf(o.toString())).collect(Collectors.toList());
return listToBytes(intList, (dos, e) -> {
try {
dos.writeInt(e);
} catch (IOException ex) {
throw new IllegalArgumentException("write to bytes failed", ex);
}
return null;
});
case LONG_LIST:
List<Long> longList = ((List<Object>) valObject).stream().map(o -> Long.valueOf(o.toString())).collect(Collectors.toList());
return listToBytes(longList, (dos, e) -> {
try {
dos.writeLong(e);
} catch (IOException ex) {
throw new IllegalArgumentException("write to bytes failed", ex);
}
return null;
});
case FLOAT_LIST:
List<Float> floatList = ((List<Object>) valObject).stream().map(o -> Float.valueOf(o.toString())).collect(Collectors.toList());
return listToBytes(floatList, (dos, e) -> {
try {
dos.writeFloat(e);
} catch (IOException ex) {
throw new IllegalArgumentException("write to bytes failed", ex);
}
return null;
});
case DOUBLE_LIST:
List<Double> doubleList = ((List<Object>) valObject).stream().map(o -> Double.valueOf(o.toString())).collect(Collectors.toList());
return listToBytes(doubleList, (dos, e) -> {
try {
dos.writeDouble(e);
} catch (IOException ex) {
throw new IllegalArgumentException("write to bytes failed", ex);
}
return null;
});
case STRING_LIST:
List<String> stringList = ((List<Object>) valObject).stream().map(o -> o.toString()).collect(Collectors.toList());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeInt(stringList.size());
int off = 0;
List<byte[]> bytesList = new ArrayList<>(stringList.size());
for (String str : stringList) {
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
bytesList.add(bytes);
off += bytes.length;
dos.writeInt(off);
}
for (byte[] bytes : bytesList) {
dos.write(bytes);
}
return bos.toByteArray();
default:
throw new IllegalStateException("Unexpected value: " + dataType);
}
} catch (Exception e) {
throw new IllegalArgumentException("unable to parse object to bytes. DataType [" + dataType + "], Object [" + valObject + "], class [" + valObject.getClass() + "]", e);
}
}
use of com.alibaba.maxgraph.compiler.api.schema.DataType in project GraphScope by alibaba.
the class TreeBuilder method convertHasContainer.
private HasContainer convertHasContainer(String key, Predicate<?> predicate, ValueType inputValueType) {
if (null == predicate) {
return new HasContainer(key, null);
}
if (predicate instanceof CustomPredicate) {
if (StringUtils.isEmpty(key)) {
checkArgument(inputValueType instanceof ListValueType || inputValueType instanceof ValueValueType, "Output value for predicate must be value while current type=>" + inputValueType);
PredicateType predicateType = ((CustomPredicate) predicate).getPredicateType();
Message.VariantType variantType = inputValueType instanceof ListValueType ? Message.VariantType.valueOf(ValueValueType.class.cast(((ListValueType) inputValueType).getListValue()).getDataType().name() + "_LIST") : ValueValueType.class.cast(inputValueType).getDataType();
validPredicateVariantType(variantType, predicateType);
return new HasContainer(key, CustomPredicate.class.cast(predicate));
} else {
Set<DataType> dataTypeSet = SchemaUtils.getPropDataTypeList(key, schema);
if (dataTypeSet.isEmpty()) {
return new HasContainer(key, CustomPredicate.class.cast(predicate));
} else {
if (dataTypeSet.size() > 1) {
logger.warn("There's multiple type=>" + dataTypeSet + " for property=>" + key);
return new HasContainer(key, CustomPredicate.class.cast(predicate));
} else {
Message.VariantType variantType = CompilerUtils.parseVariantFromDataType(dataTypeSet.iterator().next());
PredicateType predicateType = ((CustomPredicate) predicate).getPredicateType();
validPredicateVariantType(variantType, predicateType);
return new HasContainer(key, CustomPredicate.class.cast(predicate));
}
}
}
} else {
if (StringUtils.isEmpty(key)) {
Message.VariantType variantType = ValueValueType.class.cast(inputValueType).getDataType();
return createContainerFromVariantyType(key, P.class.cast(predicate), variantType);
} else {
Set<DataType> dataTypeSet = SchemaUtils.getPropDataTypeList(key, schema);
if (dataTypeSet.isEmpty()) {
return new HasContainer(key, P.class.cast(predicate));
} else {
if (dataTypeSet.size() > 1) {
logger.warn("There's multiple type=>" + dataTypeSet + " for property=>" + key);
return new HasContainer(key, P.class.cast(predicate));
} else {
Message.VariantType variantType = CompilerUtils.parseVariantFromDataType(dataTypeSet.iterator().next());
return createContainerFromVariantyType(key, P.class.cast(predicate), variantType);
}
}
}
}
}
use of com.alibaba.maxgraph.compiler.api.schema.DataType in project GraphScope by alibaba.
the class SerdeUtils method objectToBytes.
public static byte[] objectToBytes(DataType dataType, Object valObject) {
try {
switch(dataType) {
case BOOL:
return new byte[] { (byte) ((Boolean) valObject ? 1 : 0) };
case CHAR:
return ByteBuffer.allocate(Character.BYTES).putChar((Character) valObject).array();
case SHORT:
return ByteBuffer.allocate(Short.BYTES).putShort(Short.valueOf(valObject.toString())).array();
case INT:
return ByteBuffer.allocate(Integer.BYTES).putInt(Integer.valueOf(valObject.toString())).array();
case LONG:
return ByteBuffer.allocate(Long.BYTES).putLong(Long.valueOf(valObject.toString())).array();
case FLOAT:
return ByteBuffer.allocate(Float.BYTES).putFloat(Float.valueOf(valObject.toString())).array();
case DOUBLE:
return ByteBuffer.allocate(Double.BYTES).putDouble(Double.valueOf(valObject.toString())).array();
case STRING:
return ((String) valObject).getBytes(StandardCharsets.UTF_8);
case BYTES:
return (byte[]) valObject;
case INT_LIST:
List<Integer> intList = ((List<Object>) valObject).stream().map(o -> Integer.valueOf(o.toString())).collect(Collectors.toList());
return listToBytes(intList, (dos, e) -> {
try {
dos.writeInt(e);
} catch (IOException ex) {
throw new IllegalArgumentException("write to bytes failed", ex);
}
return null;
});
case LONG_LIST:
List<Long> longList = ((List<Object>) valObject).stream().map(o -> Long.valueOf(o.toString())).collect(Collectors.toList());
return listToBytes(longList, (dos, e) -> {
try {
dos.writeLong(e);
} catch (IOException ex) {
throw new IllegalArgumentException("write to bytes failed", ex);
}
return null;
});
case FLOAT_LIST:
List<Float> floatList = ((List<Object>) valObject).stream().map(o -> Float.valueOf(o.toString())).collect(Collectors.toList());
return listToBytes(floatList, (dos, e) -> {
try {
dos.writeFloat(e);
} catch (IOException ex) {
throw new IllegalArgumentException("write to bytes failed", ex);
}
return null;
});
case DOUBLE_LIST:
List<Double> doubleList = ((List<Object>) valObject).stream().map(o -> Double.valueOf(o.toString())).collect(Collectors.toList());
return listToBytes(doubleList, (dos, e) -> {
try {
dos.writeDouble(e);
} catch (IOException ex) {
throw new IllegalArgumentException("write to bytes failed", ex);
}
return null;
});
case STRING_LIST:
List<String> stringList = ((List<Object>) valObject).stream().map(o -> o.toString()).collect(Collectors.toList());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeInt(stringList.size());
int off = 0;
List<byte[]> bytesList = new ArrayList<>(stringList.size());
for (String str : stringList) {
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
bytesList.add(bytes);
off += bytes.length;
dos.writeInt(off);
}
for (byte[] bytes : bytesList) {
dos.write(bytes);
}
return bos.toByteArray();
default:
throw new IllegalStateException("Unexpected value: " + dataType);
}
} catch (Exception e) {
throw new IllegalArgumentException("unable to parse object to bytes. DataType [" + dataType + "], Object [" + valObject + "], class [" + valObject.getClass() + "]", e);
}
}
use of com.alibaba.maxgraph.compiler.api.schema.DataType in project GraphScope by alibaba.
the class PropertyDef method parseProto.
public static PropertyDef parseProto(PropertyDefPb proto) {
int id = proto.getId();
int innerId = proto.getInnerId();
String name = proto.getName();
DataType dataType = DataType.parseProto(proto.getDataType());
PropertyValue defaultValue = PropertyValue.parseProto(proto.getDefaultValue());
boolean pk = proto.getPk();
String comment = proto.getComment();
return new PropertyDef(id, innerId, name, dataType, defaultValue, pk, comment);
}
Aggregations