public void ensureCaps() {
   for (MappedClass mc : mapr.getMappedClasses())
     if (mc.getEntityAnnotation() != null && mc.getEntityAnnotation().cap().value() > 0) {
       CappedAt cap = mc.getEntityAnnotation().cap();
       String collName = mapr.getCollectionName(mc.getClazz());
       BasicDBObjectBuilder dbCapOpts = BasicDBObjectBuilder.start("capped", true);
       if (cap.value() > 0) dbCapOpts.add("size", cap.value());
       if (cap.count() > 0) dbCapOpts.add("max", cap.count());
       DB db = getDB();
       if (db.getCollectionNames().contains(collName)) {
         DBObject dbResult = db.command(BasicDBObjectBuilder.start("collstats", collName).get());
         if (dbResult.containsField("capped")) {
           // TODO: check the cap options.
           log.warning("DBCollection already exists is cap'd already; doing nothing. " + dbResult);
         } else {
           log.warning(
               "DBCollection already exists with same name("
                   + collName
                   + ") and is not cap'd; not creating cap'd version!");
         }
       } else {
         getDB().createCollection(collName, dbCapOpts.get());
         log.debug("Created cap'd DBCollection (" + collName + ") with opts " + dbCapOpts);
       }
     }
 }
  protected <T> void ensureIndex(
      Class<T> clazz,
      String name,
      BasicDBObject fields,
      boolean unique,
      boolean dropDupsOnCreate,
      boolean background,
      boolean sparse) {
    // validate field names and translate them to the stored values
    BasicDBObject keys = new BasicDBObject();
    for (Entry<String, Object> entry : fields.entrySet()) {
      StringBuffer sb = new StringBuffer(entry.getKey());
      Mapper.validate(clazz, mapr, sb, FilterOperator.IN, "", true, false);
      keys.put(sb.toString(), entry.getValue());
    }

    BasicDBObjectBuilder keyOpts = new BasicDBObjectBuilder();
    if (name != null && name.length() > 0) {
      keyOpts.add("name", name);
    }
    if (unique) {
      keyOpts.add("unique", true);
      if (dropDupsOnCreate) keyOpts.add("dropDups", true);
    }

    if (background) keyOpts.add("background", true);
    if (sparse) keyOpts.add("sparse", true);

    DBCollection dbColl = getCollection(clazz);

    BasicDBObject opts = (BasicDBObject) keyOpts.get();
    if (opts.isEmpty()) {
      log.debug("Ensuring index for " + dbColl.getName() + " with keys:" + keys);
      dbColl.ensureIndex(keys);
    } else {
      log.debug(
          "Ensuring index for " + dbColl.getName() + " with keys:" + keys + " and opts:" + opts);
      dbColl.ensureIndex(keys, opts);
    }

    // TODO: remove this once using 2.4 driver does this in ensureIndex
    CommandResult cr = dbColl.getDB().getLastError();
    cr.throwOnError();
  }