Search in sources :

Example 1 with CappedAt

use of org.mongodb.morphia.annotations.CappedAt 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)

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)1 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)1 DB (com.mongodb.DB)1 DBObject (com.mongodb.DBObject)1 CappedAt (org.mongodb.morphia.annotations.CappedAt)1 MappedClass (org.mongodb.morphia.mapping.MappedClass)1