Search in sources :

Example 6 with DataType

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;
    }
}
Also used : DataType(com.alibaba.maxgraph.compiler.api.schema.DataType)

Example 7 with DataType

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);
    }
}
Also used : DataInputStream(java.io.DataInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BiFunction(java.util.function.BiFunction) IOException(java.io.IOException) DataType(com.alibaba.maxgraph.compiler.api.schema.DataType) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 8 with DataType

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;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GraphProperty(com.alibaba.maxgraph.compiler.api.schema.GraphProperty) HashMap(java.util.HashMap) PropertyValue(com.alibaba.maxgraph.sdkcommon.schema.PropertyValue) DataType(com.alibaba.maxgraph.compiler.api.schema.DataType) PropertyDefNotFoundException(com.alibaba.maxgraph.compiler.api.exception.PropertyDefNotFoundException)

Example 9 with DataType

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;
    }
}
Also used : DataType(com.alibaba.maxgraph.compiler.api.schema.DataType)

Example 10 with DataType

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);
    }
}
Also used : DataInputStream(java.io.DataInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BiFunction(java.util.function.BiFunction) IOException(java.io.IOException) DataType(com.alibaba.maxgraph.compiler.api.schema.DataType) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream)

Aggregations

DataType (com.alibaba.maxgraph.compiler.api.schema.DataType)10 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 DataInputStream (java.io.DataInputStream)4 DataOutputStream (java.io.DataOutputStream)4 IOException (java.io.IOException)4 ByteBuffer (java.nio.ByteBuffer)4 StandardCharsets (java.nio.charset.StandardCharsets)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 BiFunction (java.util.function.BiFunction)4 Function (java.util.function.Function)4 Collectors (java.util.stream.Collectors)4 Message (com.alibaba.maxgraph.Message)1 PropertyDefNotFoundException (com.alibaba.maxgraph.compiler.api.exception.PropertyDefNotFoundException)1 GraphProperty (com.alibaba.maxgraph.compiler.api.schema.GraphProperty)1 ListValueType (com.alibaba.maxgraph.compiler.tree.value.ListValueType)1 ValueValueType (com.alibaba.maxgraph.compiler.tree.value.ValueValueType)1 CustomPredicate (com.alibaba.maxgraph.sdkcommon.compiler.custom.CustomPredicate)1 PredicateType (com.alibaba.maxgraph.sdkcommon.compiler.custom.PredicateType)1