private DBCollection getCollection(final String kind) { if (kind == null) { return null; } return getDB().getCollection(kind); }
@Override public <T> Iterable<Key<T>> insert(final String collection, final Iterable<T> entities, final InsertOptions options) { return insert(getDB().getCollection(collection), entities, options); }
@Override public DBCollection getCollection(final Class clazz) { final String collName = mapper.getCollectionName(clazz); return getDB().getCollection(collName); }
@Override public <T> Query<T> createQuery(final String collection, final Class<T> type) { return newQuery(type, getDB().getCollection(collection)); }
@Override public <T> Query<T> queryByExample(final String collection, final T ex) { return queryByExample(getDB().getCollection(collection), ex); }
@Override public AggregationPipeline createAggregation(final String collection, final Class<?> clazz) { return new AggregationPipelineImpl(this, getDB().getCollection(collection), clazz); }
@Override public <T> Iterable<Key<T>> insert(final String collection, final Iterable<T> entities, final WriteConcern wc) { return insert(getDB().getCollection(collection), entities, new InsertOptions() .writeConcern(wc)); }
@Override public <T> T get(final Class<T> clazz, final DBRef ref) { DBObject object = getDB().getCollection(ref.getCollectionName()).findOne(new BasicDBObject("_id", ref.getId())); return mapper.fromDBObject(this, clazz, object, createCache()); }
@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 (((CommandResult) dbResult).getBoolean("capped")) { LOG.debug("DBCollection already exists and is capped already; doing nothing. " + dbResult); } else { LOG.warn("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); } } } }
@Override public <T> MapreduceResults<T> mapReduce(final MapReduceOptions<T> options) { DBCollection collection = options.getQuery().getCollection(); final EntityCache cache = createCache(); MapreduceResults<T> results = new MapreduceResults<T>(collection.mapReduce(options.toCommand(getMapper()))); results.setOutputType(options.getOutputType()); if (OutputType.INLINE.equals(options.getOutputType())) { results.setInlineRequiredOptions(this, options.getResultType(), getMapper(), cache); } else { results.setQuery(newQuery(options.getResultType(), getDB().getCollection(results.getOutputCollectionName()))); } return results; }
void process(final MappedClass mc, final Validation validation) { if (validation != null) { String collectionName = mc.getCollectionName(); CommandResult result = getDB() .command(new BasicDBObject("collMod", collectionName) .append("validator", parse(validation.value())) .append("validationLevel", validation.level().getValue()) .append("validationAction", validation.action().getValue()) ); if (!result.ok()) { if (result.getInt("code") == 26) { ValidationOptions options = new ValidationOptions() .validator(parse(validation.value())) .validationLevel(validation.level()) .validationAction(validation.action()); getDatabase().createCollection(collectionName, new CreateCollectionOptions().validationOptions(options)); } else { result.throwOnError(); } } } }
results.setInlineRequiredOptions(this, outputType, getMapper(), cache); } else { results.setQuery(newQuery(outputType, getDB().getCollection(results.getOutputCollectionName())));