Example usage for com.mongodb DBCursor next

List of usage examples for com.mongodb DBCursor next

Introduction

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

Prototype

@Override
public DBObject next() 

Source Link

Document

Returns the object the cursor is at and moves the cursor ahead by one.

Usage

From source file:com.dilmus.dilshad.scabi.deprecated.DBackFileOld.java

License:Open Source License

public String getLatestFileID(String fileName) throws DScabiException {

    // This call to removeFilesIncompleteMetaData() is needed because if the last file upload failed (network issue, etc.) 
    // that incomplete file entry will cause getLatestFileID() to throw exception. 
    // So good complete files already in DB will not be served.
    // The "" as file id below is just to enable method removeFilesIncompleteMetaData() to cleanup all incomplete files with this fileName
    // Don't call this as if a put is in progress for the same fileName, it will get deleted!!
    // // // removeFilesIncompleteMetaData(fileName, ""); 

    String latestFileID = null;/*from   w  ww .  j av  a 2  s  .co m*/
    long latestServerDateTime = 0;
    int n = 0;

    // take only those file entries for fileName with complete meta-data
    BasicDBObject documentQuery = new BasicDBObject();
    documentQuery.put("PutFileName", fileName);
    documentQuery.append("PutStatus", "Completed");

    DBCursor cursorExist = m_table.find(documentQuery);
    n = cursorExist.count();
    if (1 == n) {
        while (cursorExist.hasNext()) {
            DBObject ob = cursorExist.next();
            log.debug("handlePreviousVersions() result from ob {}", ob.toString());

            String fid = (String) ((BasicBSONObject) ob).getString("PutServerFileID");
            if (null == fid) {
                throw new DScabiException("PutServerFileID is missing for file : " + fileName, "DBF.GLF.1");
            }
            return fid;
        }

    } else if (0 == n) {
        log.debug("getLatestFileID() No matches for file : " + fileName + " with PutStatus=Completed");
        throw new DScabiException(
                "getLatestFileID() No matches for file : " + fileName + " with PutStatus=Completed",
                "DBF.GLF.2");
    } else {
        while (cursorExist.hasNext()) {
            DBObject ob = cursorExist.next();
            log.debug("getLatestFileID() result from ob {}", ob.toString());

            // Analysis needed : can we just continue with next file entry instead of throwing exception?
            String fid = (String) ((BasicBSONObject) ob).getString("PutServerFileID");
            if (null == fid) {
                throw new DScabiException("PutServerFileID is missing for one version of file : " + fileName,
                        "DBF.GLF.3");
            }
            String f = (String) ((BasicBSONObject) ob).getString("PutServerUploadDateTime");
            if (null == f) {
                throw new DScabiException("PutServerUploadDateTime is missing for one version of file : "
                        + fileName + " file ID : " + fid, "DBF.GLF.4");
            }
            String f2 = (String) ((BasicBSONObject) ob).getString("PutLatestNumber");
            if (null == f2) {
                throw new DScabiException("PutLatestNumber is missing for one version of file : " + fileName
                        + " file ID : " + fid, "DBF.GLF.5");
            }
            long lf2 = Long.parseLong(f);
            if (latestServerDateTime < lf2 && f2.equals("1")) {
                // proceed with other versions
                latestServerDateTime = lf2;
                latestFileID = fid;
            }

        }
    }
    return latestFileID;
}

From source file:com.dilmus.dilshad.scabi.deprecated.DBackFileOld.java

License:Open Source License

public boolean isValidMetaData(String fileName, String strFileID) throws IOException, DScabiException {
    int n = 0;/*from   ww w  .java 2 s.co m*/
    Set<String> stMetaKeys = new HashSet<String>();
    stMetaKeys.add("PutFileName");
    stMetaKeys.add("PutServerFileID");
    stMetaKeys.add("PutServerUploadDateTime");
    stMetaKeys.add("PutType");
    stMetaKeys.add("PutContentType");
    stMetaKeys.add("PutClientDateTime");
    stMetaKeys.add("PutClientDateTimeInMillis");
    stMetaKeys.add("PutClientDateTimeInNano");
    stMetaKeys.add("PutStatus");
    stMetaKeys.add("PutLatestNumber");

    BasicDBObject documentQuery = new BasicDBObject();
    ObjectId fileID = new ObjectId(strFileID);
    // "_id" is MongoDB/GridFS specific meta data name inside fs.files collection for each file
    documentQuery.put("_id", fileID);

    DBCursor cursorExist = m_table.find(documentQuery);
    n = cursorExist.count();
    if (1 == n) {
        log.debug("isValidMetaData() Inside 1 == n");
        while (cursorExist.hasNext()) {
            DBObject ob = cursorExist.next();
            log.debug("isValidMetaData() result from ob {}", ob.toString());
            Set<String> st = ob.keySet();
            if (st.containsAll(stMetaKeys)) {
                return true;
            } else {
                return false;
            }
        }
    } else if (0 == n) {
        log.debug("isValidMetaData() No matches for file : " + fileName + " fileID : " + fileID.toHexString());
        throw new DScabiException(
                "isValidMetaData() No matches for file : " + fileName + " fileID : " + fileID.toHexString(),
                "DBF.IVM.1");
        //return false;
    } else {
        log.debug("isValidMetaData() Multiple matches for file : " + fileName + " fileID : "
                + fileID.toHexString());
        throw new DScabiException("isValidMetaData() Multiple matches for file : " + fileName + " fileID : "
                + fileID.toHexString(), "DBF.IVM.2");
        //return false;
    }
    return false;
}

From source file:com.dilmus.dilshad.scabi.deprecated.DTableOld.java

License:Open Source License

public String executeQuery(String jsonQuery) throws DScabiException, IOException {
    ArrayList<String> fieldList = fieldNamesUsingFindOne(); // fieldNames();
    DMJson djson = new DMJson(jsonQuery);
    Set<String> st = djson.keySet();
    BasicDBObject document = new BasicDBObject();
    ArrayList<String> finalList = new ArrayList<String>();
    HashMap<String, String> hmap = new HashMap<String, String>();
    DMJson djson3 = null;/*from  www .  j  a v  a  2s  .  co  m*/

    if (false == isEmpty(fieldList)) {
        if (false == fieldList.containsAll(st)) {
            throw new DScabiException(
                    "One or more field name in jsonQuery doesn't exist in fieldNames list. jsonQuery : "
                            + jsonQuery + " Field Names list : " + fieldList,
                    "DBT.EQY.1");
        }

    }

    for (String key : st) {
        // create a document to store key and value
        document.put(key, djson.getString(key));
    }
    DBCursor cursorExist = m_table.find(document);

    while (cursorExist.hasNext()) {

        hmap.clear();
        DBObject ob = cursorExist.next();
        Set<String> obkeys = ob.keySet();
        obkeys.remove("_id"); // exclude _id field
        //log.debug("executeQuery() result from ob {}", ob.toString());
        if (false == isEmpty(fieldList)) {
            if (false == obkeys.containsAll(fieldList)) {
                throw new DScabiException(
                        "One or more field name in fieldList doesn't exist in obkeys key set. obkeys : "
                                + obkeys + " Field Names list : " + fieldList,
                        "DBT.EQY.2");
            }
            for (String field : obkeys) {
                //if (field.equals("_id"))
                //   continue;
                String f = (String) ((BasicBSONObject) ob).getString(field);
                if (null == f) {
                    throw new DScabiException(
                            "Field name " + field + " doesn't exist in dbobject in dbcursor. jsonQuery : "
                                    + jsonQuery + " Field Names list : " + fieldList,
                            "DBT.EQY.3");
                }
                //log.debug("executeQuery() field is {}", field);
                //log.debug("executeQuery() f is {}", f);
                hmap.put(field, f);
            }
        } else {
            for (String key : obkeys) {
                //if (key.equals("_id"))
                //   continue;
                String f2 = (String) ((BasicBSONObject) ob).getString(key);
                if (null == f2) {
                    throw new DScabiException("Field name " + key
                            + " doesn't exist in dbobject in dbcursor. jsonQuery : " + jsonQuery, "DBT.EQY.4");
                }
                //log.debug("executeQuery() key is {}", key);
                //log.debug("executeQuery() f2 is {}", f2);
                hmap.put(key, f2);
            }
        }
        DMJson djson2 = null;
        //if (false == fieldList.isEmpty())
        //   djson2 = DJson.createDJsonList(hmap, fieldList);
        //else if (false == st.isEmpty())
        //   djson2 = DJson.createDJsonSet(hmap, st);
        if (false == obkeys.isEmpty())
            djson2 = DMJson.createDJsonSet(hmap, obkeys);
        if (null == djson2) {
            throw new DScabiException("djson2 is null. jsonQuery : " + jsonQuery, "DBT.EQY.5");
        }
        finalList.add(djson2.toString());
    }
    djson3 = DMJson.createDJsonWithCount(finalList);

    return djson3.toString();
}

From source file:com.ebay.cloud.cms.config.CMSProperties.java

License:Apache License

private void loadProperties(Mongo mongo) {
    Map<String, Object> m = new ConcurrentHashMap<String, Object>();
    DBCollection collection = getPropertiesCollection(mongo);
    collection.setReadPreference(ReadPreference.primary());
    DBCursor cursor = collection.find();
    while (cursor.hasNext()) {
        BasicDBObject object = (BasicDBObject) cursor.next();
        String key = getKey(object);
        if (key != null) {
            m.put(key, object.get(key));
        }/*from   w  w  w .  ja v  a 2 s . c  om*/
    }

    cachedConfigurations = m;
}

From source file:com.ebay.cloud.cms.config.CMSProperties.java

License:Apache License

public void updateConfig(Map<String, Object> configs) {
    DBCollection coll = getPropertiesCollection(ds.getMongoInstance());
    DBCursor cursor = coll.find();
    // update existing
    while (cursor.hasNext()) {
        BasicDBObject dbo = (BasicDBObject) cursor.next();
        String key = getKey(dbo);
        if (!configs.containsKey(key)) {
            continue;
        }/*  www . j a v a2 s.c  om*/

        BasicDBObject qObject = new BasicDBObject();
        BasicDBObject vObject = new BasicDBObject();
        qObject.append("_id", dbo.get("_id"));
        vObject.append(key, configs.get(key));

        coll.update(qObject, vObject);
        configs.remove(key);
    }

    // insert new config
    if (!configs.isEmpty()) {
        List<DBObject> list = new ArrayList<DBObject>();
        for (Entry<String, Object> entry : configs.entrySet()) {
            DBObject dbo = new BasicDBObject();
            dbo.put(entry.getKey(), entry.getValue());

            list.add(dbo);
        }
        coll.insert(list);
    }

    loadProperties(ds.getMongoInstance());
}

From source file:com.ebay.cloud.cms.metadata.dataloader.MetadataDataLoader.java

License:Apache License

private List<String> getAllRepositoryNames() {
    DBCollection repoCollection = this.mongo.getDB(CMSConsts.SYS_DB).getCollection(CMSConsts.REPOSITORY_COLL);
    BasicDBObject query = new BasicDBObject();

    List<String> repoNameList = new ArrayList<String>();
    DBCursor cursor = repoCollection.find(query);
    while (cursor.hasNext()) {
        DBObject bsonObject = cursor.next();
        String repoName = (String) bsonObject.get(Repository.REPOSITORY_FIELD_NAME);
        repoNameList.add(repoName);//w  ww  .j a v a2  s.c o  m
    }
    return repoNameList;
}

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 w  w.j a v a2s  .  co 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  w  w  . java2  s  .  c  o  m*/

    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   w  w w  . ja va 2 s . co  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 w  w  .  jav 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;
}