Пример #1
0
  @Test
  public void testStore() {
    HasExpiryField hasExpiryField = new HasExpiryField();
    hasExpiryField.setOfferIs("Good");
    Calendar c = Calendar.getInstance();
    hasExpiryField.setOfferExpiresAt(c.getTime());
    ds.getMapper().addMappedClass(HasExpiryField.class);
    ds.ensureIndexes();
    ds.save(hasExpiryField);

    DB db = ds.getDB();
    DBCollection dbCollection = db.getCollection("HasExpiryField");
    List<DBObject> indexes = dbCollection.getIndexInfo();

    Assert.assertNotNull(indexes);
    Assert.assertEquals(2, indexes.size());
    DBObject index = null;
    for (DBObject candidateIndex : indexes) {
      if (candidateIndex.containsField("expireAfterSeconds")) {
        index = candidateIndex;
      }
    }
    Assert.assertNotNull(index);
    Assert.assertTrue(index.containsField("expireAfterSeconds"));
    Assert.assertEquals(60, index.get("expireAfterSeconds"));
  }
Пример #2
0
  @Test
  public void shouldIndexSomeCollections() throws Exception {

    String feedName = "feed2";
    String channelName = "channel1";
    String configPath = "plugin_config/indexing_task_skip_channels.json";

    Channel channel = getConfiguredChannel(configPath, feedName, channelName);
    pushDataToChannel(channel, "v", 5000, 1, TimeUnit.SECONDS);

    // Wait for the task to complete
    Thread.sleep(2000);

    // Get the collections for the feed
    DB feedDB = this.testClient.getDB(feedName);
    Set<String> collNames = feedDB.getCollectionNames();
    assertEquals("Should have 5 data collections + system.indexes", 6, collNames.size());

    int indexedCount = 0;
    for (String collName : collNames) {
      if (collName.equals("system.indexes") == false) {
        DBCollection coll = feedDB.getCollection(collName);
        List<DBObject> indexes = coll.getIndexInfo();
        if (indexes.size() == 2) {
          assertEquals("Should have data.v_1 index", indexes.get(1).get("name"), "data.v_1");
          indexedCount++;
        }
      }
    }

    assertEquals("Should 3 indexed collections", 3, indexedCount);
  }
  @Test
  public void testEnsureIndex() throws Exception {

    Person p1 = new Person("Oliver");
    p1.setAge(25);
    template.insert(p1);
    Person p2 = new Person("Sven");
    p2.setAge(40);
    template.insert(p2);

    template
        .indexOps(Person.class)
        .ensureIndex(new Index().on("age", Order.DESCENDING).unique(Duplicates.DROP));

    DBCollection coll = template.getCollection(template.getCollectionName(Person.class));
    List<DBObject> indexInfo = coll.getIndexInfo();
    assertThat(indexInfo.size(), is(2));
    String indexKey = null;
    boolean unique = false;
    boolean dropDupes = false;
    for (DBObject ix : indexInfo) {
      if ("age_-1".equals(ix.get("name"))) {
        indexKey = ix.get("key").toString();
        unique = (Boolean) ix.get("unique");
        dropDupes = (Boolean) ix.get("dropDups");
      }
    }
    assertThat(indexKey, is("{ \"age\" : -1}"));
    assertThat(unique, is(true));
    assertThat(dropDupes, is(true));

    List<IndexInfo> indexInfoList = template.indexOps(Person.class).getIndexInfo();
    System.out.println(indexInfoList);
    assertThat(indexInfoList.size(), is(2));
    IndexInfo ii = indexInfoList.get(1);
    assertThat(ii.isUnique(), is(true));
    assertThat(ii.isDropDuplicates(), is(true));
    assertThat(ii.isSparse(), is(false));
    assertThat(ii.getFieldSpec().containsKey("age"), is(true));
    assertThat(ii.getFieldSpec().containsValue(Order.DESCENDING), is(true));
  }
Пример #4
0
  public static void main(String[] args) throws UnknownHostException {

    MongoClient mongo = new MongoClient("10.66.218.46", 27017);
    DB db = mongo.getDB("mydb");

    Set<String> collectionNames = db.getCollectionNames();
    for (String s : collectionNames) {
      System.out.println(s);
    }

    DBCollection conn = db.getCollection("testCollection");

    conn.drop();

    BasicDBObject doc =
        new BasicDBObject()
            .append("name", "MongoDB")
            .append("type", "database")
            .append("count", 1)
            .append("info", new BasicDBObject("x", 203).append("y", 102));

    conn.insert(doc);

    DBObject myDoc = conn.findOne();

    System.out.println(myDoc);

    for (int i = 0; i < 100; i++) {
      conn.insert(new BasicDBObject().append("i", i));
    }
    System.out.println(
        "total # of documents after inserting 100 small ones (should be 101) " + conn.getCount());

    // now use a query to get 1 document out
    DBCursor cursor = conn.find();
    try {
      while (cursor.hasNext()) {
        System.out.println(cursor.next());
      }
    } finally {
      cursor.close();
    }

    // now use a range query to get a larger subset
    BasicDBObject query = new BasicDBObject("i", 71);
    cursor = conn.find(query);

    try {
      while (cursor.hasNext()) {
        System.out.println(cursor.next());
      }
    } finally {
      cursor.close();
    }

    // range query with multiple constraints
    query =
        new BasicDBObject(
            "i", new BasicDBObject("$gt", 20).append("$lte", 30)); // i.e. 20 < i <= 30
    cursor = conn.find(query);
    try {
      while (cursor.hasNext()) {
        System.out.println(cursor.next());
      }
    } finally {
      cursor.close();
    }

    // now use a range query to get a larger subset
    query = new BasicDBObject("i", new BasicDBObject("$gt", 50)); // i.e. find all where i > 50
    cursor = conn.find(query);

    try {
      while (cursor.hasNext()) {
        System.out.println(cursor.next());
      }
    } finally {
      cursor.close();
    }

    // create an index on the "i" field
    conn.createIndex(new BasicDBObject("i", 1)); // create index on "i", ascending

    // list the indexes on the collection
    List<DBObject> list = conn.getIndexInfo();
    for (DBObject o : list) {
      System.out.println(o);
    }

    // See if the last operation had an error
    System.out.println("Last error : " + db.getLastError());

    // see if any previous operation had an error
    System.out.println("Previous error : " + db.getPreviousError());

    // force an error
    db.forceError();

    // See if the last operation had an error
    System.out.println("Last error : " + db.getLastError());

    db.resetError();

    mongo.close();
  }