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

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

Introduction

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

Prototype

public TMap(byte k, byte v, int s) 

Source Link

Usage

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

License:Apache License

public void write(TProtocol oprot) throws TException {
    validate();/*w  w w . ja v a 2 s . c o m*/

    oprot.writeStructBegin(STRUCT_DESC);
    if (this.credentials != null) {
        oprot.writeFieldBegin(CREDENTIALS_FIELD_DESC);
        {
            oprot.writeMapBegin(new TMap(TType.STRING, TType.STRING, this.credentials.size()));
            for (Map.Entry<String, String> _iter20 : this.credentials.entrySet()) {
                oprot.writeString(_iter20.getKey());
                oprot.writeString(_iter20.getValue());
            }
            oprot.writeMapEnd();
        }
        oprot.writeFieldEnd();
    }
    oprot.writeFieldStop();
    oprot.writeStructEnd();
}

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

License:Apache License

@Override
public void serialize(Object o, ObjectInspector oi, TProtocol oprot)
        throws TException, SerDeException, NoSuchFieldException, IllegalAccessException {
    DynamicSerDeTypeBase keyType = getKeyType();
    DynamicSerDeTypeBase valueType = getValueType();

    WriteNullsProtocol nullProtocol = (oprot instanceof WriteNullsProtocol) ? (WriteNullsProtocol) oprot : null;

    assert (oi.getCategory() == ObjectInspector.Category.MAP);
    MapObjectInspector moi = (MapObjectInspector) oi;
    ObjectInspector koi = moi.getMapKeyObjectInspector();
    ObjectInspector voi = moi.getMapValueObjectInspector();

    Map<?, ?> map = moi.getMap(o);
    serializeMap = new TMap(keyType.getType(), valueType.getType(), map.size());
    oprot.writeMapBegin(serializeMap);//from ww  w. java  2  s  . co  m

    for (Object element : map.entrySet()) {
        Map.Entry it = (Map.Entry) element;
        Object key = it.getKey();
        Object value = it.getValue();
        keyType.serialize(key, koi, oprot);
        if (value == null) {
            assert (nullProtocol != null);
            nullProtocol.writeNull();
        } else {
            valueType.serialize(value, voi, oprot);
        }
    }
    // in theory, the below call isn't needed in non thrift_mode, but let's not
    // get too crazy
    oprot.writeMapEnd();
}

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

License:Apache License

public <K, V> void writeMap(ThriftCodec<K> keyCodec, ThriftCodec<V> valueCodec, Map<K, V> map)
        throws Exception {

    if (map == null) {
        return;/*w ww  . jav  a 2 s  .  co  m*/
    }

    protocol.writeMapBegin(new TMap(keyCodec.getType().getProtocolType().getType(),
            valueCodec.getType().getProtocolType().getType(), map.size()));

    for (Map.Entry<K, V> entry : map.entrySet()) {
        keyCodec.write(entry.getKey(), protocol);
        valueCodec.write(entry.getValue(), protocol);
    }

    protocol.writeMapEnd();
}

From source file:com.inmobi.audit.thrift.AuditMessage.java

License:Apache License

public void write(TProtocol oprot) throws TException {
    validate();//from  ww  w. ja v a  2 s  . c o  m

    oprot.writeStructBegin(STRUCT_DESC);
    oprot.writeFieldBegin(TIMESTAMP_FIELD_DESC);
    oprot.writeI64(this.timestamp);
    oprot.writeFieldEnd();
    if (this.topic != null) {
        oprot.writeFieldBegin(TOPIC_FIELD_DESC);
        oprot.writeString(this.topic);
        oprot.writeFieldEnd();
    }
    if (this.tier != null) {
        oprot.writeFieldBegin(TIER_FIELD_DESC);
        oprot.writeString(this.tier);
        oprot.writeFieldEnd();
    }
    if (this.hostname != null) {
        oprot.writeFieldBegin(HOSTNAME_FIELD_DESC);
        oprot.writeString(this.hostname);
        oprot.writeFieldEnd();
    }
    oprot.writeFieldBegin(WINDOW_SIZE_FIELD_DESC);
    oprot.writeI32(this.windowSize);
    oprot.writeFieldEnd();
    if (this.received != null) {
        oprot.writeFieldBegin(RECEIVED_FIELD_DESC);
        {
            oprot.writeMapBegin(new TMap(TType.I64, TType.I64, this.received.size()));
            for (Map.Entry<Long, Long> _iter15 : this.received.entrySet()) {
                oprot.writeI64(_iter15.getKey());
                oprot.writeI64(_iter15.getValue());
            }
            oprot.writeMapEnd();
        }
        oprot.writeFieldEnd();
    }
    if (this.sent != null) {
        oprot.writeFieldBegin(SENT_FIELD_DESC);
        {
            oprot.writeMapBegin(new TMap(TType.I64, TType.I64, this.sent.size()));
            for (Map.Entry<Long, Long> _iter16 : this.sent.entrySet()) {
                oprot.writeI64(_iter16.getKey());
                oprot.writeI64(_iter16.getValue());
            }
            oprot.writeMapEnd();
        }
        oprot.writeFieldEnd();
    }
    if (this.filenames != null) {
        oprot.writeFieldBegin(FILENAMES_FIELD_DESC);
        {
            oprot.writeListBegin(new TList(TType.STRING, this.filenames.size()));
            for (String _iter17 : this.filenames) {
                oprot.writeString(_iter17);
            }
            oprot.writeListEnd();
        }
        oprot.writeFieldEnd();
    }
    if (this.tags != null) {
        oprot.writeFieldBegin(TAGS_FIELD_DESC);
        {
            oprot.writeMapBegin(new TMap(TType.STRING, TType.STRING, this.tags.size()));
            for (Map.Entry<String, String> _iter18 : this.tags.entrySet()) {
                oprot.writeString(_iter18.getKey());
                oprot.writeString(_iter18.getValue());
            }
            oprot.writeMapEnd();
        }
        oprot.writeFieldEnd();
    }
    oprot.writeFieldStop();
    oprot.writeStructEnd();
}

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

License:Apache License

@Override
public TMap readMapBegin() throws TException {
    getCurrentContext().read();/*from   ww  w  .j a  v a 2  s  . c  om*/

    JsonNode curElem = getCurrentContext().getCurrentChild();

    if (getCurrentContext().isMapKey()) {
        try {
            curElem = OBJECT_MAPPER.readTree(curElem.asText());
        } catch (IOException e) {
            throw new TException("Could not parse map key, is it valid json?", e);
        }
    }

    if (!curElem.isObject()) {
        throw new TException("Expected JSON Object!");
    }

    pushContext(new MapContext(curElem));

    return new TMap(UNUSED_TYPE, UNUSED_TYPE, curElem.size());
}

From source file:com.twitter.common.thrift.text.TTextProtocol.java

License:Apache License

@Override
public TMap readMapBegin() throws TException {
    getCurrentContext().read();/*www  . j a  v  a 2 s  . c o  m*/
    if (getCurrentContext().isMapKey()) {
        throw new TException(OBJECT_AS_KEY_ILLEGAL);
    }

    JsonElement curElem = getCurrentContext().getCurrentChild();
    if (!curElem.isJsonObject()) {
        throw new TException("Expected JSON Object!");
    }

    pushContext(new MapContext(curElem.getAsJsonObject()));

    return new TMap(UNUSED_TYPE, UNUSED_TYPE, curElem.getAsJsonObject().entrySet().size());
}

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

License:Apache License

public TMap readMapBegin() throws TException {
    BaseArray prevStruct = structStack.peek();
    BaseArray obj = prevStruct.getArray();
    structStack.push(obj);/*from ww w  .j  av a 2 s . c  o  m*/

    MapMetaData mm = (MapMetaData) obj.getMetaData();
    return new TMap(mm.keyMetaData.type, mm.valueMetaData.type, obj.length());
}

From source file:io.opentracing.thrift.SpanProtocol.java

License:Apache License

@Override
public void writeFieldStop() throws TException {
    if (!injected) {
        Span span = spanHolder.getSpan();
        if (span != null) {
            Map<String, String> map = new HashMap<>();
            TextMapInjectAdapter adapter = new TextMapInjectAdapter(map);
            tracer.inject(span.context(), Builtin.TEXT_MAP, adapter);

            super.writeFieldBegin(new TField("span", TType.MAP, SPAN_FIELD_ID));
            super.writeMapBegin(new TMap(TType.STRING, TType.STRING, map.size()));
            for (Entry<String, String> entry : map.entrySet()) {
                super.writeString(entry.getKey());
                super.writeString(entry.getValue());
            }//from   w  w  w.j av  a 2  s.c  o m
            super.writeMapEnd();
            super.writeFieldEnd();
            injected = true;
        }
    }

    super.writeFieldStop();
}

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  w w w  .  j av a2  s .  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. ja  va 2 s . c  om*/
        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;
    }
}