Search in sources :

Example 1 with PersistentBase

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

the class CassandraSuperColumn method getSuperValue.

private Object getSuperValue(Field field, Schema fieldSchema, Type type) {
    Object value = null;
    switch(type) {
        case ARRAY:
            List<Object> array = new ArrayList<>();
            for (HColumn<ByteBuffer, ByteBuffer> hColumn : this.hSuperColumn.getColumns()) {
                Object memberValue = fromByteBuffer(fieldSchema.getElementType(), hColumn.getValue());
                // int i = IntegerSerializer().get().fromByteBuffer(hColumn.getName());
                array.add(memberValue);
            }
            value = array;
            break;
        case MAP:
            Map<CharSequence, Object> map = new HashMap<>();
            for (HColumn<ByteBuffer, ByteBuffer> hColumn : this.hSuperColumn.getColumns()) {
                CharSequence mapKey = CharSequenceSerializer.get().fromByteBuffer(hColumn.getName());
                if (!mapKey.toString().contains(CassandraStore.UNION_COL_SUFIX)) {
                    Object memberValue = null;
                    // We need detect real type for UNION Fields
                    if (fieldSchema.getValueType().getType().equals(Type.UNION)) {
                        HColumn<ByteBuffer, ByteBuffer> cc = getUnionTypeColumn(mapKey + CassandraStore.UNION_COL_SUFIX, this.hSuperColumn.getColumns());
                        Integer unionIndex = getUnionIndex(cc);
                        Schema realSchema = fieldSchema.getValueType().getTypes().get(unionIndex);
                        memberValue = fromByteBuffer(realSchema, hColumn.getValue());
                    } else {
                        memberValue = fromByteBuffer(fieldSchema.getValueType(), hColumn.getValue());
                    }
                    map.put(mapKey, memberValue);
                }
            }
            value = map;
            break;
        case RECORD:
            String fullName = fieldSchema.getFullName();
            Class<?> claz = null;
            try {
                claz = Class.forName(fullName);
            } catch (ClassNotFoundException cnfe) {
                LOG.warn("Unable to load class " + fullName, cnfe);
                break;
            }
            try {
                value = claz.newInstance();
            } catch (InstantiationException ie) {
                LOG.warn("Instantiation error", ie);
                break;
            } catch (IllegalAccessException iae) {
                LOG.warn("Illegal access error", iae);
                break;
            }
            // we updated the value instance, now update its members
            if (value instanceof PersistentBase) {
                PersistentBase record = (PersistentBase) value;
                for (HColumn<ByteBuffer, ByteBuffer> hColumn : this.hSuperColumn.getColumns()) {
                    String memberName = StringSerializer.get().fromByteBuffer(hColumn.getName());
                    if (memberName == null || memberName.length() == 0) {
                        LOG.warn("member name is null or empty.");
                        continue;
                    }
                    if (!memberName.contains(CassandraStore.UNION_COL_SUFIX)) {
                        Field memberField = fieldSchema.getField(memberName);
                        Schema memberSchema = memberField.schema();
                        Type memberType = memberSchema.getType();
                        CassandraSubColumn cassandraColumn = new CassandraSubColumn();
                        cassandraColumn.setField(memberField);
                        cassandraColumn.setValue(hColumn);
                        if (memberType.equals(Type.UNION)) {
                            HColumn<ByteBuffer, ByteBuffer> hc = getUnionTypeColumn(memberField.name() + CassandraStore.UNION_COL_SUFIX, this.hSuperColumn.getColumns().toArray());
                            Integer unionIndex = getUnionIndex(hc);
                            cassandraColumn.setUnionType(unionIndex);
                        }
                        record.put(record.getSchema().getField(memberName).pos(), cassandraColumn.getValue());
                    }
                }
            }
            break;
        case UNION:
            int schemaPos = this.getUnionType();
            Schema unioSchema = fieldSchema.getTypes().get(schemaPos);
            Type unionType = unioSchema.getType();
            value = getSuperValue(field, unioSchema, unionType);
            break;
        default:
            Object memberValue = null;
            // Using for UnionIndex of Union type field get value. UnionIndex always Integer.  
            for (HColumn<ByteBuffer, ByteBuffer> hColumn : this.hSuperColumn.getColumns()) {
                memberValue = fromByteBuffer(fieldSchema, hColumn.getValue());
            }
            value = memberValue;
            LOG.warn("Type: " + type.name() + " not supported for field: " + field.name());
    }
    return value;
}
Also used : PersistentBase(org.apache.gora.persistency.impl.PersistentBase) HashMap(java.util.HashMap) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Field(org.apache.avro.Schema.Field) Type(org.apache.avro.Schema.Type)

Example 2 with PersistentBase

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

the class CouchDBStore method fromCouchDBRecord.

private Object fromCouchDBRecord(final Schema fieldSchema, final String docf, final Object value) {
    final Object innerValue = ((Map) value).get(docf);
    if (innerValue == null) {
        return null;
    }
    Class<?> clazz = null;
    try {
        clazz = ClassLoadingUtils.loadClass(fieldSchema.getFullName());
    } catch (ClassNotFoundException e) {
        LOG.debug(e.getMessage());
    }
    final PersistentBase record = (PersistentBase) new BeanFactoryImpl(keyClass, clazz).newPersistent();
    for (Field recField : fieldSchema.getFields()) {
        Schema innerSchema = recField.schema();
        record.put(recField.pos(), fromDBObject(innerSchema, recField, recField.name(), innerValue));
    }
    return record;
}
Also used : Field(org.apache.avro.Schema.Field) PersistentBase(org.apache.gora.persistency.impl.PersistentBase) Schema(org.apache.avro.Schema) BeanFactoryImpl(org.apache.gora.persistency.impl.BeanFactoryImpl)

Example 3 with PersistentBase

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

the class CouchDBStore method recordToCouchDB.

private Map<String, Object> recordToCouchDB(final Schema fieldSchema, final Object fieldValue) {
    final PersistentBase persistent = (PersistentBase) fieldValue;
    final Map<String, Object> newMap = new LinkedHashMap<>();
    if (persistent != null) {
        for (Field member : fieldSchema.getFields()) {
            Schema memberSchema = member.schema();
            Object memberValue = persistent.get(member.pos());
            newMap.put(member.name(), toDBObject(memberSchema, memberValue));
        }
        return newMap;
    }
    return null;
}
Also used : Field(org.apache.avro.Schema.Field) PersistentBase(org.apache.gora.persistency.impl.PersistentBase) Schema(org.apache.avro.Schema)

Example 4 with PersistentBase

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

the class MongoStore method fromMongoRecord.

@SuppressWarnings({ "unchecked", "rawtypes" })
private Object fromMongoRecord(final Schema fieldSchema, final String docf, final DBObject rec) {
    Object result;
    BSONDecorator innerBson = new BSONDecorator(rec);
    Class<?> clazz = null;
    try {
        clazz = ClassLoadingUtils.loadClass(fieldSchema.getFullName());
    } catch (ClassNotFoundException e) {
    }
    PersistentBase record = (PersistentBase) new BeanFactoryImpl(keyClass, clazz).newPersistent();
    for (Field recField : fieldSchema.getFields()) {
        Schema innerSchema = recField.schema();
        DocumentFieldType innerStoreType = mapping.getDocumentFieldType(innerSchema.getName());
        String innerDocField = mapping.getDocumentField(recField.name()) != null ? mapping.getDocumentField(recField.name()) : recField.name();
        String fieldPath = docf + "." + innerDocField;
        LOG.debug("Load from DBObject (RECORD), field:{}, schemaType:{}, docField:{}, storeType:{}", new Object[] { recField.name(), innerSchema.getType(), fieldPath, innerStoreType });
        record.put(recField.pos(), fromDBObject(innerSchema, innerStoreType, recField, innerDocField, innerBson));
    }
    result = record;
    return result;
}
Also used : Field(org.apache.avro.Schema.Field) PersistentBase(org.apache.gora.persistency.impl.PersistentBase) BSONDecorator(org.apache.gora.mongodb.utils.BSONDecorator) DocumentFieldType(org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType) Schema(org.apache.avro.Schema) BeanFactoryImpl(org.apache.gora.persistency.impl.BeanFactoryImpl)

Example 5 with PersistentBase

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

the class MongoStore method recordToMongo.

private BasicDBObject recordToMongo(final String docf, final Schema fieldSchema, final Object value) {
    BasicDBObject record = new BasicDBObject();
    for (Field member : fieldSchema.getFields()) {
        Object innerValue = ((PersistentBase) value).get(member.pos());
        String innerDoc = mapping.getDocumentField(member.name());
        Type innerType = member.schema().getType();
        DocumentFieldType innerStoreType = mapping.getDocumentFieldType(innerDoc);
        LOG.debug("Transform value to DBObject (RECORD), docField:{}, schemaType:{}, storeType:{}", new Object[] { member.name(), member.schema().getType(), innerStoreType });
        record.put(member.name(), toDBObject(docf, member.schema(), innerType, innerStoreType, innerValue));
    }
    return record;
}
Also used : Field(org.apache.avro.Schema.Field) PersistentBase(org.apache.gora.persistency.impl.PersistentBase) DocumentFieldType(org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType) Type(org.apache.avro.Schema.Type) DocumentFieldType(org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType)

Aggregations

PersistentBase (org.apache.gora.persistency.impl.PersistentBase)8 Schema (org.apache.avro.Schema)6 Field (org.apache.avro.Schema.Field)6 Type (org.apache.avro.Schema.Type)4 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 DocumentFieldType (org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType)2 BeanFactoryImpl (org.apache.gora.persistency.impl.BeanFactoryImpl)2 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 GenericArray (org.apache.avro.generic.GenericArray)1 Array (org.apache.avro.generic.GenericData.Array)1 ByteBufferInputStream (org.apache.avro.util.ByteBufferInputStream)1 ByteBufferOutputStream (org.apache.avro.util.ByteBufferOutputStream)1