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();
}
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;
}
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;
}
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);
}
}
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;
}
}
Aggregations