use of com.alibaba.maxgraph.compiler.api.schema.DataType in project GraphScope by alibaba.
the class PropertyValue method parseProto.
public static PropertyValue parseProto(PropertyValuePb proto) {
try {
DataType dataType = DataType.parseProto(proto.getDataType());
byte[] bytes = proto.getVal().toByteArray();
return new PropertyValue(dataType, bytes);
} catch (Exception e) {
return null;
}
}
use of com.alibaba.maxgraph.compiler.api.schema.DataType in project GraphScope by alibaba.
the class SerdeUtils method bytesToObject.
public static Object bytesToObject(DataType dataType, byte[] valBytes) {
try {
Object valObject;
switch(dataType) {
case BOOL:
valObject = valBytes[0] != (byte) 0;
break;
case CHAR:
valObject = ByteBuffer.wrap(valBytes).getChar();
break;
case SHORT:
valObject = ByteBuffer.wrap(valBytes).getShort();
break;
case INT:
valObject = ByteBuffer.wrap(valBytes).getInt();
break;
case LONG:
valObject = ByteBuffer.wrap(valBytes).getLong();
break;
case FLOAT:
valObject = ByteBuffer.wrap(valBytes).getFloat();
break;
case DOUBLE:
valObject = ByteBuffer.wrap(valBytes).getDouble();
break;
case STRING:
valObject = new String(valBytes, StandardCharsets.UTF_8);
break;
case BYTES:
valObject = valBytes;
break;
case INT_LIST:
valObject = parseListVal(valBytes, dis -> {
try {
return dis.readInt();
} catch (IOException e) {
throw new IllegalArgumentException("parse val failed", e);
}
});
break;
case LONG_LIST:
valObject = parseListVal(valBytes, dis -> {
try {
return dis.readLong();
} catch (IOException e) {
throw new IllegalArgumentException("parse val failed", e);
}
});
break;
case FLOAT_LIST:
valObject = parseListVal(valBytes, dis -> {
try {
return dis.readFloat();
} catch (IOException e) {
throw new IllegalArgumentException("parse val failed", e);
}
});
break;
case DOUBLE_LIST:
valObject = parseListVal(valBytes, dis -> {
try {
return dis.readDouble();
} catch (IOException e) {
throw new IllegalArgumentException("parse val failed", e);
}
});
break;
case STRING_LIST:
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(valBytes));
int size = dis.readInt();
List<Integer> strAccumulatedLength = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
strAccumulatedLength.add(dis.readInt());
}
List<String> list = new ArrayList<>(size);
if (size != 0) {
int len = strAccumulatedLength.get(0);
list.add(new String(readBytes(dis, len)));
for (int i = 1; i < size; i++) {
len = strAccumulatedLength.get(i) - strAccumulatedLength.get(i - 1);
list.add(new String(readBytes(dis, len)));
}
}
valObject = list;
break;
default:
throw new IllegalStateException("Unexpected value: " + dataType);
}
return valObject;
} catch (IOException e) {
throw new IllegalArgumentException("parse val failed", e);
}
}
use of com.alibaba.maxgraph.compiler.api.schema.DataType in project GraphScope by alibaba.
the class GraphWriter method parseRawProperties.
public static Map<Integer, PropertyValue> parseRawProperties(GraphElement graphElement, Map<String, Object> properties) {
Map<Integer, PropertyValue> res = new HashMap<>();
if (properties != null) {
properties.forEach((propertyName, valString) -> {
GraphProperty propertyDef = graphElement.getProperty(propertyName);
if (propertyDef == null) {
throw new PropertyDefNotFoundException("property [" + propertyName + "] not found in [" + graphElement.getLabel() + "]");
}
int id = propertyDef.getId();
DataType dataType = propertyDef.getDataType();
PropertyValue propertyValue = new PropertyValue(dataType, valString);
res.put(id, propertyValue);
});
}
return res;
}
use of com.alibaba.maxgraph.compiler.api.schema.DataType in project GraphScope by alibaba.
the class PropertyValue method parseProto.
public static PropertyValue parseProto(PropertyValuePb proto) {
try {
DataType dataType = DataType.parseProto(proto.getDataType());
byte[] bytes = proto.getVal().toByteArray();
return new PropertyValue(dataType, bytes);
} catch (Exception e) {
return null;
}
}
use of com.alibaba.maxgraph.compiler.api.schema.DataType in project GraphScope by alibaba.
the class SerdeUtils method bytesToObject.
public static Object bytesToObject(DataType dataType, byte[] valBytes) {
try {
Object valObject;
switch(dataType) {
case BOOL:
valObject = valBytes[0] != (byte) 0;
break;
case CHAR:
valObject = ByteBuffer.wrap(valBytes).getChar();
break;
case SHORT:
valObject = ByteBuffer.wrap(valBytes).getShort();
break;
case INT:
valObject = ByteBuffer.wrap(valBytes).getInt();
break;
case LONG:
valObject = ByteBuffer.wrap(valBytes).getLong();
break;
case FLOAT:
valObject = ByteBuffer.wrap(valBytes).getFloat();
break;
case DOUBLE:
valObject = ByteBuffer.wrap(valBytes).getDouble();
break;
case STRING:
valObject = new String(valBytes, StandardCharsets.UTF_8);
break;
case BYTES:
valObject = valBytes;
break;
case INT_LIST:
valObject = parseListVal(valBytes, dis -> {
try {
return dis.readInt();
} catch (IOException e) {
throw new IllegalArgumentException("parse val failed", e);
}
});
break;
case LONG_LIST:
valObject = parseListVal(valBytes, dis -> {
try {
return dis.readLong();
} catch (IOException e) {
throw new IllegalArgumentException("parse val failed", e);
}
});
break;
case FLOAT_LIST:
valObject = parseListVal(valBytes, dis -> {
try {
return dis.readFloat();
} catch (IOException e) {
throw new IllegalArgumentException("parse val failed", e);
}
});
break;
case DOUBLE_LIST:
valObject = parseListVal(valBytes, dis -> {
try {
return dis.readDouble();
} catch (IOException e) {
throw new IllegalArgumentException("parse val failed", e);
}
});
break;
case STRING_LIST:
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(valBytes));
int size = dis.readInt();
List<Integer> strAccumulatedLength = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
strAccumulatedLength.add(dis.readInt());
}
List<String> list = new ArrayList<>(size);
if (size != 0) {
int len = strAccumulatedLength.get(0);
list.add(new String(readBytes(dis, len)));
for (int i = 1; i < size; i++) {
len = strAccumulatedLength.get(i) - strAccumulatedLength.get(i - 1);
list.add(new String(readBytes(dis, len)));
}
}
valObject = list;
break;
default:
throw new IllegalStateException("Unexpected value: " + dataType);
}
return valObject;
} catch (IOException e) {
throw new IllegalArgumentException("parse val failed", e);
}
}
Aggregations