diff --git a/db/jsobj.cpp b/db/jsobj.cpp index a54916aa32a..f09cc6f3e45 100644 --- a/db/jsobj.cpp +++ b/db/jsobj.cpp @@ -242,6 +242,7 @@ int compareElementValues(const BSONElement& l, const BSONElement& r) { return strcmp(l.valuestr(), r.valuestr()); case Object: case Array: + return l.embeddedObject().woCompare( r.embeddedObject() ); case DBRef: { int lsz = l.valuesize(); diff --git a/db/jsobj.h b/db/jsobj.h index 325fe53c5e5..5480f508448 100644 --- a/db/jsobj.h +++ b/db/jsobj.h @@ -480,17 +480,18 @@ public: template < class T > void append( const char *fieldName, const vector< T >& vals ) { BSONObjBuilder arrBuilder; - for( int i = 0; i < vals.size(); ++i ) { - stringstream o; - o << i; - arrBuilder.append( o.str().c_str(), vals[ i ] ); - } - BSONObj arr = arrBuilder.done(); - b.append( (char) Array ); - b.append( fieldName ); - b.append( (void *) arr.objdata(), arr.objsize() ); + for( int i = 0; i < vals.size(); ++i ) + arrBuilder.append( numStr( i ).c_str(), vals[ i ] ); + marshalArray( fieldName, arrBuilder.done() ); } + void appendIntArray( const char *fieldName, const vector< int >& vals ) { + BSONObjBuilder arrBuilder; + for( int i = 0; i < vals.size(); ++i ) + arrBuilder.appendInt( numStr( i ).c_str(), vals[ i ] ); + marshalArray( fieldName, arrBuilder.done() ); + } + /* BSONObj will free the buffer when it is finished. */ BSONObj doneAndDecouple() { int l; @@ -516,6 +517,19 @@ public: void decouple() { b.decouple(); } // post done() call version. be sure jsobj frees... private: + // Append the provided arr object as an array. + void marshalArray( const char *fieldName, const BSONObj &arr ) { + b.append( (char) Array ); + b.append( fieldName ); + b.append( (void *) arr.objdata(), arr.objsize() ); + } + + string numStr( int i ) const { + stringstream o; + o << i; + return o.str(); + } + char* _done() { b.append((char) EOO); char *data = b.buf(); diff --git a/db/makefile b/db/makefile index 0d293029fb0..616b336bc5f 100644 --- a/db/makefile +++ b/db/makefile @@ -16,7 +16,7 @@ OBJS=../stdafx.o ../util/sock.o ../grid/message.o ../util/mmap.o pdfile.o query. DBGRID_OBJS=../stdafx.o json.o ../util/sock.o ../grid/message.o ../util/util.o jsobj.o ../client/dbclient.o ../dbgrid/dbgrid.o ../dbgrid/request.o ../client/connpool.o ../dbgrid/gridconfig.o commands.o ../dbgrid/dbgrid_commands.o ../dbgrid/griddatabase.o ../client/model.o ../util/background.o ../dbgrid/shard.o -DBTEST_OBJS= $(OBJS) ../dbtests/dbtests.o ../dbtests/btreetests.o ../dbtests/indexdetailstests.o ../dbtests/pairingtests.o +DBTEST_OBJS= $(OBJS) ../dbtests/dbtests.o ../dbtests/btreetests.o ../dbtests/jsobjtests.o ../dbtests/namespacetests.o ../dbtests/pairingtests.o GPP = g++ diff --git a/dbtests/dbtests.cpp b/dbtests/dbtests.cpp index 783c25243c1..96d40a17926 100644 --- a/dbtests/dbtests.cpp +++ b/dbtests/dbtests.cpp @@ -67,6 +67,7 @@ int main( int argc, char** argv ) { UnitTest::Registry tests; tests.add( btreeTests(), "btree" ); + tests.add( jsobjTests(), "jsobj" ); tests.add( namespaceTests(), "namespace" ); tests.add( pairingTests(), "pairing" ); diff --git a/dbtests/dbtests.h b/dbtests/dbtests.h index 982487f54e3..2c8acb085c5 100644 --- a/dbtests/dbtests.h +++ b/dbtests/dbtests.h @@ -20,5 +20,6 @@ #include UnitTest::TestPtr btreeTests(); +UnitTest::TestPtr jsobjTests(); UnitTest::TestPtr namespaceTests(); UnitTest::TestPtr pairingTests(); diff --git a/dbtests/namespacetests.cpp b/dbtests/namespacetests.cpp index cd8fbef4b51..e2ff3e7a1ef 100644 --- a/dbtests/namespacetests.cpp +++ b/dbtests/namespacetests.cpp @@ -117,9 +117,6 @@ namespace NamespaceTests { set< BSONObj > keys; id().getKeysFromObject( a.done(), keys ); ASSERT_EQUALS( 1, keys.size() ); - // FIXME Why doesn't woCompare expand sub elements? - // ASSERT( !keys.begin()->woCompare( e.done() ) ); - cout << "first key: " << keys.begin()->toString() << endl; ASSERT_EQUALS( string( "b" ), keys.begin()->firstElement().fieldName() ); ASSERT_EQUALS( 4, keys.begin()->firstElement().number() ); } @@ -204,7 +201,6 @@ namespace NamespaceTests { set< BSONObj > keys; id().getKeysFromObject( a.done(), keys ); - cout << "key: " << keys.begin()->toString() << endl; ASSERT_EQUALS( 3, keys.size() ); int j = 1; for( set< BSONObj >::iterator i = keys.begin(); i != keys.end(); ++i, ++j ) { diff --git a/mongo.xcodeproj/project.pbxproj b/mongo.xcodeproj/project.pbxproj index 364891bd311..ae4fd52201e 100644 --- a/mongo.xcodeproj/project.pbxproj +++ b/mongo.xcodeproj/project.pbxproj @@ -16,7 +16,6 @@ 934223860EF16D7000608550 /* btreetests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = btreetests.cpp; sourceTree = ""; }; 934223880EF16D7000608550 /* dbtests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = dbtests.cpp; sourceTree = ""; }; 934223890EF16D7000608550 /* dbtests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = dbtests.h; sourceTree = ""; }; - 9342238B0EF16D7000608550 /* indexdetailstests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = indexdetailstests.cpp; sourceTree = ""; }; 9342238C0EF16D7000608550 /* mockdbclient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = mockdbclient.h; sourceTree = ""; }; 9342238D0EF16D7000608550 /* pairingtests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = pairingtests.cpp; sourceTree = ""; }; 934223900EF16DB400608550 /* btree.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = btree.cpp; sourceTree = ""; }; @@ -69,6 +68,8 @@ 934223C70EF16DB400608550 /* scanandorder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = scanandorder.h; sourceTree = ""; }; 934223C80EF16DB400608550 /* storage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = storage.h; sourceTree = ""; }; 934223C90EF16DB400608550 /* tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = tests.cpp; sourceTree = ""; }; + 93D0C1520EF1D377005253B7 /* jsobjtests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsobjtests.cpp; sourceTree = ""; }; + 93D0C1FB0EF1E267005253B7 /* namespacetests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = namespacetests.cpp; sourceTree = ""; }; C6859E8B029090EE04C91782 /* mongo.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = mongo.1; sourceTree = ""; }; /* End PBXFileReference section */ @@ -116,10 +117,11 @@ 934223850EF16D7000608550 /* dbtests */ = { isa = PBXGroup; children = ( + 93D0C1FB0EF1E267005253B7 /* namespacetests.cpp */, + 93D0C1520EF1D377005253B7 /* jsobjtests.cpp */, 934223860EF16D7000608550 /* btreetests.cpp */, 934223880EF16D7000608550 /* dbtests.cpp */, 934223890EF16D7000608550 /* dbtests.h */, - 9342238B0EF16D7000608550 /* indexdetailstests.cpp */, 9342238C0EF16D7000608550 /* mockdbclient.h */, 9342238D0EF16D7000608550 /* pairingtests.cpp */, );