Example usage for com.mongodb BasicDBObject getString

List of usage examples for com.mongodb BasicDBObject getString

Introduction

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

Prototype

public String getString(final String key, final String def) 

Source Link

Document

Returns the value of a field as a string

Usage

From source file:com.ikanow.infinit.e.processing.generic.store_and_index.StoreAndIndexManager.java

License:Open Source License

/**
 * Remove a list of doc documents from the data store (you have a source key, so you can go much quicker)
 * CALLED FROM:   deleteSource(...) //from   ww w.  j a  v a  2 s  .c o m
 * @returns the number of docs deleted
 */
public long removeFromDatastoreAndIndex_bySourceKey(String sourceKey, ObjectId lessThanId,
        boolean definitelyNoContent, String communityId) {

    try {
        if (!definitelyNoContent) {
            DbManager.getDocument().getContent()
                    .remove(new BasicDBObject(CompressedFullTextPojo.sourceKey_, sourceKey));
            // (will just check index and pull out if the doc has no external content)
        }
        BasicDBObject query = new BasicDBObject(DocumentPojo.sourceKey_,
                SourcePojo.getDistributedKeyQueryTerm(sourceKey));
        if (null != lessThanId) { // Multiple threads running for this source
            // First check whether one of the other threads has already deleted the source:
            BasicDBObject oneFinalCheckQuery = new BasicDBObject(DocumentPojo.sourceKey_,
                    SourcePojo.getDistributedKeyQueryTerm(sourceKey));
            BasicDBObject oneFinalCheckFields = new BasicDBObject(DocumentPojo.index_, 1);
            BasicDBObject firstDocToBeUpdated = (BasicDBObject) DbManager.getDocument().getMetadata()
                    .findOne(oneFinalCheckQuery, oneFinalCheckFields);
            if ((null == firstDocToBeUpdated)
                    || firstDocToBeUpdated.getString(DocumentPojo.index_, "").equals(DELETION_INDICATOR)) {
                //(ie grab the first doc in natural order and tell me if it's been soft-deleted yet, if so do nothing)
                return 0;
            } //TESTED

            // That check isn't perfect because of race conditions, so we'll still add the !="?DEL?" check to the 
            // update as well:            
            query.put(DocumentPojo._id_, new BasicDBObject(DbManager.lte_, lessThanId));
            query.put(DocumentPojo.index_, new BasicDBObject(DbManager.ne_, DELETION_INDICATOR));
        } //TESTED

        BasicDBObject softDeleter = getSoftDeleteUpdate();
        DbManager.getDocument().getMetadata().update(query, softDeleter, false, true);
        // (don't do getLastError just yet since it can block waiting for completion)

        // Quick delete for index though:
        StringBuffer sb = new StringBuffer();
        if (null == lessThanId) {// slower version, be slightly more thorough... 
            sb.append(DocumentPojoIndexMap.globalDocumentIndexCollection_).append(",");
        }
        sb.append(DocumentPojoIndexMap.manyGeoDocumentIndexCollection_).append(",docs_").append(communityId)
                .append('/').append(DocumentPojoIndexMap.documentType_);
        ElasticSearchManager indexManager = IndexManager.getIndex(sb.toString());
        BaseQueryBuilder soloOrCombinedQuery = QueryBuilders.termQuery(DocumentPojo.sourceKey_, sourceKey);
        if (null != lessThanId) {
            //(_id isn't indexed - _uid is and == _type + "#" + _id)
            soloOrCombinedQuery = QueryBuilders.boolQuery().must(soloOrCombinedQuery)
                    .must(QueryBuilders.rangeQuery("_uid").lte("document_index#" + lessThanId.toString()));

        } //TESTED
        indexManager.doDeleteByQuery(soloOrCombinedQuery);

        CommandResult result = DbManager.getDocument().getLastError("metadata");
        return result.getLong("n", 0);

    } catch (Exception e) {
        // If an exception occurs log the error
        logger.error("Exception Message: " + e.getMessage(), e);
    }
    return 0;
}