Example usage for com.mongodb CommandResult getLong

List of usage examples for com.mongodb CommandResult getLong

Introduction

In this page you can find the example usage for com.mongodb CommandResult getLong.

Prototype

public long getLong(final String key, final long def) 

Source Link

Document

Returns the value of a field as an long .

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(...) // w ww .j  a  v  a2  s. c  om
 * @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;
}

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

License:Open Source License

public long removeFromDatastoreAndIndex_bySourceUrl(String sourceUrl, String sourceKey, ObjectId communityId) {

    try {//ww  w.  j  a  v  a  2s.c o  m
        // (never any content)
        BasicDBObject query = new BasicDBObject(DocumentPojo.sourceUrl_, sourceUrl);
        query.put(DocumentPojo.sourceKey_, SourcePojo.getDistributedKeyQueryTerm(sourceKey));
        BasicDBObject softDeleter = getSoftDeleteUpdate();
        DbManager.getDocument().getMetadata().update(query, softDeleter, false, true);
        CommandResult result = DbManager.getDocument().getLastError("metadata");

        // Quick delete for index though:
        if (!communityId.equals(_cachedCommunityIdForSourceXxxDeletion)) {
            StringBuffer sb = new StringBuffer(DocumentPojoIndexMap.manyGeoDocumentIndexCollection_)
                    .append(",docs_").append(communityId).append('/')
                    .append(DocumentPojoIndexMap.documentType_);
            _cachedIndexManagerForSourceXxxDeletion = IndexManager.getIndex(sb.toString());
            _cachedCommunityIdForSourceXxxDeletion = communityId;
        } //TESTED
        _cachedIndexManagerForSourceXxxDeletion.doDeleteByQuery(
                QueryBuilders.boolQuery().must(QueryBuilders.termQuery(DocumentPojo.sourceUrl_, sourceUrl))
                        .must(QueryBuilders.termQuery(DocumentPojo.sourceKey_, sourceKey)));

        return result.getLong("n", 0);

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