virtual std::vector<BSONObj> stopIndexBuilds(Database* db, const BSONObj& cmdObj) { std::string toDeleteNs = db->name() + "." + cmdObj.firstElement().valuestr(); Collection* collection = db->getCollection(toDeleteNs); IndexCatalog::IndexKillCriteria criteria; // Get index name to drop BSONElement toDrop = cmdObj.getField("index"); if (toDrop.type() == String) { // Kill all in-progress indexes if (strcmp("*", toDrop.valuestr()) == 0) { criteria.ns = toDeleteNs; return IndexBuilder::killMatchingIndexBuilds(collection, criteria); } // Kill an in-progress index by name else { criteria.name = toDrop.valuestr(); return IndexBuilder::killMatchingIndexBuilds(collection, criteria); } } // Kill an in-progress index build by index key else if (toDrop.type() == Object) { criteria.key = toDrop.Obj(); return IndexBuilder::killMatchingIndexBuilds(collection, criteria); } return std::vector<BSONObj>(); }
bool getFieldName(OperationContext* txn, Collection* collection, IndexCatalog* indexCatalog, string* fieldOut, string* errOut, bool* isFrom2D) { vector<IndexDescriptor*> idxs; // First, try 2d. collection->getIndexCatalog()->findIndexByType(txn, IndexNames::GEO_2D, idxs); if (idxs.size() > 1) { *errOut = "more than one 2d index, not sure which to run geoNear on"; return false; } if (1 == idxs.size()) { BSONObj indexKp = idxs[0]->keyPattern(); BSONObjIterator kpIt(indexKp); while (kpIt.more()) { BSONElement elt = kpIt.next(); if (String == elt.type() && IndexNames::GEO_2D == elt.valuestr()) { *fieldOut = elt.fieldName(); *isFrom2D = true; return true; } } } // Next, 2dsphere. idxs.clear(); collection->getIndexCatalog()->findIndexByType(txn, IndexNames::GEO_2DSPHERE, idxs); if (0 == idxs.size()) { *errOut = "no geo indices for geoNear"; return false; } if (idxs.size() > 1) { *errOut = "more than one 2dsphere index, not sure which to run geoNear on"; return false; } // 1 == idx.size() BSONObj indexKp = idxs[0]->keyPattern(); BSONObjIterator kpIt(indexKp); while (kpIt.more()) { BSONElement elt = kpIt.next(); if (String == elt.type() && IndexNames::GEO_2DSPHERE == elt.valuestr()) { *fieldOut = elt.fieldName(); *isFrom2D = false; return true; } } return false; }
void Scope::loadStored( bool ignoreNotConnected ){ if ( _localDBName.size() == 0 ){ if ( ignoreNotConnected ) return; uassert( 10208 , "need to have locallyConnected already" , _localDBName.size() ); } if ( _loadedVersion == _lastVersion ) return; _loadedVersion = _lastVersion; string coll = _localDBName + ".system.js"; static DBClientBase * db = createDirectClient(); auto_ptr<DBClientCursor> c = db->query( coll , Query() ); assert( c.get() ); set<string> thisTime; while ( c->more() ){ BSONObj o = c->next(); BSONElement n = o["_id"]; BSONElement v = o["value"]; uassert( 10209 , "name has to be a string" , n.type() == String ); uassert( 10210 , "value has to be set" , v.type() != EOO ); setElement( n.valuestr() , v ); thisTime.insert( n.valuestr() ); _storedNames.insert( n.valuestr() ); } // --- remove things from scope that were removed list<string> toremove; for ( set<string>::iterator i=_storedNames.begin(); i!=_storedNames.end(); i++ ){ string n = *i; if ( thisTime.count( n ) == 0 ) toremove.push_back( n ); } for ( list<string>::iterator i=toremove.begin(); i!=toremove.end(); i++ ){ string n = *i; _storedNames.erase( n ); execSetup( (string)"delete " + n , "clean up scope" ); } }
void Scope::loadStored(bool ignoreNotConnected) { if (_localDBName.size() == 0) { if (ignoreNotConnected) return; uassert(10208, "need to have locallyConnected already", _localDBName.size()); } if (_loadedVersion == _lastVersion) return; _loadedVersion = _lastVersion; string coll = _localDBName + ".system.js"; DBClientBase* directDBClient = createDirectClient(); auto_ptr<DBClientCursor> c = directDBClient->query(coll, Query(), 0, 0, NULL, QueryOption_SlaveOk, 0); massert(16669, "unable to get db client cursor from query", c.get()); set<string> thisTime; while (c->more()) { BSONObj o = c->nextSafe(); BSONElement n = o["_id"]; BSONElement v = o["value"]; uassert(10209, str::stream() << "name has to be a string: " << n, n.type() == String); uassert(10210, "value has to be set", v.type() != EOO); try { setElement(n.valuestr(), v); thisTime.insert(n.valuestr()); _storedNames.insert(n.valuestr()); } catch (const DBException& setElemEx) { log() << "unable to load stored JavaScript function " << n.valuestr() << "(): " << setElemEx.what() << endl; } } // remove things from scope that were removed from the system.js collection for (set<string>::iterator i = _storedNames.begin(); i != _storedNames.end(); ) { if (thisTime.count(*i) == 0) { string toDelete = str::stream() << "delete " << *i; _storedNames.erase(i++); execSetup(toDelete, "clean up scope"); } else { ++i; } } }
void Scope::loadStored( bool ignoreNotConnected ){ if ( _localDBName.size() == 0 ){ if ( ignoreNotConnected ) return; uassert( "need to have locallyConnected already" , _localDBName.size() ); } if ( _loadedVersion == _lastVersion ) return; _loadedVersion = _lastVersion; static DBClientBase * db = createDirectClient(); auto_ptr<DBClientCursor> c = db->query( _localDBName + ".system.js" , Query() ); while ( c->more() ){ BSONObj o = c->next(); BSONElement n = o["_id"]; BSONElement v = o["value"]; uassert( "name has to be a string" , n.type() == String ); uassert( "value has to be set" , v.type() != EOO ); setElement( n.valuestr() , v ); } }
StatusWithMatchExpression expressionParserWhereCallbackReal(const BSONElement& where) { if ( !haveClient() ) return StatusWithMatchExpression( ErrorCodes::BadValue, "no current client needed for $where" ); Client::Context* context = cc().getContext(); if ( !context ) return StatusWithMatchExpression( ErrorCodes::BadValue, "no context in $where parsing" ); const char* ns = context->ns(); if ( !ns ) return StatusWithMatchExpression( ErrorCodes::BadValue, "no ns in $where parsing" ); if ( !globalScriptEngine ) return StatusWithMatchExpression( ErrorCodes::BadValue, "no globalScriptEngine in $where parsing" ); auto_ptr<WhereMatchExpression> exp( new WhereMatchExpression() ); if ( where.type() == String || where.type() == Code ) { Status s = exp->init( ns, where.valuestr(), BSONObj() ); if ( !s.isOK() ) return StatusWithMatchExpression( s ); return StatusWithMatchExpression( exp.release() ); } if ( where.type() == CodeWScope ) { Status s = exp->init( ns, where.codeWScopeCode(), BSONObj( where.codeWScopeScopeDataUnsafe() ) ); if ( !s.isOK() ) return StatusWithMatchExpression( s ); return StatusWithMatchExpression( exp.release() ); } return StatusWithMatchExpression( ErrorCodes::BadValue, "$where got bad type" ); }
S2AccessMethod::S2AccessMethod(IndexDescriptor *descriptor) : BtreeBasedAccessMethod(descriptor) { // Set up basic params. _params.maxKeysPerInsert = 200; // This is advisory. _params.maxCellsInCovering = 50; // Near distances are specified in meters...sometimes. _params.radius = S2IndexingParams::kRadiusOfEarthInMeters; // These are not advisory. _params.finestIndexedLevel = configValueWithDefault(descriptor, "finestIndexedLevel", S2::kAvgEdge.GetClosestLevel(500.0 / _params.radius)); _params.coarsestIndexedLevel = configValueWithDefault(descriptor, "coarsestIndexedLevel", S2::kAvgEdge.GetClosestLevel(100 * 1000.0 / _params.radius)); uassert(16747, "coarsestIndexedLevel must be >= 0", _params.coarsestIndexedLevel >= 0); uassert(16748, "finestIndexedLevel must be <= 30", _params.finestIndexedLevel <= 30); uassert(16749, "finestIndexedLevel must be >= coarsestIndexedLevel", _params.finestIndexedLevel >= _params.coarsestIndexedLevel); int geoFields = 0; // Categorize the fields we're indexing and make sure we have a geo field. BSONObjIterator i(descriptor->keyPattern()); while (i.more()) { BSONElement e = i.next(); if (e.type() == String && S2IndexingParams::SPHERE_2D_NAME == e.valuestr()) { ++geoFields; } } uassert(16750, "Expect at least one geo field, spec=" + descriptor->keyPattern().toString(), geoFields >= 1); }
/* for index info object: { "name" : "name_1" , "ns" : "foo.index3" , "key" : { "name" : 1.0 } } we need to fix up the value in the "ns" parameter so that the name prefix is correct on a copy to a new name. */ static BSONObj fixindex(BSONObj o, const string &dbname) { BSONObjBuilder b; BSONObjIterator i(o); while ( i.moreWithEOO() ) { BSONElement e = i.next(); if ( e.eoo() ) break; // for now, skip the "v" field so that v:0 indexes will be upgraded to v:1 if ( string("v") == e.fieldName() ) { continue; } if ( string("ns") == e.fieldName() ) { uassert( 10024 , "bad ns field for index during dbcopy", e.type() == String ); const char *p = strchr(e.valuestr(), '.'); uassert( 10025 , "bad ns field for index during dbcopy [2]", p); string newname = dbname + p; b.append("ns", newname); } else b.append(e); } BSONObj res= b.obj(); return res; }
GeoHaystackSearchIndex(const IndexPlugin* plugin, const IndexSpec* spec) : IndexType(plugin, spec) { BSONElement e = spec->info["bucketSize"]; uassert(13321, "need bucketSize", e.isNumber()); _bucketSize = e.numberDouble(); uassert(16455, "bucketSize cannot be zero", _bucketSize != 0.0); // Example: // db.foo.ensureIndex({ pos : "geoHaystack", type : 1 }, { bucketSize : 1 }) BSONObjIterator i(spec->keyPattern); while (i.more()) { BSONElement e = i.next(); if (e.type() == String && GEOSEARCHNAME == e.valuestr()) { uassert(13314, "can't have more than one geo field", _geoField.size() == 0); uassert(13315, "the geo field has to be first in index", _otherFields.size() == 0); _geoField = e.fieldName(); } else { // TODO(hk): Do we want to do any checking on e.type and e.valuestr? uassert(13326, "geoSearch can only have 1 non-geo field for now", _otherFields.size() == 0); _otherFields.push_back(e.fieldName()); } } uassert(13316, "no geo field specified", _geoField.size()); // XXX: Fix documentation that says the other field is optional; code says it's mandatory. uassert(13317, "no non-geo fields specified", _otherFields.size()); }
GeoHaystackSearchIndex( const IndexPlugin* plugin , const IndexSpec* spec ) : IndexType( plugin , spec ) { BSONElement e = spec->info["bucketSize"]; uassert( 13321 , "need bucketSize" , e.isNumber() ); _bucketSize = e.numberDouble(); BSONObjBuilder orderBuilder; BSONObjIterator i( spec->keyPattern ); while ( i.more() ) { BSONElement e = i.next(); if ( e.type() == String && GEOSEARCHNAME == e.valuestr() ) { uassert( 13314 , "can't have 2 geo fields" , _geo.size() == 0 ); uassert( 13315 , "2d has to be first in index" , _other.size() == 0 ); _geo = e.fieldName(); } else { _other.push_back( e.fieldName() ); } orderBuilder.append( "" , 1 ); } uassert( 13316 , "no geo field specified" , _geo.size() ); uassert( 13317 , "no other fields specified" , _other.size() ); uassert( 13326 , "quadrant search can only have 1 other field for now" , _other.size() == 1 ); _order = orderBuilder.obj(); }
// static bool LiteParsedQuery::isDiskLocMeta(BSONElement elt) { // elt must be foo: {$meta: "diskloc"} if (mongo::Object != elt.type()) { return false; } BSONObj metaObj = elt.Obj(); BSONObjIterator metaIt(metaObj); // must have exactly 1 element if (!metaIt.more()) { return false; } BSONElement metaElt = metaIt.next(); if (!mongoutils::str::equals("$meta", metaElt.fieldName())) { return false; } if (mongo::String != metaElt.type()) { return false; } if (LiteParsedQuery::metaDiskLoc != metaElt.valuestr()) { return false; } // must have exactly 1 element if (metaIt.more()) { return false; } return true; }
INT32 _clsAlterDC::init( INT32 flags, INT64 numToSkip, INT64 numToReturn, const CHAR *pMatcherBuff, const CHAR *pSelectBuff, const CHAR *pOrderByBuff, const CHAR *pHintBuff ) { INT32 rc = SDB_OK ; try { BSONObj query( pMatcherBuff ) ; BSONElement eleAction = query.getField( FIELD_NAME_ACTION ) ; if ( String != eleAction.type() ) { PD_LOG( PDERROR, "The field[%s] is not valid in command[%s]'s " "param[%s]", FIELD_NAME_ACTION, NAME_ALTER_DC, query.toString().c_str() ) ; rc = SDB_INVALIDARG ; goto error ; } _pAction = eleAction.valuestr() ; } catch( std::exception &e ) { PD_LOG( PDERROR, "Parse params occur exception: %s", e.what() ) ; rc = SDB_INVALIDARG ; goto error ; } done: return rc ; error: goto done ; }
int _getOption( BSONElement e , int def ) { if ( e.isNumber() ) return e.numberInt(); if ( e.type() == String ) return atoi( e.valuestr() ); return def; }
void ExpressionParams::parseTwoDParams(const BSONObj& infoObj, TwoDIndexingParams* out) { BSONObjIterator i(infoObj.getObjectField("key")); while (i.more()) { BSONElement e = i.next(); if (e.type() == String && IndexNames::GEO_2D == e.valuestr()) { uassert(16800, "can't have 2 geo fields", out->geo.size() == 0); uassert(16801, "2d has to be first in index", out->other.size() == 0); out->geo = e.fieldName(); } else { int order = 1; if (e.isNumber()) { order = static_cast<int>(e.Number()); } out->other.push_back(std::make_pair(e.fieldName(), order)); } } uassert(16802, "no geo field specified", out->geo.size()); GeoHashConverter::Parameters hashParams; Status paramStatus = GeoHashConverter::parseParameters(infoObj, &hashParams); uassertStatusOK(paramStatus); out->geoHashConverter.reset(new GeoHashConverter(hashParams)); }
void fillWriterVectors(const std::deque<BSONObj>& ops, std::vector<std::vector<BSONObj>>* writerVectors) { for (std::deque<BSONObj>::const_iterator it = ops.begin(); it != ops.end(); ++it) { const BSONElement e = it->getField("ns"); verify(e.type() == String); const char* ns = e.valuestr(); int len = e.valuestrsize(); uint32_t hash = 0; MurmurHash3_x86_32(ns, len, 0, &hash); const char* opType = it->getField("op").valuestrsafe(); if (getGlobalServiceContext()->getGlobalStorageEngine()->supportsDocLocking() && isCrudOpType(opType)) { BSONElement id; switch (opType[0]) { case 'u': id = it->getField("o2").Obj()["_id"]; break; case 'd': case 'i': id = it->getField("o").Obj()["_id"]; break; } const size_t idHash = BSONElement::Hasher()(id); MurmurHash3_x86_32(&idHash, sizeof(idHash), hash, &hash); } (*writerVectors)[hash % writerVectors->size()].push_back(*it); } }
void ExpressionParams::parseHaystackParams(const BSONObj& infoObj, std::string* geoFieldOut, std::vector<std::string>* otherFieldsOut, double* bucketSizeOut) { BSONElement e = infoObj["bucketSize"]; uassert(16777, "need bucketSize", e.isNumber()); *bucketSizeOut = e.numberDouble(); uassert(16769, "bucketSize cannot be zero", *bucketSizeOut != 0.0); // Example: // db.foo.ensureIndex({ pos : "geoHaystack", type : 1 }, { bucketSize : 1 }) BSONObjIterator i(infoObj.getObjectField("key")); while (i.more()) { BSONElement e = i.next(); if (e.type() == String && IndexNames::GEO_HAYSTACK == e.valuestr()) { uassert(16770, "can't have more than one geo field", geoFieldOut->size() == 0); uassert(16771, "the geo field has to be first in index", otherFieldsOut->size() == 0); *geoFieldOut = e.fieldName(); } else { uassert(16772, "geoSearch can only have 1 non-geo field for now", otherFieldsOut->size() == 0); otherFieldsOut->push_back(e.fieldName()); } } }
void S2AccessMethod::getKeys(const BSONObj& obj, BSONObjSet* keys) { BSONObjSet keysToAdd; // We output keys in the same order as the fields we index. BSONObjIterator i(_descriptor->keyPattern()); while (i.more()) { BSONElement e = i.next(); // First, we get the keys that this field adds. Either they're added literally from // the value of the field, or they're transformed if the field is geo. BSONElementSet fieldElements; // false means Don't expand the last array, duh. obj.getFieldsDotted(e.fieldName(), fieldElements, false); BSONObjSet keysForThisField; if (IndexNames::GEO_2DSPHERE == e.valuestr()) { // We can't ever return documents that don't have geometry so don't bother indexing // them. if (fieldElements.empty()) { return; } getGeoKeys(obj, fieldElements, &keysForThisField); } else { getLiteralKeys(fieldElements, &keysForThisField); } // We expect there to be the missing field element present in the keys if data is // missing. So, this should be non-empty. verify(!keysForThisField.empty()); // We take the Cartesian product of all of the keys. This requires that we have // some keys to take the Cartesian product with. If keysToAdd.empty(), we // initialize it. if (keysToAdd.empty()) { keysToAdd = keysForThisField; continue; } BSONObjSet updatedKeysToAdd; for (BSONObjSet::const_iterator it = keysToAdd.begin(); it != keysToAdd.end(); ++it) { for (BSONObjSet::const_iterator newIt = keysForThisField.begin(); newIt!= keysForThisField.end(); ++newIt) { BSONObjBuilder b; b.appendElements(*it); b.append(newIt->firstElement()); updatedKeysToAdd.insert(b.obj()); } } keysToAdd = updatedKeysToAdd; } if (keysToAdd.size() > _params.maxKeysPerInsert) { warning() << "insert of geo object generated lots of keys (" << keysToAdd.size() << ") consider creating larger buckets. obj=" << obj; } *keys = keysToAdd; }
bool run(OperationContext* txn, const string& dbname , BSONObj& jsobj, int, string& errmsg, BSONObjBuilder& result, bool /*fromRepl*/) { DBDirectClient db(txn); BSONElement e = jsobj.firstElement(); string toDeleteNs = dbname + '.' + e.valuestr(); LOG(0) << "CMD: reIndex " << toDeleteNs << endl; Lock::DBWrite dbXLock(txn->lockState(), dbname); WriteUnitOfWork wunit(txn->recoveryUnit()); Client::Context ctx(txn, toDeleteNs); Collection* collection = ctx.db()->getCollection( txn, toDeleteNs ); if ( !collection ) { errmsg = "ns not found"; return false; } BackgroundOperation::assertNoBgOpInProgForNs( toDeleteNs ); std::vector<BSONObj> indexesInProg = stopIndexBuilds(txn, ctx.db(), jsobj); vector<BSONObj> all; { vector<string> indexNames; collection->getCatalogEntry()->getAllIndexes( &indexNames ); for ( size_t i = 0; i < indexNames.size(); i++ ) { const string& name = indexNames[i]; BSONObj spec = collection->getCatalogEntry()->getIndexSpec( name ); all.push_back( spec.getOwned() ); } } result.appendNumber( "nIndexesWas", all.size() ); Status s = collection->getIndexCatalog()->dropAllIndexes(txn, true); if ( !s.isOK() ) { errmsg = "dropIndexes failed"; return appendCommandStatus( result, s ); } for ( size_t i = 0; i < all.size(); i++ ) { BSONObj o = all[i]; LOG(1) << "reIndex ns: " << toDeleteNs << " index: " << o << endl; Status s = collection->getIndexCatalog()->createIndex(txn, o, false); if ( !s.isOK() ) return appendCommandStatus( result, s ); } result.append( "nIndexes", (int)all.size() ); result.append( "indexes", all ); IndexBuilder::restoreIndexes(indexesInProg); wunit.commit(); return true; }
ProgramRunner( const BSONObj &args , bool isMongoProgram=true) { assert( !args.isEmpty() ); string program( args.firstElement().valuestrsafe() ); assert( !program.empty() ); boost::filesystem::path programPath = find(program); if (isMongoProgram){ #if 0 if (program == "mongos") { argv_.push_back("valgrind"); argv_.push_back("--log-file=/tmp/mongos-%p.valgrind"); argv_.push_back("--leak-check=yes"); argv_.push_back("--suppressions=valgrind.suppressions"); //argv_.push_back("--error-exitcode=1"); argv_.push_back("--"); } #endif } argv_.push_back( programPath.native_file_string() ); port_ = -1; BSONObjIterator j( args ); j.next(); // skip program name (handled above) while(j.more()) { BSONElement e = j.next(); string str; if ( e.isNumber() ) { stringstream ss; ss << e.number(); str = ss.str(); } else { assert( e.type() == mongo::String ); str = e.valuestr(); } if ( str == "--port" ) port_ = -2; else if ( port_ == -2 ) port_ = strtol( str.c_str(), 0, 10 ); argv_.push_back(str); } if ( program != "mongod" && program != "mongos" && program != "mongobridge" ) port_ = 0; else { if ( port_ <= 0 ) cout << "error: a port number is expected when running mongod (etc.) from the shell" << endl; assert( port_ > 0 ); } if ( port_ > 0 && dbs.count( port_ ) != 0 ){ cerr << "count for port: " << port_ << " is not 0 is: " << dbs.count( port_ ) << endl; assert( dbs.count( port_ ) == 0 ); } }
/* * Calculates the score for all terms in a document of a collection * @param obj, the document in the collection being parsed * @param term_freqs, map<string,double> to fill up */ void FTSSpec::scoreDocument( const BSONObj& obj, TermFrequencyMap* term_freqs ) const { string language = getLanguageToUse( obj ); Stemmer stemmer(language); Tools tools(language); tools.stemmer = &stemmer; tools.stopwords = StopWords::getStopWords( language ); if ( wildcard() ) { // if * is specified for weight, we can recurse over all fields. _scoreRecurse(tools, obj, term_freqs); return; } // otherwise, we need to remember the different weights for each field // and act accordingly (in other words, call _score) for ( Weights::const_iterator i = _weights.begin(); i != _weights.end(); i++ ) { const char * leftOverName = i->first.c_str(); // name of field BSONElement e = obj.getFieldDottedOrArray(leftOverName); // weight associated to name of field double weight = i->second; if ( e.eoo() ) { // do nothing } else if ( e.type() == Array ) { BSONObjIterator j( e.Obj() ); while ( j.more() ) { BSONElement x = j.next(); if ( leftOverName[0] && x.isABSONObj() ) x = x.Obj().getFieldDotted( leftOverName ); if ( x.type() == String ) _scoreString( tools, x.valuestr(), term_freqs, weight ); } } else if ( e.type() == String ) { _scoreString( tools, e.valuestr(), term_freqs, weight ); } } }
static bool is2DIndex(const BSONObj& pattern) { BSONObjIterator it(pattern); while (it.more()) { BSONElement e = it.next(); if (String == e.type() && str::equals("2d", e.valuestr())) { return true; } } return false; }
string Command::parseNsFullyQualified(const string& dbname, const BSONObj& cmdObj) const { BSONElement first = cmdObj.firstElement(); uassert(17005, mongoutils::str::stream() << "Main argument to " << first.fieldNameStringData() << " must be a fully qualified namespace string. Found: " << first.toString(false), first.type() == mongo::String && NamespaceString::validCollectionComponent(first.valuestr())); return first.String(); }
string Command::parseNsCollectionRequired(const string& dbname, const BSONObj& cmdObj) const { // Accepts both BSON String and Symbol for collection name per SERVER-16260 // TODO(kangas) remove Symbol support in MongoDB 3.0 after Ruby driver audit BSONElement first = cmdObj.firstElement(); uassert(17009, "no collection name specified", first.canonicalType() == canonicalizeBSONType(mongo::String) && first.valuestrsize() > 0); std::string coll = first.valuestr(); return dbname + '.' + coll; }
NamespaceString Command::parseNsCollectionRequired(const string& dbname, const BSONObj& cmdObj) const { // Accepts both BSON String and Symbol for collection name per SERVER-16260 // TODO(kangas) remove Symbol support in MongoDB 3.0 after Ruby driver audit BSONElement first = cmdObj.firstElement(); uassert(40072, str::stream() << "collection name has invalid type " << typeName(first.type()), first.canonicalType() == canonicalizeBSONType(mongo::String)); NamespaceString nss(dbname, first.valuestr()); uassert(ErrorCodes::InvalidNamespace, "Not a valid namespace", nss.isValid()); return nss; }
void DBClientConnection::checkResponse( const char *data, int nReturned ) { /* check for errors. the only one we really care about at this stage is "not master" */ if ( clientPaired && nReturned ) { BSONObj o(data); BSONElement e = o.firstElement(); if ( strcmp(e.fieldName(), "$err") == 0 && e.type() == String && strncmp(e.valuestr(), "not master", 10) == 0 ) { clientPaired->isntMaster(); } } }
bool debug( const BSONObj& o , int depth=0) { string prefix = ""; for ( int i=0; i<depth; i++ ) { prefix += "\t\t\t"; } int read = 4; try { cout << prefix << "--- new object ---\n"; cout << prefix << "\t size : " << o.objsize() << "\n"; BSONObjIterator i(o); while ( i.more() ) { BSONElement e = i.next(); cout << prefix << "\t\t " << e.fieldName() << "\n" << prefix << "\t\t\t type:" << setw(3) << e.type() << " size: " << e.size() << endl; if ( ( read + e.size() ) > o.objsize() ) { cout << prefix << " SIZE DOES NOT WORK" << endl; return false; } read += e.size(); try { e.validate(); if ( e.isABSONObj() ) { if ( ! debug( e.Obj() , depth + 1 ) ) { //return false; cout << prefix << "\t\t\t BAD BAD BAD" << endl; if ( e.size() < 1000 ) { cout << "---\n" << e.Obj().hexDump() << "\n---" << endl; } } } else if ( e.type() == String && ! isValidUTF8( e.valuestr() ) ) { cout << prefix << "\t\t\t" << "bad utf8 String!" << endl; } else if ( logger::globalLogDomain()->shouldLog(logger::LogSeverity::Debug(1)) ) { cout << prefix << "\t\t\t" << e << endl; } } catch ( std::exception& e ) { cout << prefix << "\t\t\t bad value: " << e.what() << endl; } } } catch ( std::exception& e ) { cout << prefix << "\tbad\t" << e.what() << endl; cout << "----\n" << o.hexDump() << "\n---" << endl; } return true; }
void OpObserver::onCollMod(OperationContext* txn, const std::string& dbName, const BSONObj& collModCmd) { BSONElement first = collModCmd.firstElement(); std::string coll = first.valuestr(); if (!NamespaceString(NamespaceString(dbName).db(), coll).isSystemDotProfile()) { // do not replicate system.profile modifications repl::logOp(txn, "c", dbName.c_str(), collModCmd, nullptr, false); } getGlobalAuthorizationManager()->logOp(txn, "c", dbName.c_str(), collModCmd, nullptr); logOpForDbHash(txn, dbName.c_str()); }
bool RegexMatchExpression::matchesSingleElement( const BSONElement& e ) const { //log() << "RegexMatchExpression::matchesSingleElement _regex: " << _regex << " e: " << e << std::endl; switch (e.type()) { case String: case Symbol: //if (rm._prefix.empty()) return _re->PartialMatch(e.valuestr()); //else //return !strncmp(e.valuestr(), rm._prefix.c_str(), rm._prefix.size()); case RegEx: return _regex == e.regex() && _flags == e.regexFlags(); default: return false; } }
void SyncTail::fillWriterVectors(const std::deque<BSONObj>& ops, std::vector< std::vector<BSONObj> >* writerVectors) { for (std::deque<BSONObj>::const_iterator it = ops.begin(); it != ops.end(); ++it) { const BSONElement e = it->getField("ns"); verify(e.type() == String); const char* ns = e.valuestr(); int len = e.valuestrsize(); uint32_t hash = 0; MurmurHash3_x86_32( ns, len, 0, &hash); (*writerVectors)[hash % writerVectors->size()].push_back(*it); } }
MongoProgramRunner( const BSONObj &args ) { assert( args.nFields() > 0 ); string program( args.firstElement().valuestrsafe() ); assert( !program.empty() ); boost::filesystem::path programPath = ( boost::filesystem::path( argv0 ) ).branch_path() / program; massert( "couldn't find " + programPath.native_file_string(), boost::filesystem::exists( programPath ) ); port_ = -1; argv_ = new char *[ args.nFields() + 1 ]; { string s = programPath.native_file_string(); if ( s == program ) s = "./" + s; argv_[ 0 ] = copyString( s.c_str() ); } BSONObjIterator j( args ); j.next(); for( int i = 1; i < args.nFields(); ++i ) { BSONElement e = j.next(); string str; if ( e.isNumber() ) { stringstream ss; ss << e.number(); str = ss.str(); } else { assert( e.type() == mongo::String ); str = e.valuestr(); } char *s = copyString( str.c_str() ); if ( string( "--port" ) == s ) port_ = -2; else if ( port_ == -2 ) port_ = strtol( s, 0, 10 ); argv_[ i ] = s; } argv_[ args.nFields() ] = 0; if ( program != "mongod" && program != "mongos" && program != "mongobridge" ) port_ = 0; else assert( port_ > 0 ); if ( port_ > 0 && dbs.count( port_ ) != 0 ){ cerr << "count for port: " << port_ << " is not 0 is: " << dbs.count( port_ ) << endl; assert( dbs.count( port_ ) == 0 ); } }