summaryrefslogtreecommitdiff
path: root/src/mongo/bson/bsonobj.cpp
diff options
context:
space:
mode:
authorMathias Stearn <mathias@10gen.com>2015-02-19 13:33:36 -0500
committerMathias Stearn <mathias@10gen.com>2015-03-27 14:32:37 -0400
commitcafa5d7815509e5ce4fa65f4def89cf18e087bcf (patch)
tree5293417470ae7e0432eeb943f81d9e988880e669 /src/mongo/bson/bsonobj.cpp
parentee8ebe091acd1c519b6e5485e8da642f3c4af892 (diff)
downloadmongo-cafa5d7815509e5ce4fa65f4def89cf18e087bcf.tar.gz
Merge bsonobjiterator.h/cpp into bsonobj.h/cpp
Diffstat (limited to 'src/mongo/bson/bsonobj.cpp')
-rw-r--r--src/mongo/bson/bsonobj.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/mongo/bson/bsonobj.cpp b/src/mongo/bson/bsonobj.cpp
index cb57e1ed233..b9e76532fe0 100644
--- a/src/mongo/bson/bsonobj.cpp
+++ b/src/mongo/bson/bsonobj.cpp
@@ -39,6 +39,7 @@
#include "mongo/util/hex.h"
#include "mongo/util/log.h"
#include "mongo/util/mongoutils/str.h"
+#include "mongo/util/stringutils.h"
namespace mongo {
using namespace std;
@@ -814,4 +815,44 @@ namespace mongo {
return s;
}
+ /** Compare two bson elements, provided as const char *'s, by field name. */
+ class BSONIteratorSorted::ElementFieldCmp {
+ public:
+ ElementFieldCmp( bool isArray );
+ bool operator()( const char *s1, const char *s2 ) const;
+ private:
+ LexNumCmp _cmp;
+ };
+
+ BSONIteratorSorted::ElementFieldCmp::ElementFieldCmp( bool isArray ) :
+ _cmp( !isArray ) {
+ }
+
+ bool BSONIteratorSorted::ElementFieldCmp::operator()( const char *s1, const char *s2 )
+ const {
+ // Skip the type byte and compare field names.
+ return _cmp( s1 + 1, s2 + 1 );
+ }
+
+ BSONIteratorSorted::BSONIteratorSorted( const BSONObj &o, const ElementFieldCmp &cmp )
+ : _nfields(o.nFields()), _fields(new const char*[_nfields]) {
+ int x = 0;
+ BSONObjIterator i( o );
+ while ( i.more() ) {
+ _fields[x++] = i.next().rawdata();
+ verify( _fields[x-1] );
+ }
+ verify( x == _nfields );
+ std::sort( _fields.get() , _fields.get() + _nfields , cmp );
+ _cur = 0;
+ }
+
+ BSONObjIteratorSorted::BSONObjIteratorSorted( const BSONObj &object ) :
+ BSONIteratorSorted( object, ElementFieldCmp( false ) ) {
+ }
+
+ BSONArrayIteratorSorted::BSONArrayIteratorSorted( const BSONArray &array ) :
+ BSONIteratorSorted( array, ElementFieldCmp( true ) ) {
+ }
+
} // namespace mongo