Example #1
0
 @Test
 public void testDropDatabaseAlsoDropsCollectionData() throws Exception {
   DBCollection collection = newCollection();
   collection.insert(new BasicDBObject());
   collection.getDB().dropDatabase();
   assertEquals("Collection should have no data", 0, collection.count());
 }
Example #2
0
 @Test
 public void testDropCollectionAlsoDropsFromDB() throws Exception {
   DBCollection collection = newCollection();
   collection.insert(new BasicDBObject());
   collection.drop();
   assertEquals("Collection should have no data", 0, collection.count());
   assertFalse(
       "Collection shouldn't exist in DB",
       collection.getDB().getCollectionNames().contains(collection.getName()));
 }
Example #3
0
 @Test
 public void testDropDatabaseFromFongoDropsAllData() throws Exception {
   Fongo fongo = newFongo();
   DBCollection collection = fongo.getDB("db").getCollection("coll");
   collection.insert(new BasicDBObject());
   fongo.dropDatabase("db");
   assertEquals("Collection should have no data", 0, collection.count());
   assertFalse(
       "Collection shouldn't exist in DB",
       collection.getDB().getCollectionNames().contains(collection.getName()));
   assertFalse("DB shouldn't exist in fongo", fongo.getDatabaseNames().contains("db"));
 }
  private DBCollection calculateCollection(Exchange exchange) throws Exception {
    // dynamic calculation is an option. In most cases it won't be used and we should not penalise
    // all users with running this
    // resolution logic on every Exchange if they won't be using this functionality at all
    if (!endpoint.isDynamicity()) {
      return endpoint.getDbCollection();
    }

    String dynamicDB = exchange.getIn().getHeader(MongoDbConstants.DATABASE, String.class);
    String dynamicCollection =
        exchange.getIn().getHeader(MongoDbConstants.COLLECTION, String.class);

    @SuppressWarnings("unchecked")
    List<DBObject> dynamicIndex =
        exchange.getIn().getHeader(MongoDbConstants.COLLECTION_INDEX, List.class);

    DBCollection dbCol = null;

    if (dynamicDB == null && dynamicCollection == null) {
      dbCol = endpoint.getDbCollection();
    } else {
      DB db = null;

      if (dynamicDB == null) {
        db = endpoint.getDb();
      } else {
        db = endpoint.getMongoConnection().getDB(dynamicDB);
      }

      if (dynamicCollection == null) {
        dbCol = db.getCollection(endpoint.getCollection());
      } else {
        dbCol = db.getCollection(dynamicCollection);

        // on the fly add index
        if (dynamicIndex == null) {
          endpoint.ensureIndex(dbCol, endpoint.createIndex());
        } else {
          endpoint.ensureIndex(dbCol, dynamicIndex);
        }
      }
    }

    if (LOG.isDebugEnabled()) {
      LOG.debug(
          "Dynamic database and/or collection selected: {}->{}",
          dbCol.getDB().getName(),
          dbCol.getName());
    }
    return dbCol;
  }
Example #5
0
 @Test
 public void testCreateIndexes() {
   DBCollection collection = newCollection();
   collection.ensureIndex("n");
   collection.ensureIndex("b");
   List<DBObject> indexes = collection.getDB().getCollection("system.indexes").find().toArray();
   assertEquals(
       Arrays.asList(
           new BasicDBObject("v", 1)
               .append("key", new BasicDBObject("n", 1))
               .append("ns", "db.coll")
               .append("name", "n_1"),
           new BasicDBObject("v", 1)
               .append("key", new BasicDBObject("b", 1))
               .append("ns", "db.coll")
               .append("name", "b_1")),
       indexes);
 }
  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();
  }
Example #7
0
 public BsonDBCallback(DBCollection collection) {
   super(collection);
   this.db = collection == null ? null : collection.getDB();
 }
  @SuppressWarnings("rawtypes")
  public <T> MapreduceResults<T> mapReduce(
      MapreduceType type,
      Query query,
      String map,
      String reduce,
      String finalize,
      Map<String, Object> scopeFields,
      Class<T> outputType) {
    Assert.parametersNotNull("map", map);
    Assert.parameterNotEmpty(map, "map");
    Assert.parametersNotNull("reduce", reduce);
    Assert.parameterNotEmpty(reduce, "reduce");

    QueryImpl<T> qi = (QueryImpl<T>) query;

    DBCollection dbColl = qi.getCollection();
    // TODO remove this after testing.
    if (dbColl == null) dbColl = getCollection(qi.getEntityClass());

    if (log.isTraceEnabled())
      log.info(
          "Executing mapReduce("
              + dbColl.getName()
              + ") with query("
              + qi.toString()
              + ") map("
              + map
              + ") reduce("
              + reduce
              + ") finalize("
              + finalize
              + ") scope("
              + scopeFields
              + ")");

    // TODO replace this with the 2.4 driver impl.
    String outColl = mapr.getCollectionName(outputType);
    BasicDBObjectBuilder bldr =
        BasicDBObjectBuilder.start("mapreduce", mapr.getCollectionName(qi.getEntityClass()));

    switch (type) {
      case REDUCE:
        bldr.push("out").add("reduce", outColl).pop();
        break;
      case MERGE:
        bldr.push("out").add("merge", outColl).pop();
        break;
      case INLINE:
        bldr.push("out").add("inline", 1).pop();
        break;
      default:
        bldr.add("out", outColl);
        break;
    }

    if (qi.getOffset() != 0 || qi.getFieldsObject() != null)
      throw new QueryException(
          "mapReduce does not allow the offset/retrievedFields query options.");

    if (qi.getQueryObject() != null) bldr.add("query", qi.getQueryObject());
    if (qi.getLimit() > 0) bldr.add("limit", qi.getLimit());
    if (qi.getSortObject() != null) bldr.add("sort", qi.getSortObject());

    bldr.add("map", map);
    bldr.add("reduce", reduce);

    if (finalize != null && finalize.length() > 0) bldr.add("finalize", finalize);

    if (scopeFields != null && scopeFields.size() > 0)
      bldr.add("scope", mapr.toMongoObject(null, null, scopeFields));

    DBObject dbObj = bldr.get();
    CommandResult cr = dbColl.getDB().command(dbObj);
    cr.throwOnError();
    MapreduceResults mrRes =
        (MapreduceResults) mapr.fromDBObject(MapreduceResults.class, cr, createCache());

    QueryImpl baseQ = null;
    if (!MapreduceType.INLINE.equals(type))
      baseQ = new QueryImpl(outputType, db.getCollection(mrRes.getOutputCollectionName()), this);
    // TODO Handle inline case and create an iterator/able.

    mrRes.setBits(type, baseQ);
    return mrRes;
  }