Search in sources :

Example 1 with TList

use of org.apache.thrift.protocol.TList in project hive by apache.

the class DynamicSerDeTypeList method serialize.

@Override
public void serialize(Object o, ObjectInspector oi, TProtocol oprot) throws TException, SerDeException, NoSuchFieldException, IllegalAccessException {
    ListObjectInspector loi = (ListObjectInspector) oi;
    ObjectInspector elementObjectInspector = loi.getListElementObjectInspector();
    DynamicSerDeTypeBase mt = getElementType();
    WriteNullsProtocol nullProtocol = (oprot instanceof WriteNullsProtocol) ? (WriteNullsProtocol) oprot : null;
    if (o instanceof List) {
        List<?> list = (List<?>) o;
        oprot.writeListBegin(new TList(mt.getType(), list.size()));
        for (Object element : list) {
            if (element == null) {
                assert (nullProtocol != null);
                nullProtocol.writeNull();
            } else {
                mt.serialize(element, elementObjectInspector, oprot);
            }
        }
    } else {
        Object[] list = (Object[]) o;
        oprot.writeListBegin(new TList(mt.getType(), list.length));
        for (Object element : list) {
            if (element == null && nullProtocol != null) {
                assert (nullProtocol != null);
                nullProtocol.writeNull();
            } else {
                mt.serialize(element, elementObjectInspector, oprot);
            }
        }
    }
    // in theory, the below call isn't needed in non thrift_mode, but let's not
    // get too crazy
    oprot.writeListEnd();
}
Also used : TList(org.apache.thrift.protocol.TList) ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) WriteNullsProtocol(org.apache.hadoop.hive.serde2.thrift.WriteNullsProtocol) TList(org.apache.thrift.protocol.TList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with TList

use of org.apache.thrift.protocol.TList in project hive by apache.

the class DynamicSerDeTypeList method deserialize.

@Override
public ArrayList<Object> deserialize(Object reuse, TProtocol iprot) throws SerDeException, TException, IllegalAccessException {
    TList thelist = iprot.readListBegin();
    if (thelist == null) {
        return null;
    }
    ArrayList<Object> deserializeReuse;
    if (reuse != null) {
        deserializeReuse = (ArrayList<Object>) reuse;
        // Trim to the size needed
        while (deserializeReuse.size() > thelist.size) {
            deserializeReuse.remove(deserializeReuse.size() - 1);
        }
    } else {
        deserializeReuse = new ArrayList<Object>();
    }
    deserializeReuse.ensureCapacity(thelist.size);
    for (int i = 0; i < thelist.size; i++) {
        if (i + 1 > deserializeReuse.size()) {
            deserializeReuse.add(getElementType().deserialize(null, iprot));
        } else {
            deserializeReuse.set(i, getElementType().deserialize(deserializeReuse.get(i), iprot));
        }
    }
    // in theory, the below call isn't needed in non thrift_mode, but let's not
    // get too crazy
    iprot.readListEnd();
    return deserializeReuse;
}
Also used : TList(org.apache.thrift.protocol.TList)

Example 3 with TList

use of org.apache.thrift.protocol.TList in project elephant-bird by twitter.

the class ThriftBinaryProtocol method readListBegin.

@Override
public TList readListBegin() throws TException {
    TList list = super.readListBegin();
    checkContainerSize(list.size);
    checkContainerElemType(list.elemType);
    return list;
}
Also used : TList(org.apache.thrift.protocol.TList)

Example 4 with TList

use of org.apache.thrift.protocol.TList in project providence by morimekta.

the class TProtocolSerializer method readTypedValue.

private Object readTypedValue(byte tType, PDescriptor type, TProtocol protocol, boolean allowNull) throws TException, SerializerException {
    if (tType != forType(type.getType())) {
        throw new SerializerException("Expected type " + asString(forType(type.getType())) + " but found " + asString(tType));
    }
    switch(tType) {
        case BinaryType.BOOL:
            return protocol.readBool();
        case BinaryType.BYTE:
            return protocol.readByte();
        case BinaryType.I16:
            return protocol.readI16();
        case BinaryType.I32:
            if (PType.ENUM == type.getType()) {
                PEnumDescriptor<?> et = (PEnumDescriptor<?>) type;
                PEnumBuilder<?> eb = et.builder();
                int value = protocol.readI32();
                eb.setById(value);
                if (!eb.valid() && !allowNull) {
                    throw new SerializerException("Invalid enum value " + value + " for " + et.getQualifiedName());
                }
                return eb.build();
            } else {
                return protocol.readI32();
            }
        case BinaryType.I64:
            return protocol.readI64();
        case BinaryType.DOUBLE:
            return protocol.readDouble();
        case BinaryType.STRING:
            if (type == PPrimitive.BINARY) {
                ByteBuffer buffer = protocol.readBinary();
                return Binary.wrap(buffer.array());
            }
            return protocol.readString();
        case BinaryType.STRUCT:
            return readMessage(protocol, (PMessageDescriptor<?, ?>) type);
        case BinaryType.LIST:
            TList listInfo = protocol.readListBegin();
            PList<Object> lDesc = (PList<Object>) type;
            PDescriptor liDesc = lDesc.itemDescriptor();
            PList.Builder<Object> list = lDesc.builder();
            for (int i = 0; i < listInfo.size; ++i) {
                list.add(readTypedValue(listInfo.elemType, liDesc, protocol, false));
            }
            protocol.readListEnd();
            return list.build();
        case BinaryType.SET:
            TSet setInfo = protocol.readSetBegin();
            PSet<Object> sDesc = (PSet<Object>) type;
            PDescriptor siDesc = sDesc.itemDescriptor();
            PSet.Builder<Object> set = sDesc.builder();
            for (int i = 0; i < setInfo.size; ++i) {
                set.add(readTypedValue(setInfo.elemType, siDesc, protocol, false));
            }
            protocol.readSetEnd();
            return set.build();
        case BinaryType.MAP:
            TMap mapInfo = protocol.readMapBegin();
            PMap<Object, Object> mDesc = (PMap<Object, Object>) type;
            PDescriptor mkDesc = mDesc.keyDescriptor();
            PDescriptor miDesc = mDesc.itemDescriptor();
            PMap.Builder<Object, Object> map = mDesc.builder();
            for (int i = 0; i < mapInfo.size; ++i) {
                Object key = readTypedValue(mapInfo.keyType, mkDesc, protocol, false);
                Object val = readTypedValue(mapInfo.valueType, miDesc, protocol, false);
                map.put(key, val);
            }
            protocol.readMapEnd();
            return map.build();
        default:
            throw new SerializerException("Unsupported protocol field type: " + tType);
    }
}
Also used : PList(net.morimekta.providence.descriptor.PList) TSet(org.apache.thrift.protocol.TSet) PMap(net.morimekta.providence.descriptor.PMap) PEnumDescriptor(net.morimekta.providence.descriptor.PEnumDescriptor) SerializerException(net.morimekta.providence.serializer.SerializerException) ByteBuffer(java.nio.ByteBuffer) TMap(org.apache.thrift.protocol.TMap) TList(org.apache.thrift.protocol.TList) PDescriptor(net.morimekta.providence.descriptor.PDescriptor) PSet(net.morimekta.providence.descriptor.PSet)

Example 5 with TList

use of org.apache.thrift.protocol.TList in project providence by morimekta.

the class TProtocolSerializer method writeTypedValue.

private void writeTypedValue(Object item, PDescriptor type, TProtocol protocol) throws TException, SerializerException {
    switch(type.getType()) {
        case BOOL:
            protocol.writeBool((Boolean) item);
            break;
        case BYTE:
            protocol.writeByte((Byte) item);
            break;
        case I16:
            protocol.writeI16((Short) item);
            break;
        case I32:
            protocol.writeI32((Integer) item);
            break;
        case I64:
            protocol.writeI64((Long) item);
            break;
        case DOUBLE:
            protocol.writeDouble((Double) item);
            break;
        case STRING:
            protocol.writeString((String) item);
            break;
        case BINARY:
            protocol.writeBinary(((Binary) item).getByteBuffer());
            break;
        case ENUM:
            PEnumValue<?> value = (PEnumValue<?>) item;
            protocol.writeI32(value.asInteger());
            break;
        case MESSAGE:
            writeMessage((PMessage<?, ?>) item, protocol);
            break;
        case LIST:
            PList<?> lType = (PList<?>) type;
            List<?> list = (List<?>) item;
            TList listInfo = new TList(forType(lType.itemDescriptor().getType()), list.size());
            protocol.writeListBegin(listInfo);
            for (Object i : list) {
                writeTypedValue(i, lType.itemDescriptor(), protocol);
            }
            protocol.writeListEnd();
            break;
        case SET:
            PSet<?> sType = (PSet<?>) type;
            Set<?> set = (Set<?>) item;
            TSet setInfo = new TSet(forType(sType.itemDescriptor().getType()), set.size());
            protocol.writeSetBegin(setInfo);
            for (Object i : set) {
                writeTypedValue(i, sType.itemDescriptor(), protocol);
            }
            protocol.writeSetEnd();
            break;
        case MAP:
            PMap<?, ?> mType = (PMap<?, ?>) type;
            Map<?, ?> map = (Map<?, ?>) item;
            protocol.writeMapBegin(new TMap(forType(mType.keyDescriptor().getType()), forType(mType.itemDescriptor().getType()), map.size()));
            for (Map.Entry<?, ?> entry : map.entrySet()) {
                writeTypedValue(entry.getKey(), mType.keyDescriptor(), protocol);
                writeTypedValue(entry.getValue(), mType.itemDescriptor(), protocol);
            }
            protocol.writeMapEnd();
            break;
        default:
            break;
    }
}
Also used : TSet(org.apache.thrift.protocol.TSet) Set(java.util.Set) PSet(net.morimekta.providence.descriptor.PSet) PList(net.morimekta.providence.descriptor.PList) PEnumValue(net.morimekta.providence.PEnumValue) TSet(org.apache.thrift.protocol.TSet) PMap(net.morimekta.providence.descriptor.PMap) TMap(org.apache.thrift.protocol.TMap) TList(org.apache.thrift.protocol.TList) PSet(net.morimekta.providence.descriptor.PSet) TList(org.apache.thrift.protocol.TList) PList(net.morimekta.providence.descriptor.PList) List(java.util.List) Map(java.util.Map) PMap(net.morimekta.providence.descriptor.PMap) TMap(org.apache.thrift.protocol.TMap)

Aggregations

TList (org.apache.thrift.protocol.TList)17 TMap (org.apache.thrift.protocol.TMap)5 TSet (org.apache.thrift.protocol.TSet)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 TException (org.apache.thrift.TException)3 TField (org.apache.thrift.protocol.TField)3 EnumMap (java.util.EnumMap)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 PList (net.morimekta.providence.descriptor.PList)2 PMap (net.morimekta.providence.descriptor.PMap)2 PSet (net.morimekta.providence.descriptor.PSet)2 Configuration (org.apache.hadoop.conf.Configuration)2 TCTLSeparatedProtocol (org.apache.hadoop.hive.serde2.thrift.TCTLSeparatedProtocol)2 TConfiguration (org.apache.thrift.TConfiguration)2 TProtocol (org.apache.thrift.protocol.TProtocol)2 TStruct (org.apache.thrift.protocol.TStruct)2 TMemoryBuffer (org.apache.thrift.transport.TMemoryBuffer)2