Ejemplo n.º 1
0
 void LegacyReplicationCoordinator::prepareReplSetUpdatePositionCommand(
         OperationContext* txn,
         BSONObjBuilder* cmdBuilder) {
     invariant(getReplicationMode() == modeReplSet);
     boost::lock_guard<boost::mutex> lock(_mutex);
     cmdBuilder->append("replSetUpdatePosition", 1);
     // create an array containing objects each member connected to us and for ourself
     BSONArrayBuilder arrayBuilder(cmdBuilder->subarrayStart("optimes"));
     OID myID = getMyRID(txn);
     {
         for (SlaveOpTimeMap::const_iterator itr = _slaveOpTimeMap.begin();
                 itr != _slaveOpTimeMap.end(); ++itr) {
             const OID& rid = itr->first;
             const BSONObj& config = mapFindWithDefault(_ridConfigMap, rid, BSONObj());
             BSONObjBuilder entry(arrayBuilder.subobjStart());
             entry.append("_id", rid);
             entry.append("optime", itr->second);
             // SERVER-14550 Even though the "config" field isn't used on the other end in 2.8,
             // we need to keep sending it for 2.6 compatibility.
             // TODO(spencer): Remove this after 2.8 is released.
             if (rid == myID) {
                 entry.append("config", theReplSet->myConfig().asBson());
             }
             else {
                 entry.append("config", config);
             }
         }
     }
 }
Ejemplo n.º 2
0
std::string Element::toString() const {
    if (!ok())
        return "INVALID-MUTABLE-ELEMENT";

    if (hasValue())
        return getValue().toString();

    const BSONType type = getType();

    // The only types that sometimes don't have a value are Object and Array nodes.
    dassert((type == mongo::Object) || (type == mongo::Array));

    if (type == mongo::Object) {
        BSONObjBuilder builder;
        writeTo(&builder);
        BSONObj obj = builder.obj();
        return obj.firstElement().toString();
    } else {
        // It must be an array.
        BSONObjBuilder builder;
        BSONArrayBuilder arrayBuilder(builder.subarrayStart(getFieldName()));
        writeArrayTo(&arrayBuilder);
        arrayBuilder.done();
        BSONObj obj = builder.obj();
        return obj.firstElement().toString();
    }
}
Ejemplo n.º 3
0
BSONObj FindAndModifyRequest::toBSON(const BSONObj& commandPassthroughFields) const {
    BSONObjBuilder builder;

    builder.append(kCommandName, _ns.coll());
    builder.append(kQueryField, _query);

    if (_update) {
        _update->serializeToBSON(kUpdateField, &builder);

        if (_isUpsert) {
            builder.append(kUpsertField, _isUpsert);
        }
    } else {
        builder.append(kRemoveField, true);
    }

    if (_fieldProjection) {
        builder.append(kFieldProjectionField, _fieldProjection.get());
    }

    if (_sort) {
        builder.append(kSortField, _sort.get());
    }

    if (_collation) {
        builder.append(kCollationField, _collation.get());
    }

    if (_arrayFilters) {
        BSONArrayBuilder arrayBuilder(builder.subarrayStart(kArrayFiltersField));
        for (auto arrayFilter : _arrayFilters.get()) {
            arrayBuilder.append(arrayFilter);
        }
        arrayBuilder.doneFast();
    }

    if (_shouldReturnNew) {
        builder.append(kNewField, _shouldReturnNew);
    }

    if (_writeConcern) {
        builder.append(kWriteConcernField, _writeConcern->toBSON());
    }

    if (_bypassDocumentValidation) {
        builder.append(kBypassDocumentValidationField, _bypassDocumentValidation);
    }

    IDLParserErrorContext::appendGenericCommandArguments(
        commandPassthroughFields, _knownFields, &builder);

    return builder.obj();
}
Ejemplo n.º 4
0
BSONObj ClusterStatistics::ShardStatistics::toBSON() const {
    BSONObjBuilder builder;
    builder.append("id", shardId.toString());
    builder.append("maxSizeMB", static_cast<long long>(maxSizeMB));
    builder.append("currSizeMB", static_cast<long long>(currSizeMB));
    builder.append("draining", isDraining);
    if (!shardTags.empty()) {
        BSONArrayBuilder arrayBuilder(builder.subarrayStart("tags"));
        arrayBuilder.append(shardTags);
    }

    builder.append("version", mongoVersion);
    return builder.obj();
}
Ejemplo n.º 5
0
   ///PD_TRACE_DECLARE_FUNCTION ( SDB__MTHELEMMATCHGETN, "mthElemMatchGetN" )
   static INT32 mthElemMatchGetN( const CHAR *fieldName,
                                  const bson::BSONElement &in,
                                  _mthSAction *action,
                                  bson::BSONElement &out,
                                  INT32 n )
   {
      INT32 rc = SDB_OK ;
      PD_TRACE_ENTRY( SDB__MTHELEMMATCHGETN ) ;
      if ( Array == in.type() )
      {
         BSONObjBuilder objBuilder ;
         BSONArrayBuilder arrayBuilder( objBuilder.subarrayStart( fieldName ) ) ;
         _mthElemMatchIterator i( in.embeddedObject(),
                                  &( action->getMatcher() ),
                                  n ) ;
         do
         {
            BSONElement next ;
            rc = i.next( next ) ;
            if ( SDB_OK == rc )
            {
               arrayBuilder.append( next ) ;
            }
            else if ( SDB_DMS_EOC == rc )
            {
               arrayBuilder.doneFast() ;
               rc = SDB_OK ;
               break ;
            }
            else
            {
               PD_LOG( PDERROR, "failed to get next element:%d", rc ) ;
               goto error ;
            }
         } while ( TRUE ) ;

         action->setObj( objBuilder.obj() ) ;
         out = action->getObj().getField( fieldName ) ;
      }
      else
      {
         out = BSONElement() ;
      }
   done:
      PD_TRACE_EXITRC( SDB__MTHELEMMATCHGETN, rc ) ;
      return rc ;
   error:
      goto done ;
   }
void OperationLatencyHistogram::_append(const HistogramData& data,
                                        const char* key,
                                        BSONObjBuilder* builder) const {

    BSONObjBuilder histogramBuilder(builder->subobjStart(key));
    BSONArrayBuilder arrayBuilder(histogramBuilder.subarrayStart("histogram"));
    for (int i = 0; i < kMaxBuckets; i++) {
        if (data.buckets[i] == 0)
            continue;
        BSONObjBuilder entryBuilder(arrayBuilder.subobjStart());
        entryBuilder.append("micros", static_cast<long long>(kLowerBounds[i]));
        entryBuilder.append("count", static_cast<long long>(data.buckets[i]));
        entryBuilder.doneFast();
    }

    arrayBuilder.doneFast();
    histogramBuilder.append("latency", static_cast<long long>(data.sum));
    histogramBuilder.append("ops", static_cast<long long>(data.entryCount));
    histogramBuilder.doneFast();
}
Ejemplo n.º 7
0
 void ReplicationCoordinatorImpl::prepareReplSetUpdatePositionCommand(
         OperationContext* txn,
         BSONObjBuilder* cmdBuilder) {
     boost::lock_guard<boost::mutex> lock(_mutex);
     cmdBuilder->append("replSetUpdatePosition", 1);
     // create an array containing objects each member connected to us and for ourself
     BSONArrayBuilder arrayBuilder(cmdBuilder->subarrayStart("optimes"));
     {
         for (SlaveInfoMap::const_iterator itr = _slaveInfoMap.begin();
                 itr != _slaveInfoMap.end(); ++itr) {
             const OID& rid = itr->first;
             const SlaveInfo& info = itr->second;
             BSONObjBuilder entry(arrayBuilder.subobjStart());
             entry.append("_id", rid);
             entry.append("optime", info.opTime);
             // SERVER-14550 Even though the "config" field isn't used on the other end in 2.8,
             // we need to keep sending it for 2.6 compatibility.
             // TODO(spencer): Remove this after 2.8 is released.
             const MemberConfig& memberConfig = _rsConfig.getMemberAt(info.memberID);
             entry.append("config", memberConfig.toBSON(_rsConfig.getTagConfig()));
         }
     }
 }