Example usage for com.mongodb DBCursor iterator

List of usage examples for com.mongodb DBCursor iterator

Introduction

In this page you can find the example usage for com.mongodb DBCursor iterator.

Prototype

@Override
public Iterator<DBObject> iterator() 

Source Link

Document

Creates a copy of this cursor object that can be iterated.

Usage

From source file:com.imaginea.mongodb.services.GridFSServiceImpl.java

License:Apache License

/**
 * Service implementation for getting the list of files stored in GridFS of specified database.
 *
 * @param dbName     Name of Database/*from w w w .  j  av  a2  s  .  co m*/
 * @param bucketName Name of GridFS Bucket
 * @returns JSON representation of list of all files as a String.
 */
public ArrayList<DBObject> getFileList(String dbName, String bucketName)
        throws ValidationException, DatabaseException, CollectionException {

    mongoInstance = mongoInstanceProvider.getMongoInstance();

    if (dbName == null) {
        throw new EmptyDatabaseNameException("Database Name Is Null");
    }
    if (dbName.equals("")) {
        throw new EmptyDatabaseNameException("Database Name Empty");
    }

    ArrayList<DBObject> fileList = new ArrayList<DBObject>();

    try {
        if (!mongoInstance.getDatabaseNames().contains(dbName)) {
            throw new UndefinedDatabaseException(

                    "Database with dbName [ " + dbName + "] does not exist");
        }

        GridFS gridFS = new GridFS(mongoInstance.getDB(dbName), bucketName);
        Field field = GridFS.class.getDeclaredField("_filesCollection");
        field.setAccessible(true);
        DBCollection filesCollection = (DBCollection) field.get(gridFS);
        DBCursor cursor = filesCollection.find().sort(new BasicDBObject("uploadDate", -1));
        Iterator<DBObject> it = cursor.iterator();

        while (it.hasNext()) {
            fileList.add(it.next());
        }

    } catch (Exception m) {
        CollectionException e = new CollectionException(ErrorCodes.GET_COLLECTION_LIST_EXCEPTION,
                "GET_FILES_LIST_EXCEPTION", m.getCause());
        throw e;
    }
    return fileList;

}

From source file:com.petpet.c3po.dao.mongo.MongoIterator.java

License:Apache License

/**
 * Creates an iterator.//  w w  w.  j  a v  a2 s .c  om
 * 
 * @param deserializer
 *          the deserializer to use.
 * @param cursor
 *          the cursor to use.
 * @throws IllegalArgumentException
 *           if the desearializer is null.
 */
public MongoIterator(MongoModelDeserializer deserializer, DBCursor cursor) {
    if (cursor != null)
        this.iter = cursor.iterator();

    if (deserializer == null) {
        throw new IllegalArgumentException("Deserializer cannot be null");
    }

    this.deserializer = deserializer;
}

From source file:com.smbtec.xo.mongodb.impl.MongoDbRepositoryImpl.java

License:Apache License

@Override
public <T> ResultIterable<T> findAll(Class<T> type) {
    EntityTypeMetadata<DocumentMetadata> entityMetadata = checkEntityMetadata(type);
    String collectionName = entityMetadata.getDatastoreMetadata().getDiscriminator();
    DBCursor cursor = database.getCollection(collectionName).find();
    if (cursor == null) {
        return null;
    } else {/*from w w  w  .j a v  a2 s. com*/
        Iterator<DBObject> iterator = cursor.iterator();
        return xoSession.toResult(new ResultIterator<MongoDbDocument>() {

            @Override
            public boolean hasNext() {
                return iterator.hasNext();
            }

            @Override
            public MongoDbDocument next() {
                return new MongoDbDocument(iterator.next(), collectionName);
            }

            @Override
            public void close() {
            }
        });
    }
}

From source file:com.sube.daos.mongodb.CardMongoDaoImpl.java

License:Apache License

@Override
public List<SubeCard> getCards(SubeCard subeCard) throws InvalidSubeCardException {
    DBCollection collection = getCardCollection();
    DBCursor cursor = collection.find(getCardQuery(subeCard));
    Iterator<DBObject> iterator = cursor.iterator();
    List<SubeCard> cards = new ArrayList<SubeCard>();
    while (iterator.hasNext()) {
        DBObject dbObject = iterator.next();
        cards.add(subeCardParser.parse(dbObject));
    }//  ww  w . j a va 2s  .c o m
    return cards;
}

From source file:de.flapdoodle.mongoom.datastore.query.QueryResult.java

License:Apache License

private List<T> asList(DBCursor cursor) {
    return Lists.transform(Lists.newArrayList(cursor.iterator()), new DBObjectToEntityTransformator());
}

From source file:ezbake.locksmith.db.AbstractLocksmithManager.java

License:Apache License

protected boolean keyExists(String id) {
    DBObject projection = new BasicDBObject();
    projection.put(KEY_ID, 1);/* w ww.ja v  a2s.  c  o  m*/

    DBCursor cursor = mongoService.findInCollection(tableName, keyQuery(id), projection);

    Iterator<DBObject> it = cursor.iterator();
    return it.hasNext();
}

From source file:ezbake.locksmith.db.AesLocksmithManager.java

License:Apache License

@Override
public byte[] getKey(String keyId, String user) throws KeyNotFoundException {
    byte[] keyData = null;

    DBObject projection = new BasicDBObject();
    projection.put(KEY_ID, 1);/*from www  .  j  a v  a2s .c  o  m*/
    projection.put(KEY_DATA, 1);

    DBCursor cursor = mongoService.findInCollection(getTableName(), keyQuery(keyId, user), projection);
    Iterator<DBObject> it = cursor.iterator();

    if (it.hasNext()) {
        DBObject obj = it.next();
        keyData = (byte[]) obj.get(KEY_DATA);
    } else {
        throw new KeyNotFoundException("No key found for key ID: " + keyId + ", Owner: " + user);
    }

    return keyData;
}

From source file:ezbake.locksmith.db.RsaLocksmithManager.java

License:Apache License

@Override
public byte[] getKey(String keyId, String owner) throws KeyNotFoundException {
    byte[] result = null;

    BasicDBObject projection = new BasicDBObject();
    projection.put(KEY_ID, 1);/*from  w w  w  . ja  va  2  s.c o m*/
    projection.put(KEY_DATA, 1);

    DBCursor cursor = mongoService.findInCollection(getTableName(), keyQuery(keyId, owner), projection);
    Iterator<DBObject> it = cursor.iterator();

    if (it.hasNext()) {
        DBObject obj = it.next();
        result = (byte[]) obj.get(KEY_DATA);
    } else {
        throw new KeyNotFoundException("No key found for key ID: " + keyId + ", Owner: " + owner);
    }

    return result;
}

From source file:io.hipstogram.trident.mongodb.MongoDBMapState.java

License:Apache License

@Override
public List<T> multiGet(List<List<Object>> keys) {
    try {/*  ww  w . j  ava 2 s .c  om*/
        List<T> values = new ArrayList<T>();

        for (List<Object> rowKey : keys) {
            Query operation = mapper.retrieve(rowKey);

            DBCursor results = coll.find(operation.getQuery(), operation.getProjection());

            Iterator<DBObject> docIter = results.iterator();
            DBObject doc;
            if (results != null && docIter.hasNext() && (doc = docIter.next()) != null) {
                if (docIter.hasNext()) {
                    LOG.error("Found non-unique value for key [{}]", rowKey);
                } else {
                    values.add((T) mapper.getValue(doc));
                }
            } else {
                values.add(null);
            }
        }

        _mreads.incrBy(values.size());
        LOG.debug("Retrieving the following keys: {} with values: {}", keys, values);
        return values;
    } catch (Exception e) {
        e.printStackTrace();
        //throw new IllegalStateException("Impossible to reach this code");
        return new ArrayList<T>();
    }
}

From source file:MDBInt.DBMongo.java

License:Apache License

/**
 * function that returns all element in collection without
 * _id,credentialList, federationPassword
 *
 * @param dbName/*from  w  w w  . jav a 2  s . c  o  m*/
 * @param collectionName
 */
public void listFederatedUser(String dbName, String collectionName) {

    DBCursor cursore;
    DB dataBase;
    DBCollection collezione;
    BasicDBObject campi;
    Iterator<DBObject> it;
    dataBase = this.getDB(dbName);
    collezione = this.getCollection(dataBase, collectionName);
    campi = new BasicDBObject();
    campi.put("_id", 0);
    campi.put("crediantialList", 0);
    campi.put("federationPassword", 0);
    cursore = collezione.find(new BasicDBObject(), campi);
    it = cursore.iterator();

    while (it.hasNext()) {
        System.out.println(it.next());
    }
}