Search in sources :

Example 1 with DocumentFieldType

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

the class MongoStore method newUpdateUnsetInstance.

/**
   * 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 null, that is the fields that will need to be updated in the
   * store by being removed.
   * 
   * @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 formated to be passed in parameter of a
   *         $unset operator
   */
private BasicDBObject newUpdateUnsetInstance(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)

Example 2 with DocumentFieldType

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

the class MongoStore method fromMongoUnion.

private Object fromMongoUnion(final Schema fieldSchema, final DocumentFieldType storeType, final Field field, final String docf, final BSONDecorator easybson) {
    // 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("Load from DBObject (UNION), schemaType:{}, docField:{}, storeType:{}", new Object[] { innerSchema.getType(), docf, storeType });
        // Deserialize as if schema was ["type"]
        result = fromDBObject(innerSchema, storeType, field, docf, easybson);
    } 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 3 with DocumentFieldType

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

the class MongoStore method fromMongoMap.

/* pp */
Object fromMongoMap(final String docf, final Schema fieldSchema, final BSONDecorator easybson, final Field f) {
    BasicDBObject map = easybson.getDBObject(docf);
    Map<Utf8, Object> rmap = new HashMap<>();
    if (map == null) {
        return new DirtyMapWrapper(rmap);
    }
    for (Entry<String, Object> e : map.entrySet()) {
        String mapKey = e.getKey();
        String decodedMapKey = decodeFieldKey(mapKey);
        DocumentFieldType storeType = mapping.getDocumentFieldType(docf);
        Object o = fromDBObject(fieldSchema.getValueType(), storeType, f, mapKey, new BSONDecorator(map));
        rmap.put(new Utf8(decodedMapKey), o);
    }
    return new DirtyMapWrapper<>(rmap);
}
Also used : DirtyMapWrapper(org.apache.gora.persistency.impl.DirtyMapWrapper) DocumentFieldType(org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType) BSONDecorator(org.apache.gora.mongodb.utils.BSONDecorator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Utf8(org.apache.avro.util.Utf8)

Example 4 with DocumentFieldType

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

the class MongoStore method fromMongoList.

/* pp */
Object fromMongoList(final String docf, final Schema fieldSchema, final BSONDecorator easybson, final Field f) {
    List<Object> list = easybson.getDBList(docf);
    List<Object> rlist = new ArrayList<>();
    if (list == null) {
        return new DirtyListWrapper(rlist);
    }
    for (Object item : list) {
        DocumentFieldType storeType = mapping.getDocumentFieldType(docf);
        Object o = fromDBObject(fieldSchema.getElementType(), storeType, f, "item", new BSONDecorator(new BasicDBObject("item", item)));
        rlist.add(o);
    }
    return new DirtyListWrapper<>(rlist);
}
Also used : DocumentFieldType(org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType) BSONDecorator(org.apache.gora.mongodb.utils.BSONDecorator) DirtyListWrapper(org.apache.gora.persistency.impl.DirtyListWrapper)

Example 5 with DocumentFieldType

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

the class MongoStore method newInstance.

// //////////////////////////////////////////////////////// DESERIALIZATION
/**
   * Build a new instance of the persisted class from the {@link DBObject}
   * retrieved from the database.
   * 
   * @param obj
   *          the {@link DBObject} that results from the query to the database
   * @param fields
   *          the list of fields to be mapped to the persistence class instance
   * @return a persistence class instance which content was deserialized from
   *         the {@link DBObject}
   */
public T newInstance(final DBObject obj, final String[] fields) {
    if (obj == null)
        return null;
    BSONDecorator easybson = new BSONDecorator(obj);
    // Create new empty persistent bean instance
    T persistent = newPersistent();
    String[] dbFields = getFieldsToQuery(fields);
    // Populate each field
    for (String f : dbFields) {
        // Check the field exists in the mapping and in the db
        String docf = mapping.getDocumentField(f);
        if (docf == null || !easybson.containsField(docf))
            continue;
        DocumentFieldType storeType = mapping.getDocumentFieldType(docf);
        Field field = fieldMap.get(f);
        Schema fieldSchema = field.schema();
        LOG.debug("Load from DBObject (MAIN), field:{}, schemaType:{}, docField:{}, storeType:{}", new Object[] { field.name(), fieldSchema.getType(), docf, storeType });
        Object result = fromDBObject(fieldSchema, storeType, field, docf, easybson);
        persistent.put(field.pos(), result);
    }
    persistent.clearDirty();
    return persistent;
}
Also used : Field(org.apache.avro.Schema.Field) BSONDecorator(org.apache.gora.mongodb.utils.BSONDecorator) DocumentFieldType(org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType) Schema(org.apache.avro.Schema)

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