本文整理汇总了C++中BSONArrayBuilder::append方法的典型用法代码示例。如果您正苦于以下问题:C++ BSONArrayBuilder::append方法的具体用法?C++ BSONArrayBuilder::append怎么用?C++ BSONArrayBuilder::append使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BSONArrayBuilder
的用法示例。
在下文中一共展示了BSONArrayBuilder::append方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: run
bool run(OperationContext* txn,
const string& dbname,
BSONObj& jsobj,
int,
string& errmsg,
BSONObjBuilder& result,
bool /*fromRepl*/) {
Client::ReadContext ctx( txn, dbname );
const Database* d = ctx.ctx().db();
const DatabaseCatalogEntry* dbEntry = d->getDatabaseCatalogEntry();
list<string> names;
dbEntry->getCollectionNamespaces( &names );
BSONArrayBuilder arr;
for ( list<string>::const_iterator i = names.begin(); i != names.end(); ++i ) {
string ns = *i;
BSONObjBuilder b;
b.append( "name", nsToCollectionSubstring( ns ) );
CollectionOptions options =
dbEntry->getCollectionCatalogEntry( txn, ns )->getCollectionOptions();
b.append( "options", options.toBSON() );
arr.append( b.obj() );
}
result.append( "collections", arr.arr() );
return true;
}
示例2: serialize
void BitTestMatchExpression::serialize(BSONObjBuilder* out) const {
string opString = "";
switch (matchType()) {
case BITS_ALL_SET:
opString = "$bitsAllSet";
break;
case BITS_ALL_CLEAR:
opString = "$bitsAllClear";
break;
case BITS_ANY_SET:
opString = "$bitsAnySet";
break;
case BITS_ANY_CLEAR:
opString = "$bitsAnyClear";
break;
default:
invariant(false);
}
BSONArrayBuilder arrBob;
for (auto bitPosition : _bitPositions) {
arrBob.append(bitPosition);
}
arrBob.doneFast();
out->append(path(), BSON(opString << arrBob.arr()));
}
示例3: run
bool run(OperationContext* txn,
const string& dbname,
BSONObj& jsobj,
int,
string& errmsg,
BSONObjBuilder& result,
bool /*fromRepl*/) {
AutoGetDb autoDb(txn, dbname, MODE_S);
const Database* d = autoDb.getDb();
const DatabaseCatalogEntry* dbEntry = NULL;
list<string> names;
if ( d ) {
dbEntry = d->getDatabaseCatalogEntry();
dbEntry->getCollectionNamespaces( &names );
names.sort();
}
scoped_ptr<MatchExpression> matcher;
if ( jsobj["filter"].isABSONObj() ) {
StatusWithMatchExpression parsed =
MatchExpressionParser::parse( jsobj["filter"].Obj() );
if ( !parsed.isOK() ) {
return appendCommandStatus( result, parsed.getStatus() );
}
matcher.reset( parsed.getValue() );
}
BSONArrayBuilder arr;
for ( list<string>::const_iterator i = names.begin(); i != names.end(); ++i ) {
string ns = *i;
StringData collection = nsToCollectionSubstring( ns );
if ( collection == "system.namespaces" ) {
continue;
}
BSONObjBuilder b;
b.append( "name", collection );
CollectionOptions options =
dbEntry->getCollectionCatalogEntry( txn, ns )->getCollectionOptions(txn);
b.append( "options", options.toBSON() );
BSONObj maybe = b.obj();
if ( matcher && !matcher->matchesBSON( maybe ) ) {
continue;
}
arr.append( maybe );
}
result.append( "collections", arr.arr() );
return true;
}
示例4: handleCursorCommand
static void handleCursorCommand(CursorId id, BSONObj& cmdObj, BSONObjBuilder& result) {
BSONElement batchSizeElem = cmdObj.getFieldDotted("cursor.batchSize");
const long long batchSize = batchSizeElem.isNumber()
? batchSizeElem.numberLong()
: 101; // same as query
ClientCursorPin pin(id);
ClientCursor* cursor = pin.c();
massert(16958, "Cursor shouldn't have been deleted",
cursor);
verify(cursor->isAggCursor);
PipelineRunner* runner = dynamic_cast<PipelineRunner*>(cursor->getRunner());
verify(runner);
try {
const string cursorNs = cursor->ns(); // we need this after cursor may have been deleted
// can't use result BSONObjBuilder directly since it won't handle exceptions correctly.
BSONArrayBuilder resultsArray;
const int byteLimit = MaxBytesToReturnToClientAtOnce;
BSONObj next;
for (int objCount = 0; objCount < batchSize; objCount++) {
// The initial getNext() on a PipelineRunner may be very expensive so we don't do it
// when batchSize is 0 since that indicates a desire for a fast return.
if (runner->getNext(&next, NULL) != Runner::RUNNER_ADVANCED) {
pin.deleteUnderlying();
id = 0;
cursor = NULL; // make it an obvious error to use cursor after this point
break;
}
if (resultsArray.len() + next.objsize() > byteLimit) {
// too big. next will be the first doc in the second batch
runner->pushBack(next);
break;
}
resultsArray.append(next);
}
if (cursor) {
// If a time limit was set on the pipeline, remaining time is "rolled over" to the
// cursor (for use by future getmore ops).
cursor->setLeftoverMaxTimeMicros( cc().curop()->getRemainingMaxTimeMicros() );
}
BSONObjBuilder cursorObj(result.subobjStart("cursor"));
cursorObj.append("id", id);
cursorObj.append("ns", cursorNs);
cursorObj.append("firstBatch", resultsArray.arr());
cursorObj.done();
}
catch (...) {
// Clean up cursor on way out of scope.
pin.deleteUnderlying();
throw;
}
}
示例5: _getArrayBuilderFromArr
BSONArrayBuilder DocumentStructureEnumerator::_getArrayBuilderFromArr(BSONArray arr) {
BSONArrayBuilder arrBuilder;
for (auto elem : arr) {
arrBuilder.append(elem);
}
return arrBuilder;
}
示例6: writeJointNames
void Node::writeJointNames (const sm::JointState& m)
{
BSONArrayBuilder b;
BOOST_FOREACH (const string& name, m.name)
b.append(name);
BSONObj s = BSON("names" << b.arr());
conn_->update(joint_name_coll_, mongo::fromjson("{}"), s, 1);
}
示例7: newLockDetails
StatusWith<LocksType> DistLockCatalogImpl::overtakeLock(StringData lockID,
const OID& lockSessionID,
const OID& currentHolderTS,
StringData who,
StringData processId,
Date_t time,
StringData why) {
BSONArrayBuilder orQueryBuilder;
orQueryBuilder.append(
BSON(LocksType::name() << lockID << LocksType::state(LocksType::UNLOCKED)));
orQueryBuilder.append(BSON(LocksType::name() << lockID << LocksType::lockID(currentHolderTS)));
BSONObj newLockDetails(BSON(LocksType::lockID(lockSessionID)
<< LocksType::state(LocksType::LOCKED) << LocksType::who() << who
<< LocksType::process() << processId << LocksType::when(time)
<< LocksType::why() << why));
auto request = FindAndModifyRequest::makeUpdate(
_locksNS, BSON("$or" << orQueryBuilder.arr()), BSON("$set" << newLockDetails));
request.setShouldReturnNew(true);
request.setWriteConcern(_writeConcern);
auto resultStatus = _client->runCommandWithNotMasterRetries(
"config", _locksNS.db().toString(), request.toBSON());
if (!resultStatus.isOK()) {
return resultStatus.getStatus();
}
BSONObj responseObj(resultStatus.getValue());
auto findAndModifyStatus = extractFindAndModifyNewObj(responseObj);
if (!findAndModifyStatus.isOK()) {
return findAndModifyStatus.getStatus();
}
BSONObj doc = findAndModifyStatus.getValue();
auto locksTypeResult = LocksType::fromBSON(doc);
if (!locksTypeResult.isOK()) {
return {ErrorCodes::FailedToParse,
str::stream() << "failed to parse: " << doc << " : "
<< locksTypeResult.getStatus().toString()};
}
return locksTypeResult.getValue();
}
示例8: doTxn
Status doTxn(OperationContext* opCtx,
const std::string& dbName,
const BSONObj& doTxnCmd,
BSONObjBuilder* result) {
auto txnNumber = opCtx->getTxnNumber();
uassert(ErrorCodes::InvalidOptions, "doTxn can only be run with a transaction ID.", txnNumber);
auto txnParticipant = TransactionParticipant::get(opCtx);
uassert(ErrorCodes::InvalidOptions, "doTxn must be run within a transaction", txnParticipant);
invariant(txnParticipant->inMultiDocumentTransaction());
invariant(opCtx->getWriteUnitOfWork());
uassert(
ErrorCodes::InvalidOptions, "doTxn supports only CRUD opts.", _areOpsCrudOnly(doTxnCmd));
auto hasPrecondition = _hasPrecondition(doTxnCmd);
// Acquire global lock in IX mode so that the replication state check will remain valid.
Lock::GlobalLock globalLock(opCtx, MODE_IX);
auto replCoord = repl::ReplicationCoordinator::get(opCtx);
bool userInitiatedWritesAndNotPrimary =
opCtx->writesAreReplicated() && !replCoord->canAcceptWritesForDatabase(opCtx, dbName);
if (userInitiatedWritesAndNotPrimary)
return Status(ErrorCodes::NotMaster,
str::stream() << "Not primary while applying ops to database " << dbName);
int numApplied = 0;
try {
BSONObjBuilder intermediateResult;
// The transaction takes place in a global unit of work, so the precondition check
// and the writes will share the same snapshot.
if (hasPrecondition) {
uassertStatusOK(_checkPrecondition(opCtx, doTxnCmd, result));
}
numApplied = 0;
uassertStatusOK(_doTxn(opCtx, dbName, doTxnCmd, &intermediateResult, &numApplied));
txnParticipant->commitUnpreparedTransaction(opCtx);
result->appendElements(intermediateResult.obj());
} catch (const DBException& ex) {
txnParticipant->abortActiveUnpreparedOrStashPreparedTransaction(opCtx);
BSONArrayBuilder ab;
++numApplied;
for (int j = 0; j < numApplied; j++)
ab.append(false);
result->append("applied", numApplied);
result->append("code", ex.code());
result->append("codeName", ErrorCodes::errorString(ex.code()));
result->append("errmsg", ex.what());
result->append("results", ab.arr());
return Status(ErrorCodes::UnknownError, ex.what());
}
return Status::OK();
}
示例9: process
void MongoSchema::process(){
//std::cout << "Processing " << m_dbname << "." << m_col << std::endl;
std::string querystr;
querystr.clear();
querystr.append(m_dbname);
querystr.append(".");
querystr.append(m_col);
int recordscount = m_conn->count(querystr);
//std::cout << "count:" << recordscount << std::endl;
std::auto_ptr<mongo::DBClientCursor> cursor = m_conn->query(querystr, mongo::Query());
//std::set<std::string> fields;
while(cursor->more()){
mongo::BSONObj bo = cursor->next();
for( BSONObj::iterator i = bo.begin(); i.more(); ) {
BSONElement e = i.next();
if(skipField(e.fieldName())){
continue;
}
if(e.isSimpleType()){
hashmap::const_iterator keyexsit = m_map.find(e.fieldName());
SchemaModel* sm = new SchemaModel();
if(keyexsit != m_map.end()){
sm = &m_map[e.fieldName()];
sm->count ++;
}else{
sm->count = 1;
sm->datatype = getType(e);
m_map[e.fieldName()] = *sm;
}
}else if(e.isABSONObj()){
int depth = 0;
std::string parent = e.fieldName();
extractBSON(e.Obj(), depth, parent);
}
}
}
BSONObjBuilder bOb;
BSONArrayBuilder bArr;
std::tr1::hash<std::string> hashfunc = m_map.hash_function();
for( hashmap::const_iterator i = m_map.begin(), e = m_map.end() ; i != e ; ++i ) {
SchemaModel sm = i->second;
float percentage = (float)sm.count / (float)recordscount * 100.0;
std::cout.precision(4);
BSONObj bo = BSON( "field" << i->first << "percent" << percentage << "datatype" << sm.datatype );
bArr.append(bo);
//std::cout << i->first << " -> "<< "Percent: "<< percentage << " (hash = " << hashfunc( i->first ) << ")" << "\r\n";
}
bOb.append(m_col, bArr.arr());
m_schema = bOb.obj();
}
示例10: toBSON
BSONObj IndexBounds::toBSON() const {
BSONObjBuilder builder;
if (isSimpleRange) {
// TODO
}
else {
for (vector<OrderedIntervalList>::const_iterator itField = fields.begin();
itField != fields.end();
++itField) {
BSONArrayBuilder fieldBuilder(builder.subarrayStart(itField->name));
for (vector<Interval>::const_iterator itInterval = itField->intervals.begin();
itInterval != itField->intervals.end();
++itInterval) {
BSONArrayBuilder intervalBuilder;
// Careful to output $minElement/$maxElement if we don't have bounds.
if (itInterval->start.eoo()) {
BSONObjBuilder minBuilder;
minBuilder.appendMinKey("");
BSONObj minKeyObj = minBuilder.obj();
intervalBuilder.append(minKeyObj.firstElement());
}
else {
intervalBuilder.append(itInterval->start);
}
if (itInterval->end.eoo()) {
BSONObjBuilder maxBuilder;
maxBuilder.appendMaxKey("");
BSONObj maxKeyObj = maxBuilder.obj();
intervalBuilder.append(maxKeyObj.firstElement());
}
else {
intervalBuilder.append(itInterval->end);
}
fieldBuilder.append(
static_cast<BSONArray>(intervalBuilder.arr().clientReadable()));
}
}
}
return builder.obj();
}
示例11: buildOpMsg
INT32 rtnCoordDelete::buildOpMsg( const CoordCataInfoPtr &cataInfo,
const CoordSubCLlist &subCLList,
CHAR *pSrcMsg, CHAR *&pDstMsg,
INT32 &bufferSize )
{
INT32 rc = SDB_OK;
INT32 flag;
CHAR *pCollectionName = NULL;
CHAR *pDeletor = NULL;
CHAR *pHint = NULL;
BSONObj boDeletor;
BSONObj boHint;
rc = msgExtractDelete( pSrcMsg, &flag, &pCollectionName,
&pDeletor, &pHint );
PD_RC_CHECK( rc, PDERROR,
"failed to parse delete request(rc=%d)",
rc );
try
{
boDeletor = BSONObj( pDeletor );
boHint = BSONObj( pHint );
BSONArrayBuilder babSubCL;
CoordSubCLlist::const_iterator iterCL = subCLList.begin();
while( iterCL != subCLList.end() )
{
babSubCL.append( *iterCL );
++iterCL;
}
BSONObjBuilder bobNewDeletor;
bobNewDeletor.appendElements( boDeletor );
bobNewDeletor.appendArray( CAT_SUBCL_NAME, babSubCL.arr() );
BSONObj boNewDeletor = bobNewDeletor.obj();
rc = msgBuildDeleteMsg( &pDstMsg, &bufferSize, pCollectionName,
flag, 0, &boNewDeletor, &boHint );
PD_RC_CHECK( rc, PDERROR,
"failed to build delete request(rc=%d)",
rc );
{
MsgOpDelete *pReqMsg = (MsgOpDelete *)pDstMsg;
MsgOpDelete *pSrcReq = (MsgOpDelete *)pSrcMsg;
pReqMsg->version = cataInfo->getVersion();
pReqMsg->w = pSrcReq->w;
}
}
catch ( std::exception &e )
{
PD_RC_CHECK( SDB_INVALIDARG, PDERROR,
"occur unexpected error:%s",
e.what() );
}
done:
return rc;
error:
goto done;
}
示例12: compoundValueBson
/* ****************************************************************************
*
* compoundValueBson (for arrays) -
*/
void compoundValueBson(std::vector<orion::CompoundValueNode*> children, BSONArrayBuilder& b)
{
for (unsigned int ix = 0; ix < children.size(); ++ix)
{
orion::CompoundValueNode* child = children[ix];
if (child->valueType == ValueTypeString)
{
b.append(child->stringValue);
}
else if (child->valueType == ValueTypeNumber)
{
b.append(child->numberValue);
}
else if (child->valueType == ValueTypeBoolean)
{
b.append(child->boolValue);
}
else if (child->valueType == ValueTypeNone)
{
b.appendNull();
}
else if (child->valueType == ValueTypeVector)
{
BSONArrayBuilder ba;
compoundValueBson(child->childV, ba);
b.append(ba.arr());
}
else if (child->valueType == ValueTypeObject)
{
BSONObjBuilder bo;
compoundValueBson(child->childV, bo);
b.append(bo.obj());
}
else
{
LM_E(("Runtime Error (Unknown type in compound value)"));
}
}
}
示例13: rolesToBSONArray
static BSONArray rolesToBSONArray(const User::RoleDataMap& roles) {
BSONArrayBuilder arrBuilder;
for (User::RoleDataMap::const_iterator it = roles.begin(); it != roles.end(); ++it) {
const User::RoleData& role = it->second;
arrBuilder.append(BSON("name" << role.name.getRole() <<
"source" << role.name.getDB() <<
"hasRole" << role.hasRole <<
"canDelegate" << role.canDelegate));
}
return arrBuilder.arr();
}
示例14: setUp
void ShardingMongodTestFixture::setUp() {
ServiceContextMongoDTest::setUp();
const auto service = getServiceContext();
_opCtx = makeOperationContext();
// Set up this node as part of a replica set.
repl::ReplSettings replSettings;
replSettings.setOplogSizeBytes(512'000);
replSettings.setReplSetString(ConnectionString::forReplicaSet(_setName, _servers).toString());
auto replCoordPtr = makeReplicationCoordinator(replSettings);
_replCoord = replCoordPtr.get();
BSONArrayBuilder serversBob;
for (size_t i = 0; i < _servers.size(); ++i) {
serversBob.append(BSON("host" << _servers[i].toString() << "_id" << static_cast<int>(i)));
}
repl::ReplSetConfig replSetConfig;
ASSERT_OK(replSetConfig.initialize(
BSON("_id" << _setName << "protocolVersion" << 1 << "version" << 3 << "members"
<< serversBob.arr())));
replCoordPtr->setGetConfigReturnValue(replSetConfig);
repl::ReplicationCoordinator::set(service, std::move(replCoordPtr));
auto storagePtr = stdx::make_unique<repl::StorageInterfaceMock>();
repl::DropPendingCollectionReaper::set(
service, stdx::make_unique<repl::DropPendingCollectionReaper>(storagePtr.get()));
repl::ReplicationProcess::set(service,
stdx::make_unique<repl::ReplicationProcess>(
storagePtr.get(),
stdx::make_unique<repl::ReplicationConsistencyMarkersMock>(),
stdx::make_unique<repl::ReplicationRecoveryMock>()));
ASSERT_OK(repl::ReplicationProcess::get(_opCtx.get())->initializeRollbackID(_opCtx.get()));
repl::StorageInterface::set(service, std::move(storagePtr));
auto opObserver = checked_cast<OpObserverRegistry*>(service->getOpObserver());
opObserver->addObserver(stdx::make_unique<OpObserverShardingImpl>());
opObserver->addObserver(stdx::make_unique<ConfigServerOpObserver>());
opObserver->addObserver(stdx::make_unique<ShardServerOpObserver>());
repl::setOplogCollectionName(service);
repl::createOplog(_opCtx.get());
// Set the highest FCV because otherwise it defaults to the lower FCV. This way we default to
// testing this release's code, not backwards compatibility code.
serverGlobalParams.featureCompatibility.setVersion(
ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo42);
}
示例15: writeOpsDirectlyToOplog
void TxnOplog::writeOpsDirectlyToOplog(GTID gtid, uint64_t timestamp, uint64_t hash) {
dassert(logTxnOpsForReplication());
dassert(_logTxnToOplog);
// build array of in memory ops
BSONArrayBuilder b;
for (deque<BSONObj>::iterator it = _m.begin(); it != _m.end(); it++) {
b.append(*it);
}
BSONArray a = b.arr();
// log ops
_logTxnToOplog(gtid, timestamp, hash, a);
}