List of usage examples for com.mongodb DBCursor hasNext
@Override public boolean hasNext()
From source file:com.dilmus.dilshad.scabi.deprecated.DBackFileOld.java
License:Open Source License
public int updateMetaData(String fileName, ObjectId fileID, String type, String contentType) throws IOException, DScabiException, ParseException { int n = 0;/* w w w . java 2 s. c om*/ String uploadDate = null; Date datefromDB = null; BasicDBObject documentWhere = new BasicDBObject(); documentWhere.put("_id", fileID); DBCursor cursorExist = m_table.find(documentWhere); n = cursorExist.count(); if (1 == n) { log.debug("updateMetaData() Inside 1 == n"); while (cursorExist.hasNext()) { DBObject ob = cursorExist.next(); log.debug("updateMetaData() result from ob {}", ob.toString()); //datefromDB = (String) ((BasicBSONObject) ob).getString("uploadDate"); datefromDB = ((BasicBSONObject) ob).getDate("uploadDate"); if (null == datefromDB) { throw new DScabiException("updateMetaData() Unable to get uploadDate for file : " + fileName + " fileID : " + fileID.toHexString(), "DBF.UMD.1"); } log.debug("datefromDB : {}", datefromDB); } } else if (0 == n) { log.debug("updateMetaData() No matches for file : " + fileName + " fileID : " + fileID.toHexString()); throw new DScabiException( "updateMetaData() No matches for file : " + fileName + " fileID : " + fileID.toHexString(), "DBF.UMD.2"); } else { log.debug("updateMetaData() Multiple matches for file : " + fileName + " fileID : " + fileID.toHexString()); throw new DScabiException("updateMetaData() Multiple matches for file : " + fileName + " fileID : " + fileID.toHexString(), "DBF.UMD.3"); } Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS"); dateFormat.setTimeZone(TimeZone.getTimeZone("ISO")); String putClientDateTime = dateFormat.format(date); // To parse from string : Date date2 = dateFormat.parse(putDateTime); // Uses java.time java 8 : ZonedDateTime now = ZonedDateTime.now( ZoneOffset.UTC ); String millisTime = "" + System.currentTimeMillis(); String nanoTime = "" + System.nanoTime(); /* If datefromDB is String SimpleDateFormat dateFormatFromDB = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); dateFormatFromDB.setTimeZone(TimeZone.getTimeZone("ISO")); CharSequence cs1 = "T"; CharSequence cs2 = "Z"; String s1 = datefromDB.replace(cs1, " "); String s2 = s1.replace(cs2, ""); Date date2 = dateFormatFromDB.parse(s2); uploadDate = dateFormat.format(date2); */ uploadDate = dateFormat.format(datefromDB); log.debug("uploadDate : {}", uploadDate); BasicDBObject documentUpdate = new BasicDBObject(); documentUpdate.append("PutFileName", fileName); documentUpdate.append("PutServerFileID", fileID.toHexString()); documentUpdate.append("PutServerUploadDateTime", uploadDate); documentUpdate.append("PutType", type); documentUpdate.append("PutContentType", contentType); documentUpdate.append("PutClientDateTime", putClientDateTime); documentUpdate.append("PutClientDateTimeInMillis", millisTime); documentUpdate.append("PutClientDateTimeInNano", nanoTime); documentUpdate.append("PutStatus", "Completed"); documentUpdate.append("PutLatestNumber", "1"); BasicDBObject updateObj = new BasicDBObject(); updateObj.put("$set", documentUpdate); WriteResult result = m_table.update(documentWhere, updateObj); if (1 != result.getN()) throw new DScabiException( "Update meta data failed for file : " + fileName + " fileID : " + fileID.toHexString(), "DBF.UMD.4"); handlePreviousVersions(fileName, fileID.toHexString(), uploadDate); return result.getN(); }
From source file:com.dilmus.dilshad.scabi.deprecated.DBackFileOld.java
License:Open Source License
private int handlePreviousVersions(String fileName, String strFileID, String strPutServerUploadDateTime) throws IOException, DScabiException { int m = 0;/* w ww.j a va2 s . c om*/ int n = 0; // It is better to call this only after meta data is updated for currently uploaded file // This will skip checking for given input strFileID, file ID of currently uploaded file removeFilesIncompleteMetaData(fileName, strFileID); BasicDBObject documentFind = new BasicDBObject(); documentFind.put("PutFileName", fileName); documentFind.append("PutServerFileID", strFileID); documentFind.append("PutStatus", "Completed"); documentFind.append("PutLatestNumber", "1"); DBCursor cursor = m_table.find(documentFind); m = cursor.count(); if (1 == m) { log.debug("handlePreviousVersions() Inside 1 == n"); } else if (0 == m) { log.debug("handlePreviousVersions() No matches for file : " + fileName + " strFileID : " + strFileID); throw new DScabiException( "handlePreviousVersions() No matches for file : " + fileName + " strFileID : " + strFileID, "DBF.HPV.1"); } else { log.debug("handlePreviousVersions() Multiple matches for file : " + fileName + " strFileID : " + strFileID); throw new DScabiException("handlePreviousVersions() Multiple matches for file : " + fileName + " strFileID : " + strFileID, "DBF.HPV.2"); } BasicDBObject documentQuery = new BasicDBObject(); documentQuery.put("PutFileName", fileName); documentQuery.append("PutStatus", "Completed"); DBCursor cursorExist = m_table.find(documentQuery); n = cursorExist.count(); if (1 == n) { log.debug( "handlePreviousVersions() Information only : Inside 1 == n. Only one file / current file is found. No previous versions for file : " + fileName + " with PutStatus=Completed"); return 0; } else if (0 == n) { log.debug("handlePreviousVersions() No matches for file : " + fileName + " with PutStatus=Completed"); throw new DScabiException( "handlePreviousVersions()() No matches for file : " + fileName + " with PutStatus=Completed", "DBF.HPV.3"); } else { long lf1 = Long.parseLong(strPutServerUploadDateTime); 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 one version of file : " + fileName, "DBF.HPV.4"); } /* Don't use. It should be based on date-time and not on file ID if (f.equals(strFileID)) { // proceed with other versions continue; } */ 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.HPV.5"); } 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.HPV.6"); } if (f.equals(strPutServerUploadDateTime) && f2.equals("1")) { // proceed with other versions continue; } long lf2 = Long.parseLong(f); if (lf1 < lf2 && f2.equals("1")) { // proceed with other versions continue; } if (f2.equals("1")) { // all file entries here have PutServerUploadDateTime < strPutServerUploadDateTime // there can be multiple previous versions with PutLatestNumber=1 BasicDBObject documentWhere = new BasicDBObject(); documentWhere.put("PutServerFileID", fid); BasicDBObject documentUpdate = new BasicDBObject(); documentUpdate.append("PutLatestNumber", "2"); BasicDBObject updateObj = new BasicDBObject(); updateObj.put("$set", documentUpdate); // there should be only one entry for file ID fid WriteResult result = m_table.update(documentWhere, updateObj); if (result.getN() <= 0) throw new DScabiException("Update meta data to PutLatestNumber=2 failed for file : " + fileName + " file ID : " + fid, "DBF.HPV.7"); } else { // remove all other versions m_gridFSBucket.delete(new ObjectId(fid)); } } } return 0; }
From source file:com.dilmus.dilshad.scabi.deprecated.DBackFileOld.java
License:Open Source License
private int removeFilesIncompleteMetaData(String fileName, String strFileID) { int n = 0;// w w w.j a v a 2 s.c om 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(); // "filename" is MongoDB/GridFS specific meta data name inside fs.files collection for each file documentQuery.put("filename", fileName); DBCursor cursorExist = m_table.find(documentQuery); n = cursorExist.count(); if (0 == n) { log.debug("removeFilesIncompleteMetaData() Information only : No file found for file : " + fileName); return 0; } else { while (cursorExist.hasNext()) { DBObject ob = cursorExist.next(); log.debug("removeFilesIncompleteMetaData() result from ob {}", ob.toString()); // "_id" is MongoDB/GridFS specific meta data name inside fs.files collection for each file ObjectId oid = ((BasicBSONObject) ob).getObjectId("_id"); if (null == oid) { // what's the use in throwing exception here? throw new DScabiException("_id is missing for file : " + fileName, "DBF.RFI.1"); // let it continue to cleanup as much as possible continue; } if (oid.toHexString().equals(strFileID)) { log.debug( "removeFilesIncompleteMetaData() Information only : skipping given input file ID : {}", strFileID); continue; } Set<String> st = ob.keySet(); if (st.containsAll(stMetaKeys)) { continue; } else { // remove file m_gridFSBucket.delete(oid); } } } return 0; }
From source file:com.dilmus.dilshad.scabi.deprecated.DBackFileOld.java
License:Open Source License
public int removeAllFilesIncompleteMetaData() { int n = 0;/*from w ww . ja v a2s . 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"); DBCursor cursorExist = m_table.find(); n = cursorExist.count(); if (0 == n) { log.debug("removeAllFilesIncompleteMetaData() Information only : No file found"); return 0; } else { while (cursorExist.hasNext()) { DBObject ob = cursorExist.next(); log.debug("removeAllFilesIncompleteMetaData() result from ob {}", ob.toString()); // "_id" is MongoDB/GridFS specific meta data name inside fs.files collection for each file ObjectId oid = ((BasicBSONObject) ob).getObjectId("_id"); if (null == oid) { // what's the use in throwing exception here? throw new DScabiException("_id is missing for file : " + fileName, "DBF.RAF.1"); // let it continue to cleanup as much as possible continue; } Set<String> st = ob.keySet(); if (st.containsAll(stMetaKeys)) { continue; } else { // remove file m_gridFSBucket.delete(oid); } } } return 0; }
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;/*w ww . j av a 2 s . c o 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;//ww w. ja va 2 s .c o 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 ww w.j a v a 2s .c o 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 va2 s .co m } 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; }/* ww w.j ava 2s .c o m*/ 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);//from w w w . j a v a 2 s . c o m } return repoNameList; }