Document DocumentSource::documentFromBsonWithDeps(const BSONObj& bson, const ParsedDeps& neededFields) { MutableDocument md(neededFields.size()); BSONObjIterator it(bson); while (it.more()) { BSONElement bsonElement (it.next()); StringData fieldName (bsonElement.fieldName(), bsonElement.fieldNameSize()-1); Value isNeeded = neededFields[fieldName]; if (isNeeded.missing()) continue; if (isNeeded.getType() == Bool) { md.addField(fieldName, Value(bsonElement)); continue; } dassert(isNeeded.getType() == Object); if (bsonElement.type() == Object) { Document sub = documentFromBsonWithDeps(bsonElement.embeddedObject(), isNeeded.getDocument()); md.addField(fieldName, Value(sub)); } if (bsonElement.type() == Array) { md.addField(fieldName, arrayHelper(bsonElement.embeddedObject(), isNeeded.getDocument())); } } return md.freeze(); }
// static bool LiteParsedQuery::isValidSortOrder(const BSONObj& sortObj) { BSONObjIterator i(sortObj); while (i.more()) { BSONElement e = i.next(); // fieldNameSize() includes NULL terminator. For empty field name, // we should be checking for 1 instead of 0. if (1 == e.fieldNameSize()) { return false; } if (isTextScoreMeta(e)) { continue; } long long n = e.safeNumberLong(); if (!(e.isNumber() && (n == -1LL || n == 1LL))) { return false; } } return true; }
void BSONElementHasher::recursiveHash( Hasher* h , const BSONElement& e , bool includeFieldName ) { int canonicalType = e.canonicalType(); h->addData( &canonicalType , sizeof( canonicalType ) ); if ( includeFieldName ){ h->addData( e.fieldName() , e.fieldNameSize() ); } if ( !e.mayEncapsulate() ){ //if there are no embedded objects (subobjects or arrays), //compute the hash, squashing numeric types to 64-bit ints if ( e.isNumber() ){ long long int i = e.safeNumberLong(); //well-defined for troublesome doubles h->addData( &i , sizeof( i ) ); } else { h->addData( e.value() , e.valuesize() ); } } else { //else identify the subobject. //hash any preceding stuff (in the case of codeWscope) //then each sub-element //then finish with the EOO element. BSONObj b; if ( e.type() == CodeWScope ) { h->addData( e.codeWScopeCode() , e.codeWScopeCodeLen() ); b = e.codeWScopeObject(); } else { b = e.embeddedObject(); } BSONObjIterator i(b); while( i.moreWithEOO() ) { BSONElement el = i.next(); recursiveHash( h , el , true ); } } }