Search in sources :

Example 6 with DocumentFieldType

use of org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType 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 7 with DocumentFieldType

use of org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType in project gora by apache.

the class MongoStore method listToMongo.

/**
   * Convert a Java {@link GenericArray} as used in Gora generated classes to a
   * List that can safely be serialized into MongoDB.
   * 
   * @param array
   *          the {@link GenericArray} to be serialized
   * @param fieldType
   *          type of the elements within the array
   * @return a {@link BasicDBList} version of the {@link GenericArray} that can
   *         be safely serialized into MongoDB.
   */
private BasicDBList listToMongo(final String docf, final Collection<?> array, final Schema fieldSchema, final Type fieldType) {
    BasicDBList list = new BasicDBList();
    // Handle null case
    if (array == null)
        return list;
    // Handle regular cases
    for (Object item : array) {
        DocumentFieldType storeType = mapping.getDocumentFieldType(docf);
        Object result = toDBObject(docf, fieldSchema, fieldType, storeType, item);
        list.add(result);
    }
    return list;
}
Also used : DocumentFieldType(org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType)

Example 8 with DocumentFieldType

use of org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType in project gora by apache.

the class MongoStore method unionToMongo.

private Object unionToMongo(final String docf, final Schema fieldSchema, final DocumentFieldType storeType, final Object value) {
    // schema [type0, type1]
    Object result;
    Type type0 = fieldSchema.getTypes().get(0).getType();
    Type type1 = fieldSchema.getTypes().get(1).getType();
    // or ["type","null"]
    if (!type0.equals(type1) && (type0.equals(Type.NULL) || type1.equals(Type.NULL))) {
        Schema innerSchema = fieldSchema.getTypes().get(1);
        LOG.debug("Transform value to DBObject (UNION), schemaType:{}, type1:{}, storeType:{}", new Object[] { innerSchema.getType(), type1, storeType });
        // Deserialize as if schema was ["type"]
        result = toDBObject(docf, innerSchema, type1, storeType, value);
    } else {
        throw new IllegalStateException("MongoStore doesn't support 3 types union field yet. Please update your mapping");
    }
    return result;
}
Also used : DocumentFieldType(org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType) Type(org.apache.avro.Schema.Type) Schema(org.apache.avro.Schema)

Example 9 with DocumentFieldType

use of org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType in project gora by apache.

the class MongoStore method mapToMongo.

/**
   * Convert a Java Map as used in Gora generated classes to a Map that can
   * safely be serialized into MongoDB.
   * 
   * @param value
   *          the Java Map that must be serialized into a MongoDB object
   * @param fieldType
   *          type of the values within the map
   * @return a {@link BasicDBObject} version of the {@link Map} that can be
   *         safely serialized into MongoDB.
   */
private BasicDBObject mapToMongo(final String docf, final Map<CharSequence, ?> value, final Schema fieldSchema, final Type fieldType) {
    BasicDBObject map = new BasicDBObject();
    // Handle null case
    if (value == null)
        return map;
    // Handle regular cases
    for (Entry<CharSequence, ?> e : value.entrySet()) {
        String mapKey = e.getKey().toString();
        String encodedMapKey = encodeFieldKey(mapKey);
        Object mapValue = e.getValue();
        DocumentFieldType storeType = mapping.getDocumentFieldType(docf);
        Object result = toDBObject(docf, fieldSchema, fieldType, storeType, mapValue);
        map.put(encodedMapKey, result);
    }
    return map;
}
Also used : DocumentFieldType(org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType)

Example 10 with DocumentFieldType

use of org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType in project gora by apache.

the class MongoStore method newUpdateSetInstance.

// ////////////////////////////////////////////////////////// SERIALIZATION
/**
   * Build a new instance of {@link DBObject} from the persistence class
   * instance in parameter. Limit the {@link DBObject} to the fields that are
   * dirty and not null, that is the fields that will need to be updated in the
   * store.
   * 
   * @param persistent
   *          a persistence class instance which content is to be serialized as
   *          a {@link DBObject} for use as parameter of a $set operator
   * @return a {@link DBObject} which content corresponds to the fields that
   *         have to be updated... and formatted to be passed in parameter of a
   *         $set operator
   */
private BasicDBObject newUpdateSetInstance(final T persistent) {
    BasicDBObject result = new BasicDBObject();
    for (Field f : persistent.getSchema().getFields()) {
        if (persistent.isDirty(f.pos()) && (persistent.get(f.pos()) != null)) {
            String docf = mapping.getDocumentField(f.name());
            Object value = persistent.get(f.pos());
            DocumentFieldType storeType = mapping.getDocumentFieldType(docf);
            LOG.debug("Transform value to DBObject (MAIN), docField:{}, schemaType:{}, storeType:{}", new Object[] { docf, f.schema().getType(), storeType });
            Object o = toDBObject(docf, f.schema(), f.schema().getType(), storeType, value);
            result.put(docf, o);
        }
    }
    return result;
}
Also used : Field(org.apache.avro.Schema.Field) DocumentFieldType(org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType)

Aggregations

DocumentFieldType (org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType)11 Field (org.apache.avro.Schema.Field)5 Schema (org.apache.avro.Schema)4 BSONDecorator (org.apache.gora.mongodb.utils.BSONDecorator)4 Type (org.apache.avro.Schema.Type)3 PersistentBase (org.apache.gora.persistency.impl.PersistentBase)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Utf8 (org.apache.avro.util.Utf8)1 BeanFactoryImpl (org.apache.gora.persistency.impl.BeanFactoryImpl)1 DirtyListWrapper (org.apache.gora.persistency.impl.DirtyListWrapper)1 DirtyMapWrapper (org.apache.gora.persistency.impl.DirtyMapWrapper)1