use of org.apache.thrift.protocol.TField in project commons by twitter.
the class TTextProtocol method readFieldBegin.
@Override
public TField readFieldBegin() throws TException {
String name = null;
if (!getCurrentContext().hasMoreChildren()) {
return new TField("", UNUSED_TYPE, (short) 0);
}
getCurrentContext().read();
JsonElement jsonName = getCurrentContext().getCurrentChild();
if (!jsonName.getAsJsonPrimitive().isString()) {
throw new RuntimeException("Expected String for a field name");
}
return getCurrentContext().getTFieldByName(jsonName.getAsJsonPrimitive().getAsString());
}
use of org.apache.thrift.protocol.TField in project voldemort by voldemort.
the class MockMessage method read.
public void read(TProtocol iprot) throws TException {
TField field;
iprot.readStructBegin();
while (true) {
field = iprot.readFieldBegin();
if (field.type == TType.STOP) {
break;
}
switch(field.id) {
case // NAME
1:
if (field.type == TType.STRING) {
this.name = iprot.readString();
} else {
TProtocolUtil.skip(iprot, field.type);
}
break;
case // MAPPINGS
2:
if (field.type == TType.MAP) {
{
TMap _map0 = iprot.readMapBegin();
this.mappings = new HashMap<Long, Map<String, Integer>>(2 * _map0.size);
for (int _i1 = 0; _i1 < _map0.size; ++_i1) {
long _key2;
Map<String, Integer> _val3;
_key2 = iprot.readI64();
{
TMap _map4 = iprot.readMapBegin();
_val3 = new HashMap<String, Integer>(2 * _map4.size);
for (int _i5 = 0; _i5 < _map4.size; ++_i5) {
String _key6;
int _val7;
_key6 = iprot.readString();
_val7 = iprot.readI32();
_val3.put(_key6, _val7);
}
iprot.readMapEnd();
}
this.mappings.put(_key2, _val3);
}
iprot.readMapEnd();
}
} else {
TProtocolUtil.skip(iprot, field.type);
}
break;
case // INT_LIST
3:
if (field.type == TType.LIST) {
{
TList _list8 = iprot.readListBegin();
this.intList = new ArrayList<Short>(_list8.size);
for (int _i9 = 0; _i9 < _list8.size; ++_i9) {
short _elem10;
_elem10 = iprot.readI16();
this.intList.add(_elem10);
}
iprot.readListEnd();
}
} else {
TProtocolUtil.skip(iprot, field.type);
}
break;
case // STR_SET
4:
if (field.type == TType.SET) {
{
TSet _set11 = iprot.readSetBegin();
this.strSet = new HashSet<String>(2 * _set11.size);
for (int _i12 = 0; _i12 < _set11.size; ++_i12) {
String _elem13;
_elem13 = iprot.readString();
this.strSet.add(_elem13);
}
iprot.readSetEnd();
}
} else {
TProtocolUtil.skip(iprot, field.type);
}
break;
default:
TProtocolUtil.skip(iprot, field.type);
}
iprot.readFieldEnd();
}
iprot.readStructEnd();
// check for required fields of primitive type, which can't be checked
// in the validate method
validate();
}
use of org.apache.thrift.protocol.TField in project hive by apache.
the class TestTCTLSeparatedProtocol method testNulls.
@Test
public void testNulls() throws Exception {
TMemoryBuffer trans = new TMemoryBuffer(1024);
TCTLSeparatedProtocol prot = new TCTLSeparatedProtocol(trans, 10);
prot.initialize(new Configuration(), new Properties());
prot.writeStructBegin(new TStruct());
prot.writeFieldBegin(new TField());
prot.writeString(null);
prot.writeFieldEnd();
prot.writeFieldBegin(new TField());
prot.writeString(null);
prot.writeFieldEnd();
prot.writeFieldBegin(new TField());
prot.writeI32(100);
prot.writeFieldEnd();
prot.writeFieldBegin(new TField());
prot.writeString(null);
prot.writeFieldEnd();
prot.writeFieldBegin(new TField());
prot.writeMapBegin(new TMap());
prot.writeString(null);
prot.writeString(null);
prot.writeString("key2");
prot.writeString(null);
prot.writeString(null);
prot.writeString("val3");
prot.writeMapEnd();
prot.writeFieldEnd();
prot.writeStructEnd();
byte[] b = new byte[1024];
int len = trans.read(b, 0, b.length);
String written = new String(b, 0, len);
String testRef = "\\N\\N100\\N\\N\\Nkey2\\N\\Nval3";
assertTrue(testRef.equals(written));
trans = new TMemoryBuffer(1023);
trans.write(b, 0, len);
prot = new TCTLSeparatedProtocol(trans, 3);
prot.initialize(new Configuration(), new Properties());
prot.readStructBegin();
prot.readFieldBegin();
String ret = prot.readString();
prot.readFieldEnd();
assertNull(ret);
prot.readFieldBegin();
ret = prot.readString();
prot.readFieldEnd();
assertNull(ret);
prot.readFieldBegin();
int ret1 = prot.readI32();
prot.readFieldEnd();
assertTrue(ret1 == 100);
prot.readFieldBegin();
ret1 = prot.readI32();
prot.readFieldEnd();
prot.readFieldBegin();
TMap map = prot.readMapBegin();
assertTrue(map.size == 3);
assertNull(prot.readString());
assertNull(prot.readString());
assertTrue(prot.readString().equals("key2"));
assertNull(prot.readString());
assertNull(prot.readString());
assertTrue(prot.readString().equals("val3"));
prot.readMapEnd();
prot.readFieldEnd();
assertTrue(ret1 == 0);
}
use of org.apache.thrift.protocol.TField in project hive by apache.
the class TestTCTLSeparatedProtocol method testWrites.
@Test
public void testWrites() throws Exception {
TMemoryBuffer trans = new TMemoryBuffer(1024);
TCTLSeparatedProtocol prot = new TCTLSeparatedProtocol(trans, 1024);
prot.writeStructBegin(new TStruct());
prot.writeFieldBegin(new TField());
prot.writeI32(100);
prot.writeFieldEnd();
prot.writeFieldBegin(new TField());
prot.writeListBegin(new TList());
prot.writeDouble(348.55);
prot.writeDouble(234.22);
prot.writeListEnd();
prot.writeFieldEnd();
prot.writeFieldBegin(new TField());
prot.writeString("hello world!");
prot.writeFieldEnd();
prot.writeFieldBegin(new TField());
prot.writeMapBegin(new TMap());
prot.writeString("key1");
prot.writeString("val1");
prot.writeString("key2");
prot.writeString("val2");
prot.writeString("key3");
prot.writeString("val3");
prot.writeMapEnd();
prot.writeFieldEnd();
prot.writeFieldBegin(new TField());
prot.writeListBegin(new TList());
prot.writeString("elem1");
prot.writeString("elem2");
prot.writeListEnd();
prot.writeFieldEnd();
prot.writeFieldBegin(new TField());
prot.writeString("bye!");
prot.writeFieldEnd();
prot.writeStructEnd();
trans.flush();
byte[] b = new byte[1024];
int len = trans.read(b, 0, b.length);
String test = new String(b, 0, len);
String testRef = "100348.55234.22hello world!key1val1key2val2key3val3elem1elem2bye!";
assertTrue(test.equals(testRef));
trans = new TMemoryBuffer(1023);
trans.write(b, 0, len);
//
// read back!
//
prot = new TCTLSeparatedProtocol(trans, 10);
prot.initialize(new Configuration(), new Properties());
// 100 is the start
prot.readStructBegin();
prot.readFieldBegin();
assertTrue(prot.readI32() == 100);
prot.readFieldEnd();
// let's see if doubles work ok
prot.readFieldBegin();
TList l = prot.readListBegin();
assertTrue(l.size == 2);
assertTrue(prot.readDouble() == 348.55);
assertTrue(prot.readDouble() == 234.22);
prot.readListEnd();
prot.readFieldEnd();
// nice message
prot.readFieldBegin();
assertTrue(prot.readString().equals("hello world!"));
prot.readFieldEnd();
// 3 element map
prot.readFieldBegin();
TMap m = prot.readMapBegin();
assertTrue(m.size == 3);
assertTrue(prot.readString().equals("key1"));
assertTrue(prot.readString().equals("val1"));
assertTrue(prot.readString().equals("key2"));
assertTrue(prot.readString().equals("val2"));
assertTrue(prot.readString().equals("key3"));
assertTrue(prot.readString().equals("val3"));
prot.readMapEnd();
prot.readFieldEnd();
// the 2 element list
prot.readFieldBegin();
l = prot.readListBegin();
assertTrue(l.size == 2);
assertTrue(prot.readString().equals("elem1"));
assertTrue(prot.readString().equals("elem2"));
prot.readListEnd();
prot.readFieldEnd();
// final string
prot.readFieldBegin();
assertTrue(prot.readString().equals("bye!"));
prot.readFieldEnd();
// should return nulls at end
prot.readFieldBegin();
assertNull(prot.readString());
prot.readFieldEnd();
// should return nulls at end
prot.readFieldBegin();
assertNull(prot.readString());
prot.readFieldEnd();
prot.readStructEnd();
}
use of org.apache.thrift.protocol.TField in project hive by apache.
the class DynamicSerDeFieldList method deserialize.
public Object deserialize(Object reuse, TProtocol iprot) throws SerDeException, TException, IllegalAccessException {
ArrayList<Object> struct = null;
if (reuse == null) {
struct = new ArrayList<Object>(getNumFields());
for (DynamicSerDeTypeBase orderedType : ordered_types) {
struct.add(null);
}
} else {
struct = (ArrayList<Object>) reuse;
assert (struct.size() == ordered_types.length);
}
boolean fastSkips = iprot instanceof org.apache.hadoop.hive.serde2.thrift.SkippableTProtocol;
// may need to strip away the STOP marker when in thrift mode
boolean stopSeen = false;
if (fieldsPresent == null) {
fieldsPresent = new boolean[ordered_types.length];
}
Arrays.fill(fieldsPresent, false);
// Read the fields.
for (int i = 0; i < getNumFields(); i++) {
DynamicSerDeTypeBase mt = null;
TField field = null;
if (!isRealThrift && getField(i).isSkippable()) {
// PRE - all the fields are required and serialized in order - is
// !isRealThrift
mt = ordered_types[i];
if (fastSkips) {
((org.apache.hadoop.hive.serde2.thrift.SkippableTProtocol) iprot).skip(mt.getType());
} else {
TProtocolUtil.skip(iprot, mt.getType());
}
struct.set(i, null);
continue;
}
if (thrift_mode) {
field = iprot.readFieldBegin();
if (field.type >= 0) {
if (field.type == TType.STOP) {
stopSeen = true;
break;
}
mt = getFieldByFieldId(field.id);
if (mt == null) {
System.err.println("ERROR for fieldid: " + field.id + " system has no knowledge of this field which is of type : " + field.type);
TProtocolUtil.skip(iprot, field.type);
continue;
}
}
}
// field.type < 0 means that this is a faked Thrift field, e.g.,
// TControlSeparatedProtocol, which does not
// serialize the field id in the stream. As a result, the only way to get
// the field id is to fall back to
// the position "i".
// The intention of this hack (field.type < 0) is to make
// TControlSeparatedProtocol a real Thrift prototype,
// but there are a lot additional work to do to fulfill that, and that
// protocol inherently does not support
// versioning (adding/deleting fields).
int orderedId = -1;
if (!thrift_mode || field.type < 0) {
mt = ordered_types[i];
// We don't need to lookup order_column_id_by_name because we know it
// must be "i".
orderedId = i;
} else {
// Set the correct position
orderedId = ordered_column_id_by_name.get(mt.name);
}
struct.set(orderedId, mt.deserialize(struct.get(orderedId), iprot));
if (thrift_mode) {
iprot.readFieldEnd();
}
fieldsPresent[orderedId] = true;
}
for (int i = 0; i < ordered_types.length; i++) {
if (!fieldsPresent[i]) {
struct.set(i, null);
}
}
if (thrift_mode && !stopSeen) {
// strip off the STOP marker, which may be left if all the fields were in
// the serialization
iprot.readFieldBegin();
}
return struct;
}
Aggregations