Example usage for org.apache.thrift.protocol TType I32

List of usage examples for org.apache.thrift.protocol TType I32

Introduction

In this page you can find the example usage for org.apache.thrift.protocol TType I32.

Prototype

byte I32

To view the source code for org.apache.thrift.protocol TType I32.

Click Source Link

Usage

From source file:cereal.impl.ThriftStructMapping.java

License:Apache License

@Override
public List<Field> getFields(E obj) {
    checkNotNull(obj, "The struct was null");

    List<Field> fields;
    try {/*www.  ja va 2s. c o  m*/
        @SuppressWarnings("rawtypes")
        Class<? extends TBase> tbaseClz = obj.getClass();
        if (null == getFieldValue) {
            synchronized (this) {
                if (null == getFieldValue) {
                    Class<?> fieldsClz = Class.forName(obj.getClass().getName() + "$_Fields");
                    getFieldValue = tbaseClz.getMethod("getFieldValue", fieldsClz);
                    isSet = tbaseClz.getMethod("isSet", fieldsClz);
                }
            }
        }

        Map<? extends TFieldIdEnum, FieldMetaData> thriftFields = FieldMetaData.getStructMetaDataMap(tbaseClz);
        fields = new ArrayList<>();

        for (Entry<? extends TFieldIdEnum, FieldMetaData> entry : thriftFields.entrySet()) {
            TFieldIdEnum field = entry.getKey();
            FieldMetaData fMetaData = entry.getValue();
            if ((boolean) isSet.invoke(obj, field)) {
                Object value = getFieldValue.invoke(obj, field);
                FieldValueMetaData fvMetaData = fMetaData.valueMetaData;
                switch (fvMetaData.type) {
                case TType.BOOL:
                    Boolean booleanVal = (Boolean) value;
                    fields.add(new FieldImpl(text(fMetaData.fieldName), getGrouping(fMetaData),
                            getVisibility(fMetaData), value(booleanVal.toString())));
                    break;
                case TType.BYTE:
                    Byte byteVal = (Byte) value;
                    fields.add(new FieldImpl(text(fMetaData.fieldName), getGrouping(fMetaData),
                            getVisibility(fMetaData), value(byteVal.toString())));
                    break;
                case TType.DOUBLE:
                    Double dblVal = (Double) value;
                    fields.add(new FieldImpl(text(fMetaData.fieldName), getGrouping(fMetaData),
                            getVisibility(fMetaData), value(dblVal.toString())));
                    break;
                case TType.I16:
                    Short shortVal = (Short) value;
                    fields.add(new FieldImpl(text(fMetaData.fieldName), getGrouping(fMetaData),
                            getVisibility(fMetaData), value(shortVal.toString())));
                    break;
                case TType.I32:
                    Integer intVal = (Integer) value;
                    fields.add(new FieldImpl(text(fMetaData.fieldName), getGrouping(fMetaData),
                            getVisibility(fMetaData), value(intVal.toString())));
                    break;
                case TType.I64:
                    Long longVal = (Long) value;
                    fields.add(new FieldImpl(text(fMetaData.fieldName), getGrouping(fMetaData),
                            getVisibility(fMetaData), value(longVal.toString())));
                    break;
                case TType.STRING:
                    byte[] bytes;
                    if (fvMetaData.isBinary()) {
                        bytes = (byte[]) value;
                    } else {
                        String strVal = (String) value;
                        bytes = strVal.getBytes(UTF_8);
                    }
                    fields.add(new FieldImpl(text(fMetaData.fieldName), getGrouping(fMetaData),
                            getVisibility(fMetaData), new Value(bytes)));
                    break;
                default:
                    log.warn("Ignoring field: {}", field.getFieldName());
                    break;
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return fields;
}

From source file:cereal.impl.ThriftStructMapping.java

License:Apache License

@Override
public void update(Iterable<Entry<Key, Value>> iter, InstanceOrBuilder<E> instOrBuilder) {
    checkNotNull(iter, "Iterator is null");
    checkNotNull(instOrBuilder, "InstOrBuilder is null");
    checkArgument(Type.INSTANCE == instOrBuilder.getType(), "Expected INSTANCE in InstanceOrBuilder");

    try {//  w  w  w. ja  v a  2 s.c  o m
        @SuppressWarnings("rawtypes")
        Class<? extends TBase> tbaseClz = instOrBuilder.getWrappedClass();
        if (null == setFieldValue) {
            synchronized (this) {
                if (null == setFieldValue) {
                    Class<?> fieldsClz = Class.forName(instOrBuilder.getWrappedClass().getName() + "$_Fields");
                    setFieldValue = tbaseClz.getMethod("setFieldValue", fieldsClz, Object.class);
                }
            }
        }

        for (Entry<Key, Value> entry : iter) {
            String fieldName = entry.getKey().getColumnQualifier().toString();
            Map<? extends TFieldIdEnum, FieldMetaData> thriftFields = FieldMetaData
                    .getStructMetaDataMap(tbaseClz);
            for (Entry<? extends TFieldIdEnum, FieldMetaData> fieldEntry : thriftFields.entrySet()) {
                TFieldIdEnum fieldId = fieldEntry.getKey();
                if (fieldName.equals(fieldId.getFieldName())) {
                    FieldValueMetaData fvMetaData = fieldEntry.getValue().valueMetaData;
                    Value v = entry.getValue();
                    Object obj = instOrBuilder.get();
                    switch (fvMetaData.type) {
                    case TType.BOOL:
                        Boolean booleanVal = Boolean.parseBoolean(v.toString());
                        setFieldValue.invoke(obj, fieldId, booleanVal);
                        break;
                    case TType.BYTE:
                        Byte byteVal = Byte.parseByte(v.toString());
                        setFieldValue.invoke(obj, fieldId, byteVal);
                        break;
                    case TType.DOUBLE:
                        Double dblVal = Double.parseDouble(v.toString());
                        setFieldValue.invoke(obj, fieldId, dblVal);
                        break;
                    case TType.I16:
                        Short shortVal = Short.parseShort(v.toString());
                        setFieldValue.invoke(obj, fieldId, shortVal);
                        break;
                    case TType.I32:
                        Integer intVal = Integer.parseInt(v.toString());
                        setFieldValue.invoke(obj, fieldId, intVal);
                        break;
                    case TType.I64:
                        Long longVal = Long.parseLong(v.toString());
                        setFieldValue.invoke(obj, fieldId, longVal);
                        break;
                    case TType.STRING:
                        if (fvMetaData.isBinary()) {
                            setFieldValue.invoke(obj, fieldId, ByteBuffer.wrap(v.get()));
                        } else {
                            String strVal = v.toString();
                            setFieldValue.invoke(obj, fieldId, strVal);
                        }
                        break;
                    default:
                        log.warn("Ignoring field: {}", fieldName);
                        break;
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.ambiata.poacher.mr.TDeserializerCopy.java

License:Apache License

/**
 * Deserialize only an i32 field (addressed by recursively using field id)
 * from a byte record./*from   w  w w.  ja va 2s .c om*/
 * @param bytes The serialized object to read from
 * @param fieldIdPathFirst First of the FieldId's that define a path to an i32 field
 * @param fieldIdPathRest The rest FieldId's that define a path to an i32 field
 * @throws TException
 */
public Integer partialDeserializeI32(byte[] bytes, TFieldIdEnum fieldIdPathFirst,
        TFieldIdEnum... fieldIdPathRest) throws TException {
    return (Integer) partialDeserializeField(TType.I32, bytes, fieldIdPathFirst, fieldIdPathRest);
}

From source file:com.ambiata.poacher.mr.TDeserializerCopy.java

License:Apache License

private Object partialDeserializeField(byte ttype, byte[] bytes, TFieldIdEnum fieldIdPathFirst,
        TFieldIdEnum... fieldIdPathRest) throws TException {
    try {/*from w  ww . ja v a2  s  . c o m*/
        TField field = locateField(bytes, fieldIdPathFirst, fieldIdPathRest);
        if (field != null) {
            // if this point is reached, iprot will be positioned at the start of the field.
            switch (ttype) {
            case TType.BOOL:
                if (field.type == TType.BOOL) {
                    return protocol_.readBool();
                }
                break;
            case TType.BYTE:
                if (field.type == TType.BYTE) {
                    return protocol_.readByte();
                }
                break;
            case TType.DOUBLE:
                if (field.type == TType.DOUBLE) {
                    return protocol_.readDouble();
                }
                break;
            case TType.I16:
                if (field.type == TType.I16) {
                    return protocol_.readI16();
                }
                break;
            case TType.I32:
                if (field.type == TType.I32) {
                    return protocol_.readI32();
                }
                break;
            case TType.I64:
                if (field.type == TType.I64) {
                    return protocol_.readI64();
                }
                break;
            case TType.STRING:
                if (field.type == TType.STRING) {
                    return protocol_.readString();
                }
                break;
            case 100: // hack to differentiate between string and binary
                if (field.type == TType.STRING) {
                    return protocol_.readBinary();
                }
                break;
            }
        }
        return null;
    } catch (Exception e) {
        throw new TException(e);
    } finally {
        trans_.clear();
        protocol_.reset();
    }
}

From source file:com.bigdata.dastor.thrift.KeyRange.java

License:Apache License

public void read(TProtocol iprot) throws TException {
    TField field;//from  w  ww .j a v a 2  s  . c  o m
    iprot.readStructBegin();
    while (true) {
        field = iprot.readFieldBegin();
        if (field.type == TType.STOP) {
            break;
        }
        switch (field.id) {
        case 1: // START_KEY
            if (field.type == TType.STRING) {
                this.start_key = iprot.readString();
            } else {
                TProtocolUtil.skip(iprot, field.type);
            }
            break;
        case 2: // END_KEY
            if (field.type == TType.STRING) {
                this.end_key = iprot.readString();
            } else {
                TProtocolUtil.skip(iprot, field.type);
            }
            break;
        case 3: // START_TOKEN
            if (field.type == TType.STRING) {
                this.start_token = iprot.readString();
            } else {
                TProtocolUtil.skip(iprot, field.type);
            }
            break;
        case 4: // END_TOKEN
            if (field.type == TType.STRING) {
                this.end_token = iprot.readString();
            } else {
                TProtocolUtil.skip(iprot, field.type);
            }
            break;
        case 5: // COUNT
            if (field.type == TType.I32) {
                this.count = iprot.readI32();
                setCountIsSet(true);
            } 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
    if (!isSetCount()) {
        throw new TProtocolException(
                "Required field 'count' was not found in serialized data! Struct: " + toString());
    }
    validate();
}

From source file:com.bigdata.dastor.thrift.SliceRange.java

License:Apache License

public void read(TProtocol iprot) throws TException {
    TField field;/*from w ww.  j  a v  a  2s.  c  o  m*/
    iprot.readStructBegin();
    while (true) {
        field = iprot.readFieldBegin();
        if (field.type == TType.STOP) {
            break;
        }
        switch (field.id) {
        case 1: // START
            if (field.type == TType.STRING) {
                this.start = iprot.readBinary();
            } else {
                TProtocolUtil.skip(iprot, field.type);
            }
            break;
        case 2: // FINISH
            if (field.type == TType.STRING) {
                this.finish = iprot.readBinary();
            } else {
                TProtocolUtil.skip(iprot, field.type);
            }
            break;
        case 3: // REVERSED
            if (field.type == TType.BOOL) {
                this.reversed = iprot.readBool();
                setReversedIsSet(true);
            } else {
                TProtocolUtil.skip(iprot, field.type);
            }
            break;
        case 4: // COUNT
            if (field.type == TType.I32) {
                this.count = iprot.readI32();
                setCountIsSet(true);
            } 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
    if (!isSetReversed()) {
        throw new TProtocolException(
                "Required field 'reversed' was not found in serialized data! Struct: " + toString());
    }
    if (!isSetCount()) {
        throw new TProtocolException(
                "Required field 'count' was not found in serialized data! Struct: " + toString());
    }
    validate();
}

From source file:com.ebay.nest.io.sede.dynamic_type.DynamicSerDeTypei32.java

License:Apache License

@Override
public byte getType() {
    return TType.I32;
}

From source file:com.facebook.nifty.core.TestThriftFrameDecoder.java

License:Apache License

private void writeTestMessages(TBinaryProtocol protocol, int count) throws TException {
    for (int i = 0; i < count; i++) {
        protocol.writeMessageBegin(new TMessage("testmessage" + i, TMessageType.CALL, i));
        {/*  w ww.j  a v  a  2  s . c o m*/
            protocol.writeStructBegin(new TStruct());
            {
                protocol.writeFieldBegin(new TField("i32field", TType.I32, (short) 1));
                protocol.writeI32(123);
                protocol.writeFieldEnd();
            }
            {
                protocol.writeFieldBegin(new TField("strfield", TType.STRING, (short) 2));
                protocol.writeString("foo");
                protocol.writeFieldEnd();
            }
            {
                protocol.writeFieldBegin(new TField("boolfield", TType.BOOL, (short) 3));
                protocol.writeBool(true);
                protocol.writeFieldEnd();
            }
            protocol.writeFieldStop();
            protocol.writeStructEnd();
        }
        protocol.writeMessageEnd();
        protocol.getTransport().flush();
    }
}

From source file:com.facebook.swift.codec.internal.TProtocolReader.java

License:Apache License

public int readI32Field() throws TException {
    if (!checkReadState(TType.I32)) {
        return 0;
    }/*from w  w  w .ja v a2  s. c  o  m*/
    currentField = null;
    int fieldValue = protocol.readI32();
    protocol.readFieldEnd();
    return fieldValue;
}

From source file:com.facebook.swift.codec.internal.TProtocolReader.java

License:Apache License

public <T extends Enum<T>> T readEnumField(ThriftCodec<T> enumCodec) throws Exception {
    if (!checkReadState(TType.I32)) {
        return null;
    }//from w w  w  . j  a v a 2 s .c  o  m
    currentField = null;
    T fieldValue = null;
    try {
        fieldValue = enumCodec.read(protocol);
    } catch (UnknownEnumValueException e) {
        // return null
    }
    protocol.readFieldEnd();
    return fieldValue;
}