Search in sources :

Example 6 with DirtyListWrapper

use of org.apache.gora.persistency.impl.DirtyListWrapper in project gora by apache.

the class AerospikeStore method getDeserializedObject.

/**
 * Method to get Avro mapped persistent object from the record retrieved from the database
 *
 * @param binValue    value retrieved from database
 * @param binDataType data type of the database value
 * @param schema      corresponding schema in the persistent class
 * @return persistent object
 */
private Object getDeserializedObject(Object binValue, String binDataType, Schema schema) {
    Object result;
    switch(schema.getType()) {
        case MAP:
            Map<String, Object> rawMap = (Map<String, Object>) binValue;
            Map<Utf8, Object> deserializableMap = new HashMap<>();
            if (rawMap == null) {
                result = new DirtyMapWrapper(deserializableMap);
                break;
            }
            for (Map.Entry<?, ?> e : rawMap.entrySet()) {
                Schema innerSchema = schema.getValueType();
                Object obj = getDeserializedObject(e.getValue(), e.getValue().getClass().getSimpleName(), innerSchema);
                if (e.getKey().getClass().getSimpleName().equalsIgnoreCase("Utf8")) {
                    deserializableMap.put((Utf8) e.getKey(), obj);
                } else {
                    deserializableMap.put(new Utf8((String) e.getKey()), obj);
                }
            }
            result = new DirtyMapWrapper<>(deserializableMap);
            break;
        case ARRAY:
            List<Object> rawList = (List<Object>) binValue;
            List<Object> deserializableList = new ArrayList<>();
            if (rawList == null) {
                return new DirtyListWrapper(deserializableList);
            }
            for (Object item : rawList) {
                Object obj = getDeserializedObject(item, item.getClass().getSimpleName(), schema.getElementType());
                deserializableList.add(obj);
            }
            result = new DirtyListWrapper<>(deserializableList);
            break;
        case RECORD:
            result = (PersistentBase) binValue;
            break;
        case UNION:
            int index = getUnionSchema(binValue, schema);
            Schema resolvedSchema = schema.getTypes().get(index);
            result = getDeserializedObject(binValue, binDataType, resolvedSchema);
            break;
        case ENUM:
            result = AvroUtils.getEnumValue(schema, (String) binValue);
            break;
        case BYTES:
            result = ByteBuffer.wrap((byte[]) binValue);
            break;
        case STRING:
            if (binValue instanceof org.apache.avro.util.Utf8)
                result = binValue;
            else
                result = new Utf8((String) binValue);
            break;
        case INT:
            if (binDataType.equalsIgnoreCase("long")) {
                result = Math.toIntExact((Long) binValue);
            } else {
                result = binValue;
            }
            break;
        default:
            result = binValue;
    }
    return result;
}
Also used : DirtyMapWrapper(org.apache.gora.persistency.impl.DirtyMapWrapper) HashMap(java.util.HashMap) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) DirtyListWrapper(org.apache.gora.persistency.impl.DirtyListWrapper) Utf8(org.apache.avro.util.Utf8) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with DirtyListWrapper

use of org.apache.gora.persistency.impl.DirtyListWrapper in project gora by apache.

the class RethinkDBStore method convertDocFieldToAvroList.

private Object convertDocFieldToAvroList(final String docf, final Schema fieldSchema, final MapObject<String, Object> doc, final Schema.Field f, final RethinkDBMapping.DocumentFieldType storeType) throws GoraException {
    if (storeType == RethinkDBMapping.DocumentFieldType.LIST || storeType == null) {
        List<Object> list = (List<Object>) doc.get(docf);
        List<Object> rlist = new ArrayList<>();
        if (list == null) {
            return new DirtyListWrapper(rlist);
        }
        for (Object item : list) {
            MapObject<String, Object> innerDoc = new MapObject();
            innerDoc.put("item", item);
            Object o = convertDocFieldToAvroField(fieldSchema.getElementType(), storeType, f, "item", innerDoc);
            rlist.add(o);
        }
        return new DirtyListWrapper<>(rlist);
    }
    return null;
}
Also used : DirtyListWrapper(org.apache.gora.persistency.impl.DirtyListWrapper) ArrayList(java.util.ArrayList) MapObject(com.rethinkdb.model.MapObject) ArrayList(java.util.ArrayList) List(java.util.List) MapObject(com.rethinkdb.model.MapObject)

Aggregations

DirtyListWrapper (org.apache.gora.persistency.impl.DirtyListWrapper)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 Schema (org.apache.avro.Schema)4 List (java.util.List)3 Map (java.util.Map)3 Utf8 (org.apache.avro.util.Utf8)3 IOException (java.io.IOException)2 DirtyMapWrapper (org.apache.gora.persistency.impl.DirtyMapWrapper)2 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 MapObject (com.rethinkdb.model.MapObject)1 ByteBuffer (java.nio.ByteBuffer)1 LinkedHashMap (java.util.LinkedHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Type (org.apache.avro.Schema.Type)1 GenericArray (org.apache.avro.generic.GenericArray)1 Array (org.apache.avro.generic.GenericData.Array)1 DocumentFieldType (org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType)1 BSONDecorator (org.apache.gora.mongodb.utils.BSONDecorator)1 PersistentBase (org.apache.gora.persistency.impl.PersistentBase)1