Search in sources :

Example 1 with BSONDecorator

use of org.apache.gora.mongodb.utils.BSONDecorator 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 2 with BSONDecorator

use of org.apache.gora.mongodb.utils.BSONDecorator 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 3 with BSONDecorator

use of org.apache.gora.mongodb.utils.BSONDecorator 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)

Example 4 with BSONDecorator

use of org.apache.gora.mongodb.utils.BSONDecorator in project gora by apache.

the class TestMongoStore method testFromMongoList_empty.

@Test
public void testFromMongoList_empty() throws Exception {
    MongoStore store = new MongoStore();
    String field = "myField";
    BasicDBObject emptyField = new BasicDBObject(field, new BasicDBList());
    Object item = store.fromMongoList(field, null, new BSONDecorator(emptyField), null);
    assertNotNull(item);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) BSONDecorator(org.apache.gora.mongodb.utils.BSONDecorator) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 5 with BSONDecorator

use of org.apache.gora.mongodb.utils.BSONDecorator in project gora by apache.

the class TestMongoStore method testFromMongoList_null.

@Test
public void testFromMongoList_null() throws Exception {
    MongoStore store = new MongoStore();
    BasicDBObject noField = new BasicDBObject();
    String field = "myField";
    Object item = store.fromMongoList(field, null, new BSONDecorator(noField), null);
    assertNotNull(item);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BSONDecorator(org.apache.gora.mongodb.utils.BSONDecorator) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Aggregations

BSONDecorator (org.apache.gora.mongodb.utils.BSONDecorator)8 BasicDBObject (com.mongodb.BasicDBObject)4 DocumentFieldType (org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType)4 Test (org.junit.Test)4 Schema (org.apache.avro.Schema)2 Field (org.apache.avro.Schema.Field)2 BasicDBList (com.mongodb.BasicDBList)1 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 PersistentBase (org.apache.gora.persistency.impl.PersistentBase)1