Search in sources :

Example 11 with Encoder

use of co.cask.cdap.common.io.Encoder in project cdap by caskdata.

the class DatumWriterGenerator method encodeSimple.

/**
   * Generates method body for encoding simple schema type by calling corresponding write method in Encoder.
   * @param mg Method body generator
   * @param type Data type to encode
   * @param encodeMethod Name of the encode method to invoke on the given encoder.
   * @param value Argument index of the value to encode.
   * @param encoder Method argument index of the encoder
   */
private void encodeSimple(GeneratorAdapter mg, TypeToken<?> type, Schema schema, String encodeMethod, int value, int encoder) {
    // encoder.writeXXX(value);
    TypeToken<?> encodeType = type;
    mg.loadArg(encoder);
    mg.loadArg(value);
    boolean isPrimitiveBoxType = Primitives.isWrapperType(encodeType.getRawType());
    if (encodeType.getRawType().isPrimitive() || isPrimitiveBoxType) {
        // Unwrap it if it is boxed type, otherwise it won't change the encodeType.
        encodeType = TypeToken.of(Primitives.unwrap(encodeType.getRawType()));
        if (isPrimitiveBoxType) {
            mg.unbox(Type.getType(encodeType.getRawType()));
        }
        // A special case since INT type represents (byte, char, short and int).
        if (schema.getType() == Schema.Type.INT && !int.class.equals(encodeType.getRawType())) {
            encodeType = TypeToken.of(int.class);
        }
    } else if (schema.getType() == Schema.Type.STRING && !String.class.equals(encodeType.getRawType())) {
        // For non-string object that has a String schema, invoke toString().
        mg.invokeVirtual(Type.getType(encodeType.getRawType()), getMethod(String.class, "toString"));
        encodeType = TypeToken.of(String.class);
    } else if (schema.getType() == Schema.Type.BYTES && UUID.class.equals(encodeType.getRawType())) {
        // Special case UUID, encode as byte array
        // ByteBuffer buf = ByteBuffer.allocate(Longs.BYTES * 2)
        //                            .putLong(uuid.getMostSignificantBits())
        //                            .putLong(uuid.getLeastSignificantBits());
        // encoder.writeBytes((ByteBuffer) buf.flip());
        Type byteBufferType = Type.getType(ByteBuffer.class);
        Type uuidType = Type.getType(UUID.class);
        mg.push(Longs.BYTES * 2);
        mg.invokeStatic(byteBufferType, getMethod(ByteBuffer.class, "allocate", int.class));
        mg.swap();
        mg.invokeVirtual(uuidType, getMethod(long.class, "getMostSignificantBits"));
        mg.invokeVirtual(byteBufferType, getMethod(ByteBuffer.class, "putLong", long.class));
        mg.loadArg(value);
        mg.invokeVirtual(uuidType, getMethod(long.class, "getLeastSignificantBits"));
        mg.invokeVirtual(byteBufferType, getMethod(ByteBuffer.class, "putLong", long.class));
        mg.invokeVirtual(Type.getType(Buffer.class), getMethod(Buffer.class, "flip"));
        mg.checkCast(byteBufferType);
        encodeType = TypeToken.of(ByteBuffer.class);
    }
    mg.invokeInterface(Type.getType(Encoder.class), getMethod(Encoder.class, encodeMethod, encodeType.getRawType()));
    mg.pop();
}
Also used : ByteBuffer(java.nio.ByteBuffer) Buffer(java.nio.Buffer) Type(org.objectweb.asm.Type) ParameterizedType(java.lang.reflect.ParameterizedType) Encoder(co.cask.cdap.common.io.Encoder) UUID(java.util.UUID) ByteBuffer(java.nio.ByteBuffer)

Example 12 with Encoder

use of co.cask.cdap.common.io.Encoder in project cdap by caskdata.

the class QueueEntry method serializeHashKeys.

public static byte[] serializeHashKeys(Map<String, Integer> hashKeys) throws IOException {
    // many entries will have no hash keys. Reuse a static value for that
    if (hashKeys == null || hashKeys.isEmpty()) {
        return SERIALIZED_EMPTY_HASH_KEYS;
    }
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Encoder encoder = new BinaryEncoder(bos);
    encoder.writeInt(hashKeys.size());
    for (Map.Entry<String, Integer> entry : hashKeys.entrySet()) {
        encoder.writeString(entry.getKey()).writeInt(entry.getValue());
    }
    // per Avro spec, end with a (block of length) zero
    encoder.writeInt(0);
    return bos.toByteArray();
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) Encoder(co.cask.cdap.common.io.Encoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Aggregations

Encoder (co.cask.cdap.common.io.Encoder)12 BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Schema (co.cask.cdap.api.data.schema.Schema)4 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 Label (org.objectweb.asm.Label)3 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Set (java.util.Set)2 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)1 Decoder (co.cask.cdap.common.io.Decoder)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Buffer (java.nio.Buffer)1 IntBuffer (java.nio.IntBuffer)1 Collection (java.util.Collection)1 UUID (java.util.UUID)1