Example usage for com.mongodb BasicDBObject append

List of usage examples for com.mongodb BasicDBObject append

Introduction

In this page you can find the example usage for com.mongodb BasicDBObject append.

Prototype

@Override
public BasicDBObject append(final String key, final Object val) 

Source Link

Document

Add a key/value pair to this object

Usage

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;/*from  w w w  .ja  v  a2 s  . co m*/
    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

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;/*ww w.ja  va 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.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();//w  ww  . jav  a2 s . co  m
    // update existing
    while (cursor.hasNext()) {
        BasicDBObject dbo = (BasicDBObject) cursor.next();
        String key = getKey(dbo);
        if (!configs.containsKey(key)) {
            continue;
        }

        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.dal.search.SearchGroup.java

License:Apache License

private void translateAggregation(BasicDBObject group) {
    for (AggregationField aggField : aggFields.values()) {
        DBObject grpAttribute = new BasicDBObject();
        if (aggField.func == AggFuncEnum.COUNT) {
            grpAttribute.put("$" + AggFuncEnum.SUM.getName(), 1);
        } else {//from  w  w  w.ja  v a 2 s. c  o m
            grpAttribute.put("$" + aggField.func.getName(), "$" + aggField.getSearchField().getFullDbName());
        }
        group.append(aggField.getFieldName(), grpAttribute);
    }
}

From source file:com.ebay.cloud.cms.dal.search.SearchGroup.java

License:Apache License

private void translateGroupId(BasicDBObject group) {
    BasicDBObject idAttributes = new BasicDBObject();
    for (GroupField groupField : grpFields.values()) {
        idAttributes.append(groupField.getFieldName(), "$" + groupField.getSearchFiled().getFullDbName());
    }/* ww w  . ja  v a2  s  . c om*/
    group.put("_id", idAttributes);
}

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

License:Apache License

private MetaClass innerUpdateMetaClass(MetaClass metaClass, boolean updateGraph, boolean updateExistingField) {

    populateDbName(metaClass);/*w ww  .jav a 2  s .c  o m*/
    metaClass.setLastModified(new Date());

    BasicDBObject object = (BasicDBObject) converter.toBson(metaClass);

    BasicDBObject query = new BasicDBObject();
    query.append(MetaClass.NAME, metaClass.getName());
    BasicDBObject update = new BasicDBObject();
    BasicDBObject ups = new BasicDBObject();

    Collection<MetaField> updateFields = metaClass.getClassFields();
    //update fields
    BasicDBObject objectFields = (BasicDBObject) object.get("fields");
    for (MetaField f : updateFields) {
        if (!f.isInternal()) {
            String fieldKey = "fields." + f.getName();
            if (!updateExistingField) {
                query.append(fieldKey, new BasicDBObject(MongoOperand.exists, false));
            }
            ups.append(fieldKey, objectFields.get(f.getName()));
        }
    }
    // update options
    BasicDBObject objectIndexes = (BasicDBObject) object.get("options");
    Collection<IndexInfo> updateIndex = metaClass.getClassIndexes();
    if (!updateIndex.isEmpty()) {
        BasicDBObject indexOptions = (BasicDBObject) objectIndexes.get("indexes");
        for (IndexInfo index : updateIndex) {
            if (index.isInternal()) {
                continue;
            }
            String fieldKey = "options.indexes." + index.getIndexName();
            query.append(fieldKey, new BasicDBObject(MongoOperand.exists, false));
            ups.append(fieldKey, indexOptions.get(index.getIndexName()));
        }
    }
    // add optional fields if given
    addIfGiven(object, ups, "description");
    if (object.containsField("parent")) {
        addIfGiven(object, ups, "parent");
        addIfGiven(object, ups, "ancestors");
        addIfGiven(object, ups, "parentVersion");
    }
    addIfGiven(object, ups, "allowFullTableScan");
    addIfGiven(object, ups, "embed");
    addIfGiven(object, ups, "lastModified");
    addIfGiven(object, ups, "inner");

    update.append(MongoOperand.set, ups);
    BasicDBObject versionObject = new BasicDBObject();
    versionObject.put("version", 1);
    update.put(MongoOperand.inc, versionObject);

    try {
        boolean updated = MongoUtils.wrapperUpdate(collection, query, update);
        if (!updated) {
            StringBuilder sb = new StringBuilder();
            for (MetaField f : updateFields) {
                sb.append(f.getName()).append(",");
            }
            throw new MetaFieldExistsException(sb.toString());
        }
    } catch (MongoException e) {
        throw new MongoOperationException(e);
    }

    cacheManager.deleteMetaClassFromCache(metaClass);
    MetaClass result = getMetaClass(metaClass.getName());

    if (updateGraph) {
        getMetaClasses(new MetadataContext(true, true));
    }
    return result;
}

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

License:Apache License

void addIfGiven(BasicDBObject object, BasicDBObject ups, String name) {
    if (object.containsField(name)) {
        ups.append(name, object.get(name));
    }
}

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

License:Apache License

@Override
public void execute(MetadataContext context) {
    Collection<IndexInfo> updateIndex = targetOption.getIndexes();

    BasicDBObject query = new BasicDBObject();
    query.append(MetaClass.NAME, metaClass.getName());
    BasicDBObject update = new BasicDBObject();
    BasicDBObject ups = new BasicDBObject();

    BasicDBObject versionObject = new BasicDBObject();
    versionObject.put("version", 1);
    update.put(MongoOperand.inc, versionObject);

    BasicDBObject options = (BasicDBObject) converter.toBson(targetOption);
    BasicDBObject indexOptions = (BasicDBObject) options.get("indexes");
    if (!updateIndex.isEmpty()) {
        for (IndexInfo index : updateIndex) {
            if (index.isInternal()) {
                continue;
            }//from   w  w  w. j  a  va 2  s.  c  o m
            appendCommand(query, ups, indexOptions, index, context);
        }
    }

    update.append(getOperand(context), ups);

    try {
        boolean updated = MongoUtils.wrapperUpdate(dbCollection, query, update);
        if (!updated) {
            StringBuilder sb = new StringBuilder();
            for (IndexInfo f : updateIndex) {
                //                    sb.append(Objects.toStringHelper(f).toString());
                sb.append(f.getClass().getName());
            }
            throw new IndexOptionOperationException(sb.toString());
        }
    } catch (MongoException e) {
        throw new MongoOperationException(e);
    }
}

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

License:Apache License

protected void appendSetCommand(BasicDBObject query, BasicDBObject ups, BasicDBObject indexOptions,
        IndexInfo index, boolean update) {
    String fieldKey = "options.indexes." + index.getIndexName();
    query.append(fieldKey, new BasicDBObject(MongoOperand.exists, update));
    ups.append(fieldKey, indexOptions.get(index.getIndexName()));
}

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

License:Apache License

protected void appendUnsetCommand(BasicDBObject query, BasicDBObject ups, BasicDBObject indexOptions,
        IndexInfo index) {//from  w  w w .j a va  2s  .c  om
    String fieldKey = "options.indexes." + index.getIndexName();
    query.append(fieldKey, new BasicDBObject(MongoOperand.exists, true));
    ups.append(fieldKey, 1);
}