Exemple #1
0
  public MongoCache(String addresses, String database, String collection, boolean slaveok) {

    List<ServerAddress> addr = new ArrayList<ServerAddress>();
    String[] hosts = addresses.split("\\n");

    for (int i = 0; i < hosts.length; i++) {
      try {
        addr.add(new ServerAddress(hosts[i]));
      } catch (UnknownHostException e) {
        e.printStackTrace();
      }
    }

    _mongo = new Mongo(addr);

    // if using replica sets then allow query of slaves instead of just master
    if (slaveok) {
      _mongo.slaveOk();
    }

    _db = _mongo.getDB(database);
    _coll = _db.getCollection(collection);

    // create the indexes
    _coll.ensureIndex(new BasicDBObject("key", 1));
    _coll.ensureIndex(new BasicDBObject("expires", 1));
    _coll.ensureIndex(new BasicDBObject("tags", 1));
  }
  @Test
  public void testExplain() {
    DBCollection c = _db.getCollection("explain1");
    c.drop();

    for (int i = 0; i < 100; i++) c.save(new BasicDBObject("x", i));

    DBObject q = BasicDBObjectBuilder.start().push("x").add("$gt", 50).get();

    assertEquals(49, c.find(q).count());
    assertEquals(49, c.find(q).itcount());
    assertEquals(49, c.find(q).toArray().size());
    assertEquals(49, c.find(q).itcount());
    assertEquals(20, c.find(q).limit(20).itcount());
    assertEquals(20, c.find(q).limit(-20).itcount());

    c.ensureIndex(new BasicDBObject("x", 1));

    assertEquals(49, c.find(q).count());
    assertEquals(49, c.find(q).toArray().size());
    assertEquals(49, c.find(q).itcount());
    assertEquals(20, c.find(q).limit(20).itcount());
    assertEquals(20, c.find(q).limit(-20).itcount());

    assertEquals(49, c.find(q).explain().get("n"));

    assertEquals(20, c.find(q).limit(20).explain().get("n"));
    assertEquals(20, c.find(q).limit(-20).explain().get("n"));
  }
  @Override
  public void ensureIndexes(DBCollection eventsCollection, DBCollection snapshotsCollection) {
    eventsCollection.ensureIndex(
        new BasicDBObject(CommitEntry.AGGREGATE_IDENTIFIER_PROPERTY, 1)
            .append(CommitEntry.SEQUENCE_NUMBER_PROPERTY, 1),
        "uniqueAggregateIndex",
        true);

    eventsCollection.ensureIndex(
        new BasicDBObject(CommitEntry.TIME_STAMP_PROPERTY, 1)
            .append(CommitEntry.SEQUENCE_NUMBER_PROPERTY, 1),
        "orderedEventStreamIndex",
        false);
    snapshotsCollection.ensureIndex(
        new BasicDBObject(CommitEntry.AGGREGATE_IDENTIFIER_PROPERTY, 1)
            .append(CommitEntry.SEQUENCE_NUMBER_PROPERTY, 1),
        "uniqueAggregateIndex",
        true);
  }
  @Test
  public void testSpecial() {
    DBCollection c = _db.getCollection("testSpecial");
    c.insert(new BasicDBObject("x", 1));
    c.insert(new BasicDBObject("x", 2));
    c.insert(new BasicDBObject("x", 3));
    c.ensureIndex("x");

    for (DBObject o : c.find().sort(new BasicDBObject("x", 1)).addSpecial("$returnKey", 1))
      assertTrue(o.get("_id") == null);

    for (DBObject o : c.find().sort(new BasicDBObject("x", 1))) assertTrue(o.get("_id") != null);
  }