Search in sources :

Example 1 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class DatastoreImpl method ensureCaps.

@Override
public void ensureCaps() {
    for (final MappedClass mc : mapper.getMappedClasses()) {
        if (mc.getEntityAnnotation() != null && mc.getEntityAnnotation().cap().value() > 0) {
            final CappedAt cap = mc.getEntityAnnotation().cap();
            final String collName = mapper.getCollectionName(mc.getClazz());
            final BasicDBObjectBuilder dbCapOpts = start("capped", true);
            if (cap.value() > 0) {
                dbCapOpts.add("size", cap.value());
            }
            if (cap.count() > 0) {
                dbCapOpts.add("max", cap.count());
            }
            final DB database = getDB();
            if (database.getCollectionNames().contains(collName)) {
                final DBObject dbResult = database.command(start("collstats", collName).get());
                if (dbResult.containsField("capped")) {
                    LOG.debug("DBCollection already exists and is capped already; doing nothing. " + dbResult);
                } else {
                    LOG.warning("DBCollection already exists with same name(" + collName + ") and is not capped; not creating capped version!");
                }
            } else {
                getDB().createCollection(collName, dbCapOpts.get());
                LOG.debug("Created capped DBCollection (" + collName + ") with opts " + dbCapOpts);
            }
        }
    }
}
Also used : CappedAt(org.mongodb.morphia.annotations.CappedAt) BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) MappedClass(org.mongodb.morphia.mapping.MappedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DB(com.mongodb.DB)

Example 2 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class DatastoreImpl method save.

protected <T> Key<T> save(final DBCollection dbColl, final T entity, final InsertOptions options) {
    if (entity == null) {
        throw new UpdateException("Can not persist a null entity");
    }
    final MappedClass mc = mapper.getMappedClass(entity);
    if (mc.getAnnotation(NotSaved.class) != null) {
        throw new MappingException(format("Entity type: %s is marked as NotSaved which means you should not try to save it!", mc.getClazz().getName()));
    }
    // involvedObjects is used not only as a cache but also as a list of what needs to be called for life-cycle methods at the end.
    final LinkedHashMap<Object, DBObject> involvedObjects = new LinkedHashMap<Object, DBObject>();
    final DBObject document = entityToDBObj(entity, involvedObjects);
    // try to do an update if there is a @Version field
    final Object idValue = document.get(Mapper.ID_KEY);
    WriteResult wr = tryVersionedUpdate(dbColl, entity, document, idValue, enforceWriteConcern(options, entity.getClass()), mc);
    if (wr == null) {
        saveDocument(dbColl, document, options);
    }
    return postSaveOperations(singletonList(entity), involvedObjects, dbColl).get(0);
}
Also used : WriteResult(com.mongodb.WriteResult) NotSaved(org.mongodb.morphia.annotations.NotSaved) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) UpdateException(org.mongodb.morphia.query.UpdateException) MappedClass(org.mongodb.morphia.mapping.MappedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MappingException(org.mongodb.morphia.mapping.MappingException) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class DatastoreImpl method merge.

@Override
@SuppressWarnings("unchecked")
public <T> Key<T> merge(final T entity, final WriteConcern wc) {
    T unwrapped = entity;
    final LinkedHashMap<Object, DBObject> involvedObjects = new LinkedHashMap<Object, DBObject>();
    final DBObject dbObj = mapper.toDBObject(unwrapped, involvedObjects);
    final Key<T> key = mapper.getKey(unwrapped);
    unwrapped = ProxyHelper.unwrap(unwrapped);
    final Object id = mapper.getId(unwrapped);
    if (id == null) {
        throw new MappingException("Could not get id for " + unwrapped.getClass().getName());
    }
    // remove (immutable) _id field for update.
    final Object idValue = dbObj.get(Mapper.ID_KEY);
    dbObj.removeField(Mapper.ID_KEY);
    WriteResult wr;
    final MappedClass mc = mapper.getMappedClass(unwrapped);
    final DBCollection dbColl = getCollection(unwrapped);
    // try to do an update if there is a @Version field
    wr = tryVersionedUpdate(dbColl, unwrapped, dbObj, idValue, new InsertOptions().writeConcern(wc), mc);
    if (wr == null) {
        final Query<T> query = (Query<T>) createQuery(unwrapped.getClass()).filter(Mapper.ID_KEY, id);
        wr = update(query, new BasicDBObject("$set", dbObj), false, false, wc).getWriteResult();
    }
    final UpdateResults res = new UpdateResults(wr);
    if (res.getUpdatedCount() == 0) {
        throw new UpdateException("Nothing updated");
    }
    dbObj.put(Mapper.ID_KEY, idValue);
    postSaveOperations(Collections.<Object>singletonList(entity), involvedObjects, dbColl, false);
    return key;
}
Also used : Query(org.mongodb.morphia.query.Query) MappedClass(org.mongodb.morphia.mapping.MappedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) UpdateResults(org.mongodb.morphia.query.UpdateResults) LinkedHashMap(java.util.LinkedHashMap) MappingException(org.mongodb.morphia.mapping.MappingException) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) WriteResult(com.mongodb.WriteResult) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) UpdateException(org.mongodb.morphia.query.UpdateException)

Example 4 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class DatastoreImpl method update.

@Override
public <T> UpdateResults update(final Query<T> query, final UpdateOperations<T> operations, final UpdateOptions options) {
    DBCollection dbColl = query.getCollection();
    // TODO remove this after testing.
    if (dbColl == null) {
        dbColl = getCollection(query.getEntityClass());
    }
    final MappedClass mc = getMapper().getMappedClass(query.getEntityClass());
    final List<MappedField> fields = mc.getFieldsAnnotatedWith(Version.class);
    DBObject queryObject = query.getQueryObject();
    if (operations.isIsolated()) {
        queryObject.put("$isolated", true);
    }
    if (!fields.isEmpty()) {
        operations.inc(fields.get(0).getNameToStore(), 1);
    }
    final BasicDBObject update = (BasicDBObject) ((UpdateOpsImpl) operations).getOps();
    if (LOG.isTraceEnabled()) {
        LOG.trace(format("Executing update(%s) for query: %s, ops: %s, multi: %s, upsert: %s", dbColl.getName(), queryObject, update, options.isMulti(), options.isUpsert()));
    }
    return new UpdateResults(dbColl.update(queryObject, update, enforceWriteConcern(options, query.getEntityClass()).getOptions()));
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) MappedClass(org.mongodb.morphia.mapping.MappedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) UpdateResults(org.mongodb.morphia.query.UpdateResults)

Example 5 with MappedClass

use of org.mongodb.morphia.mapping.MappedClass in project morphia by mongodb.

the class DatastoreImpl method ensureIndex.

@Override
@Deprecated
public <T> void ensureIndex(final Class<T> clazz, final String name, final String fields, final boolean unique, final boolean dropDupsOnCreate) {
    MappedClass mappedClass = getMapper().getMappedClass(clazz);
    ensureIndex(mappedClass.getCollectionName(), clazz, name, fields, unique, dropDupsOnCreate);
}
Also used : MappedClass(org.mongodb.morphia.mapping.MappedClass)

Aggregations

MappedClass (org.mongodb.morphia.mapping.MappedClass)92 Test (org.junit.Test)73 MappedField (org.mongodb.morphia.mapping.MappedField)56 Mapper (org.mongodb.morphia.mapping.Mapper)53 ArrayList (java.util.ArrayList)48 BasicDBObject (com.mongodb.BasicDBObject)18 DBObject (com.mongodb.DBObject)12 BsonDocument (org.bson.BsonDocument)8 Document (org.bson.Document)8 MappingException (org.mongodb.morphia.mapping.MappingException)6 DBCollection (com.mongodb.DBCollection)5 List (java.util.List)5 SimpleEntity (org.mongodb.morphia.entities.SimpleEntity)5 ObjectId (org.bson.types.ObjectId)4 Key (org.mongodb.morphia.Key)4 Index (org.mongodb.morphia.annotations.Index)4 LinkedHashMap (java.util.LinkedHashMap)3 NotSaved (org.mongodb.morphia.annotations.NotSaved)3 UpdateResults (org.mongodb.morphia.query.UpdateResults)3 WriteResult (com.mongodb.WriteResult)2