List of usage examples for com.mongodb WriteResult getN
public int getN()
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 ww. j a va 2 s .co m*/ 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 av a 2 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.DTableOld.java
License:Open Source License
public int insert(DObject dob) throws DScabiException { WriteResult result = m_table.insert(dob.getDocument()); log.debug("insert() result is : {}", result.getN()); if (result.getN() < 0) throw new DScabiException("Insert failed for DBackObject : " + dob.toString(), "DBT.INT.1"); return result.getN(); }
From source file:com.dilmus.dilshad.scabi.deprecated.DTableOld.java
License:Open Source License
public int update(DObject dob, DObject updateObj) throws DScabiException { WriteResult result = m_table.updateMulti(dob.getDocument(), updateObj.getDocument()); log.debug("update() result is : {}", result.getN()); if (result.getN() <= 0) throw new DScabiException("Update failed for DBackObject dob : " + dob.toString() + " updateObj : " + updateObj.toString(), "DBT.UPE.1"); return result.getN(); }
From source file:com.dilmus.dilshad.scabi.deprecated.DTableOld.java
License:Open Source License
public int remove(DObject dob) throws DScabiException { WriteResult result = m_table.remove(dob.getDocument()); log.debug("remove() result is : {}", result.getN()); if (result.getN() <= 0) throw new DScabiException("Remove failed for DBackObject : " + dob.toString(), "DBT.REE.1"); return result.getN(); }
From source file:com.dilmus.dilshad.scabi.deprecated.DTableOld.java
License:Open Source License
public int insertRow(String jsonRow, String jsonCheck) throws DScabiException, IOException { log.debug("insertRow() firstTime is {}", m_firstTime); ArrayList<String> fieldList = fieldNamesUsingFindOne(); // fieldNames(); DMJson djson = new DMJson(jsonRow); Set<String> st = djson.keySet(); BasicDBObject document = new BasicDBObject(); int n = 0;//from ww w . j a v a2 s . co m WriteResult result = null; DMJson djsonCheck = new DMJson(jsonCheck); Set<String> stCheck = djsonCheck.keySet(); BasicDBObject documentCheck = new BasicDBObject(); if (false == isEmpty(fieldList)) { if (false == fieldList.containsAll(st)) { throw new DScabiException( "One or more field name in jsonRow doesn't exist in fieldNames list. jsonRow : " + jsonRow + " Field Names list : " + fieldList, "DBT.IRW.1"); } if (false == fieldList.containsAll(stCheck)) { throw new DScabiException( "One or more field name in jsonCheck doesn't exist in fieldNames list. jsonCheck : " + jsonCheck + " Field Names list : " + fieldList, "DBT.IRW.2"); } if (false == st.containsAll(fieldList)) { throw new DScabiException( "One or more field name in fieldNames doesn't exist in jsonRow key set. jsonRow : " + jsonRow + " Field Names list : " + fieldList, "DBT.IRW.3"); } if (fieldList.size() != st.size()) { throw new DScabiException("Fields count doesn't match. fieldNames : " + fieldList.toString() + " with jsonRow : " + jsonRow, "DBT.IRW.4"); } } if (false == isEmpty(fieldList)) { for (String fieldName : st) { // create a document to store key and value String f = djson.getString(fieldName); if (null == f) { throw new DScabiException("Field name " + fieldName + " doesn't exist in jsonRow : " + jsonRow + " Field Names list : " + fieldList, "DBT.IRW.5"); } document.put(fieldName, f); } for (String keyCheck : stCheck) { // create a document to store key and value String f2 = djsonCheck.getString(keyCheck); if (null == f2) { throw new DScabiException( "Field name " + keyCheck + " doesn't exist in jsonCheck : " + jsonCheck, "DBT.IRW.6"); } documentCheck.put(keyCheck, f2); } DBCursor cursorExist = m_table.find(documentCheck); n = cursorExist.count(); if (0 == n) { log.debug("insertRow() Inside 0 == n"); result = m_table.insert(document); log.debug("insertRow() result is : {}", result.getN()); if (result.getN() < 0) throw new DScabiException("Insert failed for document : " + document.toString(), "DBT.IRW.7"); } else if (1 == n) { throw new DScabiException("Row already exists. jsonCheck : " + jsonCheck, "DBT.IRW.8"); // already found } else { throw new DScabiException("Row already exists, multiple matches. jsonCheck : " + jsonCheck, "DBT.IRW.9"); // already found } } else { for (String key : st) { // create a document to store key and value String f3 = djson.getString(key); if (null == f3) { throw new DScabiException("Field name " + key + " doesn't exist in jsonRow : " + jsonRow, "DBT.IRW.10"); } document.put(key, djson.getString(key)); } for (String keyCheck : stCheck) { // create a document to store key and value String f4 = djsonCheck.getString(keyCheck); if (null == f4) { throw new DScabiException( "Field name " + keyCheck + " doesn't exist in jsonCheck : " + jsonCheck, "DBT.IRW.11"); } documentCheck.put(keyCheck, djsonCheck.getString(keyCheck)); } DBCursor cursorExist = m_table.find(documentCheck); n = cursorExist.count(); if (0 == n) { log.debug("insertRow() Inside 0 == n"); result = m_table.insert(document); log.debug("insertRow() result is : {}", result.getN()); if (result.getN() < 0) throw new DScabiException("Insert failed for document : " + document.toString(), "DBT.IRW.12"); } else if (1 == n) { throw new DScabiException("Row already exists. jsonCheck : " + jsonCheck, "DBT.IRW.13"); // already found } else { throw new DScabiException("Row already exists, multiple matches. jsonCheck : " + jsonCheck, "DBT.IRW.14"); // already found } } return result.getN(); }
From source file:com.dilmus.dilshad.scabi.deprecated.DTableOld.java
License:Open Source License
public int executeUpdate(String jsonUpdate, String jsonWhere) throws IOException, DScabiException { ArrayList<String> fieldList = fieldNamesUsingFindOne(); // fieldNames(); DMJson djsonWhere = new DMJson(jsonWhere); Set<String> stWhere = djsonWhere.keySet(); BasicDBObject documentWhere = new BasicDBObject(); DMJson djsonUpdate = new DMJson(jsonUpdate); Set<String> stUpdate = djsonUpdate.keySet(); BasicDBObject documentUpdate = new BasicDBObject(); if (false == isEmpty(fieldList)) { if (false == fieldList.containsAll(stWhere)) { throw new DScabiException( "One or more field name in jsonWhere doesn't exist in fieldNames list. jsonWhere : " + jsonWhere + " Field Names list : " + fieldList, "DBT.EUE.1"); }// w ww.j av a 2 s. c om if (false == fieldList.containsAll(stUpdate)) { throw new DScabiException( "One or more field name in jsonUpdate doesn't exist in fieldNames list. jsonUpdate : " + jsonUpdate + " Field Names list : " + fieldList, "DBT.EUE.2"); } } for (String keyWhere : stWhere) { // create a document to store key and value documentWhere.put(keyWhere, djsonWhere.getString(keyWhere)); } for (String keyUpdate : stUpdate) { // create a document to store key and value documentUpdate.put(keyUpdate, djsonUpdate.getString(keyUpdate)); } BasicDBObject updateObj = new BasicDBObject(); updateObj.put("$set", documentUpdate); WriteResult result = m_table.updateMulti(documentWhere, updateObj); log.debug("executeUpdate() result is : {}", result.getN()); if (result.getN() <= 0) throw new DScabiException("Update failed for documentWhere : " + documentWhere.toString() + " updateObj : " + updateObj.toString(), "DBT.EUE.3"); return result.getN(); }
From source file:com.dilmus.dilshad.scabi.deprecated.DTableOld.java
License:Open Source License
public int executeRemove(String jsonWhere) throws IOException, DScabiException { ArrayList<String> fieldList = fieldNamesUsingFindOne(); // fieldNames(); DMJson djsonWhere = new DMJson(jsonWhere); Set<String> stWhere = djsonWhere.keySet(); BasicDBObject documentWhere = new BasicDBObject(); if (false == isEmpty(fieldList)) { if (false == fieldList.containsAll(stWhere)) { throw new DScabiException( "One or more field name in jsonWhere doesn't exist in fieldNames list. jsonWhere : " + jsonWhere + " Field Names list : " + fieldList, "DBT.ERE.1"); }//from ww w . j a v a 2s .co m } for (String keyWhere : stWhere) { // create a document to store key and value documentWhere.put(keyWhere, djsonWhere.getString(keyWhere)); } // DBObject result = table.findAndRemove(documentWhere); WriteResult result = m_table.remove(documentWhere); log.debug("executeRemove() result is : {}", result.getN()); if (result.getN() <= 0) throw new DScabiException("Remove failed for documentWhere : " + documentWhere.toString(), "DBT.ERE.2"); return result.getN(); }
From source file:com.ebay.cloud.cms.dal.persistence.flatten.impl.embed.AbstractFieldEmbedCommand.java
License:Apache License
@Override public void execute(PersistenceContext context) { String entityId = entity.getId(); rootMetaClass = getRootEntityMetaClass(context); BitSet arrayBits = helper.checkArrayOnPath(entityId, rootMetaClass); String parentId = helper.getParentId(entityId); rootQuery = buildGetQuery(arrayBits, parentId, rootMetaClass); // get the root entity for further embed path computing DBObject rootGetFields = buildGetRootFields(entityId, rootMetaClass); DBObject rootObject = MongoExecutor.findOne(context, rootMetaClass, rootQuery, rootGetFields); if (rootObject == null) { throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND, getOperation() + " on embed field, parenet document with id " + parentId + "doesn't exist!"); }//from w w w . j a v a 2 s . c o m embedPath = getEmbedPath(arrayBits, entityId, rootObject, rootMetaClass); // new query with version buildModifyQuery(rootQuery, entity); DBObject modifyQuery = buildModifyQuery(rootQuery, entity); DBObject modifyBody = buildModifyBody(arrayBits, rootObject, rootMetaClass); WriteResult result = MongoExecutor.update(context, rootMetaClass, modifyQuery, modifyBody); if (result.getN() == 0) { // something happens between get and replace throw new CmsDalException(DalErrCodeEnum.VERSION_CONFLICT, "Version check fails for " + entity.getId() + " in class " + rootMetaClass.getName() + " and embed class " + getEntity().getType() + "! entity is " + entity.toString()); } }
From source file:com.ebay.cloud.cms.dal.persistence.flatten.impl.embed.EmbedCreateCommand.java
License:Apache License
@Override public void execute(PersistenceContext context) { MetaClass rootMetaClass = getRootEntityMetaClass(entity.getId(), context); String parentId = helper.getParentId(entity.getId()); BitSet parentBits = helper.checkArrayOnPath(parentId, rootMetaClass); // query root object with parent id DBObject rootQuery = buildGetQuery(parentBits, parentId, rootMetaClass); String id = parentId;//w w w . ja v a 2s. c o m if (parentBits.cardinality() == 0) { id = entity.getId(); } DBObject rootFields = buildGetRootFields(id, rootMetaClass); DBObject rootObject = MongoExecutor.findOne(context, rootMetaClass, rootQuery, rootFields); if (rootObject == null) { throw new CmsDalException(DalErrCodeEnum.ENTITY_NOT_FOUND, "Create, parenet document doesn't exist! " + parentId); } // add embed document into parent document DBObject createQuery = buildModifyQuery(rootQuery, entity); int newVersion = (Integer) rootObject.get(InternalFieldEnum.VERSION.getDbName()) + 1; String parentPath = getUpdatePath(parentBits, parentId, rootObject, rootMetaClass); MetaRelationship lastField = helper.getLastMetaField(entity.getId(), rootMetaClass); DBObject createBody = buildCreateBody(lastField, parentPath, entity, newVersion, rootMetaClass, rootObject, parentId); WriteResult result = MongoExecutor.update(context, rootMetaClass, createQuery, createBody); if (result.getN() == 0) { // something happens between get and replace throw new CmsDalException(DalErrCodeEnum.VERSION_CONFLICT, "Version check fails" + "! entity is " + entity.toString()); } }