本文整理汇总了C++中BSONElement::wrap方法的典型用法代码示例。如果您正苦于以下问题:C++ BSONElement::wrap方法的具体用法?C++ BSONElement::wrap怎么用?C++ BSONElement::wrap使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BSONElement
的用法示例。
在下文中一共展示了BSONElement::wrap方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: upsert
void Helpers::upsert(OperationContext* txn, const string& ns, const BSONObj& o, bool fromMigrate) {
BSONElement e = o["_id"];
verify(e.type());
BSONObj id = e.wrap();
OldClientContext context(txn, ns);
const NamespaceString requestNs(ns);
UpdateRequest request(requestNs);
request.setQuery(id);
request.setUpdates(o);
request.setUpsert();
request.setFromMigration(fromMigrate);
UpdateLifecycleImpl updateLifecycle(requestNs);
request.setLifecycle(&updateLifecycle);
update(txn, context.db(), request);
}
示例2: deleteRecord
void DataFileMgr::deleteRecord(NamespaceDetails* d, const StringData& ns, Record *todelete,
const DiskLoc& dl, bool cappedOK, bool noWarn, bool doLog ) {
dassert( todelete == dl.rec() );
if ( d->isCapped() && !cappedOK ) {
out() << "failing remove on a capped ns " << ns << endl;
uassert( 10089 , "can't remove from a capped collection" , 0 );
return;
}
BSONObj obj = BSONObj::make( todelete );
BSONObj toDelete;
if ( doLog ) {
BSONElement e = obj["_id"];
if ( e.type() ) {
toDelete = e.wrap();
}
}
Collection* collection = cc().database()->getCollection( ns );
verify( collection );
/* check if any cursors point to us. if so, advance them. */
ClientCursor::aboutToDelete(ns, d, dl);
collection->getIndexCatalog()->unindexRecord( obj, dl, noWarn );
_deleteRecord(d, ns, todelete, dl);
collection->infoCache()->notifyOfWriteOp();
if ( ! toDelete.isEmpty() ) {
// TODO: this is crazy, need to fix logOp
const char* raw = ns.rawData();
if ( strlen(raw) == ns.size() ) {
logOp( "d", raw, toDelete );
}
else {
string temp = ns.toString();
logOp( "d", temp.c_str(), toDelete );
}
}
}
示例3: upsert
void Helpers::upsert( const string& ns , const BSONObj& o, bool fromMigrate ) {
BSONElement e = o["_id"];
verify( e.type() );
BSONObj id = e.wrap();
OpDebug debug;
Client::Context context(ns);
const NamespaceString requestNs(ns);
UpdateRequest request(requestNs);
request.setQuery(id);
request.setUpdates(o);
request.setUpsert();
request.setUpdateOpLog();
request.setFromMigration(fromMigrate);
update(request, &debug);
}
示例4: logDeleteOp
void MigrationSourceManager::logDeleteOp(OperationContext* txn,
const char* ns,
const BSONObj& obj,
bool notInActiveChunk) {
ensureShardVersionOKOrThrow(txn, ns);
if (notInActiveChunk)
return;
dassert(txn->lockState()->isWriteLocked()); // Must have Global IX.
BSONElement ide = obj["_id"];
if (ide.eoo()) {
warning() << "logDeleteOp got mod with no _id, ignoring obj: " << obj << migrateLog;
return;
}
BSONObj idObj(ide.wrap());
txn->recoveryUnit()->registerChange(new LogOpForShardingHandler(this, idObj, 'd'));
}
示例5: gotHandshake
// used to establish a slave for 'w' write concern
void Client::gotHandshake( const BSONObj& o ) {
BSONObjIterator i(o);
{
BSONElement id = i.next();
verify( id.type() );
_remoteId = id.wrap( "_id" );
}
BSONObjBuilder b;
while ( i.more() )
b.append( i.next() );
b.appendElementsUnique( _handshake );
_handshake = b.obj();
if (theReplSet && o.hasField("member")) {
theReplSet->registerSlave(_remoteId, o["member"].Int());
}
}
示例6: extractID
FieldParser::FieldState FieldParser::extractID(BSONElement elem,
const BSONField<BSONObj>& field,
BSONObj* out,
string* errMsg) {
if (elem.eoo()) {
if (field.hasDefault()) {
*out = field.getDefault().firstElement().wrap("");
return FIELD_DEFAULT;
} else {
return FIELD_NONE;
}
}
if (elem.type() != Array) {
*out = elem.wrap("").getOwned();
return FIELD_SET;
}
_genFieldErrMsg(elem, field, "id", errMsg);
return FIELD_INVALID;
}
示例7: parseLegacyQuery
bool GeoQuery::parseLegacyQuery(const BSONObj &obj) {
// The only legacy syntax is {$within: {.....}}
BSONObjIterator outerIt(obj);
if (!outerIt.more()) {
return false;
}
BSONElement withinElt = outerIt.next();
if (outerIt.more()) {
return false;
}
if (!withinElt.isABSONObj()) {
return false;
}
if (!equals(withinElt.fieldName(), "$within") && !equals(withinElt.fieldName(), "$geoWithin")) {
return false;
}
BSONObj withinObj = withinElt.embeddedObject();
bool hasGeometry = false;
BSONObjIterator withinIt(withinObj);
while (withinIt.more()) {
BSONElement elt = withinIt.next();
if (equals(elt.fieldName(), "$uniqueDocs")) {
warning() << "deprecated $uniqueDocs option: " << obj.toString() << endl;
// return false;
}
else if (elt.isABSONObj()) {
hasGeometry = geoContainer.parseFrom(elt.wrap());
}
else {
warning() << "bad geo query: " << obj.toString() << endl;
return false;
}
}
predicate = GeoQuery::WITHIN;
return hasGeometry;
}
示例8: logUpdateOp
void MigrationSourceManager::logUpdateOp(OperationContext* txn,
const char* ns,
const BSONObj& updatedDoc) {
dassert(txn->lockState()->isWriteLocked()); // Must have Global IX.
if (!_sessionId || (_nss != ns))
return;
BSONElement idElement = updatedDoc["_id"];
if (idElement.eoo()) {
warning() << "logUpdateOp got a document with no _id field, ignoring updatedDoc: "
<< updatedDoc << migrateLog;
return;
}
BSONObj idObj(idElement.wrap());
if (!isInRange(updatedDoc, _min, _max, _shardKeyPattern)) {
return;
}
txn->recoveryUnit()->registerChange(new LogOpForShardingHandler(this, idObj, 'u'));
}
示例9: upsert
void Helpers::upsert( TransactionExperiment* txn,
const string& ns,
const BSONObj& o,
bool fromMigrate ) {
BSONElement e = o["_id"];
verify( e.type() );
BSONObj id = e.wrap();
OpDebug debug;
Client::Context context(ns);
const NamespaceString requestNs(ns);
UpdateRequest request(requestNs);
request.setQuery(id);
request.setUpdates(o);
request.setUpsert();
request.setUpdateOpLog();
request.setFromMigration(fromMigrate);
UpdateLifecycleImpl updateLifecycle(true, requestNs);
request.setLifecycle(&updateLifecycle);
update(txn, context.db(), request, &debug);
}
示例10: logUpdateOp
void MigrationSourceManager::logUpdateOp(OperationContext* txn,
const char* ns,
const BSONObj& pattern,
bool notInActiveChunk) {
ensureShardVersionOKOrThrow(txn, ns);
if (notInActiveChunk)
return;
dassert(txn->lockState()->isWriteLocked()); // Must have Global IX.
if (!_active || (_nss != ns))
return;
BSONElement ide = pattern.getField("_id");
if (ide.eoo()) {
warning() << "logUpdateOp got mod with no _id, ignoring obj: " << pattern << migrateLog;
return;
}
BSONObj idObj(ide.wrap());
BSONObj fullDoc;
OldClientContext ctx(txn, _nss.ns(), false);
if (!Helpers::findById(txn, ctx.db(), _nss.ns().c_str(), idObj, fullDoc)) {
warning() << "logUpdateOp couldn't find: " << idObj << " even though should have"
<< migrateLog;
dassert(false); // TODO: Abort the migration.
return;
}
if (!isInRange(fullDoc, _min, _max, _shardKeyPattern)) {
return;
}
txn->recoveryUnit()->registerChange(new LogOpForShardingHandler(this, idObj, 'u'));
}
示例11: gotHandshake
bool Client::gotHandshake( const BSONObj& o ) {
BSONObjIterator i(o);
{
BSONElement id = i.next();
verify( id.type() );
_remoteId = id.wrap( "_id" );
}
BSONObjBuilder b;
while ( i.more() )
b.append( i.next() );
b.appendElementsUnique( _handshake );
_handshake = b.obj();
if (repl::getGlobalReplicationCoordinator()->getReplicationMode() !=
repl::ReplicationCoordinator::modeReplSet || !o.hasField("member")) {
return false;
}
return repl::theReplSet->registerSlave(_remoteId, o["member"].Int());
}
示例12: _reduce
/**
* actually applies a reduce, to a list of tuples (key, value).
* After the call, tuples will hold a single tuple {"0": key, "1": value}
*/
void JSReducer::_reduce( const BSONList& tuples , BSONObj& key , int& endSizeEstimate ) {
uassert( 10074 , "need values" , tuples.size() );
int sizeEstimate = ( tuples.size() * tuples.begin()->getField( "value" ).size() ) + 128;
// need to build the reduce args: ( key, [values] )
BSONObjBuilder reduceArgs( sizeEstimate );
boost::scoped_ptr<BSONArrayBuilder> valueBuilder;
int sizeSoFar = 0;
unsigned n = 0;
for ( ; n<tuples.size(); n++ ) {
BSONObjIterator j(tuples[n]);
BSONElement keyE = j.next();
if ( n == 0 ) {
reduceArgs.append( keyE );
key = keyE.wrap();
sizeSoFar = 5 + keyE.size();
valueBuilder.reset(new BSONArrayBuilder( reduceArgs.subarrayStart( "tuples" ) ));
}
BSONElement ee = j.next();
uassert( 13070 , "value too large to reduce" , ee.size() < ( BSONObjMaxUserSize / 2 ) );
if ( sizeSoFar + ee.size() > BSONObjMaxUserSize ) {
assert( n > 1 ); // if not, inf. loop
break;
}
valueBuilder->append( ee );
sizeSoFar += ee.size();
}
assert(valueBuilder);
valueBuilder->done();
BSONObj args = reduceArgs.obj();
Scope * s = _func.scope();
s->invokeSafe( _func.func() , args );
if ( s->type( "return" ) == Array ) {
uasserted( 10075 , "reduce -> multiple not supported yet");
return;
}
endSizeEstimate = key.objsize() + ( args.objsize() / tuples.size() );
if ( n == tuples.size() )
return;
// the input list was too large, add the rest of elmts to new tuples and reduce again
// note: would be better to use loop instead of recursion to avoid stack overflow
BSONList x;
for ( ; n < tuples.size(); n++ ) {
x.push_back( tuples[n] );
}
BSONObjBuilder temp( endSizeEstimate );
temp.append( key.firstElement() );
s->append( temp , "1" , "return" );
x.push_back( temp.obj() );
_reduce( x , key , endSizeEstimate );
}
示例13: it
ProjectionExec::ProjectionExec(const BSONObj& spec,
const MatchExpression* queryExpression,
const MatchExpressionParser::WhereCallback& whereCallback)
: _include(true),
_special(false),
_source(spec),
_includeID(true),
_skip(0),
_limit(-1),
_arrayOpType(ARRAY_OP_NORMAL),
_hasNonSimple(false),
_hasDottedField(false),
_queryExpression(queryExpression),
_hasReturnKey(false) {
// Are we including or excluding fields?
// -1 when we haven't initialized it.
// 1 when we're including
// 0 when we're excluding.
int include_exclude = -1;
BSONObjIterator it(_source);
while (it.more()) {
BSONElement e = it.next();
if (!e.isNumber() && !e.isBoolean()) {
_hasNonSimple = true;
}
if (Object == e.type()) {
BSONObj obj = e.embeddedObject();
verify(1 == obj.nFields());
BSONElement e2 = obj.firstElement();
if (mongoutils::str::equals(e2.fieldName(), "$slice")) {
if (e2.isNumber()) {
int i = e2.numberInt();
if (i < 0) {
add(e.fieldName(), i, -i); // limit is now positive
} else {
add(e.fieldName(), 0, i);
}
} else {
verify(e2.type() == Array);
BSONObj arr = e2.embeddedObject();
verify(2 == arr.nFields());
BSONObjIterator it(arr);
int skip = it.next().numberInt();
int limit = it.next().numberInt();
verify(limit > 0);
add(e.fieldName(), skip, limit);
}
} else if (mongoutils::str::equals(e2.fieldName(), "$elemMatch")) {
_arrayOpType = ARRAY_OP_ELEM_MATCH;
// Create a MatchExpression for the elemMatch.
BSONObj elemMatchObj = e.wrap();
verify(elemMatchObj.isOwned());
_elemMatchObjs.push_back(elemMatchObj);
StatusWithMatchExpression swme =
MatchExpressionParser::parse(elemMatchObj, whereCallback);
verify(swme.isOK());
// And store it in _matchers.
_matchers[mongoutils::str::before(e.fieldName(), '.').c_str()] = swme.getValue();
add(e.fieldName(), true);
} else if (mongoutils::str::equals(e2.fieldName(), "$meta")) {
verify(String == e2.type());
if (e2.valuestr() == LiteParsedQuery::metaTextScore) {
_meta[e.fieldName()] = META_TEXT_SCORE;
} else if (e2.valuestr() == LiteParsedQuery::metaRecordId) {
_meta[e.fieldName()] = META_RECORDID;
} else if (e2.valuestr() == LiteParsedQuery::metaGeoNearPoint) {
_meta[e.fieldName()] = META_GEONEAR_POINT;
} else if (e2.valuestr() == LiteParsedQuery::metaGeoNearDistance) {
_meta[e.fieldName()] = META_GEONEAR_DIST;
} else if (e2.valuestr() == LiteParsedQuery::metaIndexKey) {
_hasReturnKey = true;
// The index key clobbers everything so just stop parsing here.
return;
} else {
// This shouldn't happen, should be caught by parsing.
verify(0);
}
} else {
verify(0);
}
} else if (mongoutils::str::equals(e.fieldName(), "_id") && !e.trueValue()) {
_includeID = false;
} else {
add(e.fieldName(), e.trueValue());
// Projections of dotted fields aren't covered.
if (mongoutils::str::contains(e.fieldName(), '.')) {
_hasDottedField = true;
}
// Validate input.
//.........这里部分代码省略.........
示例14: WrappedObjectMatcher
WrappedObjectMatcher(BSONElement matchCondition,
const boost::intrusive_ptr<ExpressionContext>& expCtx)
: _matchExpr(matchCondition.wrap(""),
expCtx,
stdx::make_unique<ExtensionsCallbackNoop>(),
MatchExpressionParser::kBanAllSpecialFeatures) {}
示例15: save
void Model::save( bool safe ) {
scoped_ptr<ScopedDbConnection> conn(
ScopedDbConnection::getScopedDbConnection (modelServer() ) );
BSONObjBuilder b;
serialize( b );
BSONElement myId;
{
BSONObjIterator i = b.iterator();
while ( i.more() ) {
BSONElement e = i.next();
if ( strcmp( e.fieldName() , "_id" ) == 0 ) {
myId = e;
break;
}
}
}
if ( myId.type() ) {
if ( _id.isEmpty() ) {
_id = myId.wrap();
}
else if ( myId.woCompare( _id.firstElement() ) ) {
stringstream ss;
ss << "_id from serialize and stored differ: ";
ss << '[' << myId << "] != ";
ss << '[' << _id.firstElement() << ']';
throw UserException( 13121 , ss.str() );
}
}
if ( _id.isEmpty() ) {
OID oid;
oid.init();
b.appendOID( "_id" , &oid );
BSONObj o = b.obj();
conn->get()->insert( getNS() , o );
_id = o["_id"].wrap().getOwned();
LOG(4) << "inserted new model " << getNS() << " " << o << endl;
}
else {
if ( myId.eoo() ) {
myId = _id["_id"];
b.append( myId );
}
verify( ! myId.eoo() );
BSONObjBuilder qb;
qb.append( myId );
BSONObj q = qb.obj();
BSONObj o = b.obj();
LOG(4) << "updated model" << getNS() << " " << q << " " << o << endl;
conn->get()->update( getNS() , q , o , true );
}
string errmsg = "";
if ( safe )
errmsg = conn->get()->getLastError();
conn->done();
if ( safe && errmsg.size() )
throw UserException( 9003 , (string)"error on Model::save: " + errmsg );
}