Java 类com.mongodb.client.ListIndexesIterable 实例源码

项目:restheart    文件:IndexDAO.java   
/**
 *
 * @param dbName
 * @param collName
 * @return
 */
List<BsonDocument> getCollectionIndexes(String dbName, String collName) {
    List<BsonDocument> ret = new ArrayList<>();

    ListIndexesIterable<Document> indexes = client
            .getDatabase(dbName)
            .getCollection(collName, BsonDocument.class)
            .listIndexes();

    indexes.iterator().forEachRemaining(
            i -> {
                BsonDocument bi = BsonDocument.parse(i.toJson());

                BsonValue name = bi.remove("name");
                bi.put("_id", name);

                ret.add(bi);
            });

    return ret;
}
项目:nifi-nars    文件:AbstractMongoProcessor.java   
public static Set<String> getIndexNames(MongoCollection<Document> collection) {
    ListIndexesIterable<Document> currentIndexes = collection.listIndexes();
    MongoCursor<Document> cursor = currentIndexes.iterator();
    Set<String> indexNames = new HashSet<>();
    while (cursor.hasNext()) {
        Object next = cursor.next();
        String name = ((Document) next).get("name").toString();
        indexNames.add(name);
    }
    return indexNames;
}
项目:nifi-nars    文件:StoreInMongoIT.java   
@Test
public void emptySecondaryIndexTest() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new StoreInMongo());

    addMongoService(runner);
    runner.setProperty(MongoProps.DATABASE, MONGO_DATABASE_NAME);
    runner.setProperty(MongoProps.COLLECTION, "index_test");
    // runner.setProperty(MongoProps.INDEX, "[{\"a\": 1}]");

    runner.run();

    // Connect to mongo to inspect indexes
    MongoClient client = mongo.getMongoClient();
    ListIndexesIterable<Document> indexes = client.getDatabase(MONGO_DATABASE_NAME).getCollection("index_test").listIndexes();

    List<Document> indexesArray = new ArrayList<>();
    indexesArray = indexes.into(indexesArray);

    // Check for compound index
    boolean hasCompound = false;
    for (Document doc : indexesArray) {
        if (doc.get("name").toString().equals("t_1_s_1")) {
            hasCompound = true;
        }
    }
    assertFalse("Should not have compound index", hasCompound);
}
项目:mongo-obj-framework    文件:SmofCollectionImpl.java   
private Set<InternalIndex> loadDBIndexes() {
    final ListIndexesIterable<BsonDocument> bsonIndexes = collection.listIndexes(BsonDocument.class);
    final Set<InternalIndex> indexes = Sets.newLinkedHashSet();
    for(BsonDocument bsonIndex : bsonIndexes) {
        indexes.add(InternalIndex.fromBson(bsonIndex));
    }
    return indexes;
}
项目:Rapture    文件:MongoIndexHandler.java   
private void createIndex(IndexDefinition indexDefinition, boolean force) {
    // create index if not exists... use index name too from indexDefinition
    // object
    if (indexDefinition == null) {
        return;
    }

    String indexName = IndexNameFactory.createIndexName(indexDefinition);
    MongoCollection<Document> collection = MongoDBFactory.getCollection(instanceName, tableName);

    int limit = Integer.parseInt(
            MultiValueConfigLoader.getConfig("MONGODB-" + instanceName + ".limit", MultiValueConfigLoader.getConfig("MONGODB-default.limit", "250")));

    boolean indexExists = false;
    ListIndexesIterable<Document> indexInfo = collection.listIndexes();
    for (Document dbObject : indexInfo) {
        Object name = dbObject.get("name");
        if (name != null && name.toString().equals(indexName)) {
            indexExists = true;
            break;
        }
    }
    if (force || collection.count() < limit) {
        createIt(indexDefinition, force, indexName, collection);
    } else {
        if (!indexExists) {
            log.warn(tableName + " collection has more than " + limit + " items. please index manually");
        }
    }
}
项目:mogodb-dao    文件:MongoDaoImpl.java   
@Override
public Map<String, Object> queryIndex () {
    // get current collection name
    String collectionNameForT = getCollectionName(collectionNames.get());

    // Indexes Iterator
    ListIndexesIterable iterable = database.getCollection(collectionNameForT).listIndexes();

    // cycle the indexes
    Iterator iterator = iterable.iterator();

    List<String> indexes    = new ArrayList<String>();
    List<Keys>   keys       = new ArrayList<Keys>();
    Map<String, Object> map = new HashMap<String, Object>();

    while (iterator.hasNext()) {
        // read value
        Document document = (Document) iterator.next();
        Keys indexKey = new Keys();

        // cycle key
        for (String key :document.get("key", Document.class).keySet()) {
            indexes.add(key);
            indexKey.setKey(key);
            // key's order value
            indexKey.setOrder(document.get("key", Document.class).getInteger(key));
        }

        indexKey.setName(document.getString("name"));
        indexKey.setNs(document.getString("ns"));
        indexKey.setBackground(document.getBoolean("background"));
        indexKey.setSparse(document.getBoolean("sparse"));

        keys.add(indexKey);
    }
    map.put("indexes", indexes);
    map.put("keys", keys);

    // gc
    gc();

    return map;
}
项目:ibm-performance-monitor    文件:ProfiledMongoCollection.java   
@Override
public ListIndexesIterable<Document> listIndexes()
{
    return collection.listIndexes();
}
项目:ibm-performance-monitor    文件:ProfiledMongoCollection.java   
@Override
public <TResult> ListIndexesIterable<TResult> listIndexes(Class<TResult> arg0)
{
    return collection.listIndexes(arg0);
}
项目:Camel    文件:MongoDbIndexTest.java   
@Test
public void testInsertDynamicityEnabledDBAndCollectionAndIndex() {
    assertEquals(0, testCollection.count());
    mongo.getDatabase("otherDB").drop();
    db.getCollection("otherCollection").drop();
    assertFalse("The otherDB database should not exist",
            StreamSupport.stream(mongo.listDatabaseNames().spliterator(), false)
                    .anyMatch("otherDB"::equals));

    String body = "{\"_id\": \"testInsertDynamicityEnabledDBAndCollection\", \"a\" : 1, \"b\" : 2}";
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(MongoDbConstants.DATABASE, "otherDB");
    headers.put(MongoDbConstants.COLLECTION, "otherCollection");

    List<DBObject> objIndex = new ArrayList<DBObject>();
    DBObject index1 = new BasicDBObject();
    index1.put("a", 1);
    DBObject index2 = new BasicDBObject();
    index2.put("b", -1);
    objIndex.add(index1);
    objIndex.add(index2);

    headers.put(MongoDbConstants.COLLECTION_INDEX, objIndex);

    Object result = template.requestBodyAndHeaders("direct:dynamicityEnabled", body, headers);

    assertEquals("Response isn't of type WriteResult", BasicDBObject.class, result.getClass());

    MongoCollection<BasicDBObject> dynamicCollection = mongo.getDatabase("otherDB").getCollection("otherCollection", BasicDBObject.class);

    ListIndexesIterable<DBObject> indexInfos = dynamicCollection.listIndexes(DBObject.class);

    MongoCursor<DBObject> iterator = indexInfos.iterator();
    iterator.next();
    BasicDBObject key1 = (BasicDBObject) iterator.next().get("key");
    BasicDBObject key2 = (BasicDBObject) iterator.next().get("key");

    assertTrue("No index on the field a", key1.containsField("a") && "1".equals(key1.getString("a")));
    assertTrue("No index on the field b", key2.containsField("b") && "-1".equals(key2.getString("b")));

    DBObject b = dynamicCollection.find(new BasicDBObject("_id", "testInsertDynamicityEnabledDBAndCollection")).first();
    assertNotNull("No record with 'testInsertDynamicityEnabledDBAndCollection' _id", b);

    b = testCollection.find(new BasicDBObject("_id", "testInsertDynamicityEnabledDBOnly")).first();
    assertNull("There is a record with 'testInsertDynamicityEnabledDBAndCollection' _id in the test collection", b);

    assertTrue("The otherDB database should exist",
            StreamSupport.stream(mongo.listDatabaseNames().spliterator(), false)
                    .anyMatch("otherDB"::equals));
}
项目:spring-dao-mongo    文件:Tester.java   
@Test
public void testUpdate() {
    TestObject o = new TestObject();
    o.setName("bar");
    o.setValue(new BigDecimal("5"));
    Assert.assertEquals("bar", o.getName());
    String id = o.getId();

    TestObject save = Tester.dao.save(o);
    Assert.assertEquals("bar", save.getName());
    ABaseTest.assertEquals(new BigDecimal("5"), save.getValue());
    Assert.assertNotNull(save.getId());
    Assert.assertNotNull(save.getDt());

    TestObject find = Tester.dao.findById(id);
    Assert.assertNotNull(find);
    Assert.assertEquals("bar", find.getName());
    ABaseTest.assertEquals(new BigDecimal("5"), find.getValue());
    Assert.assertEquals(id, find.getId());
    Assert.assertNotNull(find.getDt());

    find.setName("blubb");

    TestObject save2 = Tester.dao.save(find);
    Assert.assertNotNull(save2);
    Assert.assertEquals("blubb", save2.getName());
    ABaseTest.assertEquals(new BigDecimal("5"), save2.getValue());
    Assert.assertEquals(id, save2.getId());
    Assert.assertNotNull(save2.getDt());

    TestObject find3 = Tester.dao.findByName("blubb");
    Assert.assertNotNull(find3);
    Assert.assertEquals("blubb", find3.getName());
    ABaseTest.assertEquals(new BigDecimal("5"), find3.getValue());
    Assert.assertEquals(id, find3.getId());
    Assert.assertNotNull(find3.getDt());

    Tester.dao.delete(id);

    TestObject find2 = Tester.dao.findById(id);
    Assert.assertNull(find2);

    ListIndexesIterable<Document> listIndexes = ABaseTest.mongo.getDatabase(ABaseTest.dbName).getCollection("TestObject").listIndexes();
    MongoCursor<Document> iterator = listIndexes.iterator();
    while (iterator.hasNext()) {
        Object index = iterator.next();
        System.out.println(index.toString());
    }
}