/** * @param kind * @return */ protected DBCollection getCollection(String kind) { if (kind == null) return null; return getDB().getCollection(kind); }
/** * @param clazz * @return */ @SuppressWarnings("rawtypes") public DBCollection getCollection(Class clazz) { String collName = mapr.getCollectionName(clazz); DBCollection dbC = getDB().getCollection(collName); return dbC; }
/** * @param clazz * @param id * @param <T> * @param <V> * @return */ public <T, V> DBRef createRef(Class<T> clazz, V id) { if (id == null) throw new MappingException("Could not get id for " + clazz.getName()); return new DBRef(getDB(), getCollection(clazz).getName(), id); }
/** * */ public void ensureCaps() { for (MappedClass mc : mapr.getMappedClasses()) { CappedAt cap = mc.getCappedAt(); if (cap != null && cap.value() > 0) { 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); } } } }