Search in sources :

Example 6 with Encoder

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

the class AccessTokenCodec method encode.

@Override
public byte[] encode(AccessToken token) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Encoder encoder = new BinaryEncoder(bos);
    encoder.writeInt(AccessToken.Schemas.getVersion());
    DatumWriter<AccessToken> writer = writerFactory.create(ACCESS_TOKEN_TYPE, AccessToken.Schemas.getCurrentSchema());
    writer.encode(token, encoder);
    return bos.toByteArray();
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) Encoder(io.cdap.cdap.common.io.Encoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 7 with Encoder

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

the class KeyIdentifierCodec method encode.

@Override
public byte[] encode(KeyIdentifier keyIdentifier) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Encoder encoder = new BinaryEncoder(bos);
    encoder.writeInt(KeyIdentifier.Schemas.getVersion());
    DatumWriter<KeyIdentifier> writer = writerFactory.create(KEY_IDENTIFIER_TYPE, KeyIdentifier.Schemas.getCurrentSchema());
    writer.encode(keyIdentifier, encoder);
    return bos.toByteArray();
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) Encoder(io.cdap.cdap.common.io.Encoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 8 with Encoder

use of io.cdap.cdap.common.io.Encoder in project cdap by cdapio.

the class UserIdentityCodec method encode.

@Override
public byte[] encode(UserIdentity identifier) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Encoder encoder = new BinaryEncoder(bos);
    encoder.writeInt(UserIdentity.Schemas.getVersion());
    DatumWriter<UserIdentity> writer = writerFactory.create(ACCESS_TOKEN_IDENTIFIER_TYPE, UserIdentity.Schemas.getCurrentSchema());
    writer.encode(identifier, encoder);
    return bos.toByteArray();
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) Encoder(io.cdap.cdap.common.io.Encoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 9 with Encoder

use of io.cdap.cdap.common.io.Encoder in project cdap by cdapio.

the class DatumWriterGenerator method encodeArray.

/**
 * Generates method body for encoding array value. The logic is similar to the one in
 * {@link #encodeCollection}, with collection size replaced with array length.
 *
 * @param mg
 * @param componentType
 * @param componentSchema
 * @param value
 * @param encoder
 * @param schemaLocal
 * @param seenRefs
 */
private void encodeArray(GeneratorAdapter mg, TypeToken<?> componentType, Schema componentSchema, int value, int encoder, int schemaLocal, int seenRefs) {
    // Encode and store the array length locally
    mg.loadArg(value);
    mg.arrayLength();
    int length = mg.newLocal(Type.INT_TYPE);
    mg.storeLocal(length);
    mg.loadArg(encoder);
    mg.loadLocal(length);
    mg.invokeInterface(Type.getType(Encoder.class), getMethod(Encoder.class, "writeInt", int.class));
    mg.pop();
    // Store the component schema
    mg.loadArg(schemaLocal);
    mg.invokeVirtual(Type.getType(Schema.class), getMethod(Schema.class, "getComponentSchema"));
    int componentSchemaLocal = mg.newLocal(Type.getType(Schema.class));
    mg.storeLocal(componentSchemaLocal);
    // for (int idx = 0; idx < array.length; idx++)
    mg.push(0);
    int idx = mg.newLocal(Type.INT_TYPE);
    mg.storeLocal(idx);
    Label beginFor = mg.mark();
    Label endFor = mg.newLabel();
    mg.loadLocal(idx);
    mg.loadLocal(length);
    mg.ifICmp(GeneratorAdapter.GE, endFor);
    // Call encode method to encode array[idx]
    mg.loadThis();
    mg.loadArg(value);
    mg.loadLocal(idx);
    TypeToken<?> callTypeToken = getCallTypeToken(componentType, componentSchema);
    mg.arrayLoad(Type.getType(callTypeToken.getRawType()));
    mg.loadArg(encoder);
    mg.loadLocal(componentSchemaLocal);
    mg.loadArg(seenRefs);
    mg.invokeVirtual(classType, getEncodeMethod(componentType, componentSchema));
    mg.iinc(idx, 1);
    mg.goTo(beginFor);
    mg.mark(endFor);
    // if length > 0, write out 0 at the end of array.
    Label zeroLength = mg.newLabel();
    mg.loadLocal(length);
    mg.ifZCmp(GeneratorAdapter.LE, zeroLength);
    encodeInt(mg, 0, encoder);
    mg.mark(zeroLength);
}
Also used : Encoder(io.cdap.cdap.common.io.Encoder) Schema(io.cdap.cdap.api.data.schema.Schema) Label(org.objectweb.asm.Label)

Example 10 with Encoder

use of io.cdap.cdap.common.io.Encoder in project cdap by cdapio.

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(io.cdap.cdap.common.io.Encoder) UUID(java.util.UUID) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Encoder (io.cdap.cdap.common.io.Encoder)18 Schema (io.cdap.cdap.api.data.schema.Schema)8 BinaryEncoder (io.cdap.cdap.common.io.BinaryEncoder)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Label (org.objectweb.asm.Label)6 ByteBuffer (java.nio.ByteBuffer)4 Iterator (java.util.Iterator)4 Set (java.util.Set)4 BinaryDecoder (io.cdap.cdap.common.io.BinaryDecoder)2 Decoder (io.cdap.cdap.common.io.Decoder)2 IOException (java.io.IOException)2 PipedInputStream (java.io.PipedInputStream)2 PipedOutputStream (java.io.PipedOutputStream)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Buffer (java.nio.Buffer)2 IntBuffer (java.nio.IntBuffer)2 Collection (java.util.Collection)2 Map (java.util.Map)2 UUID (java.util.UUID)2 Test (org.junit.Test)2