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

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

Introduction

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

Prototype

byte BOOL

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

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 {/*from w  ww.j  a va  2 s.c om*/
        @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 {/* ww  w  . j a v  a 2s.  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 a boolean field (addressed by recursively using field id)
 * from a byte record./*from  ww  w .j  ava 2  s . c  o  m*/
 * @param bytes The serialized object to read from
 * @param fieldIdPathFirst First of the FieldId's that define a path to a boolean field
 * @param fieldIdPathRest The rest FieldId's that define a path to a boolean field
 * @throws TException
 */
public Boolean partialDeserializeBool(byte[] bytes, TFieldIdEnum fieldIdPathFirst,
        TFieldIdEnum... fieldIdPathRest) throws TException {
    return (Boolean) partialDeserializeField(TType.BOOL, 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 w w .jav 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.baidu.oped.apm.plugin.thrift.ThriftRequestProperty.java

License:Apache License

public void setTraceHeader(ThriftHeader headerKey, Object value) throws TException {
    byte headerType = headerKey.getType();
    if (headerType == TType.STRING) {
        // skipped Strings are read as byte buffer.
        // see org.apache.thrift.protocol.TProtocolUtil.skip(TProtocol, byte, int)
        this.thriftHeaders.put(headerKey, byteBufferToString((ByteBuffer) value));
    } else if (headerType == TType.I64) {
        this.thriftHeaders.put(headerKey, (Long) value);
    } else if (headerType == TType.I16) {
        this.thriftHeaders.put(headerKey, (Short) value);
    } else if (headerType == TType.BOOL) {
        this.thriftHeaders.put(headerKey, (Boolean) value);
    } else {//  w  w  w .  ja v a 2s  .c om
        throw new TProtocolException("Invalid apm header type - " + headerType);
    }
}

From source file:com.baidu.oped.apm.plugin.thrift.ThriftRequestProperty.java

License:Apache License

public void writeTraceHeader(ThriftHeader headerKey, TProtocol oprot) throws TException {
    Object headerValue = this.thriftHeaders.get(headerKey);
    if (headerValue == null) {
        return;//from w w w  .j av a2  s.  co  m
    }
    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 apm header type - " + headerType);
        }
    } finally {
        oprot.writeFieldEnd();
    }
}

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

License:Apache License

public void read(TProtocol iprot) throws TException {
    TField field;//from   ww w.j  av a 2 s .co  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.DynamicSerDeTypeBool.java

License:Apache License

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

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));
        {/*from   w  w w  . jav  a2  s  .  com*/
            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 boolean readBoolField() throws TException {
    if (!checkReadState(TType.BOOL)) {
        return false;
    }//from   ww  w.  j  a v  a  2  s .c  o m
    currentField = null;
    boolean fieldValue = protocol.readBool();
    protocol.readFieldEnd();
    return fieldValue;
}