use of co.cask.cdap.common.io.Encoder in project cdap by caskdata.
the class AccessTokenIdentifierCodec method encode.
@Override
public byte[] encode(AccessTokenIdentifier identifier) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Encoder encoder = new BinaryEncoder(bos);
encoder.writeInt(AccessTokenIdentifier.Schemas.getVersion());
DatumWriter<AccessTokenIdentifier> writer = writerFactory.create(ACCESS_TOKEN_IDENTIFIER_TYPE, AccessTokenIdentifier.Schemas.getCurrentSchema());
writer.encode(identifier, encoder);
return bos.toByteArray();
}
use of co.cask.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();
}
use of co.cask.cdap.common.io.Encoder in project cdap by caskdata.
the class DatumWriterGenerator method encodeMap.
/**
* Generates method body for encoding map value. The logic is like this:
*
* <pre>
* {@code
*
* encoder.writeInt(map.size();
*
* for (Map.Entry<Key, Value> entry : map.entrySet()) {
* encodeKey(entry.getKey(), encoder, keySchema, seenRefs);
* encodeValue(entry.getValue(), encoder, valueSchema, seenRefs);
* }
*
* if (map.size() > 0) {
* encoder.writeInt(0);
* }
* }
* </pre>
*
* @param mg
* @param keyType
* @param valueType
* @param keySchema
* @param valueSchema
* @param value
* @param encoder
* @param schemaLocal
* @param seenRefs
*/
private void encodeMap(GeneratorAdapter mg, TypeToken<?> keyType, TypeToken<?> valueType, Schema keySchema, Schema valueSchema, int value, int encoder, int schemaLocal, int seenRefs) {
// Encode and store the map length locally
mg.loadArg(value);
mg.invokeInterface(Type.getType(Map.class), getMethod(int.class, "size"));
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();
// Stores the key and value schema
mg.loadArg(schemaLocal);
mg.invokeVirtual(Type.getType(Schema.class), getMethod(Map.Entry.class, "getMapSchema"));
mg.dup();
int keySchemaLocal = mg.newLocal(Type.getType(Schema.class));
mg.invokeInterface(Type.getType(Map.Entry.class), getMethod(Object.class, "getKey"));
mg.checkCast(Type.getType(Schema.class));
mg.storeLocal(keySchemaLocal);
int valueSchemaLocal = mg.newLocal(Type.getType(Schema.class));
mg.invokeInterface(Type.getType(Map.Entry.class), getMethod(Object.class, "getValue"));
mg.checkCast(Type.getType(Schema.class));
mg.storeLocal(valueSchemaLocal);
// Store the entry set iterator
int iterator = mg.newLocal(Type.getType(Iterator.class));
mg.loadArg(value);
mg.invokeInterface(Type.getType(Map.class), getMethod(Set.class, "entrySet"));
mg.invokeInterface(Type.getType(Set.class), getMethod(Iterator.class, "iterator"));
mg.storeLocal(iterator);
// For loop the entry set iterator, encode each key-value pairs
Label beginFor = mg.mark();
Label endFor = mg.newLabel();
mg.loadLocal(iterator);
mg.invokeInterface(Type.getType(Iterator.class), getMethod(boolean.class, "hasNext"));
mg.ifZCmp(GeneratorAdapter.EQ, endFor);
int entry = mg.newLocal(Type.getType(Map.Entry.class));
mg.loadLocal(iterator);
mg.invokeInterface(Type.getType(Iterator.class), getMethod(Object.class, "next"));
mg.checkCast(Type.getType(Map.Entry.class));
mg.storeLocal(entry);
// encode key
mg.loadThis();
mg.loadLocal(entry);
mg.invokeInterface(Type.getType(Map.Entry.class), getMethod(Object.class, "getKey"));
doCast(mg, keyType, keySchema);
mg.loadArg(encoder);
mg.loadLocal(keySchemaLocal);
mg.loadArg(seenRefs);
mg.invokeVirtual(classType, getEncodeMethod(keyType, keySchema));
// encode value
mg.loadThis();
mg.loadLocal(entry);
mg.invokeInterface(Type.getType(Map.Entry.class), getMethod(Object.class, "getValue"));
doCast(mg, valueType, valueSchema);
mg.loadArg(encoder);
mg.loadLocal(valueSchemaLocal);
mg.loadArg(seenRefs);
mg.invokeVirtual(classType, getEncodeMethod(valueType, valueSchema));
mg.goTo(beginFor);
mg.mark(endFor);
// if length > 0, write out 0 at the end of map
Label zeroLength = mg.newLabel();
mg.loadLocal(length);
mg.ifZCmp(GeneratorAdapter.LE, zeroLength);
encodeInt(mg, 0, encoder);
mg.mark(zeroLength);
}
use of co.cask.cdap.common.io.Encoder in project cdap by caskdata.
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);
}
use of co.cask.cdap.common.io.Encoder in project cdap by caskdata.
the class DatumWriterGenerator method encodeCollection.
/**
* Generates method body for encoding Collection value. The logic is like this:
*
* <pre>
* {@code
*
* encoder.writeInt(collection.size());
* for (T element : collection) {
* encodeElement(element, encoder, elementSchema, seenRefs);
* }
* if (collection.size() > 0) {
* encoder.writeInt(0);
* }
* }
* </pre>
* @param mg
* @param componentType
* @param componentSchema
* @param value
* @param encoder
*/
private void encodeCollection(GeneratorAdapter mg, TypeToken<?> componentType, Schema componentSchema, int value, int encoder, int schemaLocal, int seenRefs) {
// Encode and store the collection length locally
mg.loadArg(value);
mg.invokeInterface(Type.getType(Collection.class), getMethod(int.class, "size"));
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);
// Store the iterator
int iterator = mg.newLocal(Type.getType(Iterator.class));
mg.loadArg(value);
mg.invokeInterface(Type.getType(Collection.class), getMethod(Iterator.class, "iterator"));
mg.storeLocal(iterator);
// For loop with iterator. Encode each component
Label beginFor = mg.mark();
Label endFor = mg.newLabel();
mg.loadLocal(iterator);
mg.invokeInterface(Type.getType(Iterator.class), getMethod(boolean.class, "hasNext"));
mg.ifZCmp(GeneratorAdapter.EQ, endFor);
// Call the encode method for encoding the element.
mg.loadThis();
mg.loadLocal(iterator);
mg.invokeInterface(Type.getType(Iterator.class), getMethod(Object.class, "next"));
doCast(mg, componentType, componentSchema);
mg.loadArg(encoder);
mg.loadLocal(componentSchemaLocal);
mg.loadArg(seenRefs);
mg.invokeVirtual(classType, getEncodeMethod(componentType, componentSchema));
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);
}
Aggregations