diff options
Diffstat (limited to 'db/query.h')
-rw-r--r-- | db/query.h | 106 |
1 files changed, 55 insertions, 51 deletions
@@ -23,6 +23,7 @@ #include "dbmessage.h" #include "jsobj.h" #include "diskloc.h" +#include "projection.h" /* db request message format @@ -37,29 +38,29 @@ a series of JSObjects dbDelete: string collection; - int flags=0; // 1=DeleteSingle + int flags=0; // 1=DeleteSingle JSObject query; dbUpdate: string collection; - int flags; // 1=upsert + int flags; // 1=upsert JSObject query; - JSObject objectToUpdate; + JSObject objectToUpdate; objectToUpdate may include { $inc: <field> } or { $set: ... }, see struct Mod. dbQuery: string collection; - int nToSkip; - int nToReturn; // how many you want back as the beginning of the cursor data (0=no limit) + int nToSkip; + int nToReturn; // how many you want back as the beginning of the cursor data (0=no limit) // greater than zero is simply a hint on how many objects to send back per "cursor batch". // a negative number indicates a hard limit. JSObject query; - [JSObject fieldsToReturn] + [JSObject fieldsToReturn] dbGetMore: - string collection; // redundant, might use for security. + string collection; // redundant, might use for security. int nToReturn; int64 cursorID; dbKillCursors=2007: int n; - int64 cursorIDs[n]; + int64 cursorIDs[n]; Note that on Update, there is only one object, which is different from insert where you can pass a list of objects to insert in the db. @@ -77,7 +78,7 @@ namespace mongo { struct GetMoreWaitException { }; QueryResult* processGetMore(const char *ns, int ntoreturn, long long cursorid , CurOp& op, int pass, bool& exhaust); - + struct UpdateResult { bool existing; // if existing objects were modified bool mod; // was this a $ mod @@ -85,25 +86,25 @@ namespace mongo { OID upserted; // if something was upserted, the new _id of the object UpdateResult( bool e, bool m, unsigned long long n , const BSONObj& upsertedObject = BSONObj() ) - : existing(e) , mod(m), num(n){ + : existing(e) , mod(m), num(n) { upserted.clear(); BSONElement id = upsertedObject["_id"]; - if ( ! e && n == 1 && id.type() == jstOID ){ + if ( ! e && n == 1 && id.type() == jstOID ) { upserted = id.OID(); } } - + }; class RemoveSaver; - + /* returns true if an existing object was updated, false if no existing object was found. multi - update multiple objects - mostly useful with things like $set god - allow access to system namespaces */ UpdateResult updateObjects(const char *ns, const BSONObj& updateobj, BSONObj pattern, bool upsert, bool multi , bool logop , OpDebug& debug ); - UpdateResult _updateObjects(bool god, const char *ns, const BSONObj& updateobj, BSONObj pattern, + UpdateResult _updateObjects(bool god, const char *ns, const BSONObj& updateobj, BSONObj pattern, bool upsert, bool multi , bool logop , OpDebug& debug , RemoveSaver * rs = 0 ); // If justOne is true, deletedId is set to the id of the deleted object. @@ -112,7 +113,7 @@ namespace mongo { long long runCount(const char *ns, const BSONObj& cmd, string& err); const char * runQuery(Message& m, QueryMessage& q, CurOp& curop, Message &result); - + /* This is for languages whose "objects" are not well ordered (JSON is well ordered). [ { a : ... } , { b : ... } ] -> { a : ..., b : ... } */ @@ -144,24 +145,24 @@ namespace mongo { class ParsedQuery { public: ParsedQuery( QueryMessage& qm ) - : _ns( qm.ns ) , _ntoskip( qm.ntoskip ) , _ntoreturn( qm.ntoreturn ) , _options( qm.queryOptions ){ + : _ns( qm.ns ) , _ntoskip( qm.ntoskip ) , _ntoreturn( qm.ntoreturn ) , _options( qm.queryOptions ) { init( qm.query ); initFields( qm.fields ); } ParsedQuery( const char* ns , int ntoskip , int ntoreturn , int queryoptions , const BSONObj& query , const BSONObj& fields ) - : _ns( ns ) , _ntoskip( ntoskip ) , _ntoreturn( ntoreturn ) , _options( queryoptions ){ + : _ns( ns ) , _ntoskip( ntoskip ) , _ntoreturn( ntoreturn ) , _options( queryoptions ) { init( query ); initFields( fields ); } - - ~ParsedQuery(){} + + ~ParsedQuery() {} const char * ns() const { return _ns; } bool isLocalDB() const { return strncmp(_ns, "local.", 6) == 0; } const BSONObj& getFilter() const { return _filter; } - FieldMatcher* getFields() const { return _fields.get(); } - shared_ptr<FieldMatcher> getFieldPtr() const { return _fields; } + Projection* getFields() const { return _fields.get(); } + shared_ptr<Projection> getFieldPtr() const { return _fields; } int getSkip() const { return _ntoskip; } int getNumToReturn() const { return _ntoreturn; } @@ -169,7 +170,7 @@ namespace mongo { int getOptions() const { return _options; } bool hasOption( int x ) const { return x & _options; } - + bool isExplain() const { return _explain; } bool isSnapshot() const { return _snapshot; } bool returnKey() const { return _returnKey; } @@ -180,7 +181,7 @@ namespace mongo { const BSONObj& getOrder() const { return _order; } const BSONElement& getHint() const { return _hint; } int getMaxScan() const { return _maxScan; } - + bool couldBeCommand() const { /* we assume you are using findOne() for running a cmd... */ return _ntoreturn == 1 && strstr( _ns , ".$cmd" ); @@ -193,7 +194,7 @@ namespace mongo { /* if ntoreturn is zero, we return up to 101 objects. on the subsequent getmore, there is only a size limit. The idea is that on a find() where one doesn't use much results, we don't return much, but once getmore kicks in, we start pushing significant quantities. - + The n limit (vs. size) is important when someone fetches only one small field from big objects, which causes massive scanning server-side. */ @@ -208,14 +209,14 @@ namespace mongo { return false; return n >= _ntoreturn; } - + private: - void init( const BSONObj& q ){ + void init( const BSONObj& q ) { _reset(); uassert( 10105 , "bad skip value in query", _ntoskip >= 0); - - if ( _ntoreturn < 0 ){ - /* _ntoreturn greater than zero is simply a hint on how many objects to send back per + + if ( _ntoreturn < 0 ) { + /* _ntoreturn greater than zero is simply a hint on how many objects to send back per "cursor batch". A negative number indicates a hard limit. */ @@ -223,12 +224,12 @@ namespace mongo { _ntoreturn = -_ntoreturn; } - + BSONElement e = q["query"]; if ( ! e.isABSONObj() ) e = q["$query"]; - - if ( e.isABSONObj() ){ + + if ( e.isABSONObj() ) { _filter = e.embeddedObject(); _initTop( q ); } @@ -237,7 +238,7 @@ namespace mongo { } } - void _reset(){ + void _reset() { _wantMore = true; _explain = false; _snapshot = false; @@ -246,20 +247,23 @@ namespace mongo { _maxScan = 0; } - void _initTop( const BSONObj& top ){ + void _initTop( const BSONObj& top ) { BSONObjIterator i( top ); - while ( i.more() ){ + while ( i.more() ) { BSONElement e = i.next(); const char * name = e.fieldName(); if ( strcmp( "$orderby" , name ) == 0 || - strcmp( "orderby" , name ) == 0 ){ - if ( e.type() == Object ) + strcmp( "orderby" , name ) == 0 ) { + if ( e.type() == Object ) { _order = e.embeddedObject(); - else if ( e.type() == Array ) + } + else if ( e.type() == Array ) { _order = transformOrderFromArrayFormat( _order ); - else - assert( 0 ); + } + else { + uassert(13513, "sort must be an object or array", 0); + } } else if ( strcmp( "$explain" , name ) == 0 ) _explain = e.trueValue(); @@ -277,25 +281,25 @@ namespace mongo { _maxScan = e.numberInt(); else if ( strcmp( "$showDiskLoc" , name ) == 0 ) _showDiskLoc = e.trueValue(); - + } - if ( _snapshot ){ + if ( _snapshot ) { uassert( 12001 , "E12001 can't sort with $snapshot", _order.isEmpty() ); uassert( 12002 , "E12002 can't use hint with $snapshot", _hint.eoo() ); } - + } - void initFields( const BSONObj& fields ){ + void initFields( const BSONObj& fields ) { if ( fields.isEmpty() ) return; - _fields.reset( new FieldMatcher() ); - _fields->add( fields ); + _fields.reset( new Projection() ); + _fields->init( fields ); } - ParsedQuery( const ParsedQuery& other ){ + ParsedQuery( const ParsedQuery& other ) { assert(0); } @@ -303,10 +307,10 @@ namespace mongo { int _ntoskip; int _ntoreturn; int _options; - + BSONObj _filter; - shared_ptr< FieldMatcher > _fields; - + shared_ptr< Projection > _fields; + bool _wantMore; bool _explain; @@ -319,7 +323,7 @@ namespace mongo { BSONObj _order; int _maxScan; }; - + } // namespace mongo |