Example usage for com.mongodb DBCursor hasNext

List of usage examples for com.mongodb DBCursor hasNext

Introduction

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

Prototype

@Override
public boolean hasNext() 

Source Link

Document

Checks if there is another object available.

Usage

From source file:com.ebay.cloud.cms.metadata.mongo.MongoMetadataServiceImpl.java

License:Apache License

@Override
public final List<MetaClass> getMetaClasses(MetadataContext ctx) {
    MetadataContext context = ctx != null ? ctx : new MetadataContext();
    ArrayList<MetaClass> result = new ArrayList<MetaClass>();

    if (!context.isRefreshMetadata()) {
        return cacheManager.getMetaClassesFromCache();
    }/*from w ww  . ja v  a 2 s .c o  m*/

    DBCursor cursor = collection.find();
    while (cursor.hasNext()) {
        DBObject o = cursor.next();
        MetaClass m = converter.fromBson(o, MetaClass.class);
        result.add(m);
    }

    MetaClassGraph newGraph = new MetaClassGraph(result);
    ExpirableCache<MetaClass> newNameCache = new ExpirableCache<MetaClass>(maxCacheSize, cacheExpiredTime);
    ExpirableCache<MetaClass> pluralNameCache = new ExpirableCache<MetaClass>(maxCacheSize, cacheExpiredTime);
    for (MetaClass m : result) {
        setUpMetaClass(m, newGraph);
        cacheManager.addMetaClassToCache(m, newNameCache, pluralNameCache);
        newGraph.updateMetaClass(m);
    }
    // ## this might still not thread safe between the cache and graphs. But the time window should be small enough
    this.graph = newGraph;
    cacheManager.refreshCache(newNameCache, pluralNameCache);
    return result;
}

From source file:com.ebay.cloud.cms.metadata.mongo.MongoRepositoryServiceImpl.java

License:Apache License

@Override
public List<Repository> getRepositories(MetadataContext ctx) {
    MetadataContext context = ctx != null ? ctx : new MetadataContext();
    if (!context.isRefreshRepsitory()) {
        return cache.values();
    }//from  w ww .  j  a  va 2  s . com

    ExpirableCache<Repository> newCache = new ExpirableCache<Repository>(maxCacheSize, cacheExpiredTime);
    BasicDBObject query = new BasicDBObject();
    query.put(Repository.STATE_FIELD, Repository.StateEnum.normal.toString());

    List<Repository> result = new ArrayList<Repository>();
    DBCursor cursor = repoCollection.find(query);
    while (cursor.hasNext()) {
        DBObject object = cursor.next();
        Repository r = repositoryConverter.fromBson(object, Repository.class);
        createServiceForRepository(r);
        result.add(r);
        newCache.putObject(r.getRepositoryName(), r);
    }
    cache = newCache;
    return result;
}

From source file:com.ebay.jetstream.config.mongo.MongoDAO.java

License:MIT License

public static List<JetStreamBeanConfigurationDo> findConfigurationByAppNameAndVersion(BasicDBObject query,
        MongoConnection mongoConnection) {

    List<JetStreamBeanConfigurationDo> beanConfigs = new ArrayList<JetStreamBeanConfigurationDo>();
    List<BasicDBObject> dbObjects = new ArrayList<BasicDBObject>();
    DBCollection dbCol = mongoConnection.getDBCollection();

    if (dbCol == null) {
        throw new MongoConfigRuntimeException("jetstreamconfig collection is unknown");
    }/*from ww w  .j av  a 2 s  . c o m*/

    Exception e = null;
    DBCursor cur = null;
    try {
        cur = (query == null ? dbCol.find() : dbCol.find(query));
        while (cur.hasNext()) {
            dbObjects.add((BasicDBObject) cur.next());
        }

        for (BasicDBObject dbObject : dbObjects) {
            String jsonString = dbObject.toString();
            beanConfigs.add(unMarshalJSONResponse(jsonString));
        }
    } catch (Exception err) {
        e = err;
        throw new MongoConfigRuntimeException(err);
    } finally {
        if (cur != null) {
            cur.close();
        }
    }

    return beanConfigs;
}

From source file:com.ebay.jetstream.config.mongo.MongoDAO.java

License:MIT License

public static List<JetStreamBeanConfigurationDo> findConfigurationByQuery(BasicDBObject query,
        MongoConnection mongoConnection) {

    List<JetStreamBeanConfigurationDo> beanConfigs = new ArrayList<JetStreamBeanConfigurationDo>();
    List<BasicDBObject> dbObjects = new ArrayList<BasicDBObject>();
    DBCollection dbCol = mongoConnection.getDBCollection();

    if (dbCol == null) {
        throw new MongoConfigRuntimeException("jetstreamconfig collection is unknown");
    }//from w  ww  .  ja v  a  2s.c o m

    Exception e = null;

    DBCursor cur = null;
    try {
        cur = (query == null ? dbCol.find() : dbCol.find(query));
        while (cur.hasNext()) {
            dbObjects.add((BasicDBObject) cur.next());
        }

        for (BasicDBObject dbObject : dbObjects) {
            String jsonString = dbObject.toString();
            beanConfigs.add(unMarshalJSONResponse(jsonString));
            //beanConfig = (JetStreamBeanConfigurationDo)fromJson(jsonString, JetStreamBeanConfigurationDo.class);
        }
    } catch (Exception err) {
        e = err;
        throw new MongoConfigRuntimeException(err);
    } finally {
        if (cur != null) {
            cur.close();
        }
    }

    return beanConfigs;
}

From source file:com.ebay.jetstream.configurationmanagement.MongoLogDAO.java

License:MIT License

public static List<JetStreamBeanConfigurationLogDo> findConfigurationByAppNameAndVersion(BasicDBObject query,
        MongoLogConnection mongoConnection) {

    List<JetStreamBeanConfigurationLogDo> beanConfigs = new ArrayList<JetStreamBeanConfigurationLogDo>();
    List<BasicDBObject> dbObjects = new ArrayList<BasicDBObject>();
    DBCollection dbCol = mongoConnection.getDBCollection();

    if (dbCol == null) {
        throw new MongoConfigRuntimeException("jetstreamconfiglog collection is unknown");
    }/*  w  w  w. j av  a  2 s .c  o m*/

    Exception e = null;
    DBCursor cur = null;
    try {
        cur = (query == null ? dbCol.find() : dbCol.find(query));
        while (cur.hasNext()) {
            dbObjects.add((BasicDBObject) cur.next());
        }

        for (BasicDBObject dbObject : dbObjects) {
            String jsonString = dbObject.toString();
            beanConfigs.add(unMarshalJSONResponse(jsonString));
        }
    } catch (Exception err) {
        e = err;
        throw new MongoConfigRuntimeException(err);
    } finally {
        if (cur != null) {
            cur.close();
        }
    }

    return beanConfigs;
}

From source file:com.ebay.jetstream.configurationmanagement.MongoLogDAO.java

License:MIT License

public static List<JetStreamBeanConfigurationLogDo> findConfigurationByQuery(BasicDBObject query,
        MongoLogConnection mongoLogConnection) {

    List<JetStreamBeanConfigurationLogDo> beanConfigs = new ArrayList<JetStreamBeanConfigurationLogDo>();
    List<BasicDBObject> dbObjects = new ArrayList<BasicDBObject>();
    DBCollection dbCol = mongoLogConnection.getDBCollection();

    if (dbCol == null) {
        throw new MongoConfigRuntimeException("jetstreamconfigLog collection is unknown");
    }/*from ww w. j av  a 2  s. c o  m*/

    Exception e = null;

    DBCursor cur = null;
    try {
        cur = (query == null ? dbCol.find() : dbCol.find(query));
        while (cur.hasNext()) {
            dbObjects.add((BasicDBObject) cur.next());
        }

        for (BasicDBObject dbObject : dbObjects) {
            String jsonString = dbObject.toString();
            beanConfigs.add(unMarshalJSONResponse(jsonString));
            // beanConfig =
            // (JetStreamBeanConfigurationDo)fromJson(jsonString,
            // JetStreamBeanConfigurationDo.class);
        }
    } catch (Exception err) {
        e = err;
        throw new MongoConfigRuntimeException(err);
    } finally {
        if (cur != null) {
            cur.close();
        }
    }

    return beanConfigs;
}

From source file:com.edduarte.argus.document.Document.java

License:Apache License

public List<Occurrence> getAllOccurrences(String occurrencesText) {
    if (occurrencesText.isEmpty()) {
        return null;
    }/*ww  w . j av  a2 s.c  o  m*/
    DBCursor cursor = occCollection.find(new BasicDBObject(Occurrence.TEXT, occurrencesText));
    List<Occurrence> list = new ArrayList<>();
    while (cursor.hasNext()) {
        BasicDBObject obj = (BasicDBObject) cursor.next();
        list.add(new Occurrence(obj));
    }
    cursor.close();
    return list;
}

From source file:com.edgytech.umongo.CollectionPanel.java

License:Apache License

public void find(final ButtonBase button) {
    final DBCollection col = getCollectionNode().getCollection();
    final DBObject query = ((DocBuilderField) getBoundUnit(Item.findQuery)).getDBObject();
    final DBObject fields = ((DocBuilderField) getBoundUnit(Item.findFields)).getDBObject();
    final DBObject sort = ((DocBuilderField) getBoundUnit(Item.findSort)).getDBObject();
    final DBObject hint = ((DocBuilderField) getBoundUnit(Item.findHint)).getDBObject();
    final int skip = getIntFieldValue(Item.findSkip);
    final int limit = getIntFieldValue(Item.findLimit);
    final int batchSize = getIntFieldValue(Item.findBatchSize);
    final boolean explain = getBooleanFieldValue(Item.findExplain);
    final boolean export = getBooleanFieldValue(Item.findExport);

    if (export) {
        exportToFile(col, query, fields, sort, skip, limit, batchSize);
    } else {/*from ww  w  . j a v  a 2 s .  c  o m*/
        new DbJob() {
            @Override
            public Object doRun() {
                // this does not actually block, may not need dbjob
                DBCursor cur = col.find(query, fields, skip, batchSize);
                if (sort != null) {
                    cur.sort(sort);
                }
                if (limit > 0) {
                    cur.limit(limit);
                }
                if (hint != null) {
                    cur.hint(hint);
                }
                if (explain) {
                    return cur.explain();
                }

                // force cursor to start
                cur.hasNext();
                return cur;
            }

            @Override
            public String getNS() {
                return col.getFullName();
            }

            @Override
            public String getShortName() {
                return "Find";
            }

            @Override
            public DBObject getRoot(Object result) {
                if (result == null || !(result instanceof DBCursor)) {
                    return null;
                }
                DBCursor res = (DBCursor) result;
                BasicDBObject obj = new BasicDBObject("cursorId", res.getCursorId());
                obj.put("server", res.getServerAddress().toString());
                obj.put("query", res.getQuery());
                obj.put("fields", res.getKeysWanted());
                obj.put("options", res.getOptions());
                obj.put("readPreference", res.getReadPreference().toDBObject());
                obj.put("numSeen", res.numSeen());
                obj.put("numGetMores", res.numGetMores());
                // want skip, limit, batchsize
                return obj;
            }

            @Override
            public ButtonBase getButton() {
                return button;
            }
        }.addJob();

    }
}

From source file:com.edgytech.umongo.CollectionPanel.java

License:Apache License

static void doFind(final DBCollection col, final DBObject query, final DBObject fields, final DBObject sort,
        final int skip, final int limit, final int batchSize, final boolean explain, final DBObject hint,
        final int options) {
    new DbJob() {
        @Override//from  w  w w . j a v a  2s.co  m
        public Object doRun() {
            // this does not actually block, may not need dbjob
            DBCursor cur = col.find(query, fields).skip(skip).batchSize(batchSize).addOption(options);
            if (sort != null) {
                cur.sort(sort);
            }
            if (limit > 0) {
                cur.limit(limit);
            }
            if (hint != null) {
                cur.hint(hint);
            }
            if (explain) {
                return cur.explain();
            }

            // force cursor to start
            cur.hasNext();
            return cur;
        }

        @Override
        public String getNS() {
            return col.getFullName();
        }

        @Override
        public String getShortName() {
            return "Find";
        }

        @Override
        public DBObject getRoot(Object result) {
            if (result == null || !(result instanceof DBCursor)) {
                return null;
            }
            DBCursor res = (DBCursor) result;
            BasicDBObject obj = new BasicDBObject("cursorId", res.getCursorId());
            obj.put("query", res.getQuery());
            obj.put("fields", res.getKeysWanted());
            obj.put("options", res.getOptions());
            obj.put("readPreference", res.getReadPreference().toDBObject());
            obj.put("numSeen", res.numSeen());
            obj.put("numGetMores", res.numGetMores());
            // want skip, limit, batchsize
            return obj;
        }
    }.addJob();
}

From source file:com.edgytech.umongo.CollectionPanel.java

License:Apache License

private void exportToFile(final DBCollection col, final DBObject query, final DBObject fields,
        final DBObject sort, final int skip, final int limit, final int batchSize) {
    ExportDialog dia = UMongo.instance.getGlobalStore().getExportDialog();
    if (!dia.show()) {
        return;/*w w  w. ja v a 2 s . c om*/
    }
    final DocumentSerializer ds = dia.getDocumentSerializer();
    final boolean continueOnError = dia.getBooleanFieldValue(ExportDialog.Item.continueOnError);
    new DbJob() {
        @Override
        public Object doRun() throws Exception {
            try {
                try {
                    DBCursor cur = col.find(query, fields);
                    if (skip > 0) {
                        cur.skip(skip);
                    }
                    if (batchSize != 0) {
                        cur.batchSize(batchSize);
                    }
                    if (sort != null) {
                        cur.sort(sort);
                    }
                    if (limit > 0) {
                        cur.limit(limit);
                    }
                    while (cur.hasNext() && !stopped) {
                        ds.writeObject(cur.next());
                    }
                } catch (Exception e) {
                    if (continueOnError) {
                        getLogger().log(Level.WARNING, null, e);
                    } else {
                        throw e;
                    }
                }
            } finally {
                ds.close();
            }
            return null;
        }

        @Override
        public String getNS() {
            return col.getFullName();
        }

        @Override
        public String getShortName() {
            return "Export";
        }
    }.addJob();
}