Files
mongo/bson/bsonobjiterator.h

162 lines
4.8 KiB
C
Raw Normal View History

2010-04-22 10:31:16 -04:00
// bsonobjiterator.h
/* Copyright 2009 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
2010-07-14 12:28:15 -04:00
#include <boost/preprocessor/cat.hpp> // like the ## operator but works with __LINE__
2010-04-22 10:31:16 -04:00
2010-07-14 12:28:15 -04:00
namespace mongo {
2010-11-29 23:50:20 -05:00
2010-04-22 10:31:16 -04:00
/** iterator for a BSONObj
Note each BSONObj ends with an EOO element: so you will get more() on an empty
object, although next().eoo() will be true.
2011-06-27 17:45:31 -04:00
The BSONObj must stay in scope for the duration of the iterator's execution.
2010-04-22 10:31:16 -04:00
todo: we may want to make a more stl-like iterator interface for this
with things like begin() and end()
*/
class BSONObjIterator {
public:
2011-01-04 00:40:41 -05:00
/** Create an iterator for a BSON object.
2010-04-22 10:31:16 -04:00
*/
BSONObjIterator(const BSONObj& jso) {
int sz = jso.objsize();
if ( MONGO_unlikely(sz == 0) ) {
2010-04-22 10:31:16 -04:00
_pos = _theend = 0;
return;
}
_pos = jso.objdata() + 4;
2011-05-23 10:27:58 -04:00
_theend = jso.objdata() + sz - 1;
2010-04-22 10:31:16 -04:00
}
2010-05-23 18:35:08 -04:00
2011-01-04 00:40:41 -05:00
BSONObjIterator( const char * start , const char * end ) {
2010-04-22 10:31:16 -04:00
_pos = start + 4;
2011-05-23 10:27:58 -04:00
_theend = end - 1;
2010-04-22 10:31:16 -04:00
}
2011-01-04 00:40:41 -05:00
2010-04-22 10:31:16 -04:00
/** @return true if more elements exist to be enumerated. */
2011-05-23 10:27:58 -04:00
bool more() { return _pos < _theend; }
2010-11-29 23:50:20 -05:00
/** @return true if more elements exist to be enumerated INCLUDING the EOO element which is always at the end. */
2011-05-23 10:27:58 -04:00
bool moreWithEOO() { return _pos <= _theend; }
2010-11-29 23:50:20 -05:00
2010-04-22 10:31:16 -04:00
/** @return the next element in the object. For the final element, element.eoo() will be true. */
2011-03-29 07:49:54 -04:00
BSONElement next( bool checkEnd ) {
2011-05-23 10:27:58 -04:00
assert( _pos <= _theend );
BSONElement e( _pos, checkEnd ? (int)(_theend + 1 - _pos) : -1 );
_pos += e.size( checkEnd ? (int)(_theend + 1 - _pos) : -1 );
2010-04-22 10:31:16 -04:00
return e;
}
2011-03-29 07:49:54 -04:00
BSONElement next() {
2011-05-23 10:27:58 -04:00
assert( _pos <= _theend );
2011-03-29 08:20:21 -04:00
BSONElement e(_pos);
_pos += e.size();
2011-03-29 07:49:54 -04:00
return e;
}
2010-05-23 18:35:08 -04:00
void operator++() { next(); }
void operator++(int) { next(); }
BSONElement operator*() {
2011-05-23 10:27:58 -04:00
assert( _pos <= _theend );
2011-03-29 08:42:36 -04:00
return BSONElement(_pos);
2010-05-23 18:35:08 -04:00
}
2010-04-22 10:31:16 -04:00
private:
const char* _pos;
const char* _theend;
};
class BSONObjIteratorSorted {
public:
BSONObjIteratorSorted( const BSONObj& o );
2011-01-04 00:40:41 -05:00
~BSONObjIteratorSorted() {
2010-04-22 10:31:16 -04:00
assert( _fields );
delete[] _fields;
_fields = 0;
}
2011-01-04 00:40:41 -05:00
bool more() {
2010-04-22 10:31:16 -04:00
return _cur < _nfields;
}
2011-01-04 00:40:41 -05:00
BSONElement next() {
2010-04-22 10:31:16 -04:00
assert( _fields );
if ( _cur < _nfields )
return BSONElement( _fields[_cur++] );
return BSONElement();
}
private:
const char ** _fields;
int _nfields;
int _cur;
};
/** transform a BSON array into a vector of BSONElements.
we match array # positions with their vector position, and ignore
any fields with non-numeric field names.
*/
inline vector<BSONElement> BSONElement::Array() const {
chk(mongo::Array);
vector<BSONElement> v;
BSONObjIterator i(Obj());
while( i.more() ) {
BSONElement e = i.next();
const char *f = e.fieldName();
try {
unsigned u = stringToNum(f);
assert( u < 1000000 );
if( u >= v.size() )
v.resize(u+1);
v[u] = e;
}
catch(unsigned) { }
}
return v;
}
2011-01-04 00:40:41 -05:00
/** Similar to BOOST_FOREACH
*
* because the iterator is defined outside of the for, you must use {} around
* the surrounding scope. Don't do this:
*
* if (foo)
* BSONForEach(e, obj)
* doSomething(e);
*
* but this is OK:
*
* if (foo) {
* BSONForEach(e, obj)
* doSomething(e);
* }
*
*/
2010-07-14 12:28:15 -04:00
#define BSONForEach(e, obj) \
BSONObjIterator BOOST_PP_CAT(it_,__LINE__)(obj); \
for ( BSONElement e; \
2011-01-04 00:40:41 -05:00
(BOOST_PP_CAT(it_,__LINE__).more() ? \
(e = BOOST_PP_CAT(it_,__LINE__).next(), true) : \
false) ; \
/*nothing*/ )
2010-04-22 10:31:16 -04:00
}