Example usage for com.mongodb DBCollection getIndexInfo

List of usage examples for com.mongodb DBCollection getIndexInfo

Introduction

In this page you can find the example usage for com.mongodb DBCollection getIndexInfo.

Prototype

public List<DBObject> getIndexInfo() 

Source Link

Document

Return a list of the indexes for this collection.

Usage

From source file:com.andreig.jetty.IndexServlet.java

License:GNU General Public License

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.fine("doGet()");

    if (!can_read(req)) {
        res.sendError(SC_UNAUTHORIZED);/*  w  ww.ja v  a2 s.  c o m*/
        return;
    }

    String db_name = req.getParameter("dbname");
    String col_name = req.getParameter("colname");
    if (db_name == null || col_name == null) {
        error(res, SC_BAD_REQUEST, Status.get("param name missing"));
        return;
    }

    DB db = mongo.getDB(db_name);

    // mongo auth
    String user = req.getParameter("user");
    String passwd = req.getParameter("passwd");
    if (user != null && passwd != null && (!db.isAuthenticated())) {
        boolean auth = db.authenticate(user, passwd.toCharArray());
        if (!auth) {
            res.sendError(SC_UNAUTHORIZED);
            return;
        }
    }

    DBCollection col = db.getCollection(col_name);

    List<DBObject> info = col.getIndexInfo();
    out_json(req, info);

}

From source file:com.arhs.spring.cache.mongo.MongoCache.java

License:Open Source License

private void updateExpireIndex(Index newExpireIndex) {
    final IndexOperations indexOperations = mongoTemplate.indexOps(collectionName);
    final DBCollection collection = mongoTemplate.getCollection(collectionName);
    final List<DBObject> indexes = collection.getIndexInfo();

    final Optional<DBObject> expireOptional = indexes.stream()
            .filter(index -> INDEX_NAME.equals(index.get("name"))).findFirst();

    if (expireOptional.isPresent()) {
        final DBObject expire = expireOptional.get();
        final long ttl = (long) expire.get("expireAfterSeconds");

        if (ttl != this.ttl) {
            indexOperations.dropIndex(INDEX_NAME);
        }//from ww w . jav  a2 s.  co  m
    }

    indexOperations.ensureIndex(newExpireIndex);
}

From source file:com.cyslab.craftvm.rest.mongo.IndexServlet.java

License:GNU General Public License

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.trace("doGet()");

    if (!can_read(req)) {
        res.sendError(SC_UNAUTHORIZED);/* www .ja  va  2  s.  c o m*/
        return;
    }

    String db_name = req.getParameter("dbname");
    String col_name = req.getParameter("colname");
    if (db_name == null || col_name == null) {
        error(res, SC_BAD_REQUEST, Status.get("param name missing"));
        return;
    }

    DB db = mongo.getDB(db_name);

    // mongo auth
    String user = req.getParameter("user");
    String passwd = req.getParameter("passwd");
    if (user != null && passwd != null && (!db.isAuthenticated())) {
        boolean auth = db.authenticate(user, passwd.toCharArray());
        if (!auth) {
            res.sendError(SC_UNAUTHORIZED);
            return;
        }
    }

    DBCollection col = db.getCollection(col_name);

    List<DBObject> info = col.getIndexInfo();
    out_json(req, info);

}

From source file:com.ebay.cloud.cms.dal.persistence.flatten.impl.IndexBuildCommand.java

License:Apache License

private Map<String, DBObject> getCollectionIndexMap(PersistenceContext context, MetaClass metaClass) {
    DBCollection collection = context.getDBCollection(metaClass);
    List<DBObject> indexInfo = collection.getIndexInfo();

    Map<String, DBObject> indexMap = new HashMap<String, DBObject>();
    for (DBObject indexObject : indexInfo) {
        String name = (String) indexObject.get("name");
        indexMap.put(name, indexObject);
    }//w w w.j a v  a2s .c  om
    return indexMap;
}

From source file:com.github.maasdi.mongo.wrapper.NoAuthMongoClientWrapper.java

License:Apache License

public List<String> getIndexInfo(String dbName, String collection) throws KettleException {
    try {/*from   ww  w.  j a  v a2 s  .  c om*/
        DB db = getDb(dbName);

        if (db == null) {
            throw new Exception(
                    BaseMessages.getString(PKG, "MongoNoAuthWrapper.ErrorMessage.NonExistentDB", dbName)); //$NON-NLS-1$
        }

        if (Const.isEmpty(collection)) {
            throw new Exception(
                    BaseMessages.getString(PKG, "MongoNoAuthWrapper.ErrorMessage.NoCollectionSpecified")); //$NON-NLS-1$
        }

        if (!db.collectionExists(collection)) {
            db.createCollection(collection, null);
        }

        DBCollection coll = db.getCollection(collection);
        if (coll == null) {
            throw new Exception(
                    BaseMessages.getString(PKG, "MongoNoAuthWrapper.ErrorMessage.UnableToGetInfoForCollection", //$NON-NLS-1$
                            collection));
        }

        List<DBObject> collInfo = coll.getIndexInfo();
        List<String> result = new ArrayList<String>();
        if (collInfo == null || collInfo.size() == 0) {
            throw new Exception(
                    BaseMessages.getString(PKG, "MongoNoAuthWrapper.ErrorMessage.UnableToGetInfoForCollection", //$NON-NLS-1$
                            collection));
        }

        for (DBObject index : collInfo) {
            result.add(index.toString());
        }

        return result;
    } catch (Exception e) {
        log.logError(BaseMessages.getString(PKG, "MongoNoAuthWrapper.ErrorMessage.GeneralError.Message") //$NON-NLS-1$
                + ":\n\n" + e.getMessage(), e); //$NON-NLS-1$
        if (e instanceof KettleException) {
            throw (KettleException) e;
        } else {
            throw new KettleException(e);
        }
    }
}

From source file:com.google.api.ads.adwords.jaxws.extensions.report.model.persistence.mongodb.MongoEntityPersister.java

License:Open Source License

/**
 * Adds a list of fields as DB indexes// www.  jav  a2s.  c  o  m
 *
 * @param classT the entity T class
 */
@Override
public <T> void createIndex(Class<T> classT, List<String> keys) {
    BasicDBObject dbObject = new BasicDBObject();
    if (keys != null) {
        for (String key : keys) {
            dbObject.put(key, 1);
        }
    }
    DBCollection dbcollection = getDBCollection(classT, true);
    dbcollection.ensureIndex(dbObject);
    List<DBObject> list = dbcollection.getIndexInfo();
    for (DBObject o : list) {
        System.out.println("index: " + o);
    }
}

From source file:com.hangum.tadpole.erd.core.dnd.TableTransferDropTargetListener.java

License:Open Source License

/**
 * table?   ./*from w  w  w  . ja va2s.  co m*/
 * 
 * @param strTBName
 * @return
 * @throws Exception
 */
public List<TableColumnDAO> getColumns(String strTBName) throws Exception {
    if (DBDefine.getDBDefine(userDB.getTypes()) != DBDefine.MONGODB_DEFAULT) {
        SqlMapClient sqlClient = TadpoleSQLManager.getInstance(userDB);

        Map<String, String> param = new HashMap<String, String>();
        param.put("db", userDB.getDb());
        param.put("table", strTBName);

        return sqlClient.queryForList("tableColumnList", param);
    } else if (DBDefine.getDBDefine(userDB.getTypes()) == DBDefine.MONGODB_DEFAULT) {

        Mongo mongo = new Mongo(new DBAddress(userDB.getUrl()));
        com.mongodb.DB mongoDB = mongo.getDB(userDB.getDb());
        DBCollection coll = mongoDB.getCollection(strTBName);

        return MongoDBTableColumn.tableColumnInfo(coll.getIndexInfo(), coll.findOne());
    }

    return null;
}

From source file:com.hangum.tadpole.erd.core.utils.TadpoleModelUtils.java

License:Open Source License

/**
 * table?   .//  ww  w. java  2 s .com
 * 
 * @param strTBName
 * @return
 * @throws Exception
 */
public List<TableColumnDAO> getColumns(String db, String strTBName) throws Exception {
    if (DBDefine.getDBDefine(userDB.getTypes()) != DBDefine.MONGODB_DEFAULT) {
        SqlMapClient sqlClient = TadpoleSQLManager.getInstance(userDB);

        Map<String, String> param = new HashMap<String, String>();
        param.put("db", db);
        param.put("table", strTBName);

        return sqlClient.queryForList("tableColumnList", param);
    } else if (DBDefine.getDBDefine(userDB.getTypes()) == DBDefine.MONGODB_DEFAULT) {

        Mongo mongo = new Mongo(new DBAddress(userDB.getUrl()));
        com.mongodb.DB mongoDB = mongo.getDB(userDB.getDb());
        DBCollection coll = mongoDB.getCollection(strTBName);

        return MongoDBTableColumn.tableColumnInfo(coll.getIndexInfo(), coll.findOne());
    }

    return null;
}

From source file:com.hangum.tadpole.mongodb.core.query.MongoDBQuery.java

License:Open Source License

/**
 * collection column list/*  w  w w. ja v a  2 s . co  m*/
 * 
 * @param userDB
 * @param colName
 * @return
 * @throws Exception
 */
public static List<CollectionFieldDAO> collectionColumn(UserDBDAO userDB, String colName) throws Exception {
    DB mongoDB = MongoConnectionManager.getInstance(userDB);
    DBCollection coll = mongoDB.getCollection(colName);

    return MongoDBTableColumn.tableColumnInfo(coll.getIndexInfo(), coll.findOne());
}

From source file:com.hangum.tadpole.mongodb.core.test.ConAndAuthentication.java

License:Open Source License

/**
 * collection info//w w w  .  j  a v  a 2  s.  c o  m
 * 
 * @param db
 * @param collection
 * @return
 */
public String getCollectionInfo(DB db, String collection) {
    DBCollection coll = db.getCollection(collection);

    System.out.println("#################################################");
    System.out.println("[collection name]" + coll.getName());
    System.out.println("[count]" + coll.getCount());

    // index list
    List<DBObject> listIndex = coll.getIndexInfo();
    List<CollectionFieldDAO> columnInfo = MongoDBTableColumn.tableColumnInfo(listIndex,
            db.getCollection(collection).findOne());
    for (CollectionFieldDAO collectionFieldDAO : columnInfo) {
        System.out.println("[field]" + collectionFieldDAO.getField());

        if (!collectionFieldDAO.getChildren().isEmpty()) {
            List<CollectionFieldDAO> childColl = collectionFieldDAO.getChildren();
            for (CollectionFieldDAO collectionFieldDAO2 : childColl) {
                System.out.println("\t [child field]" + collectionFieldDAO2.getField());

                if (!collectionFieldDAO2.getChildren().isEmpty()) {
                    List<CollectionFieldDAO> childColl2 = collectionFieldDAO2.getChildren();
                    for (CollectionFieldDAO collectionFieldDAO3 : childColl2) {
                        System.out.println("\t\t [child field]" + collectionFieldDAO3.getField());
                    }

                }
            }

        }
    }

    //      //
    //      StringBuffer sbJson = new StringBuffer();
    //
    //      DBCursor cursor = coll.find();
    //      while (cursor.hasNext()) {
    //         DBObject dbObj = cursor.next();
    //         String jsonData = dbObj.toString();
    //         System.out.println("[data] \t" + jsonData);
    //
    //         sbJson.append(jsonData);
    //      }
    //
    //      System.out
    //            .println("#####[fully text]#########################################################");

    // System.out.println("\t\t ******[detail data][start]*******************************************");
    // ObjectMapper om = new ObjectMapper();
    // try {
    // Map<String, Object> mapObj = om.readValue(sbJson.toString(),
    // new TypeReference<Map<String, Object>>() {});
    // System.out.println("[json to object]" + mapObj);
    //
    // Set<String> keys = mapObj.keySet();
    // for (String key : keys) System.out.print(key + "\t:\t");
    // System.out.println();
    //
    //
    // } catch (JsonParseException e) {
    // e.printStackTrace();
    // } catch (JsonMappingException e) {
    // e.printStackTrace();
    // } catch (IOException e) {
    // e.printStackTrace();
    // }

    System.out.println("\t\t ******[detail data][end]*******************************************");

    return "";
}