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();
}
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();
}
Aggregations