Example usage for org.apache.thrift.protocol TSet TSet

List of usage examples for org.apache.thrift.protocol TSet TSet

Introduction

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

Prototype

public TSet(byte t, int s) 

Source Link

Usage

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

License:Apache License

@Override
public void serialize(Object o, ObjectInspector oi, TProtocol oprot)
        throws TException, SerDeException, NoSuchFieldException, IllegalAccessException {

    ListObjectInspector loi = (ListObjectInspector) oi;

    Set<Object> set = (Set<Object>) o;
    DynamicSerDeTypeBase mt = getElementType();
    tset = new TSet(mt.getType(), set.size());
    oprot.writeSetBegin(tset);//from  w w  w  .j a v  a 2s  .  c  o m
    for (Object element : set) {
        mt.serialize(element, loi.getListElementObjectInspector(), oprot);
    }
    // in theory, the below call isn't needed in non thrift_mode, but let's not
    // get too crazy
    oprot.writeSetEnd();
}

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

License:Apache License

public <T> void writeSet(ThriftCodec<T> elementCodec, Set<T> set) throws Exception {
    if (set == null) {
        return;/* www  .java2 s . c o  m*/
    }

    protocol.writeSetBegin(new TSet(elementCodec.getType().getProtocolType().getType(), set.size()));

    for (T element : set) {
        elementCodec.write(element, protocol);
    }

    protocol.writeSetEnd();
}

From source file:com.linecorp.armeria.common.thrift.text.TTextProtocol.java

License:Apache License

@Override
public TSet readSetBegin() throws TException {
    int size = readSequenceBegin();
    return new TSet(UNUSED_TYPE, size);
}

From source file:io.nettythrift.protocol.TSimpleJSONProtocol.java

License:Apache License

public TSet readSetBegin() throws TException {
    BaseArray prevStruct = structStack.peek();
    BaseArray obj = prevStruct.getArray();
    structStack.push(obj);//  w ww  .  j a v a2 s  .c o m

    SetMetaData lm = (SetMetaData) obj.getMetaData();
    return new TSet(lm.elemMetaData.type, obj.length());
}

From source file:mt.swift.MapSerializer.java

License:Apache License

private void write(TProtocol protocol, Object value, Type type, Field field) throws TException {
    if (type == BasicType.BINARY) {
        if (value instanceof byte[]) {
            protocol.writeBinary((byte[]) value);
        } else if (value instanceof ByteBuffer) {
            protocol.writeBinary(((ByteBuffer) value).array());
        } else {//from ww w  .  j a va  2s  .  c o m
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.BOOLEAN) {
        if (value instanceof Boolean) {
            protocol.writeBool((Boolean) value);
        } else {
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.BYTE) {
        if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long
                || value instanceof BigInteger) {
            Number number = (Number) value;
            if (number.shortValue() < Byte.MIN_VALUE || number.shortValue() > Byte.MAX_VALUE) {
                throwOverflowException(field, number, Byte.MIN_VALUE, Byte.MAX_VALUE);
            }

            protocol.writeByte(number.byteValue());
        } else {
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.I16) {
        if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long
                || value instanceof BigInteger) {
            Number number = (Number) value;
            if (number.intValue() < Short.MIN_VALUE || number.intValue() > Short.MAX_VALUE) {
                throwOverflowException(field, number, Short.MIN_VALUE, Short.MAX_VALUE);
            }

            protocol.writeI16(number.shortValue());
        } else {
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.I32) {
        if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long
                || value instanceof BigInteger) {
            Number number = (Number) value;
            if (number.longValue() < Integer.MIN_VALUE || number.longValue() > Integer.MAX_VALUE) {
                throwOverflowException(field, number, Integer.MIN_VALUE, Integer.MAX_VALUE);
            }

            protocol.writeI32(number.intValue());
        } else {
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.I64) {
        if (value instanceof Byte || value instanceof Short || value instanceof Integer
                || value instanceof Long) {
            protocol.writeI64(((Number) value).longValue());
        } else if (value instanceof BigInteger) {
            BigInteger number = (BigInteger) value;
            if (number.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0
                    || number.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0) {
                throwOverflowException(field, number, Long.MAX_VALUE, Long.MIN_VALUE);
            }

            protocol.writeI64(((Number) value).longValue());
        } else {
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.DOUBLE) {
        if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long
                || value instanceof Float || value instanceof Double) {
            protocol.writeDouble(((Number) value).doubleValue());
        }
        // TODO: support BigDecimal, BigInteger if they fit
        else {
            throwInvalidTypeException(field, value);
        }
    } else if (type == BasicType.STRING) {
        if (value instanceof String) {
            protocol.writeString((String) value);
        } else if (value instanceof CharSequence) {
            protocol.writeString(value.toString());
        }
        // TODO: support any object by calling toString
        else {
            throwInvalidTypeException(field, value);
        }
    } else if (type instanceof MapType) {
        Map<?, ?> mapValue = (Map<?, ?>) value;
        MapType mapType = (MapType) type;
        TMap tmap = new TMap(mapType.getKeyType().getTType(), mapType.getValueType().getTType(),
                mapValue.size());
        protocol.writeMapBegin(tmap);
        for (Map.Entry<?, ?> entry : mapValue.entrySet()) {
            write(protocol, entry.getKey(), mapType.getKeyType(), field);
            write(protocol, entry.getValue(), mapType.getValueType(), field);
        }
        protocol.writeMapEnd();
    } else if (type instanceof ListType) {
        List<?> list = (List<?>) value;
        ListType listType = (ListType) type;
        TList tlist = new TList(listType.getValueType().getTType(), list.size());
        protocol.writeListBegin(tlist);
        for (Object obj : list) {
            write(protocol, obj, listType.getValueType(), field);
        }
        protocol.writeListEnd();
    } else if (type instanceof SetType) {
        Set<?> set = (Set<?>) value;
        SetType setType = (SetType) type;
        TSet tset = new TSet(setType.getValueType().getTType(), set.size());
        protocol.writeSetBegin(tset);
        for (Object obj : set) {
            write(protocol, obj, setType.getValueType(), field);
        }
        protocol.writeSetEnd();
    } else if (type instanceof StructureType) {
        if (value instanceof Map) {
            Map<String, ?> child = (Map<String, ?>) value;
            StructureType structureType = (StructureType) type;
            String structureName = structureType.getName();
            serialize(child, structureName, protocol);
        } else {
            throwInvalidTypeException(field, value);
        }
    } else {
        throw new IllegalArgumentException(String.format("Don't know how to serialize '%s' (%s)",
                field.getName(), field.getType().getSignature()));
    }
}

From source file:net.morimekta.providence.thrift.TProtocolSerializer.java

License:Apache License

private void writeTypedValue(Object item, PDescriptor type, TProtocol protocol)
        throws TException, SerializerException {
    switch (type.getType()) {
    case BOOL:/*from   w ww .j  av a 2s.  co m*/
        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.getValue());
        break;
    case MESSAGE:
        writeMessage((PMessage<?, ?>) item, protocol);
        break;
    case LIST:
        PList<?> lType = (PList<?>) type;
        List<?> list = (List<?>) item;
        TList listInfo = new TList(getFieldType(lType.itemDescriptor()), 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(getFieldType(sType.itemDescriptor()), 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(getFieldType(mType.keyDescriptor()),
                getFieldType(mType.itemDescriptor()), 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;
    }
}

From source file:org.apache.hadoop.hive.serde2.thrift.TBinarySortableProtocol.java

License:Apache License

/**
 * This method always return the same instance of TSet to avoid creating new
 * instances. It is the responsibility of the caller to read the value before
 * calling this method again.// www .j a v a 2 s. c  o  m
 */
@Override
public TSet readSetBegin() throws TException {
    stackLevel++;
    set = new TSet(ORDERED_TYPE, readI32());
    if (set.size == 0 && lastPrimitiveWasNull()) {
        return null;
    }
    return set;
}

From source file:org.breizhbeans.thrift.tools.thriftmongobridge.protocol.TBSONProtocol.java

License:Apache License

public TSet readSetBegin() throws TException {
    StructContext context = (StructContext) peekContext();
    if (context.fieldsStack.isEmpty()) {
        return EMPTY_SET;
    }/*from ww w  .ja va 2s. c  om*/
    String fieldName = context.fieldsStack.peek();

    ListContext listContext = new ListContext();
    BasicDBList dbList = (BasicDBList) context.dbObject.get(fieldName);

    listContext.dbList = dbList;
    listContext.thriftObject = getThriftObject(context.thriftObject, fieldName);
    pushContext(listContext);
    return new TSet(TType.SET, dbList.size());
}

From source file:org.breizhbeans.thrift.tools.thriftmongobridge.protocol.TBSONUnstackedProtocol.java

License:Apache License

@Override
public TSet readSetBegin() throws TException {
    //System.out.println("readSetBegin");

    // Get the IO Stack
    Stack<ThriftIO> stack = threadSafeSIOStack.get();

    ThriftIO currentIO = stack.peek();//from   www  .  j a  v  a 2s .c  o m
    ThriftFieldMetadata thriftFieldMetadata = currentIO.fieldsStack.peek();

    // field related to the list
    SetMetaData setMetaData = (SetMetaData) thriftFieldMetadata.fieldMetaData.valueMetaData;

    // extract the DBList
    BasicDBList dbList = (BasicDBList) currentIO.mongoIO.get(currentIO.fieldsStack.peek().tfield.name);

    ThriftIO thriftListIO = null;
    if (setMetaData.elemMetaData.isStruct()) {
        thriftListIO = new ThriftIO(((StructMetaData) setMetaData.elemMetaData).structClass, dbList, null,
                false, true);
    } else {
        thriftListIO = new ThriftIO(null, dbList, null, false, true);
    }

    stack.push(thriftListIO);
    threadSafeSIOStack.set(stack);

    return new TSet(TType.SET, dbList.size());
}