Search in sources :

Example 1 with DataType

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

Example 2 with DataType

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);
    }
}
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) DataOutputStream(java.io.DataOutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with DataType

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);
                }
            }
        }
    }
}
Also used : P(org.apache.tinkerpop.gremlin.process.traversal.P) ConnectiveP(org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP) Message(com.alibaba.maxgraph.Message) PredicateType(com.alibaba.maxgraph.sdkcommon.compiler.custom.PredicateType) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) ListValueType(com.alibaba.maxgraph.compiler.tree.value.ListValueType) DataType(com.alibaba.maxgraph.compiler.api.schema.DataType) CustomPredicate(com.alibaba.maxgraph.sdkcommon.compiler.custom.CustomPredicate) ValueValueType(com.alibaba.maxgraph.compiler.tree.value.ValueValueType)

Example 4 with DataType

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);
    }
}
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) DataOutputStream(java.io.DataOutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with DataType

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

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