use of org.apache.thrift.protocol.TField in project providence by morimekta.
the class TProtocolSerializer method writeMessage.
private void writeMessage(PMessage<?, ?> message, TProtocol protocol) throws TException, SerializerException {
PMessageDescriptor<?, ?> type = message.descriptor();
protocol.writeStructBegin(new TStruct(message.descriptor().getQualifiedName()));
for (PField field : type.getFields()) {
if (!message.has(field.getId())) {
continue;
}
protocol.writeFieldBegin(new TField(field.getName(), forType(field.getDescriptor().getType()), (short) field.getId()));
writeTypedValue(message.get(field.getId()), field.getDescriptor(), protocol);
protocol.writeFieldEnd();
}
protocol.writeFieldStop();
protocol.writeStructEnd();
}
use of org.apache.thrift.protocol.TField in project parquet-mr by apache.
the class ProtocolReadToWrite method readOneStruct.
private void readOneStruct(TProtocol in, TProtocol out) throws TException {
final TStruct struct = in.readStructBegin();
out.writeStructBegin(struct);
TField field;
while ((field = in.readFieldBegin()).type != TType.STOP) {
out.writeFieldBegin(field);
readOneValue(in, out, field.type);
in.readFieldEnd();
out.writeFieldEnd();
}
out.writeFieldStop();
in.readStructEnd();
out.writeStructEnd();
}
use of org.apache.thrift.protocol.TField in project parquet-mr by apache.
the class BufferedProtocolReadToWrite method readOneStruct.
private boolean readOneStruct(TProtocol in, List<Action> buffer, StructType type) throws TException {
final TStruct struct = in.readStructBegin();
buffer.add(new Action() {
@Override
public void write(TProtocol out) throws TException {
out.writeStructBegin(struct);
}
@Override
public String toDebugString() {
return "(";
}
});
TField field;
boolean hasFieldsIgnored = false;
int childFieldsPresent = 0;
while ((field = in.readFieldBegin()).type != TType.STOP) {
final TField currentField = field;
ThriftField expectedField;
if ((expectedField = type.getChildById(field.id)) == null) {
handleUnrecognizedField(field, type, in);
hasFieldsIgnored |= true;
continue;
}
childFieldsPresent++;
buffer.add(new Action() {
@Override
public void write(TProtocol out) throws TException {
out.writeFieldBegin(currentField);
}
@Override
public String toDebugString() {
return "f=" + currentField.id + "<t=" + typeName(currentField.type) + ">: ";
}
});
hasFieldsIgnored |= readOneValue(in, field.type, buffer, expectedField.getType());
in.readFieldEnd();
buffer.add(FIELD_END);
}
// check that union had exactly 1 (no more no less) child fields.
assertUnionHasExactlyOneChild(type, childFieldsPresent);
in.readStructEnd();
buffer.add(STRUCT_END);
return hasFieldsIgnored;
}
use of org.apache.thrift.protocol.TField in project parquet-mr by apache.
the class TestProtocolReadToWrite method testMissingFieldHandling.
/**
* When data contains extra field, it should notify the handler and read the data with extra field dropped
* @throws Exception
*/
@Test
public void testMissingFieldHandling() throws Exception {
CountingErrorHandler countingHandler = new CountingErrorHandler() {
@Override
public void handleFieldIgnored(TField field) {
assertEquals(field.id, 4);
fieldIgnoredCount++;
}
};
BufferedProtocolReadToWrite structForRead = new BufferedProtocolReadToWrite(ThriftSchemaConverter.toStructType(StructV3.class), countingHandler);
// Data has an extra field of type struct
final ByteArrayOutputStream in = new ByteArrayOutputStream();
StructV4WithExtracStructField dataWithNewSchema = new StructV4WithExtracStructField("name");
dataWithNewSchema.setAge("10");
dataWithNewSchema.setGender("male");
StructV3 structV3 = new StructV3("name");
structV3.setAge("10");
dataWithNewSchema.setAddedStruct(structV3);
dataWithNewSchema.write(protocol(in));
// read using the schema that doesn't have the extra field
final ByteArrayOutputStream out = new ByteArrayOutputStream();
structForRead.readOne(protocol(new ByteArrayInputStream(in.toByteArray())), protocol(out));
// record will be read without extra field
assertEquals(1, countingHandler.recordCountOfMissingFields);
assertEquals(1, countingHandler.fieldIgnoredCount);
StructV4WithExtracStructField b = StructV4WithExtracStructField.class.newInstance();
b.read(protocol(new ByteArrayInputStream(out.toByteArray())));
assertEquals(dataWithNewSchema.getName(), b.getName());
assertEquals(dataWithNewSchema.getAge(), b.getAge());
assertEquals(dataWithNewSchema.getGender(), b.getGender());
assertEquals(null, b.getAddedStruct());
}
use of org.apache.thrift.protocol.TField in project pinpoint by naver.
the class ThriftRequestProperty method writeTraceHeader.
public void writeTraceHeader(ThriftHeader headerKey, TProtocol oprot) throws TException {
Object headerValue = this.thriftHeaders.get(headerKey);
if (headerValue == null) {
return;
}
byte headerType = headerKey.getType();
TField traceField = new TField(headerKey.name(), headerKey.getType(), headerKey.getId());
oprot.writeFieldBegin(traceField);
try {
if (headerType == TType.STRING) {
// these will be read as byte buffer although it's probably safe to just use writeString here.
// see org.apache.thrift.protocol.TProtocolUtil.skip(TProtocol, byte, int)
oprot.writeBinary(stringToByteBuffer((String) headerValue));
} else if (headerType == TType.I64) {
oprot.writeI64((Long) headerValue);
} else if (headerType == TType.I16) {
oprot.writeI16((Short) headerValue);
} else if (headerType == TType.BOOL) {
oprot.writeBool((Boolean) headerValue);
} else {
throw new TProtocolException("Invalid pinpoint header type - " + headerType);
}
} finally {
oprot.writeFieldEnd();
}
}
Aggregations