diff options
author | Eliot Horowitz <eliot@10gen.com> | 2010-11-15 16:19:10 -0500 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2010-11-15 16:36:01 -0500 |
commit | 48ad70487a5e0e4ba5b6399e01dedcb1702e235d (patch) | |
tree | 6917420abba3ec59319fe26dd52774f1927b861b /db | |
parent | 16d9785166314fbfb31ea2ede0f06f2951b4b027 (diff) | |
download | mongo-48ad70487a5e0e4ba5b6399e01dedcb1702e235d.tar.gz |
starting FieldMatcher cleaning
Diffstat (limited to 'db')
-rw-r--r-- | db/query.h | 2 | ||||
-rw-r--r-- | db/queryutil.cpp | 14 | ||||
-rw-r--r-- | db/queryutil.h | 28 |
3 files changed, 39 insertions, 5 deletions
diff --git a/db/query.h b/db/query.h index 2b597fff9f3..e4ba08d33a9 100644 --- a/db/query.h +++ b/db/query.h @@ -293,7 +293,7 @@ namespace mongo { if ( fields.isEmpty() ) return; _fields.reset( new FieldMatcher() ); - _fields->add( fields ); + _fields->init( fields ); } ParsedQuery( const ParsedQuery& other ){ diff --git a/db/queryutil.cpp b/db/queryutil.cpp index 2b6322ecbc3..3500852f6a6 100644 --- a/db/queryutil.cpp +++ b/db/queryutil.cpp @@ -794,7 +794,7 @@ namespace mongo { // FieldMatcher // /////////////////// - void FieldMatcher::add( const BSONObj& o ){ + void FieldMatcher::init( const BSONObj& o ){ massert( 10371 , "can only add to FieldMatcher once", _source.isEmpty()); _source = o; @@ -854,7 +854,8 @@ namespace mongo { void FieldMatcher::add(const string& field, bool include){ if (field.empty()){ // this is the field the user referred to _include = include; - } else { + } + else { _include = !include; const size_t dot = field.find('.'); @@ -892,6 +893,15 @@ namespace mongo { return _source; } + BSONObj FieldMatcher::transform( const BSONObj& in ) const { + BSONObjBuilder b; + BSONObjIterator i(in); + while ( i.more() ) + append( b , i.next() ); + return b.obj(); + } + + //b will be the value part of an array-typed BSONElement void FieldMatcher::appendArray( BSONObjBuilder& b , const BSONObj& a , bool nested) const { int skip = nested ? 0 : _skip; diff --git a/db/queryutil.h b/db/queryutil.h index e73c0c5d9c5..763cc7a6cd5 100644 --- a/db/queryutil.h +++ b/db/queryutil.h @@ -553,12 +553,35 @@ namespace mongo { , _limit(-1) {} - void add( const BSONObj& o ); + /** + * called once per lifetime + */ + void init( const BSONObj& spec ); + + /** + * @return the spec init was called with + */ + BSONObj getSpec() const; + /** + * appends e to b if user wants it + * will descend into e if needed + */ void append( BSONObjBuilder& b , const BSONElement& e ) const; - BSONObj getSpec() const; + + /** + * transforms in according to spec + * NOTE: this will stricy obey _id, which is not true + * for normal queries + */ + BSONObj transform( const BSONObj& in ) const; + + /** + * @return if _id should be returned + */ bool includeID() { return _includeID; } + private: void add( const string& field, bool include ); @@ -567,6 +590,7 @@ namespace mongo { bool _include; // true if default at this level is to include bool _special; // true if this level can't be skipped or included without recursing + //TODO: benchmark vector<pair> vs map typedef map<string, boost::shared_ptr<FieldMatcher> > FieldMap; FieldMap _fields; |